Page 1 of 1

How to resolve a collision between two rectangles

Posted: Wed Dec 02, 2015 1:48 am
by BruceTheGoose
Disclaimer: This is all (PSEUDOCODE)

I have a Tile Map in an array like so

Code: Select all

{0,0,0,0,0,0,0,0,0,0,0
 0,0,0,0,0,0,0,0,0,0,0
 0,0,0,0,0,0,0,0,0,0,0}
I then use a for loop to create an individual tile for each element in the array like so

Code: Select all

newTile(x,y)
Every tile has the same width and height and none of the tiles are moving.

While the player can move freely throughout the map without any fixed movement.

Now that I have covered some background I can continue with my actual problem. I'm having no issues detecting if there is a collision but with actually solving the collision and determining which side the collision was detected.

My current approach is similar to as follows

Code: Select all

if(colliding(player, tile))
    if(playerX + playerW >= tileX)
        return rightSide
    if(playerX <= tileX + tileW)
        return leftSide
    if(playerY + playerH >= tileY)
        return bottomSide
    if(playerY <= tileY + tileH)
        return topSide
Then I use what is returned and proceed to actually solve the collision.

Code: Select all

 if(checkSide() == rightSide)
     // What now???
The method that returns what side is being intersected doesn't work as intended. I also do not know how to resolve the collision. Which is why I ask How to resolve a collision between two rectangles in a 2D grid?

http://stackoverflow.com/questions/3403 ... -a-2d-grid

Re: How to resolve a collision between two rectangles

Posted: Wed Dec 02, 2015 4:53 am
by kbmonkey
I see you got this working according to Stack Overflow. But just in case I wanted to mention an alternative method, since checking x/y seems a messy approach.

All about the angles

Calculate the angle between your player character and the centre of the tile - let us assume XY

Code: Select all

angleInDegrees = math.atan2(TileY - PlayerY, TileX - PlayerX) * 180 / math.pi + 180
Then you can simply check

Code: Select all

if angleInDegrees > 30 and angleInDegrees < 130 then
    'TOP'
elseif angleInDegrees > 130 and angleInDegrees < 220 then
    'RIGHT'
...
The angle should range 0 - 360. You can take it from there :cool:

Re: How to resolve a collision between two rectangles

Posted: Wed Dec 02, 2015 6:01 am
by micha
You can try to resolve collisions by using the velocity of the player. For example, if the player collides with a wall and the player is currently moving to the right, then you have to resolve the collision to the left.

Re: How to resolve a collision between two rectangles

Posted: Wed Dec 02, 2015 7:55 am
by Jasoco
If you only have rectangular collisions, look into Bump 3.0.

Re: How to resolve a collision between two rectangles

Posted: Wed Dec 02, 2015 8:07 pm
by BruceTheGoose
kbmonkey wrote:I see you got this working according to Stack Overflow. But just in case I wanted to mention an alternative method, since checking x/y seems a messy approach.

All about the angles

Calculate the angle between your player character and the centre of the tile - let us assume XY

Code: Select all

angleInDegrees = math.atan2(TileY - PlayerY, TileX - PlayerX) * 180 / math.pi + 180
Then you can simply check

Code: Select all

if angleInDegrees > 30 and angleInDegrees < 130 then
    'TOP'
elseif angleInDegrees > 130 and angleInDegrees < 220 then
    'RIGHT'
...
The angle should range 0 - 360. You can take it from there :cool:

I'm very intriqued by this approach and I'll try it later. Thank you! :crazy:

Re: How to resolve a collision between two rectangles

Posted: Wed Dec 02, 2015 8:08 pm
by BruceTheGoose
micha wrote:You can try to resolve collisions by using the velocity of the player. For example, if the player collides with a wall and the player is currently moving to the right, then you have to resolve the collision to the left.
I also like that. I suck at thinking outside the box.