So I've made AI thread and its working good except the movement. Main thread and AI thread are both on 60fps (testing stuff) and if I put AI on AI thread it does not always move smoothly it kinda vibrates sometimes (the units look like they vibrate while moving sometimes) /
Here is that AI thread :
private Stopwatch stopwatch;
private const double TargetFPS = 60.0;
private const double TargetFrameTime = 1.0 / TargetFPS;
public void UpdateAI()
{
stopwatch = Stopwatch.StartNew();
while (true)
{
double elapsedSeconds = stopwatch.Elapsed.TotalSeconds;
stopwatch.Restart();
for (int i = 0; i < aiList.Count; i++)
{
if (IsInstanceValid(aiList[i]))
{
aiList[i].Update((float)elapsedSeconds);
}
else
{
aiList.RemoveAt(i);
}
}
double frameTime = stopwatch.Elapsed.TotalSeconds;
double sleepTime = TargetFrameTime - frameTime;
if (sleepTime > 0)
{
System.Threading.Thread.Sleep((int)(sleepTime * 1000));
}
}
}
Now can someone tell me if this is correctly capped at 60 fps? Also how would I measure fps here?
Thanks in advance.