3

The problem:

I know this has been asked for many times, but I didnt find any good answers.
So I have got some entities for my game, now what is the best way for checking collisions?

Links:

The game (finished)

Code explanation:
I've got a list of entities in my world class:

List<Entity> entities = new ArrayList<Entity>();
// The list in the World class

I update them with this code (only if inside the view range):

public void update(GameContainer gc, int delta) {
    for (int i = 0; i < entities.size(); i++) {
        entities.get(i).update(gc, delta);
    }
}
// Update entities

So now I wanna check for collision inside the entities update method.

I tried this as my update method:

@Override
public void update(GameContainer gc, int delta) {
    for (Entity entity : world.entities) {
        if (intersects(entity)) {
           // world.remove(this);
        }
    }
}
// The update method of an entitiy

And this is the current instersects method:

public boolean intersects(Entity entity) {
    if (entity instanceof Player) {
        // Do nothing
    } else {
        if (shape != null) {
            return this.getShape().intersects(entity.getShape());
        }
    }

    return false;
}
// The intersects method of the abstract Entity class

Currently the intersects method always returns true. But why?

HUNeater
  • 166
  • 3
  • 16

1 Answers1

6

The classic way to do this is to have a Shape boundingBox associated with each Entity and then use Slick2d's "intersects" method on the shapes:

http://www.slick2d.org/javadoc/org/newdawn/slick/geom/Shape.html#intersects(org.newdawn.slick.geom.Shape)

I believe there is a way to do per-pixel checks on sprites but the bounding box method is more efficient if you don't need pixel-level accuracy.

Some code:

In your Entity abstract class add:

private Shape boundingBox;

public Shape getBoundingBox() {
  return this.boundingBox;
}

Then your intersects method is:

public boolean intersects(Entity entity) {
    if (this.getBoundingBox() == null) {
        return false;
    }
    return this.getBoundingBox().intersects(entity.getBoundingBox());
}

Then you need to set up a bounding box for each Entity that you want to check for collisions.

disperse
  • 1,216
  • 10
  • 22
  • I tried your solution, but not works. The method always returns true. – HUNeater Apr 17 '13 at 19:51
  • 1
    What's missing here is setting the initial size of the boundingBox and binding it to the location of the sprite. Make sure you set the boundingBox size appropriately and update the location of the shape. – disperse Apr 18 '13 at 12:41
  • I already changed the whole entity code to use shapes instead of the x, y, width and hight values. Please check the pastebin sources, maybe you can find something. – HUNeater Apr 18 '13 at 12:56
  • I'm glad you were able to get it working, best of luck with your game. – disperse Apr 18 '13 at 20:14