< Zurück | Inhalt | Weiter >

16.8 Using picking for collision detection

The Java 3D 1.2 picking utilities can also be used to implement simple collision detection with a scene. The basic idea is to create a custom behavior that checks for picking intersections at runtime. By triggering the behavior in every frame, it is possible to detect collisions between objects and add application logic to respond to them. Unfortunately this is not a 100 percent robust mechanism for detecting collisions—it is possible for an object to be moving so fast that in the time between frames it passes right through an object in the scene.

By the time the behavior is invoked again, the object is no longer in collision, and the intersection will have been missed. One possible application−specific workaround is to do a single frame look ahead (or look behind) to check whether an intersection was missed. A commercial collision detection engine (such as VCollide) provides much more scaleable collision detection and can handle cases such as that just described. If your application relies heavily on collision detection (and there are no cheats that you can use), you should probably investigate a commercial library. Defining a scene with thousands of objects, which could all potentially collide with one another, requires specialized collision detection algorithms that fall outside of Java 3D.


The remainder of this chapter will discuss an example that uses picking to implement simple collision detection. There are a limited number of collidable objects in the scene (10), and the speed of the moving objects has been defined such that it is not possible for the objects to pass through one another between frames (figure 16.3).

image


Figure 16.3 Two frames from the PickCollisionTest. The spheres bounce around within a large box, each side of which is defined by a scaled and translated ColorCube object. The spheres can bounce off one another as well as the sides of the box


 

From PickCollisionTest.java