Sign in to follow this  
Metallon

Decceleration for a moving object in space

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:

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

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 this post


Link to post
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:
[code]
if length(OldVelocity) < SecondsPerStep
NewVelocity = (0,0,0)
else
NewVelocity = OldVelocity - (normalize(OldVelocity) * SecondsPerStep)
[/code]

Analytical exponential stop:
[code]
NewVelocity = OldVelocity * (PerSecondMultiplier ^ SecondsPerStep)
[/code]

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

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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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).

[code]
Velocity = (Velocity + (Controller * Force * TimeStep)) * (1 - (Resistance ^ TimeStep))
Position = Position + (Velocity * TimeStep)
[/code]

Share this post


Link to post
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:

[code]

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;
}
[/code]

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this