Jump to content
  • Advertisement
Sign in to follow this  
homer_3

Quaternion.... angles?

This topic is 2510 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm working in Unity and I'm trying to get an enemy to turn a certain way. Doing it with Euler angles is just not working and using quaternions works better, but still isn't quite there. Basically, imagine an enemy standing up straight and rotating him so the top of his head points at a desired position. This is only on 2 axis, so to get the angle I want to rotate I do


vec = targetPos - position;
vec = vec.normalize;
dotProd = Vector3.Dot((Vector3.up), vec);
angleToTarget = Mathf.Acos(dotProd) * (180/Mathf.PI);


So now I have the angle I want to rotate. I want to do this rotation about the X axis, since that is the axis that goes into the screen for me. How would I go about modifying the existing quanternion that is being used for rotations (which is just the identity quaternion) so it will rotate the object for the calculated angle about the X axis?

Share this post


Link to post
Share on other sites
Advertisement
Try a Quaternion from axis angle. You have the angle, you know you want to rotate about the x axis.

angleRad *= 0.5
q.w = cos(angleRad);
q.v = nrmAxis * sin(angleRad)

then combine this quaternion with the existing one, and use the result.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!