Why is the simulation running a bit slower or faster depending on FPS?

I created this simulation for a ball, and it is running at a fixedTimeStep but the issue seems to be that the lower the FPS is the slowed the simulation runs and the opposite is true on a higher FPS, i tested with 60 FPS vs 240 fps

this is on the client btw

 PredictConnection = RunService.Heartbeat:Connect(function(DT)
         local currentTime = workspace:GetServerTimeNow()
         accumulator += DT

         if clientControl and accumulator >= BallSimulation.fixedTimeStep then
            predictedVelocity = BallSimulation.updateVelocity(predictedVelocity, BallSimulation.fixedTimeStep)
            predictedVelocity = BallSimulation.limitSpeed(predictedVelocity)
            predictedPos =  predictedPos + predictedVelocity * BallSimulation.fixedTimeStep
            predictedPos, predictedVelocity = BallSimulation.handleGroundCollision(predictedPos, predictedVelocity, GroundY, ball)
            accumulator = 0
         end

         if clientControl and currentTime < clientControlendTime then
            -- The client is in control
            ball.Position = ball.Position:Lerp(predictedPos, .1)
            return
         elseif clientControl then
            clientControl = false
            blendBackStartTime = currentTime
         end

         local latency = currentTime - lastUpdateTime

         if currentTime < blendBackStartTime + blendBackDuration then
            local blendAlpha = (currentTime - blendBackStartTime) / blendBackDuration
            predictedPos = predictedPos:Lerp(serverPos, blendAlpha)
            predictedVelocity = predictedVelocity:Lerp(serverVelocity, blendAlpha)
         elseif latency < interpolationTime then
            local alpha = latency / interpolationTime
            predictedPos = predictedPos:Lerp(serverPos, alpha)
            predictedVelocity = predictedVelocity:Lerp(serverVelocity, alpha)
         else
            predictedPos = serverPos
            predictedVelocity = serverVelocity
         end

         ball.Position = predictedPos
      end)
   end
1 Like

RunService.Heartbeat runs every frame, FPS stays for “frames per second”.

That means if your pc is doing low fps heartbeat will also slow down.

For example if i do 60 FPS and i put a print in Heartbeat i will get 60 prints in a second, if i do 15 FPS i will get only 15 prints in a second.

2 Likes

oh alright that makes sense but dont all the runservice loops run every frame though. whats the possible solution?

1 Like

Usually, it’s multiplying your end result with the delta time. Watch some general tutorials about delta time and Linear Interpolation, you’ll understand what I mean. Im kind of lazy to explain it here.

Youtube video.

1 Like

Im assuming you are updating the position of a ball, meaby ?

You could add a maxframeValue and incrementing a frameValue every frame, so when you collect enough frames you update the ball position, kind like a timer.

In this way you make sure that the ball waits enough time before updating its position.

As others have said, RunService heartbeats occur once every frame, so a different FPS will cause the simulation to run at differing speeds