i have been playing around with the NetworkPrediction sample from Microsoft. Trying to adapt it to use gameTime. The client is running without fixed TimeStep and the server is custom C# and updates a lot faster than client.
Both client and server runs this code at update:
protected void UpdateState(ref ShipState state, GameTime gameTime)
{
if (turnRightAcceleration)
state.Rotation += (this.RotationSpeed * (float)gameTime.ElapsedGameTime.TotalMilliseconds);
else if (turnLeftAcceleration)
state.Rotation -= (this.RotationSpeed * (float)gameTime.ElapsedGameTime.TotalMilliseconds);
Vector2 shipForward = new Vector2((float)Math.Cos(state.Rotation),
(float)Math.Sin(state.Rotation));
if (trustAccelerating)
state.Velocity += ((shipForward * this.TrustSpeed) * (float)gameTime.ElapsedGameTime.TotalMilliseconds);
// Update the position and velocity.
state.Position += (state.Velocity * (float)gameTime.ElapsedGameTime.TotalMilliseconds);
}
This seems theoretically correct, every time a value is increased or decreased its affected by ElapsedGameTime.
And this is my Server Pump:
public void Run()
{
foreach (GameServerComponent component in this.Components)
component.Initialize();
Console.WriteLine("Server running...");
//DateTime serverStart = DateTime.Now;
//DateTime lastUpdate = DateTime.Now;
Stopwatch sw = Stopwatch.StartNew();
TimeSpan lastTime = sw.Elapsed;
GameTime gameTime;
while (!exit)
{
TimeSpan currentTime = sw.Elapsed;
TimeSpan elapsedTime = currentTime - lastTime;
lastTime = currentTime;
gameTime = new GameTime(currentTime, elapsedTime);
foreach (GameServerComponent component in this.Components)
component.Update(gameTime);
}
sw.Stop();
}
This seems theoretically correct as well. Unless there is something about time management in .NET and XNA, but i cant find anything indicating it.
But the server and client updates at different rates, and the further you get from Vector 0,0 the greater the difference becomes and close to 0,0 it works quite well. But at Vector2(-1387865f, -1158338f) the "Ship" is barley able to move. I believe this tells me the server "Clock" is running slower than the client (the ship moves slower on server than on client), but i dont understand why it would be more prominent further away from 0,0 (makes me think its the UpdateState() function I need to look at)
I dont know what to test next and I have no idea how to fix this behavior.
Please, i need ideas. Or even better, can you see something obvious wrong with the code i have?
Thank you.
Update: This are the results from some measurements, recommended from William 'MindWorX' Mariager. Im currently trying to throttle the server to get closer to the clients updates. And i think i need to look at some performance from the client as well (later), since it seems it runs slower than 60fps atm. Ill update with results.
Server updateCount 252092674
gameTime.TotalGameTime {00:09:59.9810854}
avgElapsed.TotalMilliseconds 0.0021000000000000003
(float)avgElapsed.TotalMilliseconds 0.0021
Client updateCount 24027
gameTime.TotalGameTime {00:09:55.5527941}
avgElapsed.TotalMilliseconds 24.03
(float)avgElapsed.TotalMilliseconds 24.03