Page 1 of 1
keeping everything at 60fps
Posted: Sun Jan 05, 2025 2:01 am
by bigfeetedboard
I want to make an image that kind of bops to the beat of the music, but I don't really know how to make it consistent with the framerate.
In this code, I want it to go every second
Code: Select all
function love.update(dt)
print(dt)
if count <= 60 then
count = count+(1*dt*100)
beat = false
else
count = 0
beat = true
end
if beat then
scalem = 0.2
end
scalem = scalem-scalem*0.1
end
I think this is possible but I can't get it. I'm very new to Love2d so I ask for help. thanks in advance
Re: keeping everything at 60fps
Posted: Sun Jan 05, 2025 8:51 am
by ivan
It is not possible on modern operating system because there are many processes running at the same time.
You can't ensure with 100% certainty that your function will be executed within less than a second.
Also, your question has already been asked/answered several times on here.
Re: keeping everything at 60fps
Posted: Mon Jan 06, 2025 1:09 am
by RNavega
You can simplify things a lot by modeling beats out of the total time passed, plus using some multiplications, divisions and modulo (%) to transform that number into other useful numbers.
Here's the context:
- You need to accumulate 'dt' into some variable. Let's call it "gameTime" or something like that. It's the total time in seconds that have passed since the program started, and you accumulate it by simply doing "gameTime = gameTime + dt" inside love.update(). Note that gameTime starts as zero.
- You know how many seconds-per-beat you want to have. It's a constant that you define early in your code, let's call it BPS, for example "BPS = 2.5" (you want 2 and a half beats-per-second, AKA 150 beats per minute).
- A simple division multiplication of gameTime by BPS will give you how many beats have happened in total since the game started, that is, "totalBeats = gameTime * BPS".
With this totalBeats number you can do many fun things: after you do a math.floor() on totalBeats so you only keep the integer part of it, you can find out what's the current beat of the measure you're in (you modulo that floored totalBeats by the desired number of beats-per-measure, another constant, and this will give you a number in the range 0, 1, 2, 3...n), and you can also find out what measure you're in (you divide the totalBeats by that beats-per-measure number, giving you totalMeasures, a soft number where the whole part means how many full measures have passed, and the decimal part is the percentage of the current measure already complete).
While ivan is technically correct that the FPS of your program might oscillate as it runs, I think this will usually be imperceptible (unless the user is doing some other heavy thing in the background).
Re: keeping everything at 60fps
Posted: Mon Jan 06, 2025 5:21 am
by bigfeetedboard
okay so rnavega's solution didnt work, but i found one that did
Code: Select all
--for 120bpm
beats = 1
function love.update(dt)
local songtime = song:tell("seconds")
if songtime >= beats then
beats = beats+1
end
end
source:tell checks for the time that the song has elapsed instead of the game. my code might be a bit shoddy but it works the best
Re: keeping everything at 60fps
Posted: Mon Jan 06, 2025 7:59 am
by ivan
OK, I misread your original post. You are trying to sync the movement on the screen with music which is different than "keeping everything at 60fps". You may want to look into
waveforms as well.
Re: keeping everything at 60fps
Posted: Mon Jan 06, 2025 8:41 am
by BrotSagtMist
You are only counting seconds, there is no need to interact with the soundfile at this point.
That IS what dt is for after all.
But note that everything done in love.update will be out of sync by 1/60s
So if you want to have an accurate audio/video coupling, its complicated.
Meantime, thats enough:
Code: Select all
C,S=0,0
bps=1
function love.update(dt)
C=C+dt*bps
if S ~= math.floor(C) then
S= math.floor(C)
print(C)
end
end
Re: keeping everything at 60fps
Posted: Mon Jan 06, 2025 12:16 pm
by glitchapp
I think you are looking for this:
https://github.com/rxi/lovebpm
You can then get the values from beat and subbeat like this:
Code: Select all
local beat, subbeat = music:getBeat()
and use it to animate anything, I did that successfully in a game and all I can say it works.