# Why does Intersector.overlaps return false?

I am trying my hand at creating a simple game using libGDX.

The idea is: We have a hero who jumps and world consist of dynamically created moving platforms in an auto side-scrolling manner. In order to progress our hero need to jump on these platforms and avoid falling. I have figured out how to do this using the built-in libGDX collision detection mechanism.

Code is:

    public boolean collides(Hero hero){
if (position.y < hero.getY() + hero.getHeight()) {
return Intersector.overlaps(hero.getBoundingRect(),boundingRect);
}
return false;
}


But, our hero need to die when hits side of a platform. Following code should do the trick but even though bounding rectangle of hero surely overlaps(see below image) the bounding rectangle of platform Intersector.overlaps returns false no matter what.

    public boolean   collidesSide(Hero hero){
float heroYH = hero.getY() + hero.getHeight();
float heroXW = hero.getX() + hero.getWidth();
if (heroYH > position.y && heroXW>position.x){ // Platforms position
Gdx.app.log("Platform","hero.bound:" + hero.getBoundingRect().toString() + " plat.bound:" +boundingRect.toString());
Gdx.app.log("Platform","Intersect: " + Intersector.overlaps(hero.getBoundingRect(),boundingRect));
if (Intersector.overlaps(hero.getBoundingRect(),boundingRect)){
Gdx.app.log("Platform","CollidesSide: True");
return true;
}
return false;
}

return false;
}


It seems that I have missed some logic or doing something wrong.

How should I handle this issue?

Also this is the output log

Platform: Intersect: false
Platform: hero.bound:20.0,64.65544,25.0,32.0   plat.bound:-3.0893557,96.0,19.0,5.0
Platform: Intersect: false
Platform: hero.bound:20.0,65.13417,25.0,32.0 plat.bound:-4.4350085,96.0,19.0,5.0
Platform: Intersect: false
Platform: hero.bound:20.0,65.74593,25.0,32.0 plat.bound:-5.779882,96.0,19.0,5.0
Platform: Intersect: false
Platform: hero.bound:20.0,66.43762,25.0,32.0 plat.bound:-7.042242,96.0,19.0,5.0
Platform: Intersect: false
Platform: hero.bound:20.0,67.29663,25.0,32.0 plat.bound:-8.372085,96.0,19.0,5.0
Platform: Intersect: false


UPDATE:

The Red and Cyan rectangles seen in images are rendered output of Bounding Rectangle(hit boxes) of each object, not actual sprite.

• Every plat.bound.x is negative, whereas hero.bound.x is 20. So your boundingRect which is supposed to be the plat.bound is wrong. – dimitris93 Sep 9 '15 at 8:19
• Could you explain a little more? I am confused by your comment :/ – raidensan Sep 9 '15 at 8:59
• How do you get/calculate boundingRect ? it seams wrong. Or its the boundingRect of another object (not the platform) – dnk drone.vs.drones Sep 9 '15 at 9:12
• boundingRect is set every time object(platform) updates and setting code is boundingRect.set(position.x,position.y, width,height) – raidensan Sep 9 '15 at 9:44

It very much looks like your boundingRect is incorrectly calculated. From your log you've got 5 boundingRects (I've re-ordered them here, right-to-left);

-3.0893557, 96.0, 19.0, 5.0
-4.4350085, 96.0, 19.0, 5.0
-5.779882,  96.0, 19.0, 5.0
-7.042242,  96.0, 19.0, 5.0
-8.372085,  96.0, 19.0, 5.0


This would mean that all of these 19x5 rectangles are located at Y=96 and then they overlap (for example 3.0893557 + 19.0 = 22.0893557, which is overlapping all of them). This seems wrong to me because in your screen shot none of the platforms are overlapping with eachother.

Further, your Hero is located at 20.0 on the X-axis, but since all platforms are only 19.0 wide, and the right-most one is at -3.0893557, none of them will reach the Hero. The closest will be 20.0 - (-3.0893557 + 19.0) = 4.0893557 away from the Hero.

Double check how the bounding rectangles are calculated (or how the parameters for the bounding rectangles are calculated) and consider debug-rendering them using the ShapeRenderer class to visualize them.

After trying a few revisions I solved the issue. But not sure why it is resolved.

I was checking for collision in my GameWorld object as shown below:

    if (scroller.platformCollides(hero)){
hero.walk();
}
else if (scroller.platformCollidesSide(hero)) {
if (hero.isAlive()) {
scroller.stop();
hero.die();
hero.decelerate();
currentState=GameState.GAMEOVER;
}


Then I changed the code to:

    if (scroller.platformCollidesSide(hero)) {
if (hero.isAlive()) {
scroller.stop();
hero.die();
hero.decelerate();
currentState=GameState.GAMEOVER;
}
}
else if (scroller.platformCollides(hero)){
hero.walk();
}


which magically solved my issue. But as I said above I don't understand why this worked.