Public Group

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

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

## 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 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

1. 1
Rutin
26
2. 2
3. 3
JoeJ
20
4. 4
5. 5

• 10
• 9
• 9
• 10
• 46
• ### Forum Statistics

• Total Topics
631751
• Total Posts
3002078
×