如何根据鼠标点击获取3D点的坐标?

时间:2011-01-31 22:17:36

标签: java 3d java-3d

当我点击屏幕上的特定点时,我想要一个函数调用,它返回我点击的3d场景中的点。

例如,当我点击屏幕的左上角时,它应该返回x = 0,y = 1,z = 1;请帮我创建一个方法来做到这一点。

修改

Root = new BranchGroup();

setLayout(new BorderLayout());
add(canvas3D,BorderLayout.CENTER);
SimpleUniverse universe = new SimpleUniverse(canvas3D);

Shape();
universe.addBranchGraph(Root);
ViewingPlatform viewingPlatform = universe.getViewingPlatform();
OrbitBehavior behavior = new OrbitBehavior(canvas3D);
behavior.setSchedulingBounds(bounds);
viewingPlatform.setViewPlatformBehavior(behavior);
viewingPlatform.setNominalViewingTransform();
}

我在NetBeans中使用SimpleUniverse

2 个答案:

答案 0 :(得分:3)

我担心答案不是直截了当的。根据场景中的内容,单击屏幕时鼠标坐标应该会发生变化。例如,如果你有两个物体使前面的物体遮挡后面的物体那么你就想要获得前面的物体坐标。这称为mouse picking,有几种技术可供选择。我发现了一些论坛,解释了它是如何完成的,并有代码示例。

基本上,我们的想法是,您可以想象在用户和屏幕之间存在激光(或其他投射光线的东西)。然后,这个东西投射光线通过屏幕表面上的点,鼠标点击“进入”屏幕。然后将拾取光线路径上的任何内容,并且可选地解决光线路径中对象的遮挡顺序,以便为您提供最接近“屏幕”的对象。

我不熟悉J3D,但是从一些教程中删除了以下代码。它应该至少让你开始。你正在寻找的是这一行Point3D intercept = ...在底部。

http://www.java3d.org/selection.html

package j3d_picking;

import java.awt.*;
import javax.swing.*;
import javax.media.j3d.*;
import com.sun.j3d.utils.universe.*;
import com.sun.j3d.utils.picking.behaviors.*;
import com.sun.j3d.utils.geometry.*;
import com.sun.j3d.utils.picking.PickIntersection;
import com.sun.j3d.utils.picking.PickResult;
import com.sun.j3d.utils.picking.PickTool;
import javax.vecmath.Point3d;

public class HelloJava3D
        extends JFrame
{

    public HelloJava3D()
    {
        GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration();
        Canvas3D canvas3D = new Canvas3D(config);

        BranchGroup scene = createSceneGraph();

        // SimpleUniverse is a Convenience Utility class
        SimpleUniverse simpleU = new SimpleUniverse(canvas3D);

        // This moves the ViewPlatform back a bit so the
        // objects in the scene can be viewed.
        simpleU.getViewingPlatform().setNominalViewingTransform();

        BoundingSphere behaveBounds = new BoundingSphere();
        ExamplePickBehavior behavior = new ExamplePickBehavior(canvas3D, scene, behaveBounds);
        scene.addChild(behavior);

        scene.compile();
        simpleU.addBranchGraph(scene);

        getContentPane().add(canvas3D, BorderLayout.CENTER);
    } // end of HelloJava3D (constructor)

    public BranchGroup createSceneGraph()
    {
        // Create the root of the branch graph
        BranchGroup objRoot = new BranchGroup();

        // Create a simple shape leaf node, add it to the scene graph.
        // ColorCube is a Convenience Utility class
        ColorCube cube = new ColorCube(0.4);
        cube.setCapability(Node.ENABLE_PICK_REPORTING);
        PickTool.setCapabilities(cube, PickTool.INTERSECT_FULL);
        objRoot.addChild(cube);

        return objRoot;
    } // end of createSceneGraph method of HelloJava3D

    public static void main(String[] args)
    {
        JFrame frame = new HelloJava3D();
        frame.setTitle("Hello Java3D");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setBounds(0, 0, 400, 300);
        frame.setVisible(true);
    }

    private class ExamplePickBehavior extends PickMouseBehavior
    {

        public ExamplePickBehavior(Canvas3D canvas, BranchGroup bg, Bounds bounds)
        {
            super(canvas, bg, bounds);
            setSchedulingBounds(bounds);

            pickCanvas.setMode(PickTool.GEOMETRY_INTERSECT_INFO);
            // allows PickIntersection objects to be returned
        }

        public void updateScene(int xpos, int ypos)
        {
            pickCanvas.setShapeLocation(xpos, ypos);
            // register mouse pointer location on the screen (canvas)

            Point3d eyePos = pickCanvas.getStartPosition();
            // get the viewer's eye location

            PickResult pickResult = null;
            pickResult = pickCanvas.pickClosest();
            // get the intersected shape closest to the viewer

            if (pickResult != null) {
                PickIntersection pi = pickResult.getClosestIntersection(eyePos);
                // get the closest intersect to the eyePos point
                Point3d intercept = pi.getPointCoordinatesVW();
                System.out.println(intercept);
                // extract the intersection pt in scene coords space
                // use the intersection pt in some way...
            }
        } // end of updateScene(  )
    } // end of ExamplePickBehavior class
}

答案 1 :(得分:-1)

enter code here下面的Java代码在3d屏幕中打印3d对象的(形状)中心坐标。结果是(x = -0.5,y = 0.0,z = 0.4)

         public class secim2 extends MouseAdapter{


private PickCanvas pickCanvas;





public secim2(){
    JFrame pencere=new JFrame();
    pencere.setSize(300, 300);
    pencere.setVisible(true);
    JFrame frame = new JFrame(" 3D Box Select");

    GraphicsConfiguration config =                 SimpleUniverse.getPreferredConfiguration();

    Canvas3D canvas = new Canvas3D(config);

    SimpleUniverse universe = new SimpleUniverse(canvas);

    BranchGroup group = new BranchGroup();


    // create a color cube


       Transform3D transform= new Transform3D();
    Vector3d vector = new Vector3d(-0.5, 0.0, 0.4);

        Transform3D transform = new Transform3D();

        transform.setTranslation(vector);

        TransformGroup transformGroup = new TransformGroup(transform);

        ColorCube cube = new ColorCube(0.1f);

        transformGroup.addChild(cube);

        group.addChild(transformGroup);


        universe.getViewingPlatform().setNominalViewingTransform();

        universe.addBranchGraph(group);


      pickCanvas = new PickCanvas(canvas, group);

      pickCanvas.setMode(PickCanvas.GEOMETRY_INTERSECT_INFO);

     pencere.add(canvas);

      canvas.addMouseListener(this);


}

public void mouseClicked(MouseEvent e)

{



    pickCanvas.setShapeLocation(e);

    PickResult result = pickCanvas.pickClosest();

    if (result == null) {


    } else {

       Primitive p = (Primitive)result.getNode(PickResult.PRIMITIVE);  

       Shape3D s = (Shape3D)result.getNode(PickResult.SHAPE3D);

       if (p != null) {

         System.out.println(p.getClass().getName());


       } else if (s != null) {

             System.out.println(s.getClass().getName());

             Vector3f position = new Vector3f(); 

      s.getLocalToVworld(transform);  

        transform.get(position);

       System.out.print(position);

      // System.out.print( s.getLocalToVworld(transform2);
       } else{

          System.out.println("null");

       }

    }

}

}

public class tuval1 {

public static void main(String[] args) {
    // TODO Auto-generated method stub

new secim2();

}

}