Inconsistent Movement

I’m making my first game with MonoGame (and Nez), a very simple game where you shoot aliens that fly up from the bottom of the screen. I thought everything was fine until I did a release build of the game, opened the exe, and saw that the movement of the aliens were slower than in Visual Studio. When I run a debug build of the game in Visual Studio, the performance is completely fine (3500-4000 fps). Also, the movement of the aliens is always the same after each build, with them moving at the correct speed. However, when I run the exe of the same build outside of Visual Studio (either a release or debug exe), the fps is consistently about 1000 fps lower (though this is not noticeable, and I wouldn’t know this without the fps counter). However, the biggest problem is that the aliens sometimes move slower than they’re supposed to, despite the builds being the same. Everything else in the game runs completely fine, however. The menus don’t lag, and the timer in the corner seems to be running correctly; the aliens are the problem. Also, the speed doesn’t vary between alien to alien while the game is running; the alien speed is consistent when the game is running, it just varies between launches of the game.

I then wanted to see if this problem occurred on another computer (both the computer it’s being developed on and the test computer are running Windows 10), and after running the exe on the second computer, the aliens moved significantly slower than on my main computer. However, like before, the menus and timer are completely unaffected.

Is this a problem with my code, the way the exe is being built, or something else? I’m pretty new to MonoGame/Nez/C#, so my code might not be the most optimized/efficient. Linked below are related classes:
Alien (the individual aliens)
AlienScene (the Scene where the game is actually played)

you can try to use the gametime if you haven’t use that yet. Like speed * gameTime.TotalMilliseconds. It sounds to me that the FPS changes the speed of your aliens. And with the gametime you should not notice the difference in FPS and have an overall same speed.

1 Like

Changing the line var velocity = TargetDirection * t; to var velocity = TargetDirection * t * 3500; and the line Entity.Position += velocity; to Entity.Position += velocity * Time.DeltaTime; (both in the Alien class) seems to have fixed the slow-moving aliens on my end, however, when I tested it on the second computer, they jitter around, without moving much (this is from the same exe). Did you mean to change these lines, or something else?

You need to take a step back and do some reading on it

Have a read through here

Basically your not running your game at a fixed time step (uncapped fps) this is fine and allows for smooth movement.

However if your alien is set to move 1 pixel each game frame, at 60fps he will move 60 pixels in 1 second. At 600fps he will move 600 pixels in one second

To counter that monogame has a built in counter which shows how many milliseconds the last frame took. So you would times your velocity by this amount so movement will be consistent no matter the framerate.

If you start using acceleration that increases over time things do get a bit more complex.

You need the GameTime variable from your main game’s Update method. Don’t inherit from IUpdateable. Make your own interface and ensure that the update takes in a gametime parameter…

public interface IUpdateable
{
  void Update(GameTime gametime);
}

Use GameTime.Elapsed.TotalSeconds to calculate how much time has passed and use that in your velocity calculation.

public class Alien : IUpdateable
{
  private Vector2 _position = Vector2.Zero;
  private Vector2 _direction = Vector2.Right;
  private float _speed = 0.05f;

  public void Update(GameTime gameTime)
  {
    _position += _direction * _speed * gameTime.Elapsed.TotalSeconds;
  }
}

This should result in an Alien that moves to the right at a speed of 0.05 pixels per second (assuming you’re not doing any render scaling).

This should give you consistent movement, regardless of whether or not you’re using fixed or variable time step. At least, consistent enough that you shouldn’t notice anything drastic. However, if you’re including a physics engine, you may want to consider using a fixed time step for your physics calculations only. This makes sure that everything moves in step together and can cut down on floating point errors. I think there’s other reasons too but I forget them :smiley:

To achieve this, in your main update loop, enforce an update call to your entities at some fixed interval. You probably don’t need to worry about that just yet though. Test the above first and see how it goes for you :slight_smile:

*Edit: I looked around your code. It doesn’t look like you even use an Update method from AlienGame at all, which inherits from Core. I suspect this comes from Nez, yea? This might change things a bit… but the same principles should apply. I don’t have any experience with Nez, but as long as you’re moving with respect to the amount of time that has passed, you should be ok.

Thanks for the help! I think I might be doing something wrong that’s Nez-related, as in a non-Nez game, I have the line Position += velocity * (float)gameTime.ElapsedGameTime.TotalMilliseconds * Speed; that works perfectly fine, but when I put this in Nez, it behaves very strangely.

I don’t know anything about Nez, but since it’s a physics engine, I’m wondering if you’re not supposed to update the position manually. I’d bet that you set the motion properties (direction, velocity, acceleration) on the component itself and the physics engine will just handle it for you.

Maybe look at what represents a Nez component, do you see anything like that there?