Game Development Reference
In-Depth Information
if(distance >= RANGE){
alive = false;
}
5. The Bullet class also has a checkCollision method. It takes a list of tar-
gets as input and tries a collision between each of them and the ray. If any colli-
sion is detected, alive will be set to false and the closest CollisionRes-
ult will be returned to the calling method as follows:
public CollisionResult
checkCollision(List<Collidable> targets){
CollisionResults collRes = new CollisionResults();
for(Collidable g: targets){
g.collideWith(ray, collRes);
}
if(collRes.size() > 0){
alive = false;
return collRes.getClosestCollision();
}
return null;
}
6. Next, we'll add some code to the application class. It needs to keep track of
List<Collidable> , called targets and List<Bullet> , called bul-
lets .
7. The simpleUpdate method updates the movement of all bullets by calling
their update method before seeing whether any collisions have occurred or not.
Any depleted bullets are removed in a way that avoids ArrayIn-
dexOutOfBounds exceptions:
Bullet b = bullets.get(i);
b.update(tpf);
CollisionResult result = b.checkCollision(targets);
if(result != null){
System.out.println("hit " + result);
}
if(!b.isAlive()){
bullets.remove(b);
bulletAmount--;
Search WWH ::




Custom Search