   Horizontal acceleration and deceleration (side scroller)
  Hi, how do I make my character move slowly then at some point stops moving (decelerate) when I released the button for movement? Say if I press the right arrow button, it sets the acceleration of my character to 2 m/s.if right button is pressed {    acceleration = 2.0    direction = 1; //1 for right, -1 for left}Then, for each game loop, I update the position of my character.void update() {    currentSpeedX += (direction * accelerationX) * time;    x += currentSpeedX * time;}Now, when I release the right button, I cannot make my character stop. I tried this:if (right button is released) {    acceleration = 0;}But this line only makes my character move in constant speed. Then I triedif (right button is released) {    acceleration = -2.0;}to negate the current acceleration. It started to decelerate but after that, it went to the other direction. What formula could I use for this kind of problem? Do I need extra variables (deceleration)?These should also be implemented:1. If I released the button to decelerate then I pressed the button of the other direction, the deceleration should be faster than just releasing the button.2. The deceleration should not be of constant value. For example, the character is moving on ice, the deceleration should be smaller compared to when moving in rubber.Thanks!   
  Try something more like:if right button is pressed {    acceleration += 1;}if left button is pressed {    acceleration -= 1;}void update() {    x += acceleration;    acceleration *= 0.1;}It's acceleration that should be variable.Basically, if a key is pressed, acceleration is increased. That acceleration is applied to x.and every frame, acceleration is slightly decreased (gravity, friction, etc are applied).So, when the key is released, acceleration is automatically handled.Just tweak the code to match with what you want, give it more or less acceleration on keypress, add more weight by increasing the *= 0.1 number. Might want to try out acceleration *= 1.1 on keypress to slowly accelerate. Just make sure you accelerate more each frame than you decelerate :P.Might want to add a speed cap too, or you'll accelerate to infinity. And add a minimum cap to deceleration, cos it will never reach 0 and will slight jit around instead of completely stopping.   ;  The formula for linear velocity is:Vf = Vi + a * tWhere:Vf  = Final VelocityVi = Initial Velocitya  = Accelerationt  = Time;Your acceleration should not change, it's your velocity that changes over time. Your direction will be determined by the sign of the acceleration (+ == right, - == left). So your code would look something like this:// Arrow key being pressedif( keyDown( RIGHT_ARROW ) ) {acceleration = 2.0f;} else if( keyDown( LEFT_ARROW ) ) {acceleration = -2.0f;}// Game loopvoid Update(){// Vf = Vi + a * t (Vi here is 0)currentSpeedX = 0 + acceleration * time;// Then you just have to add your current speed to your position to get your new positionx += currentSpeedX;// At this point, your currentSpeedX should be > or < 0. If it's not, it means// you've been slowing down, so once you reach a speed of 0, stop   // accelerating. Since it's unlikely that you'll ever land EXACTLY on 0// you should check a small boundary to determine when to stop, otherwise// you'll eventually just reverse speed and start going the other way! if( currentSpeedX >= -0.05 || currentSpeedX <= 0.05 ) acceleration = 0.0;}// Arrow key releasedif( keyUp( RIGHT_ARROW ) || keyUp( LEFT_ARROW ) ){// You want to slow down, so accelerate in the opposite directionacceleration = -acceleration;}Hope this is of use to you!   ;  Just to say, first, ShadowFlare's post is very useful and will work well for what you're doing.  I just wanted to come in and say how I did things, multiple views are good.To automatically decelerate, all you have to do is add a friction variable, and multiply your velocity by it.  Looks something like this.Velocity = 0f;Friction = .95f;   //Should be close to 1, but never 1 and never over 1.If(Right || Left)  set acceleration to +-2;Velocity = acceleration * time;Velocity = Velocity * Friction;   //this makes Velocity lower every time.if(Velocity > Max_Velocity)  //make sure you don't go faster than your person should go  Velocity = Max_Velocity;else if (Velocity <= .1 || Velocity >= .1)  //helps with stopping  Velocity = 0;;  Hi! Thanks for the suggestions. Maybe I just did not fully understand your explanations but please clarify some things :)@-KAS-I think acceleration should always be constant unless you are about to slow down or you reached your maximum velocity. If you want to slow down, you should just change the acceleration to the opposite direction. If you reached max speed, just set acceleration to 0. But I still cannot solve my problems :)@ShadowflareWhy is initial velocity always 0? The update on the game loop happens about every 16ms. The aceleration is always constant so the line// Vf = Vi + a * t (Vi here is 0)currentSpeedX = 0 + acceleration * time;will always be of constant value. I think it should becurrentSpeedX += acceleration * time;Also, with the lineif( currentSpeedX >= -0.05 || currentSpeedX <= 0.05 ) acceleration = 0.0;you cannot accelerate if you are not moving (initial state)@DekasaI tried your "friction" approach.if(Velocity > Max_Velocity)  //make sure you don't go faster than your person should go   Velocity = Max_Velocity;else if (Velocity <= .1 || Velocity >= .1)  //helps with stopping  Velocity = 0;But with the "else" clause, my player wont move because its initial velocity is 0. So it always go to the else clause and sets the velocity to 0.Thanks again!   ;  I think some terms are getting confused here. "Velocity" is your current speed whilst "Acceleration" changes that velocity. If you add on a constant acceleration all the time, then your velocity should in ever larger amounts, but the actual Accleration value is a constant for what you want.Therefore you want to maintain a variable called "velocity" which is added on to your "position" each frame. If velocity is !0 then your man will keep moving at the same rate until you add or subtract some amount from the velocity, which is known as accelerating.Therefore its velocity you want to mess with, using a constant acceleration.IE (without any time delta stuff)Position+=Velocity;if (ForwardKey){  Velocity+=Acceleration (Acceleration is a const);}else{  Velocity-=Acceleration;  if (Velocity<0) Velocity=0;}// Extra credit bit, simulate friction/dragVelocity*=0.99f;  (Or some other value around there);  Hi all, I already found a solution for my problem. I just need a deceleration variable when I release a button. This is the code:if (button is released) {    deceleration = 5.0;}update() {    double temp = currentSpeedX;double result = (direction * accelerationX - oppositeDirection * decelerationX) * time;//check if player is going to the opposite directionif (temp * (currentSpeedX + result) < 0) {oppositeDirection = direction;decelerationX = 0;currentSpeedX += direction * accelerationX * time;   //set a threshold when stoppingif (Math.abs(currentSpeedX) < 0.03) {currentSpeedX = 0;}}Thanks for the help!   