检查 libgdx 中 2 个对象的碰撞
Check collision of 2 objects in libgdx
我正在 libgdx 制作一款游戏,您可以在其中用子弹射击外星人。我有 2 ArrayList
个对象,想检查 bulletArrayList
中的任何对象是否与 alienArrayList
中的任何对象发生碰撞。最好的方法是什么?我在想contactListener
。
在屏幕中 class 我正在生成这样的对象:
public class PlayScreen implements Screen, InputProcessor,ContactListener {
public ArrayList<Alien> alienArrayList = new ArrayList<Alien>();
public ArrayList<Bullet> bulletArrayList = new ArrayList<Bullet>();
public void generateAlien() {
alien = new Alien();
alienArrayList.add(alien);
}
public void shootBullet(float x, float y) {
//send x,y moving coordiantes
bullet = new Bullet(x,y);
bulletArrayList.add(bullet);
}
}
在对象 class 中,我有 Rectangle
盒子,我要像这样移动它:
public class Alien {
public Alien() {
bound = new Rectangle( x, y, alienRegion.getRegionWidth(), alienRegion.getRegionHeight());
}
public void update(float delta) {
bound.y -= speed * delta;
}
public void render(SpriteBatch batch, float delta) {
update(delta);
elapsedTime += Gdx.graphics.getDeltaTime();
alienRegion = (TextureRegion) alien.getKeyFrame(elapsedTime, true);
batch.draw(alienRegion, getBound().x, getBound().y);
}
}
因为您在 Alien
class 中使用 Rectangle
,我们可以使用名为 Intersector
的 class,它具有静态方法来检查碰撞检测。
for(Alien alien1: alienArrayList) {
for(Alien alien2 : bulletArrayList) {
if(Intersector.overlaps(alien1.rect, alien2.rect)) {
// Collision code
}
}
}
首先,我们使用 嵌套的特殊 for 循环遍历两个列表。然后我们将两个 Rectangle
传递给 Intersector.overlaps(rect1, rect2)
。这是 Intersector
class 中定义的静态方法,如果矩形重叠,它将 return 为真。
此外,此代码可以直接进入您的 render
方法。
这段代码不是最优化的,因为它会检查 2 个矩形两次,但是,我会把优化留给你。
我希望这个回答对您有所帮助,如果您有任何其他问题,请随时 post 在下面发表评论。
我正在 libgdx 制作一款游戏,您可以在其中用子弹射击外星人。我有 2 ArrayList
个对象,想检查 bulletArrayList
中的任何对象是否与 alienArrayList
中的任何对象发生碰撞。最好的方法是什么?我在想contactListener
。
在屏幕中 class 我正在生成这样的对象:
public class PlayScreen implements Screen, InputProcessor,ContactListener {
public ArrayList<Alien> alienArrayList = new ArrayList<Alien>();
public ArrayList<Bullet> bulletArrayList = new ArrayList<Bullet>();
public void generateAlien() {
alien = new Alien();
alienArrayList.add(alien);
}
public void shootBullet(float x, float y) {
//send x,y moving coordiantes
bullet = new Bullet(x,y);
bulletArrayList.add(bullet);
}
}
在对象 class 中,我有 Rectangle
盒子,我要像这样移动它:
public class Alien {
public Alien() {
bound = new Rectangle( x, y, alienRegion.getRegionWidth(), alienRegion.getRegionHeight());
}
public void update(float delta) {
bound.y -= speed * delta;
}
public void render(SpriteBatch batch, float delta) {
update(delta);
elapsedTime += Gdx.graphics.getDeltaTime();
alienRegion = (TextureRegion) alien.getKeyFrame(elapsedTime, true);
batch.draw(alienRegion, getBound().x, getBound().y);
}
}
因为您在 Alien
class 中使用 Rectangle
,我们可以使用名为 Intersector
的 class,它具有静态方法来检查碰撞检测。
for(Alien alien1: alienArrayList) {
for(Alien alien2 : bulletArrayList) {
if(Intersector.overlaps(alien1.rect, alien2.rect)) {
// Collision code
}
}
}
首先,我们使用 嵌套的特殊 for 循环遍历两个列表。然后我们将两个 Rectangle
传递给 Intersector.overlaps(rect1, rect2)
。这是 Intersector
class 中定义的静态方法,如果矩形重叠,它将 return 为真。
此外,此代码可以直接进入您的 render
方法。
这段代码不是最优化的,因为它会检查 2 个矩形两次,但是,我会把优化留给你。
我希望这个回答对您有所帮助,如果您有任何其他问题,请随时 post 在下面发表评论。