How to resolve a collision between two rectangles

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
User avatar
BruceTheGoose
Citizen
Posts: 76
Joined: Sat Sep 20, 2014 2:54 pm

How to resolve a collision between two rectangles

Post 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
"I don't know."
User avatar
kbmonkey
Party member
Posts: 139
Joined: Tue Sep 01, 2015 12:19 pm
Location: Sydney
Contact:

Re: How to resolve a collision between two rectangles

Post 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:
User avatar
micha
Inner party member
Posts: 1083
Joined: Wed Sep 26, 2012 5:13 pm

Re: How to resolve a collision between two rectangles

Post 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.
User avatar
Jasoco
Inner party member
Posts: 3727
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

Re: How to resolve a collision between two rectangles

Post by Jasoco »

If you only have rectangular collisions, look into Bump 3.0.
User avatar
BruceTheGoose
Citizen
Posts: 76
Joined: Sat Sep 20, 2014 2:54 pm

Re: How to resolve a collision between two rectangles

Post 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:
"I don't know."
User avatar
BruceTheGoose
Citizen
Posts: 76
Joined: Sat Sep 20, 2014 2:54 pm

Re: How to resolve a collision between two rectangles

Post 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.
"I don't know."
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 3 guests