Platformer game troubles

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
Squeegy
Prole
Posts: 3
Joined: Sun Jun 16, 2013 1:09 am

Platformer game troubles

Post by Squeegy »

So, I've been working on a game of my own in the Löve engine, and the platforming is getting along pretty well. You can see it in the prototype below. Thanks to MasterShizzle's platforming tutorial, YellowAfterlife's platformer game engine, and the amazing Mr Rescue for their help in my figuring out how to create an engine I'm satisfied with.

You can also see the problem I'm having in the prototype below. It seems that whenever the player hits the ground, there's a little bit of a stutter where it doesn't recognize them as having landed right away, but rather flips back between them several times before settling on the ground. I suspect this has to do with the collision, and the way the game detects whether you're on land:

Code: Select all

function Player:collideY(dt)
	local halfX = self.width / 2
	local halfY = self.height / 2
	local nextY = math.floor(self.y + (self.yspeed * dt))
	local nextX = math.floor(self.x + (self.xspeed * dt))
	self.onGround = false
	if self.yspeed < 0 then -- check upward
		if(self:isColliding(map, self.x - halfX, nextY - halfY))
		  or(self:isColliding(map, self.x + halfX - 1, nextY - halfY)) then
			return true
		end
	else if self.yspeed > 0 then -- check downward
		if(self:isColliding(map, self.x - halfX, nextY + halfY))
		  or(self:isColliding(map, self.x + halfX - 1, nextY + halfY)) then
			self.onGround = true
			return true
		end
	end
	return false
end
Any ideas?
Attachments
platformer-demo.love
Earliest prototype build
(54.05 KiB) Downloaded 89 times
User avatar
micha
Inner party member
Posts: 1083
Joined: Wed Sep 26, 2012 5:13 pm

Re: Platformer game troubles

Post by micha »

The collision detection is fine. The question is, what do you do, when you encounter a collision?
In each time step you check if the new position causes a collision. If it does so, then you do not do the step but keep the player at the previous position, but you set the vertical velocity to zero. Now suppose, the player moves downwards very fast. In the current frame he is 5 pixels above the ground and in the next one he is 5 pixels below. Then you detect a collision and the player stays at 5 pixels above with zero velocity. From there he falls down.

To correct this you slightly have to change, what you do, whenever a collision happens. Instead of leaving the player, where he is, you need to find the spot, that right on the ground (0 pixels above).

You can most simply do this by calculating the coordinates of the players feed (self.y+halfY) and then rounding it using math.ceil. Then subtract halfy again to get the new self.y

Code: Select all

self.newY = math.ceil(self.y+halfY-1)-halfY
This assumed, you have a gridsize of 1. If the grid size is different, you need to divide accordingly.
Squeegy
Prole
Posts: 3
Joined: Sun Jun 16, 2013 1:09 am

Re: Platformer game troubles

Post by Squeegy »

That seems to be the solution, but it presents a new problem: since the game can never just set the speed to 0 and forget about it, the player starts jittering about constantly as their position is reset every time gravity kicks in, thus making the problem worse.
User avatar
micha
Inner party member
Posts: 1083
Joined: Wed Sep 26, 2012 5:13 pm

Re: Platformer game troubles

Post by micha »

Make sure the correction of the players position is the last thing you do, before the drawing begins. Then the position should stay constant.
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 9 guests