< Zurück | Inhalt | Weiter >

From KeyNavigateTest.java


//return true if the Transform3D would put us into collision

//with a “solid” object in the world.

public boolean isCollision( Transform3D t3d, boolean bViewSide )

{

//get the translation from the Transform3D t3d.get( m_Translation );


//we need to scale up by the scale that was applied to the root TG

//on the view side of the scenegraph if( bViewSide != false )

m_Translation.scale( 1.0 / getScale() ); Vector3d mapSquareSize = getMapSquareSize();

//first check that we are still inside the "world"

//because we can’t walk outside it

if( m_Translation.x <−FLOOR_WIDTH + mapSquareSize.x ||

m_Translation.x > FLOOR_WIDTH − mapSquareSize.x || m_Translation.y <−FLOOR_LENGTH + mapSquareSize.y || m_Translation.y > FLOOR_LENGTH − mapSquareSize.y )

return true;


//then do a pixel based look up using the map if( bViewSide != false )

return isCollision( m_Translation );


return false;

}


image


If the very fast check that we are still inside the world passes, then we need to look up the pixel in the map image that our new position will fall within. Once we have queried the color of that pixel, we will know if we can enter that location.


//return true if the given x,z location in the world

//corresponds to a wall section

protected boolean isCollision( Vector3d worldCoord )

{

Point2d point = convertToMapCoordinate( worldCoord ); int nImageWidth = m_MapImage.getWidth();

int nImageHeight = m_MapImage.getHeight();


//outside of image

if( point.x <0 || point.x >= nImageWidth || point.y <0 || point.y >= nImageHeight ) return true;


int color = m_MapImage.getRGB( (int) point.x, (int) point.y );


//we can't walk through walls or bookcases

return( color == m_ColorWall || color == m_ColorBookcase );

}


This very simple grid−based collision detection algorithm works fairly well for this application since it exploits knowledge of the scene, as well as constraint on the user’s movement. Incidentally, the guard objects that move around the scene also hook into the same CollisionDetection interface implemented by the application object. For these objects, however, bViewSize = true, and they are allowed to penetrate through walls and bookcases to catch the unwary by surprise.


Another neat feature of the example is the use of transparent bitmaps for both water and flaming torches. The flaming torches also use a computationally inexpensive and simple form of texture animation, which is discussed in the next section.