Picking Scene Objects
Shooter3D
's gun shoots at a point on the floor selected by the user clicking the mouse. The cursor position on the screen is translated into scene coordinates using Java 3D's picking. In general, picking is a more user-friendly way of obtaining input than asking the user to type in a coordinate or the name of an object.
Picking is the selection of a shape (or shapes) in the scene, usually accomplished by having the user click the mouse while the pointer is over a particular shape. This is implemented by projecting a pick shape (a line or ray) into the scene from the user's viewpoint, through the mouse pointer position on screen, to intersect with the nearest object in the scene (see Figure 23-3).
Figure 23-3. Picking using a ray
There are many variations of this idea, such as using a different pick shape instead of a line—a cone or cylinder. Another possibility is to return a list of all the intersected objects rather than the one nearest to the viewer.
The selected object will correspond to a leaf node in the scene graph. Leaf nodes are the visual parts of the scene, such as meshes and models, and internal nodes are typically Group
nodes (or subclasses of Group
).
By default, leaf nodes, such as Shape3D
s and OrientedShape3D
s, are pickable, so programmers should switch off picking (with setPickable(false)
) in as many nodes as possible to reduce the cost of intersection ...
Get Killer Game Programming in Java now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.