所以我的场景图中有一个相机对象,我想围绕另一个对象旋转并仍然注视它。
到目前为止,我尝试转换其位置的代码只是不断地向右来回移动它并向后移动少量。
这是我在游戏更新循环中尝试使用的代码:
//ang is set to 75.0f
camera.position += camera.right * glm::vec3(cos(ang * deltaTime), 1.0f, 1.0f);
我不太确定哪里出错了。我看过其他围绕对象旋转的代码,它们使用 cos 和 sine,但由于我只沿 x 轴平移,所以我认为我只需要这个。
最佳答案
首先,您必须创建一个旋转 vector 。这可以通过 glm::rotateZ
来完成.请注意,自 glm 版本 0.9.6 以来,角度必须以弧度设置。
float ang = ....; // angle per second in radians
float timeSinceStart = ....; // seconds since the start of the animation
float dist = ....; // distance from the camera to the target
glm::vec3 cameraVec = glm::rotateZ(glm::vec3(dist, 0.0f, 0.0f), ang * timeSinceStart);
此外,您必须知道相机应该围绕哪个点转动。可能对象的位置:
glm::vec3 objectPosition = .....; // position of the object where the camera looks to
相机的新位置是物体的位置,由旋转 vector 位移:
camera.position = objectPosition + cameraVec;
相机的目标必须是物体位置,因为相机应该看向物体:
camera.front = glm::normalize(objectPosition - camera.position);
相机的up
vector 应该是z轴(旋转轴):
camera.up = glm::vec3(0.0f, 0.0f, 1.0f);
关于c++ - 围绕另一个对象旋转对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47045305/