Question about collision detection with other objects

Are you new to 6502, NES, or even programming in general? Post any of your questions here. Remember - the only dumb question is the question that remains unasked.

Moderator: Moderators

Pokun
Posts: 1746
Joined: Tue May 28, 2013 5:49 am
Location: Hokkaido, Japan

Re: Question about collision detection with other objects

Post by Pokun » Sun Dec 06, 2020 3:36 pm

That wouldn't work if the object moves with a speed (in pixel/frame) greater than half of the tile's width (in pixels). It would be ejected from the opposite side it entered from.

Drag
Posts: 1335
Joined: Mon Sep 27, 2004 2:57 pm
Contact:

Re: Question about collision detection with other objects

Post by Drag » Sun Dec 06, 2020 8:05 pm

I'm kinda late to the party, but here's how I've done actor vs. actor collision detection:
Take "A", calculate A's top-left and bottom-right hitbox coordinates, store each X and Y coordinate as A.top, A.bottom, A.left, and A.right.

Take "B", do the same thing.

If A.left > B.right, exit.
If A.right < B.left, exit.
If A.top > B.bottom, exit.
If A.bottom < B.top, exit.

If you reach this point, A and B are overlapping.
Then, for movement vs BG collisions:
Apply Y-delta to Y position.
If the updated Y position produces a collision on your top or bottom edge, eject Y coordinate out of the BG.
Apply X-delta to X position.
If the updated X position produces a collision on your left or right edge, eject X coordinate out of the BG.
Splitting this up per-axis will avoid one interfering with the other. The order you check X and Y in will determine what happens when you clip around corners.
MarioCollision.png
MarioCollision.png (6.66 KiB) Viewed 1325 times
If Mario's X and Y speed makes him perfectly clip through that corner (i.e, moving straight down will collide, but moving down and right will clear the ledge), then checking Y first will pop Mario UP as he clears the ledge, and checking X first won't. This is where you might want to determine based on which of X and Y is moving farther, which axis you check first.

User avatar
tokumaru
Posts: 11991
Joined: Sat Feb 12, 2005 9:43 pm
Location: Rio de Janeiro - Brazil

Re: Question about collision detection with other objects

Post by tokumaru » Sun Dec 06, 2020 8:44 pm

Drag wrote:
Sun Dec 06, 2020 8:05 pm
MarioCollision.png[/attachment]
If Mario's X and Y speed makes him perfectly clip through that corner (i.e, moving straight down will collide, but moving down and right will clear the ledge), then checking Y first will pop Mario UP as he clears the ledge, and checking X first won't. This is where you might want to determine based on which of X and Y is moving farther, which axis you check first.
More important than the popping IMO, is that moving horizontally first when falling will help with landing on corners, while moving vertically first could cause the player to hit the wall instead. Because of this, I'd do X first when going down, Y first when going up.

Post Reply