sprite movement not smooth using delta

Hello

I am new to monogame and c#

I was updating a sprite X for movement however is it not smooth, so i have tried using delta gametime, however it does not seem any better, I am making a noob mistake no doubt but can not suss it, any help would be very much appreciated, thank you

My Update is as follows

var delta = (float)gameTime.ElapsedGameTime.TotalMilliseconds;


            if (Keyboard.GetState().IsKeyDown(Keys.Left))
            {
                vecPlayerPostion.X -= 1  * delta;
            }
            else if (Keyboard.GetState().IsKeyDown(Keys.Right)) 
            {
                vecPlayerPostion.X += 1 * delta;
            }

and draw is

spriteBatch.Draw(texPlayerTexture, vecPlayerPostion, null, Color.White, 0f, Vector2.Zero, 1f, SpriteEffects.None, 1f);

and if needed my whole code is as follows

game1.cs
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace SpaceShip
{
    /// <summary>
    /// This is the main type for your game.
    /// </summary>
    public class Game1 : Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        PlayerShip PlayerShipUser1;


        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);

            graphics.PreferredBackBufferWidth = 1920;  // set this value to the desired width of your window
            graphics.PreferredBackBufferHeight = 1080;   // set this value to the desired height of your window
            graphics.IsFullScreen = true;
            graphics.ApplyChanges();

            Content.RootDirectory = "Content";
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here


            PlayerShipUser1 = new PlayerShip();
            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);

            PlayerShipUser1.texPlayerTexture = Content.Load<Texture2D>("player");
            
            // TODO: use this.Content to load your game content here
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// game-specific content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
                Exit();

            // TODO: Add your update logic here
            PlayerShipUser1.Update(gameTime);

            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            spriteBatch.Begin();

            PlayerShipUser1.Draw(spriteBatch);

            spriteBatch.End();

            // TODO: Add your drawing code here

            base.Draw(gameTime);
        }
    }
}

playshipclass.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Audio;
namespace SpaceShip
{
    class PlayerShip
    {
        public Texture2D texPlayerTexture;
        
        private Vector2 vecPlayerPostion;

        public PlayerShip()
        {
            vecPlayerPostion.X = 800;
            vecPlayerPostion.Y = 900;

        }

        public void Update(GameTime gameTime)
        {

            var delta = (float)gameTime.ElapsedGameTime.TotalMilliseconds;


            if (Keyboard.GetState().IsKeyDown(Keys.Left))
            {
                vecPlayerPostion.X -= 1  * delta;
            }
            else if (Keyboard.GetState().IsKeyDown(Keys.Right)) 
            {
                vecPlayerPostion.X += 1 * delta;
            }

        }


        public void Draw(SpriteBatch spriteBatch)
        {

            spriteBatch.Draw(texPlayerTexture, vecPlayerPostion, null, Color.White, 0f, Vector2.Zero, 1f, SpriteEffects.None, 1f);


        }


    }
}

When scaling movement by elapsed time, think of it as how many pixels the object should move in a second. Right now the code is saying to move the player 1 pixel every second, which is very slow.

if (Keyboard.GetState().IsKeyDown(Keys.Left))
{
    vecPlayerPostion.X -= 60  * delta;
}
else if (Keyboard.GetState().IsKeyDown(Keys.Right)) 
{
    vecPlayerPostion.X += 60 * delta;
}

The above code should move it 60 pixels over a second, presuming you’re targeting a framerate of 60 frames a second. That should look smoother since moving it by very small values won’t be very noticeable difference when drawing.

thanks for you reply

That change seem to make it fly of the screen very quick, sub 1 second

Should you code work with .TotalMilliseconds; ?

Your smoothing looks good, increasing the value should help like @Kimimaru said. There is no reason from what you provided why it should fly off screen. Yo set the screen at 1920 pixels across and start the sprite half way so it should take ~16 seconds to fly off screen.

Instead of:

Just use:

spriteBatch.Draw(texPlayerTexture, vecPlayerPostion, Color.White);

and see if that helps.

I just came accross this topic, and wanted to point out :

The OP is using var delta = (float)gameTime.ElapsedGameTime.TotalMilliseconds;, so the speed will be 1 pixel per millisecond (not per second) in the original example (vecPlayerPostion.X +-= 1 * delta;).

It shouldn’t do any difference, since it’s equivalent to the original call.

I tried the code myself and can’t see how it’s not smooth. Can you describe a bit more what you meant by “is it not smooth” ? Maybe try to enable multisampling (graphics.PreferMultiSampling = true;), sometimes it makes movement appearing less blurry (at least to me).