The Spatial Hash Grid

Our cannon will be bounded by a rectangle of 1 x1 meters, the cannonball will have a bounding rectangle of 0.2x0.2 meters, and the targets will each have a bounding rectangle of 0.5x0.5 meters. The bounding rectangles are centered around each object's positions to make our lives a little easier.

When our cannon example starts up, we'll simply place a number of targets at random positions. Here's how we could set up the objects in our world:

DynamicGameObject ball = new DynamicGame0bject(0, 0, 0.2f, 0.2f); Game0bject[] targets = new Game0bject[NUM_TARGETS]; for(int i = 0; i < NUM_TARGETS; i++) {

targets[i] = new Game0bject((float)Math.random() * W0RLD_WIDTH,

(float)lMath. random() * W0RLD_HEIGHT, 0.5f, 0.5f);

The constants W0RLD_WIDTH and W0RLD_HEIGHT define the size of our game world. Everything should happen inside the rectangle bounded by (0,0) and (W0RLD_WIDTH,W0RLD_HEIGHT). Figure 8-17 shows a little mock-up of our game world so far.

0 0

Post a comment