Jump to content
  • Advertisement
Sign in to follow this  
Degra

Frame Rate Independant Velocity

This topic is 4812 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm working on a simply 3D Pong game, and have come to write the physics for the ball's movement. What I've done of the physics routine so far is:
Public LastPhysicsUpdate As Long

Public Sub DoPhysics()

    Dim TimeChange  As Single
    
    TimeChange = (GetTickCount - LastPhysicsUpdate) / 1000
    
    ballPos.X = ballPos.X + ballVelocity.X * TimeChange
    ballPos.Y = ballPos.Y + ballVelocity.Y * TimeChange
    ballPos.Z = ballPos.Z + ballVelocity.Z * TimeChange
    
    LastPhysicsUpdate = GetTickCount

End Sub

Basically, the velocity is in texels(?) per second and is multiplied by the time since the last physics update and added to the old ball position. I was wondering - is this the way to do it or is there a better method? Degra

Share this post


Link to post
Share on other sites
Advertisement
Yeah, lock your frame rate. Don't continue if your diff is too small. I think 60fps should be fine, but make it a variable you can play with.

I don't really know VB but this would be the general idea:


Public LastPhysicsUpdate As Long
Public physicsFrameRate As Float


Public Sub initPhysics()
physicsFrameRate = 1.0f / 60.0f
End Sub

Public Sub DoPhysics()

Dim TimeChange As Single

TimeChange = (GetTickCount - LastPhysicsUpdate) / 1000

if( TimeChange >= physicsFrameRate){
ballPos.X = ballPos.X + ballVelocity.X * TimeChange
ballPos.Y = ballPos.Y + ballVelocity.Y * TimeChange
ballPos.Z = ballPos.Z + ballVelocity.Z * TimeChange

// This is only updated when we enter the loop
LastPhysicsUpdate = GetTickCount
}



End Sub

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!