恐怕答案不是直截了当的。根据场景中的内容,单击屏幕时鼠标坐标应该会发生变化。例如,如果您有两个对象,使得前面的对象遮挡后面的对象,那么您需要获取前面对象的坐标。这被称为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
}