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:
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?

boolean intersects = new Rectangle(x1, y1, width1, height1).intersects(new Rectangle(x2, y2, width2, height2));fromjava.awtand see how that goes - Coderino Javarinoposition.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