Skip to main content
Changed question wording to a more specfic question
Source Link

How to properly handle top down 2D collision?

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:

  • Check for collisionLoop through rectangles polled by a quad tree
  • check if collision is foundthese rectangles intersect with the player
  • if yes then immediately set their position outsideundo the tilevelocity that we just applied to the player
  • if no then do nothing

However thisThis approach is very jittery and corners work poorly at bestless 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. If

For example: if the pushplayer collides against the tile while slidinga 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 sprite gets jitterycentre'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 wellthe player is bigger than the tiles.

Is there any best practices when working with collisiona way to find which axis is colliding so I can undo velocity on that specific axis?

How to properly handle collision?

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:

  • Check for collision
  • if collision is found then immediately set their position outside the tile

However this is very jittery and corners work poorly at best. If the push against the tile while sliding up and down the sprite gets jittery as well.

Is there any best practices when working with collision?

How to properly handle top down 2D collision?

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?

Source Link

How to properly handle collision?

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:

  • Check for collision
  • if collision is found then immediately set their position outside the tile

However this is very jittery and corners work poorly at best. If the push against the tile while sliding up and down the sprite gets jittery as well.

Is there any best practices when working with collision?