Rotate object towards 3D point

1k views Asked by At

I'm trying to rotate an object towards a 3d point using OpenGL and glm. To find xRotation I am doing this:

xRotation=atan2(lookAtDiff.x,lookAtDiff.z)

Where xRotation is around the Y axis and lookAtDiff is a vec3 that is the difference between the object's position and what I am trying to make it face. This works flawlessly. So I decided to do yRotation (Rotation around X axis) in the same manner by doing this:

yRotation=atan2(lookAtDiff.y,lookAtDiff.x)

This gives me the wrong rotation. My question is why am I getting the wrong rotation from this and how can I fix it?

1

There are 1 answers

0
Nico Schertler On BEST ANSWER

You have to use the diagonal:

yRotation = atan2(lookAtDiff.y, sqrt(lookAtDiff.x * lookAtDiff.x + lookAtDiff.z * lookAtDiff.z));