SFML community forums

Help => General => Topic started by: Rabees on July 05, 2013, 12:06:05 am

Title: Problem with using Velocity to Stop a Sprite
Post by: Rabees on July 05, 2013, 12:06:05 am
Hello, I wanted to add a bit of a gradual X-axis acceleration and deceleration to my player sprite, and I am having a slight issue with deceleration. When I release the movement keys, the character almost stops, but inches ever so slightly to the right (A few pixels per second probably). This is how I'm doing it:

if(!isRightPressed && !isLeftPressed)
    {
        if(playerXVel > 0)
            playerXVel -= 8;

        if(playerXVel < 0)
            playerXVel += 8;
    }


I thought that these two statements would eventually neutralize each other and make the velocity zero, but I guess not.

That's all the code I think is necessary to post, because changing the lines inside the block to just

if(!isRightPressed && !isLeftPressed)
    {
       playerXVel = 0;
    }

solves the problem but gets rid of the deceleration. How can I do this differently and fix the bug?
Title: Re: Problem with using Velocity to Stop a Sprite
Post by: Jebbs on July 05, 2013, 12:17:32 am
I would actually like to see more code. I think it will shed some light on what is actually going on here.
Title: Re: Problem with using Velocity to Stop a Sprite
Post by: The Hatchet on July 05, 2013, 01:02:36 am
Without seeing more code I can already see some issues with your first try the 'playerXVel -= 8;' stuff.  Lets say going into the check that 'playerXVel' is actually greater than 0 but not an increment of 8, lets say it is 3.  So then you have 3-8 = -5, so now you are slowly drifting, but then your next loop sees that its -5<0, so it goes -5+8 = 3....see where i'm going? 

Have it decel at the rate you want, but have a secondary check in there that says once its speed is !=0 but is still under a small threshold, then make it 0.  Such as:
if(!isRightPressed && !isLeftPressed)
    {
        if(playerXVel > 0 && playerXVel > 8)
            playerXVel -= 8;
        else if(playerXVel > 0 && playerXVel <=8)
            playerXVel = 0;

        if(playerXVel < 0 && playerXVel < -8)
            playerXVel += 8;
        else if(playerXVel < 0 && playerXVel >=-8)
            playerXVel = 0;
    }
 

I'm sure there are better ways to do this but for a cheap fix this should work
Title: Re: Problem with using Velocity to Stop a Sprite
Post by: Rabees on July 05, 2013, 01:46:50 am
Thank you so much, that did the trick. If anybody still wants to offer an alternative solution, you can ask for some more code or just go ahead and let me know. I'm not an incredibly good problem-solver on my own so seeing how other people do things helps me out a bunch.
Title: Re: Problem with using Velocity to Stop a Sprite
Post by: Jebbs on July 05, 2013, 01:50:35 am
Lets say going into the check that 'playerXVel' is actually greater than 0 but not an increment of 8.

That was my guess too, but I wanted to make sure.