How do i detect collision between 2 rectangles??? LIBGDX

WannabeHacker

So I have this problem that the Rectangle.overlaps isn't working for some reason or I don't know. 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"); 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 () {

}

}

Hacketo

The issue here is that the bulletRectangle is not updated with the new position of the bullet, bulletX is not directly linked to 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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

libGDX, detect side touching between rectangles (side collision)

From Dev

Collision detection between rectangles (no overlap) - libgdx

From Dev

How do I implement a collision response between two non-rotatable rectangles

From Dev

How to resolve a collision between two rectangles in a 2D grid?

From Dev

How do I detect collision with spawned objects?

From Dev

How do I detect collision with spawned objects?

From Dev

Detect collision between two bodies in Box2d and libgdx(android)

From Dev

Detect collision between two bodies in Box2d and libgdx(android)

From Dev

How to create a circle variable in Monogame and detect collision with other circles/rectangles

From Dev

How do I detect the collision of two sprites using pygame?

From Dev

How do I detect collision with C++, OpenGL, and freeglut?

From Dev

How do I detect the collision of two sprites using pygame?

From Dev

libGDX - Trying to implement collision between 2 sprites

From Dev

How can I rotate Rectangles in Libgdx?

From Dev

Libgdx and Box2d Detect Collision of two specific bodies

From Dev

How do I detect if a sprite was touched in Java libGDX?

From Dev

How do I detect if a sprite was touched in Java libGDX?

From Dev

How to detect collision between two sprites

From Dev

How to detect collision between SKSpriteNode and SKView SpriteKit

From Dev

Detect a collision between a CCSprite

From Dev

How do I implement simple 2d ball collision?

From Dev

Collision detection between two rectangles in java

From Dev

Check collision between rectangles AS3

From Dev

Detecting Collision between two filled Rectangles

From Dev

How can I detect and resolve collision in my 2D tilebased game?

From Dev

How do I draw the outline of a collection of rectangles?

From Dev

Ran out of short values to detect collision in LIBGDX

From Dev

How to detect collision between two objects in JavaScript with Three.js?

From Dev

How do I do collision between two RectangleShapes in SFML 2.5.0? (C++)

Related Related

  1. 1

    libGDX, detect side touching between rectangles (side collision)

  2. 2

    Collision detection between rectangles (no overlap) - libgdx

  3. 3

    How do I implement a collision response between two non-rotatable rectangles

  4. 4

    How to resolve a collision between two rectangles in a 2D grid?

  5. 5

    How do I detect collision with spawned objects?

  6. 6

    How do I detect collision with spawned objects?

  7. 7

    Detect collision between two bodies in Box2d and libgdx(android)

  8. 8

    Detect collision between two bodies in Box2d and libgdx(android)

  9. 9

    How to create a circle variable in Monogame and detect collision with other circles/rectangles

  10. 10

    How do I detect the collision of two sprites using pygame?

  11. 11

    How do I detect collision with C++, OpenGL, and freeglut?

  12. 12

    How do I detect the collision of two sprites using pygame?

  13. 13

    libGDX - Trying to implement collision between 2 sprites

  14. 14

    How can I rotate Rectangles in Libgdx?

  15. 15

    Libgdx and Box2d Detect Collision of two specific bodies

  16. 16

    How do I detect if a sprite was touched in Java libGDX?

  17. 17

    How do I detect if a sprite was touched in Java libGDX?

  18. 18

    How to detect collision between two sprites

  19. 19

    How to detect collision between SKSpriteNode and SKView SpriteKit

  20. 20

    Detect a collision between a CCSprite

  21. 21

    How do I implement simple 2d ball collision?

  22. 22

    Collision detection between two rectangles in java

  23. 23

    Check collision between rectangles AS3

  24. 24

    Detecting Collision between two filled Rectangles

  25. 25

    How can I detect and resolve collision in my 2D tilebased game?

  26. 26

    How do I draw the outline of a collection of rectangles?

  27. 27

    Ran out of short values to detect collision in LIBGDX

  28. 28

    How to detect collision between two objects in JavaScript with Three.js?

  29. 29

    How do I do collision between two RectangleShapes in SFML 2.5.0? (C++)

HotTag

Archive