Game Development Reference
In-Depth Information
cannon = new Cannon(0, 0, 1, 1);
ball = new DynamicGameObject(0, 0, 0.2f, 0.2f);
targets = new ArrayList<GameObject>(NUM_TARGETS);
grid = new SpatialHashGrid(WORLD_WIDTH, WORLD_HEIGHT, 2.5f);
for ( int i = 0; i < NUM_TARGETS; i++) {
GameObject target = new GameObject(( float )Math. random () * WORLD_WIDTH,
( float )Math. random () * WORLD_HEIGHT,
0.5f, 0.5f);
grid.insertStaticObject(target);
targets.add(target);
}
cannonVertices = new Vertices(glGraphics, 3, 0, false , false );
cannonVertices.setVertices( new float [] { -0.5f, -0.5f,
0.5f, 0.0f,
-0.5f, 0.5f }, 0, 6);
ballVertices = new Vertices(glGraphics, 4, 6, false , false );
ballVertices.setVertices( new float [] { -0.1f, -0.1f,
0.1f, -0.1f,
0.1f, 0.1f,
-0.1f, 0.1f }, 0, 8);
ballVertices.setIndices( new short [] {0, 1, 2, 2, 3, 0}, 0, 6);
targetVertices = new Vertices(glGraphics, 4, 6, false , false );
targetVertices.setVertices( new float [] { -0.25f, -0.25f,
0.25f, -0.25f,
0.25f, 0.25f,
-0.25f, 0.25f }, 0, 8);
targetVertices.setIndices( new short [] {0, 1, 2, 2, 3, 0}, 0, 6);
}
We can bring over a lot from the CannonGravityScreen . We start off with a couple of constant
definitions, governing the number of targets and our world's size. Next, we have the GLGraphics
instance, as well as the objects for the cannon, the ball, and the targets, which we store in
a list. We also have a SpatialHashGrid , of course. For rendering our world, we need a few
meshes: one for the cannon, one for the ball, and one to render each target. Remember that we
only needed a single rectangle in BobTest to render the 100 Bobs to the screen. We reuse that
principle here, by having only a single Vertices instance holding the triangles (rectangles) of our
targets. The last two members are the same as those in the CannonGravityTest . We use them to
shoot the ball and apply gravity when the user touches the screen.
The constructor does all the things discussed previously. Instantiate our world objects and
meshes. The only interesting thing is that we also add the targets as static objects to the spatial
hash grid.
Now check out the next method of the CollisionTest class in Listing 8-13.
Listing 8-13. Excerpt from CollisionTest.java; the update() Method
@Override
public void update( float deltaTime) {
List<TouchEvent> touchEvents = game.getInput().getTouchEvents();
game.getInput().getKeyEvents();
Search WWH ::




Custom Search