Scaling Sound Effects and Physics with Framerate

Posted by Thomas Bradsworth on Game Development See other posts from Game Development or by Thomas Bradsworth
Published on 2012-11-23T01:59:10Z Indexed on 2012/11/23 5:11 UTC
Read the original article Hit count: 281

Filed under:
|
|
|
|

(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:

  1. Velocity increased to 0.224
  2. Not on ground, so velocity decreased by X
  3. Position increased by (0.224 - X) <-- this is the "intermediate" velocity

At 30 FPS, the following occurs:

  1. Velocity increased to 0.224
  2. Not on ground, so velocity decreased by 2X
  3. Position increased by (0.224 - 2X) <-- the "intermediate" velocity was lost

All help is appreciated!

© Game Development or respective owner

Related posts about XNA

Related posts about c#