Slick2d | Entity collision detection

2019-02-11 04:53发布

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?

标签: java slick2d
1条回答
等我变得足够好
2楼-- · 2019-02-11 05:18

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.

查看更多
登录 后发表回答