< Zurück | Inhalt | Weiter >

From KeyCollisionBehavior.java

//The KeyCollisionBehavior class adds simple collision detection

//to a keyboard behavior

public class KeyCollisionBehavior extends KeyBehavior

{

private CollisionChecker m_CollisionChecker = null;


public KeyCollisionBehavior( TransformGroup tg, CollisionDetector collisionDetector )

{

super( tg );


m_CollisionChecker = new CollisionChecker( tg, collisionDetector, true );

}


//before the TransformGroup is updated, we need to ensure

//that we are not going to walk into anything solid. protected void updateTransform()

{

if( m_CollisionChecker.isCollision( transform3D ) == false ) transformGroup.setTransform(transform3D);

}


//disallow rotation up or down protected void altMove(int keycode)

{

}


//disallow moving up or down

protected void controlMove(int keycode)

{

}

}


image


The main Applet class implements the CollisionDetector interface with its single method, isCollision, as follows. The first method does a quick check to ensure that we are still within the boundaries of our world; if this passes, then the second method is used to check which pixel in the map image corresponds to our 3D world coordinate position (only the x and z coordinates are used).