Wil*_*sio 5 java math 3d rotation
我想旋转一个物体面对一个我有点麻烦的点.
所以我开始使用一个基数为零的对象,并在y轴上对齐.

我想旋转它,使对象的顶部朝向目的地

到目前为止,我的过程是:给定轴A
这是代码表示.我不确定这究竟是什么问题我已经在纸上解决了问题,据我所知,这种方法应该有效,但结果在绘制时完全不正确.如果有人看到任何瑕疵,并指出我正确的方向,那将是伟大的.
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);
Run Code Online (Sandbox Code Playgroud)
Tim*_*lds 13
这里的基本思想如下.
directionAdirectionBrotationAnglerotationAxis这是修改后的代码.
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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2387 次 |
| 最近记录: |