Public Group

# Frame Rate Independant Velocity

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

## 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 on other sites
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 LongPublic physicsFrameRate As FloatPublic Sub initPhysics()    physicsFrameRate = 1.0f / 60.0fEnd SubPublic Sub DoPhysics()    Dim TimeChange  As Single        TimeChange = (GetTickCount - LastPhysicsUpdate) / 1000    if( TimeChange &gt;= 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

• 16
• 9
• 13
• 41
• 15