[英]Rotate object towards 3D point
I'm trying to rotate an object towards a 3d point using OpenGL and glm. 我正在尝试使用OpenGL和glm向3d点旋转对象。 To find xRotation I am doing this: 要找到xRotation,我正在这样做:
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. 其中xRotation围绕Y轴,而lookAtDiff是vec3,它是对象位置与我要使其面对的对象之间的差异。 This works flawlessly. 这可以完美地工作。 So I decided to do yRotation (Rotation around X axis) in the same manner by doing this: 因此,我决定以相同的方式执行yRotation(绕X轴旋转):
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? 我的问题是,为什么我会从中得到错误的旋转,并且该如何解决?
您必须使用对角线:
yRotation = atan2(lookAtDiff.y, sqrt(lookAtDiff.x * lookAtDiff.x + lookAtDiff.z * lookAtDiff.z));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.