FPS issues in platformer

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.
the_nonameguy
Prole
Posts: 3
Joined: Sun Feb 05, 2012 12:02 pm

FPS issues in platformer

Post 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! :?
User avatar
molul
Party member
Posts: 264
Joined: Sun Feb 05, 2012 6:51 pm
Location: Valencia, Spain
Contact:

Re: FPS issues in platformer

Post 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 :)
Last edited by molul on Tue Feb 21, 2012 5:35 pm, edited 1 time in total.
User avatar
thelinx
The Strongest
Posts: 857
Joined: Fri Sep 26, 2008 3:56 pm
Location: Sweden

Re: FPS issues in platformer

Post by thelinx »

No.. No! Why do you have game logic in love.draw?
User avatar
molul
Party member
Posts: 264
Joined: Sun Feb 05, 2012 6:51 pm
Location: Valencia, Spain
Contact:

Re: FPS issues in platformer

Post 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?
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: FPS issues in platformer

Post 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
User avatar
MarekkPie
Inner party member
Posts: 587
Joined: Wed Dec 28, 2011 4:48 pm
Contact:

Re: FPS issues in platformer

Post by MarekkPie »

The OPs code looks like its already scaling by dt.
User avatar
molul
Party member
Posts: 264
Joined: Sun Feb 05, 2012 6:51 pm
Location: Valencia, Spain
Contact:

Re: FPS issues in platformer

Post 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! :)
the_nonameguy
Prole
Posts: 3
Joined: Sun Feb 05, 2012 12:02 pm

Re: FPS issues in platformer

Post 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.
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: FPS issues in platformer

Post by bartbes »

I must have misunderstood, what is the advantage of a fixed fps if your code works without it?
the_nonameguy
Prole
Posts: 3
Joined: Sun Feb 05, 2012 12:02 pm

Re: FPS issues in platformer

Post 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?
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 57 guests