我如何检测2个矩形之间的碰撞??? LIBGDX

时间:2016-03-08 13:27:38

标签: java libgdx

所以我有这个问题,Rectangle.overlaps由于某种原因不起作用或我不知道。 所有这一切都是它绘制一个喷气机(MyActor)和一个子弹演员和触摸它使子弹移向蓝色的敌人演员,我想要它到System.out.println(“触摸”);如果子弹击中敌人。

我只需粘贴整个代码。

public class MyGdxGame implements ApplicationListener{

private Texture texture;
private Texture bulletTexture;
private Texture enemyTexture;
private MyActor myActor;
private BulletActor bulletActor;
private EnemyActor enemyActor;
private Stage stage;
private BackGroundActor backGroundActor;

private Rectangle bulletRectangle;

private Rectangle enemyRectangle;
float X=500,Y =600;

float bulletX = X, bulletY = Y+43;
float enemyX =0, enemyY = 615;
boolean started;
int touched = 0;

public class EnemyActor extends Actor{

    @Override
    public void draw(Batch batch, float parentAlpha) {
        batch.draw(enemyTexture,enemyX,enemyY);
    }

    @Override
    public void act(float delta) {
        if(bulletRectangle.overlaps(enemyRectangle)){
            System.out.println("Overlaps");
        }
    }
}

public class BackGroundActor extends Actor{
    public BackGroundActor() {
        setBounds(0,0,1280,720);
        addListener(new InputListener(){
            @Override
            public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
                started = true;
                touched++;
                System.out.println(touched);
                return true;
            }
        });
    }
}

public class MyActor extends Actor{
    @Override
    public void draw(Batch batch, float parentAlpha) {
        batch.draw(texture,X,Y);
    }
}

public class BulletActor extends Actor{

    @Override
    public void draw(Batch batch, float parentAlpha) {
        batch.draw(bulletTexture,bulletX,bulletY);
    }

    @Override
    public void act(float delta) {
        if(started) {
            bulletX -= 3;
        }
    }
}
@Override
public void create() {

    texture = new Texture("C:\\Users\\User\\Documents\\LibGdxMainProjects\\SampleGame1\\android\\assets\\0001.png");
    bulletTexture = new Texture("C:\\Users\\User\\Documents\\LibGdxMainProjects\\SampleGame1\\android\\assets\\bullet.png");
    enemyTexture = new Texture("C:\\Users\\User\\Documents\\LibGdxMainProjects\\SampleGame1\\android\\assets\\fuel.png");

    bulletRectangle = new Rectangle(bulletX,bulletY,bulletTexture.getWidth(),bulletTexture.getHeight());
    enemyRectangle = new Rectangle(enemyX,enemyY,enemyTexture.getWidth(),enemyTexture.getHeight());

    stage = new Stage();
    myActor = new MyActor();
    enemyActor = new EnemyActor();
    bulletActor = new BulletActor();
    backGroundActor = new BackGroundActor();
    backGroundActor.setTouchable(Touchable.enabled);

    stage.addActor(bulletActor);
    stage.addActor(myActor);
    stage.addActor(backGroundActor);
    stage.addActor(enemyActor);

    Gdx.input.setInputProcessor(stage);

}

@Override
public void dispose() {
}

@Override
public void render() {
    Gdx.gl.glClearColor(1, 1, 1, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    stage.act(Gdx.graphics.getDeltaTime());
    stage.draw();

    if(enemyRectangle.overlaps(bulletRectangle)){
        System.out.println("HIT I SAY");
    }

}

@Override
public void resize ( int width, int height){

}

@Override
public void pause () {

}

@Override
public void resume () {

}

}

3 个答案:

答案 0 :(得分:0)

根据java documentation,有一种方法intersects(Rectangle r)。您可以使用它来确定两个矩形是否相交。看看here

答案 1 :(得分:0)

此处的问题是bulletRectangle未使用项目符号的新位置进行更新,bulletX未直接与bulletRectangle相关联。

你可以做到

if(started) {
    bulletX -= 3;
    bulletRectangle.setX(bulletX);
}

您可能还希望使用敌人的正确位置更新另一个矩形

答案 2 :(得分:0)

if(enemyRectangle.overlaps(bulletRectangle)){
    System.out.println("HIT I SAY");
}

在您的更新方法中添加此代码