Java Reference
In-Depth Information
Figure 17. First code segment for collision detection
1.
public class Collision extends Behavior
2.
{
3.
private TransformGroup collisiontran, collisionbodyTrans;
4.
private Shape3D target;
5.
private WakeupCriterion events[] = new WakeupCriterion[3]; private WakeupOr allEvents;
6.
private BoundingSphere bounds
= new BoundingSphere(new
7.
Point3d(0.0,0.0,0.0), 0.1);
8.
9.
public Collision(TransformGroup collisiontran, Shape3D target, TransformGroup collisionbodyTrans)
10.
{
11.
this.collisiontran = collisiontran;
12.
this.collisionbodyTrans = collisionbodyTrans;
13.
this.target = target;
14.
}
15.
16.
public void initialize()
17.
{
18.
events[0]=new WakeupOnCollisionEntry(collisionbodyTrans);
19.
events[1]=new WakeupOnCollisionExit(collisionbodyTrans);
20.
events[2]=new WakeupOnCollisionMovement(collisionbodyTrans);
21.
allEvents=new WakeupOr(events);
22.
wakeupOn(allEvents);
23.
}
24.
Based on the triggers generated from the above collision criteria, appropriate methods
must be provided to, say, prevent objects from passing through one another or for the viewer
to walk through solid walls in the 3D universe (The Colliding Grabbers, 2006).
Figures 17 and 18 give the code segment for handling collisions in a navigation example.
As with other behavior classes, the customized collision behavior class contains an initialize
and a processStimulus method. The former sets the collision detection criteria of interest,
while the latter detects the collision type and takes the necessary actions. Specifically,
in lines 60 and 61, the method CollisionEvent is invoked to prevent a collision whenever
WakeupOnCollisionEntry is detected.
CollisionEvent prevents collision through re-position the relevant colliding object ap-
propriately. Note that the new position of the object after collision is crucial as it must be
fully out of the collision zone. Lines 27 to 54 illustrates this.
Search WWH ::




Custom Search