Log in or register to post comments

mouse drag and Rotate Object

October 20, 2011 - 8:25am #1

Ok Guys, I tried everything but again it doesn't work as expected so I hope in your help. I'll attach the script which rotates an object as soon as the mouse is down. I adapted it to the Touch of the IPhone/Pad. For non-iOS applications, it works very well (of course no augmented reality), but as soon as I use the script attached to an object within the project of Augented reality the object rotates in the most weird ways. ARCamera WorldCenterMode was set on NONE and AUTO. So I beg your help ! :) see below:

 var numberAverages : int = 3;

private var originalRotation : Quaternion;
private var offsetRotation : Quaternion;

// Make sure there is always a Rigidbody
@script RequireComponent (Rigidbody)
@script RequireComponent (SphereCollider)


function Awake () {
	
	numberAverages = Mathf.Clamp (numberAverages, 1, numberAverages);
	
}


function OnTouchDown () {
	var hit : RaycastHit;
	var dir : Vector3;
	
	// Stop spinning
	rigidbody.angularVelocity = Vector3.zero;
	
	// Record initial variables 
	if (Physics.Raycast (camera.main.ScreenPointToRay(Input.mousePosition), hit)) {
		originalRotation = transform.localRotation;//rotation;
		dir = hit.point - transform.position;
		offsetRotation = Quaternion.Inverse (Quaternion.LookRotation (dir));
		Spin (dir);
	}
}

function Update() {

if (Input.touchCount > 0) {
	
	if (Input.GetTouch(0).phase == TouchPhase.Began || Input.GetTouch(0).phase == TouchPhase.Moved) OnTouchDown();

}

}



function Spin (dir : Vector3) {
	var hit : RaycastHit;
	var previousDirList : Array = new Array ();
	//var previousDirList: Vector3[];
	var currentDir : Vector3;
	
	// Initialize previous dir list
	for (var i : int = 0; i < numberAverages; i++) {
		previousDirList.Add (currentDir);
//		previousDirList.Push(currentDir);
	}
	
	currentDir = dir;
	
	// Make the object rotate with the cursor while we are grabbing it
	while (Input.GetButton ("Fire1") && Physics.Raycast (camera.main.ScreenPointToRay(Input.mousePosition), hit)) {
		// Remove first element of the array
		previousDirList.RemoveAt (0);
		// Add current dir to the end
		previousDirList.Add (currentDir);
		currentDir = hit.point - transform.localPosition;//position;
		transform.localRotation = Quaternion.LookRotation (currentDir) * offsetRotation * originalRotation;//rotation =  
		yield;
	}	

	// User let go of the mouse so make the object spin on its own
	var avgPreviousDir : Vector3 = Vector3.zero;
	
	for (dir in previousDirList) {
		var tmpDir: Vector3 = dir;
		avgPreviousDir = avgPreviousDir + tmpDir;
	}
	
	avgPreviousDir /= numberAverages;
	//Kick (currentDir, avgPreviousDir);
}

mouse drag and Rotate Object

May 16, 2013 - 2:53am #15

This is not a Vuforia issue.

You may have more luck searching over at the Unity forums or doing a web search as this has most likely come up before.

 

N

mouse drag and Rotate Object

May 16, 2013 - 1:39am #14

Hi ,

 I have a 3D object and i have to rotate/spin it with touch and zoom in and out with pinch. I have tried the code above but its not working  and I used this method its also not working

       _cachedTransform.Rotate(new Vector3(touch.deltaPosition.y, -touch.deltaPosition.x,0)*_speed, Space.World);

Its working but not smoothly, Kindly help me with it.

Re: mouse drag and Rotate Object

October 27, 2011 - 7:51am #13

Actually I'm tracking 5 ImageTargets, I've set the ARCamera to NONE or AUTO. I guess because each object as a collider/rigid body and the isKinematic wasn't on, the object was kinda grabbed by my finger and pulled back and forth towards the camera causing the movement. Apparently, switching the isKinematic on, physics is ignored and it seems to be more stable.

