1

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.

1 Answer 1

2

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;
Sign up to request clarification or add additional context in comments.

2 Comments

Hi, I don't understand your answer. rotationAxis is perpendicular to myVector in what direction. It's 3D space after all.
@Boon yes the sentence does not fully-define the perpendicular, however the equation below it explains what it means. You can experiment with it and see which perpendicular you get.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.