Public Group

# Decceleration for a moving object in space

This topic is 2942 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Making an object move in 2D is pretty simple. You just take your current position and add to that a vector with speed and direction. When I do this with an Xbox 360 gamepad, I just take the player's X and Y positions and for each of those add the product of the value given by the thumbstick input and a velocity with an arbitrary value. Something like this:

 player.position.x += currentGamePadState.ThumbSticks.Left.X * velocity; player.position.y -= currentGamePadState.ThumbSticks.Left.Y * velocity; 

But I want the moving object to deccelerate when the thumbstick is released, instead of just coming to a complete halt. An idea I had was that you take the last non-zero value that was given by the thumbstick's output, saved it into a variable and then multiplied that by 0.9 or something for each frame, which would basically mean the ship gradually moving less and less, simulating decceleration. I haven't figured out how to do that though because of the state changes that occur about 30 times a second.

##### Share on other sites
A ^ B is A raised to the power of B
length(V) is sqrt(V.X^2 + V.Y^2 + V.Z^2)
normalize(V) is V / length(V)
saturate(X) = the value closest to X from 0 to 1

Analytical linear stop:
 if length(OldVelocity) < SecondsPerStep NewVelocity = (0,0,0) else NewVelocity = OldVelocity - (normalize(OldVelocity) * SecondsPerStep) 

Analytical exponential stop:
 NewVelocity = OldVelocity * (PerSecondMultiplier ^ SecondsPerStep) 

Approximated air resistance for fixed timesteps:
 NewVelocity = OldVelocity * (1 - saturate(AirResistance * (length(OldVelocity) ^ 2))) 

You might want to have an air resistance all the time and an engine with a constant force.

For walking on the ground, the friction's difference between ground velocity and actor velocity can be calculated with a velocity offset for the player in the opposite walking direction. When walking forward with velocity (1,0), offset (-1,0) and ground velocity (0,0), you will have no friction because the foot is standing still relative to the ground. -offset is the actor's target velocity.

##### Share on other sites
I don't know how to put this in any other way... I don't find this helpful, because I don't understand it. I get the mathematical part, but what are these code snippets and these variables you're using? Why aren't they taking player input into account? Granted, I've had about a breakfast worth of food the past 3-4 days (throat problem) so I'm far from my best, but... what?

##### Share on other sites
Think in terms of acceleration. When the player moves the thumbstick, apply a constant (proportional) acceleration force to the player's current velocity. When the thumbstick is centered, apply a constant deceleration force. The trick is to think in terms of changes over time instead of changes per frame; this allows your framerate to vary (which it can and probably will) without affecting the simulation of movement.

##### Share on other sites
By applying air resistance all the time and just adding the force from the controller, you won't have drifting when the control is just badly calibrated and giving (0.001, -0.023).

 Velocity = (Velocity + (Controller * Force * TimeStep)) * (1 - (Resistance ^ TimeStep)) Position = Position + (Velocity * TimeStep) 

##### Share on other sites
ApochPiQ: I realize now that my mentioning frames in my original post might've given the impression that I have used some sort of frame count. My code snippet in the first post shows how I first handled movement. I think I'm getting the hang of working in terms of acceleration rather than applying constant velocities, but I'm having trouble controlling them. I threw in an if else statement to check that the velocity doesn't exceed a certain value, but it's not really giving me the smooth movement I'm looking for.

Dawoodoz: Your code snippet there looks like it could work, but I don't know what you mean by TimeStep. Is it a certain amount of time that has passed since... since what? Since the value of the thumbstick input changed, or became non-zero? Or time elapsed since the game started, or since the last update? Here's what I have to simulate horizontal movement with acceleration and decceleration:

 if (currentGamePadState.ThumbSticks.Left.X != 0) { playerVelocity.X += currentGamePadState.ThumbSticks.Left.X * playerAcceleration; player.Position.X += playerVelocity.X; } if (currentGamePadState.ThumbSticks.Left.X == 0) { playerVelocity *= playerDecceleration; player.Position.X += playerVelocity.X; } 

##### Share on other sites
TimeStep is how much time in seconds that it takes to render a frame. This can be measured by taking the system time before and after rendering 10 frames, subtracting to get the duration and divide by ten to get the duration per frame.

You can keep the resistance at all time to protect the player from moving in unreal velocity.

• ### Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 15
• 12
• 9
• 11
• 15