Hi guys
I have been trying to work out how to make an object rotate to face another object in Opengl (with the glm library) but in still a little confused on a couple of things and was hoping you guys could help me out
At this moment I think what I have to do is have a normalized direction vector, and then a normalized vector of the object I want to face - current object and then computer the dot and cross product from this for the axis and angle of rotation. It is at this point where I am stuck on what to do next (if I even have the first part right). I should also mention that I am trying to use quaternions so any help that you can give me on applying it both for a rotation matrix and a quaternions would be very appreciated.
For the original set up, I use a set of Euler angles (in a vec 3 for pitch, yaw and roll) and do the following myquat = glm::quat(Eulerangles);. and then turn the quaternion into a matrix glm::mat4 RotationMatrix = glm::toMat4(myquat);
Here is the code I have so far
void Gamemodelimported::rotateobject(glm::vec3 otherposition)
{
glm::vec3 currentlook;
currentlook.x = glm::cos(glm::pitch(myquat)) * glm::cos(glm::yaw(myquat) + (0.0174532925 * 90));
currentlook.z = -glm::cos(glm::pitch(myquat)) * glm::sin(glm::yaw(myquat) + (0.0174532925 * 90));
currentlook.y = glm::sin(glm::pitch(myquat));
currentlook = glm::normalize(currentlook);
glm::vec3 diff = otherposition - _position;
diff = glm::normalize(diff);
float angle = glm::acos(glm::dot(diff,currentlook));
glm::vec3 corrsspro = glm::cross(diff,currentlook);
}
If you also have time can you suggest a good way to make the object slowly rotate to face the vector point.
Thanks for you time and help
Edit: I should mention that the + 90 on the yaw is to compensate for the original model rotation (I think that's the correct way not sure but it does move in the correct direction with that code)