Page 1 of 1

[Solved] Yet another frame rate-related question

Posted: Wed Aug 31, 2011 6:17 pm
by Darkslime[Z]
I'm new to LOVE and these forums(and to Lua, really), but I decided to see what's up and try to program a few simple things. Like others, I assume, I soon ran into a slight problem regarding timing and frame rates.

After searching around this forum a little, I found that you can somewhat limit the frame rate by using the love.timer.sleep() according to dt. I also have been using dt in all my update functionality(update object movement, etc) as is recommended to do.

I have two problems with both of these scenarios. Basically, I have a player on the bottom of the screen that shoots bullets towards the top when you hold down the z button. For testing purposes, these bullets contain an event to make them veer to the right after a certain amount of time has passed.

The first problem is that as more bullets are drawn on the screen, the game's frame rate begins to slow down(I'm not using sleep(), I'm outputting 1/dt onto the screen). LOVE seems to want to stay at about 1000 updates per second, but once I have around ten or so bullets on the screen, it slows down to like 200. I obviously only want 60; maybe then it would be drawing at a constant rate.

That might not be as much of a problem if not for this second element. I'm giving the bullet objects a table to list events that modify its behavior - such as "at step 20, make y-speed 50". Each bullet keeps its own timer, which(at the moment) increments by 100*dt each time the bullet is updated. My current approach to this is: if the timer has exceeded the frame of the event between this update and the previous one, then execute the event.

The clear problem with this is that now when I fire a stream of bullets, they all end up at different y-positions(that is, they go up, then veer directly right at "step" 20, but plus or minus a couple of pixels). If I'm going to make patterns of bullets that require timing precision, this is going to cause everything to be off a bit. And since I obviously can't be sure about the value of dt every single frame, I can't get it so that a bullet's timer ends up exactly on every whole number.

Does anyone have any advice? Is LOVE suited to something that requires a bit more timing like this? I can upload my .love file if needed. I'm not too familiar with the language yet, so some of this might sound a little strange; if it does, I'll try and clarify it. :death:

Re: Yet another frame rate-related question, it seems...

Posted: Wed Aug 31, 2011 10:25 pm
by kikito
hello there!

First of all - you will get better and faster help if you upload a löve file displaying the problem you are having.

Second, have you considered not using sleep at all? Simply calculate the new position of the player and bullets using their speed and dt.

Re: Yet another frame rate-related question, it seems...

Posted: Wed Aug 31, 2011 10:28 pm
by GijsB
make a timer when updating things=

Code: Select all

local timer1 = 0
local fps = 60

function love.update(dt)
 timer1 = timer1+dt
 if timer1 > 1/fps then
  --DO STUFF HERE
  timer1 = 0
 end
end

Re: Yet another frame rate-related question, it seems...

Posted: Wed Aug 31, 2011 11:07 pm
by Rad3k
Or you can try using my version of love.run function. If you use it, you'll get constant (mostly) framerate and fixed dt in love.update.

Re: Yet another frame rate-related question, it seems...

Posted: Thu Sep 01, 2011 8:56 am
by vrld
If you want exact timing behavior, you can define parametric curve, where the paramter is the time that has passed and the target variable(s) are the x and y coordinate the bullet has to be at a given time. For example, to go up and then make a 90 degree right turn after 1 second:

Code: Select all

function right_turn(t, start_x, start_y, velocity)
    if t <= 1 then
        return start_x, start_y + velocity * t
    end
    return start_x + velocity * (t-1), start_y + velocity * 1
end
To change the speed at some timestep s:

Code: Select all

function speed_change(t, s, start_x, start_y, dx, dy, vel1, vel2)
    if t < s then return start_x + dx * vel1 * t, start_y + dy * vel1 * t end
    local r = s - t
    return start_x + dx * (vel1 * s + vel2 * r), start_y + dy * (vel1 * s + vel2 * r)
end
Does that help?

Re: Yet another frame rate-related question, it seems...

Posted: Thu Sep 01, 2011 12:50 pm
by kraftman
GijsB wrote:make a timer when updating things=

Code: Select all

local timer1 = 0
local fps = 60

function love.update(dt)
 timer1 = timer1+dt
 if timer1 > 1/fps then
  --DO STUFF HERE
  timer1 = 0
 end
end
you should change timer1 = 0 to timer1 = timer1-1/fps

Re: Yet another frame rate-related question, it seems...

Posted: Thu Sep 01, 2011 4:06 pm
by Darkslime[Z]
Thanks for the quick replies, everyone.
kraftman wrote:
GijsB wrote:make a timer when updating things=

Code: Select all

local timer1 = 0
local fps = 60

function love.update(dt)
 timer1 = timer1+dt
 if timer1 > 1/fps then
  --DO STUFF HERE
  timer1 = 0
 end
end
you should change timer1 = 0 to timer1 = timer1-1/fps
I used this in the update() method, and everything seems to be hunky-dory after taking out all the dt factors in my movement routines. Game speed seems to be stable, and the bullets' movements appear exact. Thank you!

@vlrd: I'll keep your parametric code in mind in case something goes wrong. Thanks!