Skip to content

Instantly share code, notes, and snippets.

@Mr00Anderson
Created February 5, 2021 18:12
Show Gist options
  • Save Mr00Anderson/369ffa6d4ffd40dce33affd72a114699 to your computer and use it in GitHub Desktop.
Save Mr00Anderson/369ffa6d4ffd40dce33affd72a114699 to your computer and use it in GitHub Desktop.
using System;
using System.Diagnostics;
namespace ServerCommon
{
public class FixedPhysicsTimeStep : AbsLooper
{
/**
* This may range between 15-200
*/
private readonly int framesPerSecond;
/**
* Server Hertz
*/
private readonly float step;
/* Fixed Time Step for Physics Simulation */
private float accumulator;
private long currentTime;
public FixedPhysicsTimeStep(int framesPerSecond)
{
this.framesPerSecond = framesPerSecond;
this.step = 1.0f / framesPerSecond;
currentTime = 0;
}
public override void start()
{
// TODO Add processing stats to know if a zone needs to be transfered POSSIBLY BY ITS HERTZ BY REVERSING THE CALCULATION
var stopwatch = Stopwatch.StartNew();
currentTime = DateTime.Now.Millisecond;
while (running)
{
long newTime = DateTime.Now.Millisecond;
float delta = newTime - currentTime;
if (delta > step) delta = step;
currentTime = newTime;
accumulator += delta;
while (accumulator >= step)
{
parent.process(step);
accumulator -= step;
}
// Would need a new loop or custom done for unity here
// for (final ServerZone serverZone : zones) {
//// serverZone.interpolate(accumulator / step);
// }
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment