[英]How do i detect collision between 2 rectangles??? LIBGDX
So I have this problem that the Rectangle.overlaps isn't working for some reason or I don't know. 所以我有一个问题,即Rectangle.overlaps由于某种原因不起作用或我不知道。 All this does is it draws a jet (MyActor) and a bullet Actor and on touch it makes the bullet move towards the blue enemy Actor and i want it to System.out.println("touched"); 所有这些操作就是绘制一架飞机(MyActor)和一个子弹Actor,并在触摸时使子弹向蓝色敌方Actor移动,我希望它到达System.out.println(“ touched”); if bullet hits the enemy. 如果子弹击中敌人。
I'll just paste the whole code. 我将粘贴整个代码。
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 () {
}
} }
According to the java documentation , there is a method intersects(Rectangle r)
. 根据java文档 ,有一个方法intersects(Rectangle r)
。 You can use this to determine whether two rectangles intersect. 您可以使用它来确定两个矩形是否相交。 Take a look here . 在这里看看。
The issue here is that the bulletRectangle
is not updated with the new position of the bullet, bulletX
is not directly linked to bulletRectangle
. 这里的问题是bulletRectangle
不会更新为bullet的新位置, bulletX
没有直接链接到bulletRectangle
。
You could do 你可以做
if(started) {
bulletX -= 3;
bulletRectangle.setX(bulletX);
}
You might want also to update the other rectangle with the proper position of the enemy 您可能还想用敌人的正确位置更新另一个矩形
if(enemyRectangle.overlaps(bulletRectangle)){
System.out.println("HIT I SAY");
}
add this code in your update method 将此代码添加到您的更新方法中
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.