实现此目的的经典方法是与每个实体关联一个Shape boundingBox,然后在形状上使用Slick2d的“相交”方法:
http://www.slick2d.org/javadoc/org/newdawn/slick/geom/Shape.html#intersects(org.newdawn.slick.geom.Shape)
我相信有一种方法可以对精灵进行逐像素检查,但是如果您不需要像素级精度,则包围盒方法会更有效。
一些代码:
在您的Entity抽象类中添加:
private Shape boundingBox;public Shape getBoundingBox() { return this.boundingBox;}然后,您的相交方法是:
public boolean intersects(Entity entity) { if (this.getBoundingBox() == null) { return false; } return this.getBoundingBox().intersects(entity.getBoundingBox());}然后,您需要为要检查碰撞的每个实体设置一个边界框。



