Question

I'm trying to rotate a vector with a Quaternion, constructed with Quaternion.Euler(x,y,z) it works as expected, but the problem is, rotation is done with respect to the world coordinate system, whereas I want to rotate with respect to the vector itself.

So I want to take the coordinate system as

z = vector's direction,     
x = vector's perpendicular (any)  
y = world y.

so my code is this :

myVector = euler * myVector;

however I need

myVector = localEuler * myVector;

so I need to calculate localEuler as a function of euler and myVector.

How can I do this ?

Thanks for any help !

I tried :

localEuler = Quaternion.Euler(euler.x * myVector.x ,euler.y * myVector.y ,euler.z * myVector.z );

it didn't work. I guess this is the dot product.

P.S.

I think this has a built-in code for Transforms : Transform.RotateAround(), but I need to do it with a Vector3, not a Transform.

Was it helpful?

Solution

I solved my issue.

My solution was this :

  • Use Quaternion.AxisAngle(angle : float , rotationAxis : Vector3);

so for example, to rotate around "local" x, the rotationAxis should be myVector's perpendicular, which is computed like this :

var rotationAxis : Vector3 = Quaternion.Euler(0,90,0) * myVector;
rotationAxis.y = 0;

then the new quaternion is computed like this :

var localEuler:Quaternion = Quaternion.AngleAxis(euler.x , rotationAxis);

then the vector is rotated correctly :

myVector = localEuler * myVector;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top