Page 1 of 2

FPS issues in platformer

Posted: Tue Feb 21, 2012 4:48 pm
by the_nonameguy
Hey fellow Lovers!

I started making games in Löve 2 weeks before and it's amazing! :awesome:
My only problem is that in my platformer that has been written 48 hours, I have issues regarding the Fps (dt exactly),
trying out multiple solutions and limiting the fps didn't work, so here's my crappy OOP code:

Code: Select all

function Player:handleInput(dt)
	if self.onGround==false then
		if self.velocity.y<400 then
			self.velocity.y=self.velocity.y+1
		end
	else
		self.velocity.y=0
	end
	self.TLbind:update()
	if self.control.left and not self.hitLeft then
		if self.onGround then
			self.velocity.x=-200
		else
			self.velocity.x=-150
		end
	elseif self.control.right and not self.hitRight then 
		if self.onGround then
			self.velocity.x=200
		else
			self.velocity.x=150
		end
	else
		self.velocity.x = self.velocity.x * (1 - math.min(dt*10, 1))
	end
	if self.control.jump and self.onGround then
		self.velocity.y=-300*dt
		self.onGround=false
	end	
	if self.control.tap.menu then
		self:die()
	end
end

function Player:stayInScreen(dt)
	local 	center=self.body:center()
	if center.x-16+self.velocity.x*dt<=0 or center.x+16+self.velocity.x*dt>=650 then
		self.velocity.x=0
	end
	if center.y-16>650 then
		self:die()
	elseif center.y-16+velocity.y*dt<=0 then
		self.velocity.y=0
	end
end

function Player:move(dt)
	self.body:move(self.velocity.x*dt,self.velocity.y*dt)
	self.leg:move(self.velocity.x*dt,self.velocity.y*dt)
	self.left:move(self.velocity.x*dt,self.velocity.y*dt)
	self.right:move(self.velocity.x*dt,self.velocity.y*dt)
	self.head:move(self.velocity.x*dt,self.velocity.y*dt)
end
My issue is that on my low-end laptop the game runs just fine, but on my friend's High-end pc the player jumps to the top of the screen. Please help! :?

Re: FPS issues in platformer

Posted: Tue Feb 21, 2012 5:28 pm
by molul
Try to edit your main.lua like this (it worked for me):

Code: Select all

function love.load()
	-- this is the first step for limiting the fps
	min_dt = 1/60 -- <-- WITH THIS YOU GET 60FPS. You can change it to whatever you want.
	next_time = love.timer.getMicroTime()

	<YOUR CODE>

end

--********************************************************
-- love.update
--********************************************************
function love.update(dt)
	next_time = next_time + min_dt -- second step for fps limit
	
	<YOUR CODE>
end

--********************************************************
-- love.draw
--********************************************************
function love.draw()
	<YOUR CODE>

	-- last step to limit the fps
	local cur_time = love.timer.getMicroTime()
	if next_time <= cur_time then
		next_time = cur_time
		return
	   end
	love.timer.sleep(1000*(next_time - cur_time))
end
And yes, LÖVE is amazing :)

Re: FPS issues in platformer

Posted: Tue Feb 21, 2012 5:30 pm
by thelinx
No.. No! Why do you have game logic in love.draw?

Re: FPS issues in platformer

Posted: Tue Feb 21, 2012 5:39 pm
by molul
thelinx wrote:No.. No! Why do you have game logic in love.draw?
LOL, I guess because I didn't know it was wrong to do so. I learnt this "trick" from the wiki:

https://love2d.org/wiki/love.timer.sleep (search for "More sophisticated way to cap FPS (in 0.7.2 or older)").

What's the right way?

Re: FPS issues in platformer

Posted: Tue Feb 21, 2012 6:33 pm
by bartbes
You're suppose to put the logic in love.update, and use its dt argument to scale your movement.
If you want to move 5 pixels per second, you add (or subtract) 5*dt, according to:

Code: Select all

dx = v*dt
--or
movement = speed * time
--so
pixels moved = pixels/second * time

Re: FPS issues in platformer

Posted: Tue Feb 21, 2012 7:24 pm
by MarekkPie
The OPs code looks like its already scaling by dt.

Re: FPS issues in platformer

Posted: Tue Feb 21, 2012 7:32 pm
by molul
Actually I was already using dt in my love.update.

This is funny. I added those lines to one of my first programs when I was just starting learning LÖVE, and I kept them until now. Later I learnt love.graphics.setMode() and how to initialize the window from the conf.lua file, so I was setting vsync On, which resulted in having 60fps already. I've just removed those lines I posted two posts ago and it works exactly the same (lol).

Well, it's always good to remove unneccessary parts of my code. Thanks a lot everybody! :)

Re: FPS issues in platformer

Posted: Tue Feb 21, 2012 7:49 pm
by the_nonameguy
I implemented the 60 fps limit, but it screwed up the collision detection and i had to use very high values for movement.
This method doesn't seems to be working for me... so I'm posting the .love file:

http://minus.com/mMW9JfWY0#

Please someone could give any general tips on the code (i would like to have that functionality to make the players stick to the platforms they are standing on), I'm just 16 and started writing Lua games after C#, C++ and Python last week.

Re: FPS issues in platformer

Posted: Wed Feb 22, 2012 3:59 pm
by bartbes
I must have misunderstood, what is the advantage of a fixed fps if your code works without it?

Re: FPS issues in platformer

Posted: Wed Feb 22, 2012 6:15 pm
by the_nonameguy
As I said: on a slow pc the player jumps just fine, but on a fast one it jumps through the roof...

It seems like I'm using the deltaTime correctly, but why doesn't it actually work?