Search code examples
c#xnacollision-detection

Collision depending of side of the rectangle?


Im not intrested in the code for this problem i just want to be pointed in the right direction.

Im using C# XNA if that helps.

Basicaly for my game i am adding collisions for example the player can't walk or fall through stones.. I have the rectangle and i know i use the .intersects comand but would i need to check collision depending on the side of the rectangle here?

I know if the player is falling i can make if playerRectangle intersects stoneRectangle playerY = stopFalling..

But if the rectangle is coliding on the side of the player.. That would be different wouldn't it ?


Solution

  • One simple way of dealing with this is to work out the main axis of penetration, i.e. is the player further into the rectangle horizontally, or vertically. Then, using this information, you'd move the player so they were just touching the rectangle.

    i.e. Player bumps into a wall on his right.

    Main axis is Horizontal, and he's to the left of the centre of the rectangle, so we know to move him left.


    Searching on gamedev.stackexchange.com will give you lots of different collision detection/response options.