I've been trying to implement collision for a platformer but I can't find a good way to get the collision of certain sides without it get glitchy. So i try to get the collision of the top rectangle first which works, but any other side has a problem. I have looked at days worths of tutorials and articles but they were either too complex or not calculating collision for one side of a rectangle. I am truely stumped and can't find a solution that works for me.
What I need:
- working collision that wont give the bullet through paper problem *simple gravity (I have a way of doing this)
- the collision allowing me to know what side is touching what
Now I don't want to do the method where the player is made up of several small rectangles because of possible future problems and the fact that it's not acurate. Can I have a small bite of code that shows how I can handle collision in these cases? Also, here's my current way of doing in OpenGL + Java:
public class AABBDemo {
public AABBDemo() {
try {
Display.setDisplayMode(new DisplayMode(640, 480));
Display.setTitle("test");
Display.create();
} catch (LWJGLException e) {
e.printStackTrace();
}
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, 640, 480, 0, -1, 1);
glMatrixMode(GL_MODELVIEW);
Rectangle A = new Rectangle(100, 100, 50, 50);//player
Rectangle B = new Rectangle(100, 300, 90, 20);//enemy
int leftA, leftB;
int rightA, rightB;
int topA, topB;
int bottomA, bottomB;
int xVel = 0;
int yVel = 1;
int size = 50;
boolean jumping = false;
float jump = 0;
boolean jumped = false;
while (!Display.isCloseRequested()) {
glClear(GL_COLOR_BUFFER_BIT);
A.setX(A.getX() + xVel);
A.setY(A.getY() + yVel);
if(jumping)
{
A.setY((int) (A.getY() - jump));
jump -= 0.3f;
if(jump < -5)
jump = -5;
}
if(!jumping)
jump = 10;
if(A.getX() < 0 || A.getX() + size > 640 || check_collision(A, B))
{
jumping = false;
A.setX(A.getX() - xVel);
A.setY(A.getY() - 2);
}
if(Keyboard.isKeyDown(Keyboard.KEY_D))
xVel = 2;
else if(Keyboard.isKeyDown(Keyboard.KEY_A))
xVel = -2;
else
xVel = 0;
if(Keyboard.isKeyDown(Keyboard.KEY_W) && !jumping && !jumped)
{
jumped = true;
jumping = true;
}
if(!Keyboard.isKeyDown(Keyboard.KEY_W))
jumped = false;
glBegin(GL_QUADS);
glColor3f(1, 0, 0);
glVertex2i(A.getX(), A.getY()); // Upper-left
glVertex2i(A.getX() + A.getWidth(), A.getY()); // Upper-right
glVertex2i(A.getX() + A.getWidth(), A.getY() +A.getHeight()); // Bottom-right
glVertex2i(A.getX(), A.getY() + A.getHeight()); // Bottom-left
glEnd();
glBegin(GL_QUADS);
glColor3f(0, 1, 0);
glVertex2i(B.getX(), B.getY());
glVertex2i(B.getX() + B.getWidth(), B.getY());
glVertex2i(B.getX() + B.getWidth(), B.getY() + B.getHeight());
glVertex2i(B.getX(), B.getY() + B.getHeight());
glEnd();
Display.update();
Display.sync(60);
}
Display.destroy();
System.exit(0);
}
public boolean check_collision( Rectangle A, Rectangle B )
{
//The sides of the rectangles
int leftA, leftB;
int rightA, rightB;
int topA, topB;
int bottomA, bottomB;
//Calculate the sides of rect A
leftA = A.getX();
rightA = A.getX() + A.getWidth();
topA = A.getY();
bottomA = A.getY() + A.getHeight();
//Calculate the sides of rect B
leftB = B.getX();
rightB = B.getX() + B.getWidth();
topB = B.getY();
bottomB = B.getY() + B.getHeight();
//If any of the sides from A are outside of B
if( bottomA <= topB )
{
return false;
}
if( topA >= bottomB )
{
return false;
}
if( rightA <= leftB )
{
return false;
}
if( leftA >= rightB )
{
return false;
}
//If none of the sides from A are outside B
return true;
}
public static void main(String[] args) {
new AABBDemo();
}
}
if( bottomA <= topB ) return false;
?check_collision(A,B)
would give a different result thancheck_collision(B,A)
, how does this work ? \$\endgroup\$if( bottomA <= topB )
wouldreturn false
when there clearly is an intersection when at the same time bottomA is lower than topB \$\endgroup\$