Picking Scene ObjectsShooter3D'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 rayThere are many variations of this idea, such as using a different pick shape instead of a linea 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 Shape3Ds and OrientedShape3Ds, are pickable, so programmers should switch off picking (with setPickable(false)) in as many nodes as possible to reduce the cost of intersection testing. This is an important optimization in a large scene that may contain thousands of visible items. The information generated by picking can include more than the leaf node: it may include the path from the root of the scene graph (the Locale node) down to the selected node. This can be useful since the path will include the TRansformGroup nodes used to position or orient the selected item. However, this requires that the Group nodes have their ENABLE_PICK_REPORTING capability set to TRue. When picking returns more information (such as the path), processing times increase, and the operation requires the setting of a bewildering range of capability bits. Fortunately, Java 3D's PickTool class offers a simple setCapabilities( ) method for setting the necessary bits in Java 3D's Shape3D nodes. (The Shape3D class is used to build leaf nodes representing geometric shapes.) The method supports three different types (levels) of picking: static void setCapabilities(Node node, int level); The three levels return increasing amounts of information:
What to Pick in Shooter3D?A consideration of the Shooter3D scene (as shown in Figure 23-1) reveals many potentially pickable shapes:
Shooter3D switches off picking for all of these, with the exception of the floor tiles and the red tile at the origin. This matches the intended behavior of the application: the user clicks on a floor tile and the gun shoots at it. Switching off picking capabilities reduces the cost of the intersection calculations and makes the picking result easier to analyze since few things remain that can be picked. Small changes must be made to the familiar CheckerFloor and ColouredTiles classes to disable the picking of the axis labels and to reduce the amount of picking information gathered for the tiles. In CheckerFloor, makeText( ) is employed to create an axis label and now includes a call to setPickable( ): private TransformGroup makeText(Vector3d vertex, String text) // Create a Text2D object at the specified vertex { Text2D message = new Text2D(text, white, "SansSerif", 36, Font.BOLD); message.setPickable(false); // cannot be picked TransformGroup tg = new TransformGroup( ); Transform3D t3d = new Transform3D( ); t3d.setTranslation(vertex); tg.setTransform(t3d); tg.addChild(message); return tg; } In ColouredTile, picking is left on, but the amount of detail returned is set with a call to setCapabilities( ). Only intersection coordinates are requirednot information about the shape's color, normals, etc.so the INTERSECT_COORD picking level is sufficient: public ColouredTiles(ArrayList coords, Color3f col) { plane = new QuadArray(coords.size( ), GeometryArray.COORDINATES | GeometryArray.COLOR_3); createGeometry(coords, col); createAppearance( ); // set the picking capabilities so that intersection // coords can be extracted after the shape is picked PickTool.setCapabilities(this, PickTool.INTERSECT_COORD); } The other objects in the scenethe gun, laser beam, and explosioncontain calls to setPickable(false). ShootingBehaviour extracts and utilizes the intersection information, so a detailed discussion of that side of picking will be delayed until later. Essentially, the class uses a ray to find the intersection point on a tile nearest to the viewer. There's no need to obtain path information about the Group nodes above it in the graph, so there's no need to set ENABLE_PICK_REPORTING capability bits for Group nodes. |