Scaling Sound Effects and Physics with Framerate
- by Thomas Bradsworth
(I'm using XNA and C#)
Currently, my game (a shooter) runs flawlessly with 60 FPS (which I developed around).
However, if the framerate is changed, there are two major problems:
Gunshot sound effects are slower
Jumping gets messed up
Here's how I play gunshot sounds:
update(gametime)
{
if(leftMouseButton.down)
{
enqueueBulletForSend();
playGunShot();
}
}
Now, obviously, the frequency of playGunShot depends on the framerate. I can easily fix the issue if the FPS is higher than 60 FPS by capping the shooting rate of the gun, but what if the FPS is less than 60?
At first I thought to just loop and play more gunshots per frame, but I found that this can cause audio clipping or make the bullets fire in "clumps."
Now for the second issue:
Here's how jumping works in my game:
if(jumpKey.Down && canJump)
{
velocity.Y += 0.224f;
}
// ... (other code) ...
if(!onGround)
velocity.Y += GRAVITY_ACCELERATION * elapsedSeconds;
position += velocity;
The issue here is that at < 60 FPS, the "intermediate" velocity is lost and therefore the character jumps lower. At 60 FPS, the game adds more "intermediate" velocities, and therefore the character jumps higher.
For example, at 60 FPS, the following occurs:
Velocity increased to 0.224
Not on ground, so velocity decreased by X
Position increased by (0.224 - X) <-- this is the "intermediate" velocity
At 30 FPS, the following occurs:
Velocity increased to 0.224
Not on ground, so velocity decreased by 2X
Position increased by (0.224 - 2X) <-- the "intermediate" velocity was lost
All help is appreciated!