1
votes

I have a 2D OpenGL game which I am trying to implement aabb collision detection in. Here is the collision detection code:

boolean collisionX = position.x + size.x + dx >= collision.position.x && collision.position.x + collision.size.x >= position.x + dx;
boolean collisionY = position.y + size.y + dy >= collision.position.y && collision.position.y + collision.size.y >= position.y + dy;
return collisionX && collisionY;

For the size I am using this calculation, because some of these sprite are from texture atlases.

size.x = pixelWidth * scaleX / modelTexture.spriteWidth,
size.y = pixelHeight * scaleY / modelTexture.spriteWidth

I use modelTexture.spriteWidth for both because the height and width of the texture atlas is the same, because it is a 2^x square.

PixelWidth and PixelHeight are the pixel size of the image on the texture, counted by me.

I think the problem could be because of these calculations, if they are wrong. Please let me know.

However, the collision detection seems to be happening in the wrong places, as if the bounding box of every sprite begins in the center and extends towards the top right. I have tried to account for this using a displacement vector which is equal to -size / 2f. But unfortunately it didn't change anything.

Here is an example of a collision, which doesn't appear to be alligned correctly:

enter image description here

Do you think the problem could be the origin of the sprites/objects? Which is at the center. If so how would I set the origin to the top left?

1
Try one of the default methods, e.g. boolean intersects = new Rectangle(x1, y1, width1, height1).intersects(new Rectangle(x2, y2, width2, height2)); from java.awt and see how that goes - Coderino Javarino
If you need to scale the sizes, you'll probably have to scale the positions accordingly? - Reto Koradi
position.x + size.x + dx >= collision.position.x && collision.position.x + collision.size.x >= position.x + dx; shouldn't it be an || here ? (Same of y) Just an intuition reading the code, I haven't done the math - Zouch
@CoderinoJavarino I'm not doing that because OpenGL world space is based on floats, and the awt Rectangle only does integers. I suppose I could scale up the position and scale to make them whole numbers? But I'm not sure how I would do that. If you know let me know. - Dylan Deshler
@RetoKoradi no because the position is already in world space, the reason I have to adjust the size is because it is calculated from the texture itself. - Dylan Deshler

1 Answers

0
votes

I think you just validate collision against one side. To validate collisions you must do somethint like that:

collisionX = (right_side_Obj1 > left_side_Obj2 && left_side_Obj1 < left_side_Obj2) 
|| (left_side_Obj1 < right_side_Obj2 && right_side_Obj1 > right_side_Obj2);

In your case i think it should be:

boolean collisionX = (position.x + size.x + dx >= collision.position.x 
            && position.x <= collision.position.x)
        || (position.x - dx <= collision.position.x + collision.size.x
            && position.x + + size.x >= collision.position.x + collision.size.x);