私は答えがまっすぐではないことを恐れています。あなたのシーンにあるものに応じて、画面上をクリックするとマウス座標が変わるはずです。たとえば、フロントオブジェクトがバックオブジェクトを隠すように2つのオブジェクトがある場合は、フロントオブジェクトの座標を取得する必要があります。これは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
}
enter code here
、あなたは、任意の特定の3Dエンジンを使用していますか? –
いいえNetBeansを使用しています。 –
"私はNetBeansを使用していません。"これはSwingアプリケーションのようですが、正しいのですか?このアプリケーションにMouseListenerを使用していますか?または、GUIの外のユーザーのコンピュータでマウスのクリックを待ち受けようとしていますか?もしそうなら、Javaはこの仕事のための間違ったツールかもしれません。 –