Reputation: 1407
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.
Upvotes: 3
Views: 8587
Reputation: 3723
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.
Upvotes: 2
Reputation: 2724
I made some tests and everything work as expected. Here below the classe I used for testing:
Value holds the the values for screen height / width and actor size
public class Values {
public static final int SCREEN_HEIGHT = 240;
public static final int SCREEN_WIDTH = 400;
public static float Hero_Width = 20;
public static float Hero_Height = 30;
}
This is the Hero, similar to your version I just removed what is not need specifically required for the test.
public class Hero {
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);
}
public boolean isCollided(Rectangle rect) {
Gdx.app.log("Collision Detected", "" + body.overlaps(rect));
return rect.overlaps(body);
}
}
The ***Hero* according to the Values that I chose would have:
body = new Rectangle(80,48,20,30);
Then I made 3 tests with this testing code:
Hero hero = new Hero();
Rectangle rectangle = new Rectangle(10, 10, 100, 100);
// is true
boolean isCollided = hero.isCollided(rectangle);
Rectangle rectangle2 = new Rectangle(10, 10, 40, 40);
// is false
boolean isCollided2 = hero.isCollided(rectangle2);
Rectangle rectangle3 = new Rectangle(50, 30, 40, 20);
// is true
boolean isCollided3 = hero.isCollided(rectangle3);
I assume that there are some incorrect values passed to the isCollide method. As the other comments suggested, try debug which values of the rectangle are given when you call that method. Somehow the Hero object or the test rectangle are not like you would expect them.
If this would not fix your issue, consider posting more description / code in your original post.
Upvotes: 0