Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rectangle overlaps method libgdx

I'm trying to use the overlaps method to determine whether two sprites collide but from some reason it simply doesn't work. It always returns false, even when I clearly see the sprites collide with each other.

The funny thing is, this is not the first time it's happenning to me. I don't know why the overlaps method doesn't work.

Here is my Hero class which has a method called isCollided.

package world_objects;

import helpers.Values;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.math.Rectangle;

/* Created by David Lasry : 10/25/14 */
public class Hero{
    public enum HeroState {

        Walking_Forward,
        Walking_Left,
        Walking_Right,
        Dead
    }
    public HeroState state;
    private Rectangle body;
    private float x,y;
    public Hero() {
        x = Values.SCREEN_WIDTH/5;
        y = Values.SCREEN_HEIGHT/5;
        body = new Rectangle(x, y, Values.Hero_Width, Values.Hero_Height);
        state = HeroState.Walking_Forward;
    }
    public Rectangle getBody() {
        return body;
    }
    public boolean isCollided(Rectangle rect) {
        Gdx.app.log("Collision Detected", ""+body.overlaps(rect));
        return rect.overlaps(body);
    }
    public HeroState getState() {
        return state;
    }
    public float getX() {
        return x;
    }
    public float getY() {
        return y;
    }
    public void setX(float x) {
        this.x = x;
    }
    public void setY(float y) {
        this.y = y;
    }
}

As you can see, I tried to debug it by figuring out what the returned value is. Every single time it is false, even when it should be true.

What can I do? Is there any alternative method that I can use?

Edit: After debugging a little bit and trying to figure out the values of the two objects(Hero, Object) in real time, I came up with this screen shot: http://i.gyazo.com/852fea520b060870a4cb5731c21fa833.png. The values of the two objects in this exact same position are(The X/Y is at the bottom-left corner):

**Hero X/Y: 64.5,283.40985**
**Hero WIDTH/HEIGHT: 25,50**
**Square X/Y: 76.25,0.0**
**Square WIDTH/HEIGHT: 47.5,309.13348** 

As you can see, the values are perfectly normal. I don't understand where is the problem.

like image 253
David Lasry Avatar asked Nov 26 '25 04:11

David Lasry


1 Answers

The problem is probably this - as Angel Angel suggested in the comments - when you create your Hero he has his x, y location values. The hero's body (a rectangle) also has its own x, y values. When he moves, you are probably only updating the Hero's x, y values and not the body's x, y values or vice versa. If you looked at the values with the debugger you would see exactly what's happening. My guess is you looked at the wrong values and made some incorrect assumptions that the method doesn't work. I guarantee you, the overlap method is very simple and does work. If you don't know how to use the debugger, this is the perfect time to learn. It will pay off many many times in the future as you continue developing your game. Unless you enjoy writing thousands of System.out calls to find every little bug you face, learn to use the debugger now! Had you known how to use the debugger this bug could have been fixed within a couple of minutes and without the need to write a question in stack overflow.

like image 100
Barodapride Avatar answered Nov 28 '25 21:11

Barodapride



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!