EDIT :
i changed the title from “A better way than AABB collision to solve precision problem” to this new one cause i found that my AABB method is working fine and the real problem is due when i call it
Hello,
i don’t know if this is happening because of the collision method i wrote, or this is a common problem due of AABB use,
the main problem is precision, the collision doesn’t always happens in the correct “time”, for example in a platformer game, there is always those time when the player get under the “ground” a little bit, also when it comes to find from which direction the object it’s colliding, i always found myself stacked :-\
the method i use :
public boolean aabb(double x1, double y1, double w1, double h1, double x2,
double y2, double w2, double h2) {
double cntrX1 = x1 + w1 / 2;
double cntrY1 = y1 + h1 / 2;
double cntrX2 = x2 + w2 / 2;
double cntrY2 = y2 + h2 / 2;
double distanceX = Math.abs(cntrX1 - cntrX2);
double distanceY = Math.abs(cntrY1 - cntrY2);
double sumWidth = (w1 / 2 + w2 / 2);
double sumHeight = (h1 / 2 + h2 / 2);
if (distanceX < sumWidth && distanceY < sumHeight ) {
return true;
}
return false;
}
the way i call it :
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
int blockX = x * 32;
int blockY = y * 32;
String block = myWorld[y][x];
// testing if hitting the ground from the buttom
if (tool.aabb(hero.getX(), hero.getY(), hero.getWidth(),
hero.getHeight(), blockX, blockY, 32, 32)) {
if (block.equals("1")) {
onGround = true;
} else {
onGround = false;
}
}
// testing if from the right
if (tool.aabb(hero.getX() + hero.getWidth(), hero.getY(),
hero.getWidth(), hero.getHeight(), blockX, blockY, 32,
32)) {
if (block.equals("4")) {
System.out.println("right");
draw.fillRect(hero.getX() + hero.getWidth() / 2,
hero.getY(), 5, 5);
}
}
}
}
what am trying to do is :
-checking if the bottom of the player is colliding with the ground which cause him to stop falling (done)
-checking if the upper of the player is colliding with the ground which can stop the jump
-checking if the player is colliding from left/right with the ground/wall which will stop the movement in the current direction
PS :
i saw demos about jBox2D, do you suggest that i start using it to solve problems like these or is it too advanced for me now ??
thank you