# How to calculate a score algorithm based on elapsed time?

I'm trying to calculate an alogirthm based on elapsed time to calculate score based on a music beat. I have the music beat divided into 4 because that's the time measure of that beat. So If the user presses the correct key at the third beat, which is of course when the perfect beat would be, I'd like it to obtain the best score. I've tried doing this:

score=percent(bpmt.elapsed,bpm/3);
int scoref(int int1,int int2) {
return int1*100/int2;
}


bpmt.elapsed being how much time was elapsed between the start of that beat and the time that the user pressed the key and bpm being the total number of milliseconds of that particular beat.

Can anyone tell me a better way to do this please? I think it requires more advanced math and it's kind of beyond me.

Most rhythm games actually have a pre-recorded or procedurally generated rhythm track, but this is okay for testing. This is because many songs' tempos very over time, the downbeat (the very first beat heard in the song or measure) can take some time to get to, and typically we want to have more complex input than a single key press.

That said, assuming ideal conditions where

1. The rhythm starts immediately
2. Never drops out and comes back off-measure
3. And the tempo never changes

...each beat will be found at n * (bpm/60) * 1000 milliseconds, where n is the n-th beat. You then need to tweak your score thresholds. Here are a few as an example:

• Miss: 60 > t > 80 ms, -4 pts
• Bad: 40 > t > 60 ms, -3 pts
• Okay: 20 > t > 40 ms, 0 pts
• Great: 10 > t > 20 ms, 1 pts
• Great: 0 > t > 10 ms, 2 pts

Those will probably need to vary based on song speed.

Now, on each frame:

nextBeat = (beatCounter+1)*(bpm/60) * 1000;
previousBeat = (beatCounter)*(bpm/60) * 1000;
timeUntilNext = nextBeat - curentTime;
timeFromPrevious = currentTime - previousBeat;


where beatCounter is incremented according to the above ranges - ie whether the beat was hit, how much time has passed, etc.