Jump to content
  • Advertisement
Sign in to follow this  

How to rotate my enemy to the same direction that it walks towards? (quaternions)

This topic is 728 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

What's up, guys.


I switched from euler to quaternions because I needed to slerp some stuff using glm::mix( quat, quat ).


But now I can't come up with a way to rotate the enemy based on the direction that he is moving towards.


There's kind of nothing more to explain. I have a 'glm::vec3 directionVec' that has a certain value. How to use quaternions to rotate the enemy so that his body is rotated towards that directionVec?


For starters, I would just want to rotate it horizontally.


dot product doesn't work on 360 degrees, I need something else. ( I know I can do two dot products using the cross product of the two vectors and make it work for 360, but I don't like this, can you propose something else? )


Thanks for reading, any ideas are appreciated. ^_^

Edited by codeBoggs

Share this post

Link to post
Share on other sites

Solved it. I guess I should have waited a bit before posting. :cool:

    float verticalAngle   = atan2( directionVec.x, directionVec.y ); //Just 'sift' out the x- and y- coordinates and then find the tangent.
    float horizontalAngle = atan2( directionVec.x, directionVec.z ); //Same stuff, but with x and z.
    glm::quat pitch = glm::angleAxis( verticalAngle,   glm::vec3( 1.0f, 0.0f, 0.0f ) ); //Create quaternion from angle-axis.
    glm::quat yaw   = glm::angleAxis( horizontalAngle, glm::vec3( 0.0f, 1.0f, 0.0f ) ); //Same.
    glm::mat4 rotationMatrix = glm::toMat4( glm::normalize( pitch*yaw ) ); //Cumulate the two quaternions and convert to matrix.

:cool: :cool:  :cool:  

Edited by codeBoggs

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!