Slick2d | Entity collision detection

前端 未结 1 555
耶瑟儿~
耶瑟儿~ 2021-01-03 10:59

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

相关标签:
1条回答
  • 2021-01-03 11:41

    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.

    0 讨论(0)
提交回复
热议问题