Java 3d - PickTranslateBehavior - 对象没有移动

时间:2021-04-06 13:14:30

标签: java java-3d

我在 Java3D 中制作了一个球体对象,现在我试图通过鼠标选择使其移动。 因此我使用 PickTranslateBehavior 但对象根本没有移动。

当我使用 PickRotateBehavior 类旋转时,对象正在按照建议旋转。

这是我使用的代码:

import com.sun.j3d.utils.geometry.Sphere;
import com.sun.j3d.utils.picking.PickTool;
import com.sun.j3d.utils.picking.behaviors.PickRotateBehavior;
import com.sun.j3d.utils.picking.behaviors.PickTranslateBehavior;
import com.sun.j3d.utils.picking.behaviors.PickingCallback;
import com.sun.j3d.utils.universe.SimpleUniverse;

import javax.media.j3d.*;
import javax.swing.*;
import javax.vecmath.Color3f;
import javax.vecmath.Point3d;
import javax.vecmath.Vector3f;
import java.awt.*;

public class bla extends JFrame {

    public Canvas3D canvas3D;
    private static final long serialVersionUID = 1L;

    public bla() {
        getContentPane().setLayout(new BorderLayout());
        GraphicsConfiguration graphicsConfiguration = SimpleUniverse.getPreferredConfiguration();

        //Canvas-3d Objekt erstellen
        canvas3D = new Canvas3D(graphicsConfiguration);
        getContentPane().add("Center", canvas3D);

        //ein Simple-Universe erstellen und es beschreiben
        SimpleUniverse simpleUniverse = new SimpleUniverse(canvas3D);
        simpleUniverse.getViewingPlatform().setNominalViewingTransform();

        //Erzeuge eine Appearance fuer die Kugel
        Color3f ambientColourSphere = new Color3f(0.2f, 0.2f, 0.0f);
        Color3f emissiveColourSphere = new Color3f(0.0f, 0.0f, 0.0f);
        Color3f diffuseColourSphere = new Color3f(0.4f, 0.4f, 0.0f);
        Color3f specularColourSphere = new Color3f(0.8f, 0.8f, 0.0f);
        float shininessSphere = 120.0f;

        Appearance sphereApp = new Appearance();

        sphereApp.setMaterial(new Material(ambientColourSphere, emissiveColourSphere,
                diffuseColourSphere, specularColourSphere, shininessSphere));

        Sphere mySphere = new Sphere(0.3f, Sphere.GENERATE_NORMALS, 100, sphereApp);
        Transform3D tfSphere = new Transform3D();
        //tfSphere.setTranslation(new Vector3f(0.0f,0.0f,-1.0f));

        TransformGroup tgSphere = new TransformGroup(tfSphere);
        tgSphere.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
        tgSphere.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
        tgSphere.setCapability(TransformGroup.ENABLE_PICK_REPORTING);

        tgSphere.addChild(mySphere);

        BranchGroup root_obj = new BranchGroup();

        BoundingSphere myBounds = new BoundingSphere(new Point3d( ), 1000.0 );

        PickRotateBehavior pickDreh = new PickRotateBehavior(root_obj, canvas3D, myBounds);
        PickTranslateBehavior pickTrans = new PickTranslateBehavior(root_obj,canvas3D,myBounds,  PickTool.GEOMETRY_INTERSECT_INFO);
        root_obj.addChild(pickTrans);

        //Fuege den Wuerfel und die Kugel zur Szene hinzu.
        root_obj.addChild(tgSphere);

        //Erzeuge einen weissen Hintergrund
        //Background setzen
        Background hintergrund = new Background(new Color3f(Color.lightGray));
        hintergrund.setApplicationBounds(new BoundingSphere(new Point3d(), 1000.0f));
        root_obj.addChild(hintergrund);

        root_obj.compile();

        //Hinzufuegen der Szene
        simpleUniverse.addBranchGraph(root_obj);

        //Fenstereigenschaften
        setSize(400, 400);
        setVisible(true);
    }

    public static void main(String args[]) {
        new uebung_4_3d();
    }
}

有没有人知道我做错了什么或者缺少一些代码来使对象移动?

非常感谢

0 个答案:

没有答案
相关问题