Sign in to follow this  
TheGag96

Basic collision detection

Recommended Posts

TheGag96    100
Hello. A little while ago I started making a game in Java with the libgdx library. Until now, I've been doing it almost entirely by myself, and although the code is probably not as good as it should be, it's almost working.

Problem is, I cannot get collisions to work. I have almost gotten them to work thanks to a friend of mine. HOWEVER, the left and right and up and down collisions seem to collide with each other. I don't know why it is, but they just do, and I can't figure it out. It's like they compete with each other... Or something.

Here's some code:

[code]for (int i = 0; i < blocks.size(); i++) {
Block b = blocks.get(i);

if (isCollidingDown(b)) {
b.doTopCollision(this);
}

else if (isCollidingUp(b)) {
b.doBottomCollision(this);
}

else if (isCollidingLeft(b)) {
b.doRightCollision(this);
}

else if (isCollidingRight(b)) {
b.doLeftCollision(this);
}

}

...


public boolean isCollidingDown(Block b) {
if ((Float.compare(this.yPos,b.yPos+b.height) <= 0) && (Float.compare(this.yPos,b.yPos)>=0)) {

if (((Float.compare(this.xPos, b.xPos)>=0) && (Float.compare(this.xPos, b.xPos+b.width)<= 0)) ||
((Float.compare(this.xPos+this.width,b.xPos)>=0) && (Float.compare(this.xPos+this.width,b.xPos+b.width)<=0))) {

return true;
}
}
return false;
}

public boolean isCollidingUp(Block b) {
if ((Float.compare(this.yPos+this.height,b.yPos) >=0) && (Float.compare(this.yPos+this.height,b.yPos+b.height)<=0)) {

if (((Float.compare(this.xPos, b.xPos)>=0) && (Float.compare(this.xPos, b.xPos+b.width)<= 0)) ||
((Float.compare(this.xPos+this.width,b.xPos)>=0) && (Float.compare(this.xPos+this.width,b.xPos+b.width)<=0))) {

return true;
}
}
return false;
}

public boolean isCollidingLeft(Block b) {
if ((Float.compare(this.xPos,b.xPos+b.width) <=0) && (Float.compare(this.xPos,b.xPos)>=0)) {

if (((Float.compare(this.yPos, b.yPos)>=0) && (Float.compare(this.yPos, b.yPos+b.height)<=0)) ||
((Float.compare(this.yPos+this.height,b.yPos)>=0) && (Float.compare(this.yPos+this.height,b.yPos+b.height)<=0))) {

return true;
}
}
return false;
}

public boolean isCollidingRight(Block b) {
if ((Float.compare(this.xPos+this.width,b.xPos) >=0) && (Float.compare(this.xPos+this.width,b.xPos+b.width)<=0)) {

if (((Float.compare(this.yPos, b.yPos)>=0) && (Float.compare(this.yPos, b.yPos+b.height)<= 0)) ||
((Float.compare(this.yPos+this.height,b.yPos)>=0) && (Float.compare(this.yPos+this.height,b.yPos+b.height)<=0))) {

return true;
}
}
return false;
}
[/code]


Now, let me clear some stuff up.

The idea for the collision is based upon this: "if A or B of the box is bellow A or B of the floor AND A or B of the box is between A or B of the floor collide" (and so on for the other sides of course)

The xpos and ypos of both objects are at the bottom right corner, so to achieve the position of "A" or "B" of any vertex I take the corresponding x or y position and add the width/height of the object.

Also, I have to use float.compare() to properly compare floating point values with Java.

Can someone help me? If I need to explain some things I will. If you want the JAR/rest of the source I can do that too.

Thank you!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this