View more

View more

View more

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

# 2D Platformer Collision Correction?

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

3 replies to this topic

### #1Oyed  Members

Posted 03 May 2014 - 06:32 PM

Hey there,

I'm pretty new to Game Development and am just learning mostly theory and simple physics by developing a HTML5 Canvas Game before moving on to other languages (Being a Web Developer all I had to learn was the Canvas API).

I'm having some issues with my Collision Detection/Correction. To put it simply, here is a run down of what happens to the Player Entity in every loop:

1. Apply the physics to a temporary X and Y Co-ordinates to find the new position of the Player (E.g. Friction and Gravity)
2. Generate points around the sprite to check for collisions and assign them directions (Which allows for collision correction based on the direction of the collision)
3. Loop through each of the points, find the Tile each point is on and check whether it is solid. If it is, mark that direction (The direction linked to the point, that I created earlier) as solid.
4. After checking all of the points, only apply the physics to X and/or Y if no collision is detected for that direction.

The issue is, that when it's finished checking for collisions, if the player is falling the collision for Left and Right will be triggered, as well as Down. This is because it doesn't order collisions; or limit it to just one collision per loop. Here is a "pseudo-code" version of the loop:

LOOP

tempX = x
tempY = y

IF up_arrow IS pressed THEN

IF NOT jumping AND grounded THEN

jumping = TRUE
grounded = FALSE
yVelocity = -(speed / 2) * 2 // Ignore division and then multiplication by 2, as I'll be using one as a modifier

END IF

END IF

IF left_arrow IS pressed THEN

IF xVelocity < speed THEN
xVelocity++
END IF

END IF

IF right_arrow IS pressed THEN

IF xVelocity > -speed THEN
xVelocity--
END IF

END IF

xVelocity *= friction
yVelocity += gravity
grounded = false

// COLLISION POINT GENERATION - See below

moveLeft = true
moveRight = true
moveDown = true
moveUp = true

FOREACH points AS p

thisTile = findTile(p.x, p.y) // Gets the Tile instance for the given co-ords
thisSolid = thisTile.isSolid()

IF thisSolid THEN

SWITCH p.direction

CASE 0 // Left
moveLeft = false
BREAK

CASE 1 // Right
moveRight = false
BREAK

CASE 2 // Up
moveUp = false
BREAK

CASE 3 // Down
moveDown = false
BREAK

END SWITCH

END IF

END FOREACH

IF !moveDown THEN
yVelocity = 0
jumping = false
grounded = true
END IF

IF !moveLeft OR !moveRight THEN
xVelocity = 0
jumping = false
END IF

IF grounded THEN
yVelocity = 0
END IF

x += xVelocity
y += yVelocity

END LOOP


Collision Point Generation:

In order to check for collisions around the player, the sprite is used to calculate points in which to check for collisions. The points is generates look like this:

Each point has a Direction value so that the collision can be corrected based on that. Each point is also generated using a tempX and tempY value, which are just temporary variables which will have the players velocities applied to them before point generation, and then the point generation will use these values.

So, even if the player falls a long distance and hits the floor, because the new X and Y values it uses are un a tile, the Left and Right points are also in the floor tiles and trigger a collision.

I have no idea how to get around this; so any help is greatly appreciated!

### #2elbenko  Members

Posted 04 May 2014 - 11:42 AM

Hi mate,

Just a thought, but which directions does the 4 corner points return? If they return Left / Right it would probably cause detection issues when your character stands on top of a solid.

Personally I'd probably structure my detection code like this: (easier to follow in terms of readability)

1. Check for collsion & set the appropriate collision flags -> 2. Set / apply velocity for each axis if collision hasn't been flagged -> 3. Update sprite position.

In either case, it would probably be easier to find the problem with the actual code rather than pseudo code.

Edited by elbenko, 04 May 2014 - 11:59 AM.

### #3Oyed  Members

Posted 04 May 2014 - 11:44 AM

Hi mate,

Just a thought, I could be wrong, but which directions does the 4 corner points return?

If they return Left / Right it would probably cause detection issues when your character stands on top of a solid.

If you look at the image, the top and bottom 3 points are above and below the left and right points, so when standing on a solid tile the left and right points are 1 pixel above the surface and aren't triggered.

Personally I'd probably structure my detection code like this: (easier to follow in terms of readability)

1. Check for collsion & set the appropriate collision flags -> 2. Set / apply velocity for each axis if collision hasn't been flagged -> 3. Update sprite position.

In either case, it would probably be easier to find the problem with the actual code rather than pseudo code.

This is exactly what I do - but collision for both Left, Right and Down is triggered when the player hits the floor after falling due to checking for collision on all sides and correcting as such, so they loose their X Velocity even if they're just hitting the ground.

Edited by Oyed, 04 May 2014 - 01:53 PM.

### #4BeerNutts  Members

Posted 05 May 2014 - 11:54 AM

My suggestion for these kind of questions is to break it down by axis, and apply collision/correction per axis.

Meaning, move your object along 1 axis (say x-axis).  Check if it's colliding with anything solid (a simple rectangle check will suffice).  If it is, then you know movement along the x-axis caused some collision, so move object back to original x position (advanced option: calculate how far into the object you moved, and only move back that far).

Then move the object along the other axis, y-axis.  Check for collision.  If collided, move it back.

This handles many situations in a platformer game, and it's easy to implement.

My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.