我用以下代码在 Java 3D 中绘制了一个圆锥体:
Cone cone = new Cone(2f, 3f);
Transform3D t3d = new Transform3D();
TransformGroup coneTransform = new TransformGroup(t3d);
coneTransform.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
t3d.setTranslation(new Vector3f(0f,0f,0f);
coneTransform.setTransform(t3d);
coneTransform.addChild(cone);
this.addChild(coneTransform);
假设我将圆锥体放在点 (1,1,1) 上,我希望圆锥体的尖端指向一条穿过 (0,0,0) 和 (1,1,1) 的假想线。 .. 我该怎么做?
这是我一直在尝试的示例:
Transform3D t3d = new Transform3D();
Vector3f direction = new Vector3f(1,2,1);
final double angleX = direction.angle(new Vector3f(1,0,0));
final double angleY = direction.angle(new Vector3f(0,1,0));
final double angleZ = direction.angle(new Vector3f(0,0,1));
t3d.rotX(angleX);
t3d.rotY(angleY);
t3d.rotZ(angleZ);
t3d.setTranslation(direction);
coneTransform.setTransform(t3d);
提前感谢所有帮助!
最佳答案
目前我自己正在学习 Java 3D,根据我目前的知识,旋转方法将变换设置为仅围绕该轴旋转。
因此,如果您希望围绕多个轴执行旋转,那么您将需要使用第二个 Transform3D。
即:
Transform3D rotation = new Transform3D();
Transform3D temp = new Transform3D();
rotation.rotX(Math.PI/2);
temp.rotZ(Math.PI/2);
rotation.mul(temp); // multiply the 2 transformation matrices together.
至于Math.PI的原因,这是因为它使用的是弧度而不是度数,其中Math.PI相当于180度。
找到当前方向和预期方向之间的角度并不难 - 您可以使用 Vector3fs 和 angle() 方法。 Vector 将设置为初始方向,另一个设置为预期方向。 但是,这并没有告诉您角度位于哪个轴上。这样做需要检查 vector 以查看设置了哪些段。 [当然,API 中可能还有一些我目前不知道的东西]
关于java - 如何在 Java 3D 中旋转对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1330727/