Sign in to follow this  

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

This topic is 415 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.
    //Enjoy.

:cool: :cool:  :cool:  

Edited by codeBoggs

Share this post


Link to post
Share on other sites

This topic is 415 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this