Welcome, Guest. Please login or register.

Login with username, password and session length

 
Advanced search

1411283 Posts in 69325 Topics- by 58380 Members - Latest Member: bob1029

March 29, 2024, 02:19:29 AM

Need hosting? Check out Digital Ocean
(more details in this thread)
TIGSource ForumsDeveloperTechnical (Moderator: ThemsAllTook)LWJGL 2: AABB Collision code not working?
Pages: [1]
Print
Author Topic: LWJGL 2: AABB Collision code not working?  (Read 1944 times)
Digi-Space Productions
Level 0
*


Minecraft (alpha)


View Profile
« on: July 28, 2022, 02:26:12 PM »

Hello, I am developing a game in Java 1.6 using LWJGL 2.

I am trying to make AABB collision, here is my code:

Code:
public boolean isIntersecting(AABB box2) {
        if
        (center.x + half_extent.x < box2.center.x + box2.half_extent.x && center.x - half_extent.x > box2.center.x - box2.half_extent.x &&
        center.y + half_extent.y < box2.center.y + box2.half_extent.y && center.y - half_extent.y > box2.center.y - box2.half_extent.y)
        {
            return true;
        }  else return false;
    }

When I take a AABB box, say, box1, and I see if it intersects with box2, it never results in a true, something wrong here?
Logged

ThemsAllTook
Administrator
Level 10
******



View Profile WWW
« Reply #1 on: July 29, 2022, 06:18:58 AM »

I spot what might be the problem. My parsing of the conditional boils down to this:

Code:
if (left1 < left2 && right1 > right2 &&
    bottom1 < bottom2 && top1 > top2)
//...

If you run through the logic, that won't ever be true except maybe in some cases where one of your boxes has a negative size. What you probably want is to check opposite sides against each other, like this:

Code:
if (left1 < right2 && right1 > left2 &&
    bottom1 < top2 && top1 > bottom2)
//...
Logged

Digi-Space Productions
Level 0
*


Minecraft (alpha)


View Profile
« Reply #2 on: July 29, 2022, 07:14:19 PM »

Thanks a bunch!
Logged

Pages: [1]
Print
Jump to:  

Theme orange-lt created by panic