Advertisement

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

Started by October 25, 2016 10:15 PM
0 comments, last by Heelp 8 years, 1 month ago

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. ^_^

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:

This topic is closed to new replies.

Advertisement