I can never seem to get this right, well polished at least. I'm creating a top down game in XNA, and collision has always been an issue for me to do properly. Detecting collision is fine, but resolving it is where I get messed up.
My approach so far:
- Loop through rectangles polled by a quad tree
- check if these rectangles intersect with the player
- if yes then undo the velocity that we just applied to the player
- if no then do nothing
This approach is less than ideal but it's close, I'm trying to allow players to move along the wall even if they're applying the force on the wall.
For example: if the player collides against a vertical wall, they should be able to move up and down even if they are still trying to move left/right.
I can't seem to find a good way to do this. Maybe I'm overthinking, but I tried doing vector subtraction on the centre's of the player and tile and seeing where the vector pointed. But that didn't give me much helpful information. I tried seeing if it was the x-axis or y-axis that was colliding but that didn't work as the player is bigger than the tiles.
Is there a way to find which axis is colliding so I can undo velocity on that specific axis?