GC.

Re: mouse drag and Rotate Object

October 27, 2011 - 6:53am #12

Are you tracking off a single target? If so, you may want to set the World Center Mode on the ARCamera to USER, and then drag that target onto the World Center field that appears in the Inspector. This will guarantee that the target never moves in the scene in response to tracking events.

- Kim

Re: mouse drag and Rotate Object

October 27, 2011 - 12:34am #11

Kim,

sorry to bother you again with this, but ... although the script works fine, It happens that at a certain point (can't say yet when), the objects start floating away from their positions and I don't quite get if it is a problem of the script or of the QCAR.... did you experience that too? especially if you have more than 1 object.

many Thanks,
GC.

Re: mouse drag and Rotate Object

October 26, 2011 - 7:47am #10

ALthough I don't get why if I change Input.mousePosition with Input.GetTouch(0).position... it doesn't work anymore :(

bah!!!

Re: mouse drag and Rotate Object

October 26, 2011 - 7:06am #9

Kim,

many thanks for your great help! It works nicely!

GC.

Re: mouse drag and Rotate Object

October 26, 2011 - 6:17am #8

I think you were closer with the first script. I added the Kick method to that, and changed locationRotation and localPosition back to rotation and position. Then I attached it to the teapot in the ImageTargets sample, it seems to work fine. Here's the final script I used:

var numberAverages : int = 3;

private var originalRotation : Quaternion;
private var offsetRotation : Quaternion;

// Make sure there is always a Rigidbody
@script RequireComponent (Rigidbody)
@script RequireComponent (SphereCollider)


function Awake () {
	
	numberAverages = Mathf.Clamp (numberAverages, 1, numberAverages);
	
}


function OnTouchDown () {
	var hit : RaycastHit;
	var dir : Vector3;
	
	// Stop spinning
	rigidbody.angularVelocity = Vector3.zero;
	
	// Record initial variables 
	if (Physics.Raycast (camera.main.ScreenPointToRay(Input.mousePosition), hit)) {
		originalRotation = transform.rotation;//rotation;
		dir = hit.point - transform.position;
		offsetRotation = Quaternion.Inverse (Quaternion.LookRotation (dir));
		Spin (dir);
	}
}

function Update() {

if (Input.touchCount > 0) {
	
	if (Input.GetTouch(0).phase == TouchPhase.Began || Input.GetTouch(0).phase == TouchPhase.Moved) OnTouchDown();

}

}



function Spin (dir : Vector3) {
	var hit : RaycastHit;
	var previousDirList : Array = new Array ();
	//var previousDirList: Vector3[];
	var currentDir : Vector3;
	
	// Initialize previous dir list
	for (var i : int = 0; i < numberAverages; i++) {
		previousDirList.Add (currentDir);
//		previousDirList.Push(currentDir);
	}
	
	currentDir = dir;
	
	// Make the object rotate with the cursor while we are grabbing it
	while (Input.GetButton ("Fire1") && Physics.Raycast (camera.main.ScreenPointToRay(Input.mousePosition), hit)) {
		// Remove first element of the array
		previousDirList.RemoveAt (0);
		// Add current dir to the end
		previousDirList.Add (currentDir);
		currentDir = hit.point - transform.position;//position;
		transform.rotation = Quaternion.LookRotation (currentDir) * offsetRotation * originalRotation;//rotation =  
		yield;
	}	

	// User let go of the mouse so make the object spin on its own
	var avgPreviousDir : Vector3 = Vector3.zero;
	
	for (dir in previousDirList) {
		var tmpDir: Vector3 = dir;
		avgPreviousDir = avgPreviousDir + tmpDir;
	}
	
	avgPreviousDir /= numberAverages;
	Kick (currentDir, avgPreviousDir);
}

function Kick (r2 : Vector3, r1 : Vector3) {
	var linearVelocity : Vector3;
	var angVelocity : Vector3;
	
	// Calculate the angular velocity:  omega = r x v / r^2
	linearVelocity = (r2 - r1) / Time.deltaTime;
	rigidbody.angularVelocity = Vector3.Cross (r2, linearVelocity) / r2.sqrMagnitude;
}

- Kim

Re: mouse drag and Rotate Object

October 26, 2011 - 6:13am #7
ksiva wrote:

Does that script work for you without QCAR? I created a simple scene with a camera, textured sphere, and a light. I dropped that script on the sphere, checked off Use Gravity, and ran the app on an iPhone 4. The sphere doesn't respond at all to touch. Have you gotten this to work?

- Kim

You can also try this one as I tried it on my IPad too... attach the script to a Cube and use the finger to rotate it. As soon as I attach the QCAR, I can't move the object anymore...

public var amountToInterpolate: float = 2f;
private var startQuat: Quaternion; 
private var targetQuat : Quaternion ;
private var offSetQuat:Quaternion ;
private var lastRot:Quaternion;
private var objectHit: boolean = false;
private var objHit: GameObject;

function Start() {

	//lastRot = transform.localRotation;
}


function ShootRay (p:Vector3)
 
{
 		var ray: Ray = Camera.main.ScreenPointToRay (p);
 
		var tempQuat : Quaternion = new Quaternion();
		var rhit: RaycastHit;
 
		if (Physics.Raycast (ray, rhit, 1000.0f)) {
 			Debug.Log("Ray Shot and hit!");
 			objectHit =  true;
 			objHit = rhit.collider.gameObject;
 			lastRot = objHit.transform.localRotation;
 			Debug.Log("Name: " + objHit.name);
		}
		
		if(objectHit) {
			tempQuat.x = ray.GetPoint(10).x;//rhit.point.x;
 			tempQuat.y = ray.GetPoint(10).y;//tempQuat.y = rhit.point.y;
 			tempQuat.z = ray.GetPoint(10).z;//tempQuat.z = rhit.point.z;
		}
 
		return tempQuat;
 
}
 




function Update () {

var theTouch : Touch;
Debug.Log("ObjectHit: " + objectHit);

if(Input.touchCount > 0) {
	theTouch = Input.GetTouch(0);
	
		
	//This is for the MouseDown
	if( Input.GetTouch(0).phase == TouchPhase.Began) {
			
			if(objectHit == false) {
				Debug.Log("Begin Touch");
				startQuat = lastRot;
				offSetQuat = ShootRay(theTouch.position) * startQuat;
			}
			
	}
	
	//This is for MouseUp
	if(Input.GetTouch(0).phase == TouchPhase.Ended) {
	
		if(objectHit == true)
			lastRot = objHit.transform.localRotation;
		
		Debug.Log("End Touch");
		objectHit = false;
	}
	
	//This is for the MouseDrag
	if (Input.GetTouch(0).phase == TouchPhase.Moved) {
			
			if(objectHit == true) {	
				Debug.Log("Move Touch");	
				targetQuat = ShootRay(theTouch.position) * offSetQuat;
				var endQuat: Quaternion = Quaternion.Slerp (startQuat, targetQuat, Time.time * amountToInterpolate);
				objHit.transform.localRotation = endQuat;
	 			startQuat = targetQuat;			
	 		}
	}
	
	}

}	

Re: mouse drag and Rotate Object

October 26, 2011 - 3:23am #6

I've edited my post so now you can download the script... sorry for the mistake.

GC

Re: mouse drag and Rotate Object

October 26, 2011 - 12:49am #5

Hi KSiva,

that's weird, I attached the full script this works if I create a different application to run on the iPad with a cube and a simple camera, the behaviour of the touch changes as soon as I use this script with QCAR :(

Let me know how that works.
Thank you for your help.

GC.:)


#pragma strict
#pragma downcast

var numberAverages : int = 3;

private var originalRotation : Quaternion;
private var offsetRotation : Quaternion;

// Make sure there is always a Rigidbody
@script RequireComponent (Rigidbody)
@script RequireComponent (SphereCollider)


function Awake () {
	
	numberAverages = Mathf.Clamp (numberAverages, 1, numberAverages);
	
}


function OnTouchDown (tuch:Touch) {
	var hit : RaycastHit;
	var dir : Vector3;
	
	// Stop spinning
	rigidbody.angularVelocity = Vector3.zero;
	
	// Record initial variables 
	if (Physics.Raycast (camera.main.ScreenPointToRay(tuch.position/*Input.mousePosition*/), hit)) {
		originalRotation = transform.localRotation;/*transform.rotation*/;
		dir = hit.point - transform.localPosition;/*transform.position;*/
		offsetRotation = Quaternion.Inverse (Quaternion.LookRotation (dir));
		Spin (dir, tuch);
	}
}

function Update() {

//if (Input.GetMouseButtonDown(0)) OnTouchDown();
if(Input.touchCount > 0) 
	OnTouchDown (Input.GetTouch(0));

}

function Spin (dir : Vector3, tch: Touch) {
	
	var hit : RaycastHit;
	var previousDirList : Array = new Array ();
	var currentDir : Vector3;
	
	// Initialize previous dir list
	for (var i : int = 0; i < numberAverages; i++) {
		previousDirList.Add (currentDir);
	}
	
	currentDir = dir;
	
	// Make the object rotate with the cursor while we are grabbing it
	while (Physics.Raycast (camera.main.ScreenPointToRay(tch.position/*Input.mousePosition*/), hit)) {
		// Remove first element of the array
		previousDirList.RemoveAt (0);
		// Add current dir to the end
		previousDirList.Add (currentDir);
		currentDir = hit.point - transform.localPosition;/*transform.position;*/
		/*transform.rotation*/ transform.localRotation =  Quaternion.LookRotation (currentDir) * offsetRotation * originalRotation;
		yield;
	}
	
	// User let go of the mouse so make the object spin on its own
	var avgPreviousDir : Vector3 = Vector3.zero;
	for (dir in previousDirList) {
		var tmpDir : Vector3 = dir;
		avgPreviousDir += tmpDir;
	}
	avgPreviousDir /= numberAverages;
	Kick (currentDir, avgPreviousDir);
}


function Kick (r2 : Vector3, r1 : Vector3) {
	var linearVelocity : Vector3;
	var angVelocity : Vector3;
	
	// Calculate the angular velocity:  omega = r x v / r^2
	linearVelocity = (r2 - r1) / Time.deltaTime;
	rigidbody.angularVelocity = Vector3.Cross (r2, linearVelocity) / r2.sqrMagnitude;

}

Re: mouse drag and Rotate Object

October 21, 2011 - 1:07pm #4

I think something is missing from the script you copied here. You set the rigidbody.angularVelocity to zero in OnTouchDown, but you never set it to anything else... You have a call to a "Kick" function commented out, perhaps we're missing that? As it stands this script doesn't do anything.

- Kim

Re: mouse drag and Rotate Object

October 21, 2011 - 2:45am #3

Yes it does work if I drop the QCAR off, I created an appilcation with Cube, and it does respond to the touch, but as soon as I add the QCAR ... it keeps flipping ....
I tried many other ways, found on internet, they all work without QCAR, but as soon as I add the QCAR, the behavious is unpredictable.

Re: mouse drag and Rotate Object

October 20, 2011 - 12:19pm #2

Does that script work for you without QCAR? I created a simple scene with a camera, textured sphere, and a light. I dropped that script on the sphere, checked off Use Gravity, and ran the app on an iPhone 4. The sphere doesn't respond at all to touch. Have you gotten this to work?

- Kim

Log in or register to post comments