java - 将对象旋转到面点

标签 java math 3d rotation

我想旋转一个物体以面对一个我有点麻烦的点。

所以我从一个以零为底并在 y 轴上对齐的对象开始。

enter image description here

我想旋转它,使物体的顶部朝向目的地

enter image description here

到目前为止,我的流程是: 给定轴 A

  1. 找到我的位置和我的注视位置之间的距离:D
  2. 创建一个方向 vector :V = D.normalize()
  3. 找到正确的 vector :R = A cross D
  4. 求上 vector :U = D cross R
  5. 找到向上和方向之间的角度:ANGLE = acos((U dot D)/(U.length * D.length))
  6. 按每个轴上的方向缩放的角度旋转

这是它的代码表示。我不确定这到底有什么问题我已经在纸上解决了,据我所知,这种方法应该有效,但绘制时结果完全不正确。如果有人看到任何缺陷并能指出我正确的方向,那就太好了。

    Vector3 distance = new Vector3(from.x, from.y, from.z).sub(to.x, to.y, to.z);
    final Vector3 axis = new Vector3(0, 1, 0);
    final Vector3 direction = distance.clone().normalize();

    final Vector3 right = (axis.clone().cross(direction));
    final Vector3 up = (distance.clone().cross(right));

    float angle = (float) Math.acos((up.dot(direction)/ (up.length() * direction.length()))); 
    bondObject.rotateLocal(angle, direction.x , direction.y, direction.z);

最佳答案

这里的基本思想如下。

  • 确定物体朝向的方向:directionA
  • 确定对象应该面向哪个方向:directionB
  • 确定这些方向之间的角度:rotationAngle
  • 确定旋转轴:rotationAxis

这是修改后的代码。

Vector3 distance = new Vector3(from.x, from.y, from.z).sub(to.x, to.y, to.z);

if (distance.length() < DISTANCE_EPSILON)
{
    //exit - don't do any rotation
    //distance is too small for rotation to be numerically stable
}

//Don't actually need to call normalize for directionA - just doing it to indicate
//that this vector must be normalized.
final Vector3 directionA = new Vector3(0, 1, 0).normalize();
final Vector3 directionB = distance.clone().normalize();

float rotationAngle = (float)Math.acos(directionA.dot(directionB));

if (Math.abs(rotationAngle) < ANGLE_EPSILON)
{
    //exit - don't do any rotation
    //angle is too small for rotation to be numerically stable
}

final Vector3 rotationAxis = directionA.clone().cross(directionB).normalize();

//rotate object about rotationAxis by rotationAngle

关于java - 将对象旋转到面点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23692077/

相关文章:

java - 找不到与 graph.facebook.com 匹配的名称(SSLHandshakeException/CertificateException)

java - Spring Data Redis存储库不支持集合查询?

java - 在查询生成器中使用子字符串操作字符串

opengl - gl_ClipVertex 相对于 gl_ClipDistance 如何工作?

c++ - OpenGL 在 3D 空间中渲染 2D

java - 使用 Java 的正则表达式从路径名中提取单词

PHP:如何以相等的概率将值分配给变量?

algorithm - 在列表中查找重复项的最少检查

php - 评级公式设计(概念而非编码)

c# - 将 3D 网格分割为任意大小的 block