Platform Collision problems

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
roggie
Prole
Posts: 22
Joined: Sat May 31, 2014 2:52 pm
Location: I think it's Earth
Contact:

Platform Collision problems

Post by roggie »

I've been developing a platformer and have come across a problem. If the player jumps from the top platform to the bottom he goes right through it but if he jumps onto it from the ground, he stays on it. I don't know what's going on.

Controls:
Arrow keys to move right/left
Spacebar to jump.
Hold lshift to run.
Also, I am using love 0.8.0

I'll appreciate any help, thank you in advance :)
Attachments
Platformer.love
(1.78 KiB) Downloaded 65 times
User avatar
micha
Inner party member
Posts: 1083
Joined: Wed Sep 26, 2012 5:13 pm

Re: Platform Collision problems

Post by micha »

Hi,

the problem is in the AABB-collision-check. It should be this:

Code: Select all

if player.y + player.h > v.y and player.y < v.y + v.h and player.x + player.w > v.x and player.x < v.x + v.w then 
you had an additional "+ player.h" in there.

The next thing you should to is refine the collision resolution: If a collision occurs, then the player should not be set back to the previous position, but exactly onto the ground/platform. For that you need to know the direction in which the player moves: If he moves down and a collision occurs, then the player must be moved up onto the platform. If he moves up, then he must be moved down.

Besides that, you should move these two lines, from the love.draw to the love.load:

Code: Select all

addplatform(0,love.graphics.getHeight() - 100,100,10)
addplatform(100,love.graphics.getHeight() - 180,100,10)
Otherwise two platforms are added each frame, which makes the table larger and larger over time.
User avatar
roggie
Prole
Posts: 22
Joined: Sat May 31, 2014 2:52 pm
Location: I think it's Earth
Contact:

Re: Platform Collision problems

Post by roggie »

micha wrote:Hi,

the problem is in the AABB-collision-check. It should be this:

Code: Select all

if player.y + player.h > v.y and player.y < v.y + v.h and player.x + player.w > v.x and player.x < v.x + v.w then 
you had an additional "+ player.h" in there.

The next thing you should to is refine the collision resolution: If a collision occurs, then the player should not be set back to the previous position, but exactly onto the ground/platform. For that you need to know the direction in which the player moves: If he moves down and a collision occurs, then the player must be moved up onto the platform. If he moves up, then he must be moved down.

Besides that, you should move these two lines, from the love.draw to the love.load:

Code: Select all

addplatform(0,love.graphics.getHeight() - 100,100,10)
addplatform(100,love.graphics.getHeight() - 180,100,10)
Otherwise two platforms are added each frame, which makes the table larger and larger over time.
Thank you, the code works perfectly and I like your advice when you said:
Besides that, you should move these two lines, from the love.draw to the love.load:

Code: Select all

addplatform(0,love.graphics.getHeight() - 100,100,10)
addplatform(100,love.graphics.getHeight() - 180,100,10)
Otherwise two platforms are added each frame, which makes the table larger and larger over time.
Looking back, that would have been a smart thing to do when i started coding the game. :awesome:
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Semrush [Bot] and 4 guests