Silverlight / XNA animation delay

When I use the XNA Framework (for Windows Phone), my game works fine, but when I migrate to Silverlight / XNA Framework, I had a problem with animation delay. The problem is this: When I set a fixed time step in GameTimer (timer.UpdateInterval = TimeSpan.FromTicks (333333)), the REAL time step is not FIXED, and timer events (OnUpdate, OnDraw) fire at different intervals. This code more clearly shows my problem:

Silverlight / XNA Framework: (animation delay):

TimeSpan curNow; TimeSpan lastUpdate; TimeSpan lastDraw; public GamePage() { timer = new GameTimer(); timer.UpdateInterval = TimeSpan.FromTicks(333333); timer.Update += OnUpdate; timer.Draw += OnDraw; } private void OnUpdate(object sender, GameTimerEventArgs e) { curNow = new TimeSpan(DateTime.Now.Ticks); TimeSpan elapsed=e.ElapsedTime;//Always constant and has value: 33ms TimeSpan realElapsed = curNow - lastUpdate;//Real elapsed time always changing and has a value between: 17-39ms (sometimes more then 39ms) lastUpdate = curNow; } private void OnDraw(object sender, GameTimerEventArgs e) { curNow = new TimeSpan(DateTime.Now.Ticks); TimeSpan elapsed=e.ElapsedTime;//Always changing and has a value between: 17-39ms (sometimes more then 39ms) TimeSpan realElapsed = curNow -lastDraw;//Always changing and has a value between: 17-39ms (sometimes more then 39ms) lastDraw= curNow; } 

XNA Framework: (everything works fine):

 TimeSpan curNow; TimeSpan lastUpdate; TimeSpan lastDraw; public Game() { // Frame rate is 30 fps by default for Windows Phone. TargetElapsedTime = TimeSpan.FromTicks(333333); } protected override void Update(GameTime gameTime) { curNow = new TimeSpan(DateTime.Now.Ticks); TimeSpan elapsed=gameTime.ElapsedGameTime;//Always constant and has value: 33ms TimeSpan realElapsed = curNow - lastUpdate;//Real elapsed time has a value between: 34-35ms (sometimes more then 35ms) lastUpdate = curNow; } protected override void Draw(GameTime gameTime) { curNow = new TimeSpan(DateTime.Now.Ticks); TimeSpan elapsed=gameTime.ElapsedGameTime ;//Value between: 33-34ms (sometimes more then 34ms) TimeSpan realElapsed = curNow - lastDraw;//Value between: 34-35ms (sometimes more then 35ms) lastDraw = curNow; } 
+6
source share
2 answers

Based on years of experience in the video game industry: you should not animate against a fixed frame rate in Silverlight / XNA / Mobile.

If possible, change the application to work exclusively with game time, and work with the estimated fixed frame rate (which you will never get on your mobile device - at least until they become much more powerful).

The reason XNA game methods accept GameTime is because you cannot rely on frame rate ... ever.

Do not fight the city hall ... go with the standards. Make your application as fast as possible (for example, if your animations skip too far in the frame) or slow down the animation speed according to the platform (i.e. slow down the game / application).

Note. The reason Silverlight animations usually look smoother than Flash animations is because everything is time-based and interpolated rather than a fixed frame.

+3
source

Change to

 TimeSpan.Zero; 

Yes, I know this is strange, and that is a lot. Got it from the official game guide. If I do not use it, my game will seem crazy.

+4
source

Source: https://habr.com/ru/post/913698/


All Articles