0

我希望下面的代码翻译 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();
    }
}

嗯......它似乎确实翻译了一瞬间,然后旋转开始并撤消翻译。

4

1 回答 1

0

解决这个问题的方法是让 tgRot 成为 tg 的父节点,让 rotator 查看 tgRot,这样树最终看起来像这样:

BranchGroup root
---- TransformGroup tgRot
     ---- RotationInterpolator rotator
     ---- TransformGroup tg
          ---- ColorCube cube
          ---- Transform3D translator
于 2013-02-21T18:26:59.390 回答