Frame Rate Questions

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.
koopatroopa
Prole
Posts: 6
Joined: Thu Dec 08, 2011 11:35 pm

Frame Rate Questions

Post by koopatroopa »

Hey

So this is sorta a basic question:
Until now I have been doing all my coding in the love.update() function but i began to think "If this was being played on a much faster computer that could manage a faster frame rate wouldn't the game be played much quicker to because it would be updating far faster" ?

if i say as long as the mouse is down change X by 3 this will happen every frame but if the frame rate is higher it will happen faster???

if this is True how do you work around it
User avatar
ivan
Party member
Posts: 1911
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: Frame Rate Questions

Post by ivan »

Yes, you have to take into account delta(dt) which is the time elapsed between updates.
So instead of change X by 3 you would write change X by (3*dt)
where 3 is the speed (3 units, usually pixels, per second)
and dt is delta in seconds
User avatar
Ellohir
Party member
Posts: 235
Joined: Sat Oct 22, 2011 11:12 pm

Re: Frame Rate Questions

Post by Ellohir »

dt is the elapsed time. So, if one call is made 0.1 seconds after the previous, that code would move the player 0.3 pixels on position; so, in the end, after all the different times we can have, the player will always move 3 pixels on one second, whatever machine it is played on :awesome:
User avatar
verilog
Citizen
Posts: 97
Joined: Thu Nov 03, 2011 3:15 am
Contact:

Re: Frame Rate Questions

Post by verilog »

Hey folks,

I've a similar question. Suppose you have a function which contains an array and a counter to drive its index. The function is called randomly by the user. The functionality you’re looking for is to cycle trough the array elements each time a frame is drawn, that is, we get element number 1 (index = 1) of the array at frame 1, we get element number 2 (index = 2) at frame 2, and so on.

Here, we need the counter to update after every dt time, but the counter can’t be a direct function of dt, as we strictly need integer values (incremented in exactly 1 unit) to drive the indices of our array. How would you do that? :P
User avatar
MarekkPie
Inner party member
Posts: 587
Joined: Wed Dec 28, 2011 4:48 pm
Contact:

Re: Frame Rate Questions

Post by MarekkPie »

If I understand correctly, then can't you just increment the counter in love.update(dt)?

Code: Select all

array = {--[[stuff]]}

function A()
  return array[index]
end

function love.update(dt)
  index = index + 1
end
I'm a little confused by what exactly you're looking for. Give an example.
User avatar
verilog
Citizen
Posts: 97
Joined: Thu Nov 03, 2011 3:15 am
Contact:

Re: Frame Rate Questions

Post by verilog »

Hey MarekkPie,

I’ll tell you why I’m asking this. In the game I’m working on, I wanted to give the illusion of a “smooth” character jump; I decided to move the player along the Y axis according to a free fall formula, a parabola.

Anyway, I computed the Y points needed for this and stored them in an array, so each time the “jump” function was called the character was translated to a Y position according to the Y values in my array. The character needed to move one position exactly on each new frame, so I was a little bit unsure of how to do this, hence my previous question.

What I ended up doing was exactly what you posted, but I was wondering if there is an alternate method for achieving this.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Frame Rate Questions

Post by Robin »

You could use dt. ;)

Code: Select all

function A()
  return array[math.floor(index)] -- this is the trick that makes it work
  -- it rounds index to the nearest smaller-or-equal integer.
end

function love.update(dt)
  index = index + dt * speed
end
See math.floor for more information.
Help us help you: attach a .love.
User avatar
miko
Party member
Posts: 410
Joined: Fri Nov 26, 2010 2:25 pm
Location: PL

Re: Frame Rate Questions

Post by miko »

verilog wrote:Here, we need the counter to update after every dt time, but the counter can’t be a direct function of dt, as we strictly need integer values (incremented in exactly 1 unit) to drive the indices of our array. How would you do that? :P
The counter can be float, only array index should be integer. So:

Code: Select all

char=Char[math.floor(counter)]
would do it.
My lovely code lives at GitHub: http://github.com/miko/Love2d-samples
User avatar
MarekkPie
Inner party member
Posts: 587
Joined: Wed Dec 28, 2011 4:48 pm
Contact:

Re: Frame Rate Questions

Post by MarekkPie »

Depending on his system speed, just flooring dt might skip or repeat frames and whatnot. For example, an average dt of 1.5 would give him:

Code: Select all

First pass:
array[math.floor(dt)] or array[1]

Second pass:
array[math.floor(dt * 2)] or array[3]
I think the only way to guarantee a number will give you the correct integer value is if you increment it by an integer. At which point just having a separate integer-based counter is best.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Frame Rate Questions

Post by Robin »

MarekkPie wrote:Depending on his system speed, just flooring dt might skip or repeat frames and whatnot. For example, an average dt of 1.5 would give him:
You know, if dt is 1.5, you have an FPS of 0.67. Skipping frames is not your worst problem then.
Help us help you: attach a .love.
Post Reply

Who is online

Users browsing this forum: No registered users and 200 guests