简体   繁体   English

如何在不沿方向矢量发生意外旋转的情况下将对象朝向3D中的点定向

[英]How to orient object towards point in 3D without undesired rotations along direction vector

I'm trying to orient an object towards a point in space, using: 我正在尝试使用以下方法将对象朝向空间中的点:

m_Z = glm::normalize(target - m_Position);
m_Y = glm::normalize(m_Y - glm::dot(m_Y, m_Z) * m_Z);
m_X = glm::normalize(glm::cross(m_Y, m_Z));

And while the object does "look at" the point in 3D, it seems to rotate around it's own forward vector (m_X) meaning the UP vector is in an incorrect direction. 尽管对象确实“看”了3D中的点,但它似乎绕着其自身的前向矢量(m_X)旋转,这意味着UP矢量的方向不正确。 So the object is sometimes looking at the point while being upside down... So it's like a head that's tilted (rotated around local forward vector) 因此,对象有时会颠倒地看着该点。。。就像是倾斜的头(围绕局部前向矢量旋转)

I know how to orient an object towards a point in 2D - that is not what I'm after - I'm looking for a way to correct the upvector in 3d to an extent that the object's "top" is always (more or less) facing up, so I need the object to follow (look at) the point not just left and right, but also up & down. 我知道如何将对象定向到2D中的点-这不是我要追求的目标-我正在寻找一种方法来将3d中的upvector校正到对象的“顶部”始终((或多或少) )朝上,因此我需要对象不仅要跟踪(观察)左右点,而且还要跟踪(上下)点。

m_X = left vector of object
m_Y = up vector of object
m_Z = forward vector of object

target = point's world position
m_Position = object's world position

Just re-arrange the order in which you do the calculations: 只需重新排列计算顺序即可:

m_Z = glm::normalize(target - m_Position);
m_X = glm::normalize(glm::cross(up, m_Z));
m_Y = glm::normalize(glm::cross(m_Z, m_X));

Here, up is the perfect up-direction, eg (0, 1, 0) . 此处, up是理想的向上方向,例如(0, 1, 0)

Note that this will fail if up is parallel to target - m_Position as you will have an unconstrained degree of freedom. 请注意,如果uptarget - m_Position平行,这将失败,因为您将拥有不受限制的自由度。 You would need to add assumptions for that case. 您需要为这种情况添加假设。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM