Domanda

I recently started working with XNA and have a little problem. I have a sprite, which moves perfectly fine and everything, but the velocity doesnt reach 0. Its always 2.5 or -2.5 or something like that.

    protected override void Update(GameTime gameTime)
    {

        //Controls
        if (Keyboard.GetState().IsKeyDown(Keys.Right))
        {
            if(vel.X<maxVel)
                vel.X += acc.X * 3 * (float)gameTime.ElapsedGameTime.TotalSeconds;
            player.spritePos.X += vel.X * (float)gameTime.ElapsedGameTime.TotalSeconds;
        }
        else if (Keyboard.GetState().IsKeyDown(Keys.Left))
        {
            if (vel.X > maxVel*(-1))
                vel.X -= acc.X * 3 * (float)gameTime.ElapsedGameTime.TotalSeconds;
            player.spritePos.X += vel.X * (float)gameTime.ElapsedGameTime.TotalSeconds;
        }

        else if(vel.X!=0)
        {
        //The velocity should be set to 0 if no button is pressed. It doesn't work.
        //It just stops at 2.5 or -2.5, which leads to slow movement of the  charactereven if no button is pressed.

            if (vel.X > 0)
            {
                player.spritePos.X += vel.X * (float)gameTime.ElapsedGameTime.TotalSeconds;
                vel.X -= acc.X * (float)gameTime.ElapsedGameTime.TotalSeconds;
            }
            if (vel.X < 0)
            {
                player.spritePos.X += vel.X * (float)gameTime.ElapsedGameTime.TotalSeconds;
                vel.X += acc.X * (float)gameTime.ElapsedGameTime.TotalSeconds;
            }
        }
        //Collision with Screen Boundaries
        if (player.spritePos.X + player.spriteTexture.Width > GraphicsDevice.Viewport.Width || player.spritePos.X <= 0)
            vel.X = 0;
        Console.WriteLine("Velocity: " + vel.X);
        base.Update(gameTime); 
    }

As I said, I'm new to coding with C#/XNA, therefore the code is quite unclean.

È stato utile?

Soluzione

The cause is this part:

if (vel.X > 0)
{
    player.spritePos.X += vel.X * (float)gameTime.ElapsedGameTime.TotalSeconds;
    vel.X -= acc.X * (float)gameTime.ElapsedGameTime.TotalSeconds;
}
if (vel.X < 0)
{
    player.spritePos.X += vel.X * (float)gameTime.ElapsedGameTime.TotalSeconds;
    vel.X += acc.X * (float)gameTime.ElapsedGameTime.TotalSeconds;
}

If I rewrite it using the value of acc.X you provided and I am going to guess gameTime.ElapsedGameTime.TotalSeconds is 1/30 and XNA is running at 30 updates a second:

if (vel.X > 0)
{
    vel.X -= 150 * (float)1/30;
}
if (vel.X < 0)
{
    vel.X += 150 * (float)1/30;
}

Which is:

if (vel.X > 0)
{
    vel.X -= 5;
}
if (vel.X < 0)
{
    vel.X += 5;
}

So if vel.X is 2.5, it will reduce it to -2.5 Then next update it's -2.5 so it increases it to 2.5, and it keeps switching between the two but it never gets to 0.

To fix it you could try this:

if (vel.X > 0)
{
    player.spritePos.X += vel.X * (float)gameTime.ElapsedGameTime.TotalSeconds;
    vel.X -= acc.X * (float)gameTime.ElapsedGameTime.TotalSeconds;
    if (vel.X < 0)
        vel.X = 0;
}
if (vel.X < 0)
{
    player.spritePos.X += vel.X * (float)gameTime.ElapsedGameTime.TotalSeconds;
    vel.X += acc.X * (float)gameTime.ElapsedGameTime.TotalSeconds;
    if (vel.X > 0)
        vel.X = 0;
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top