我期望以下代码翻译ColorCube,然后围绕它自己旋转,而不是翻译,它只是围绕原点旋转。我在这里做错了什么?
import javax.media.j3d.Alpha;
import javax.media.j3d.BoundingSphere;
import javax.media.j3d.BranchGroup;
import javax.media.j3d.RotationInterpolator;
import javax.media.j3d.Transform3D;
import javax.media.j3d.TransformGroup;
import javax.media.j3d.View;
import javax.vecmath.Vector3f;
import com.sun.j3d.utils.geometry.ColorCube;
import com.sun.j3d.utils.universe.SimpleUniverse;
public class TestRotTrans {
public TestRotTrans(){
SimpleUniverse su = new SimpleUniverse();
BranchGroup root = new BranchGroup();
TransformGroup tg = new TransformGroup();
tg.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
root.addChild(tg);
// Move to center
Transform3D translater = new Transform3D();
translater.setTranslation(new Vector3f(-0.3f, -0.3f, -0.3f));
tg.setTransform(translater);
// Rotate around y
TransformGroup tgRot = new TransformGroup();
RotationInterpolator rotator = new RotationInterpolator(new Alpha(-1,5000), tg);
BoundingSphere bounds = new BoundingSphere();
rotator.setSchedulingBounds(bounds);
tgRot.addChild(rotator);
tg.addChild(tgRot);
tg.addChild(new ColorCube(0.4f));
su.addBranchGraph(root);
su.getViewingPlatform().setNominalViewingTransform();
su.getViewer().getView().setProjectionPolicy(View.PERSPECTIVE_PROJECTION);
}
public static void main(String[] args) {
new TestRotTrans();
}
}
嗯......在旋转开始并撤消翻译之前,它确实会翻译一瞬间。
答案 0 :(得分:0)
该问题的解决方案是使tgRot成为tg的父级,并让旋转器查看tgRot,以便树最终看起来像这样:
BranchGroup root
---- TransformGroup tgRot
---- RotationInterpolator rotator
---- TransformGroup tg
---- ColorCube cube
---- Transform3D translator