Sign in to follow this  
danlthemanl

Can somebody explain Velocity and Acceleration in layman's terms?!

Recommended Posts

I've searched everywhere for a tutorial, but it just seems like beginners are suppose to already know how this works. I haven't the slightest idea how to get smooth movement or jumping using velocity and acceleration. Can someone please give me a basic understanding using programming syntax how I can take a sprite and use manipulate it's position in a smooth way.

Thanks in advanced.

Share this post


Link to post
Share on other sites
You actually have a relationship between Position, Velocity and Acceleration. Acceleration is the change of Velocity over time and Velocity is the change of Position over time.

The units are like so:
Position = meters = m
Velocity = meters/second = m/s
Acceleration = meters/second^2 = m/s^2

If you have calculus experience this is more obvious.

Something else to mention is the magnitude of velocity is speed. Magnitude means the length of the vector without sign.
var magnitude = sqrt(x * x + y * y + z * z);
That would calculate the magnitude of a 3D vector.

In a very simple model you can write:
Acceleration = some constants like gravity
[code]velocity += acceleration;
position += velocity;[/code]

Each of these ideas is represented by a vector. In 3D you'd have (x, y, z) where each of the 3 components is a magnitude along an axis. In 2D it's just (x, y). Adding vector is performed per component so in 3D the two statements below are equivalent:
[code]velocity += acceleration;
position += velocity;[/code]
and
[code]velocity.X += acceleration.X;
velocity.Y += acceleration.Y;
velocity.Z += acceleration.Z;
position.X += velocity.X;
position.Y += velocity.Y;
position.Z += velocity.Z;
[/code]
Assuming z positive going up and the only force affecting the player is gravity. We also have boolean input as moveForward, moveBackword, moveLeft, and moveRight. Angle is the currently facing angle around the Z axis. Assuming angle=0 is looking down the x axis.
[code]acceleration.X = (moveForward - moveBackword) * cos(angle) + (moveLeft - moveRight) * cos(angle + PI / 2);
acceleration.Y = (moveForward - moveBackword) * sin(angle) + (moveLeft - moveRight) * sin(angle + PI / 2);
// Normalize acceleration in the x and y plane
float length = sqrt(acceleration.X * acceleration.X + acceleration.Y * acceleration.Y);
acceleration.X = acceleration.X / length * accelerationChange;
acceleration.Y = acceleration.Y / length * accelerationChange;
acceleration.Z = -9.8; // aka 9.8 m/s^2
velocity.X += acceleration.X;
velocity.Y += acceleration.Y;
// Put a bounding on the x and y velocity
length = sqrt(velocity.X * velocity.X + velocity.Y * velocity.Y);
if (length > maxSpeed)
{
velocity.X = velocity.X / length * maxSpeed;
velocity.Y = velocity.Y / length * maxSpeed;
}
velocity.Z += acceleration.Z;
position.X += velocity.X;
position.Y += velocity.Y;
position.Z += velocity.Z;[/code]

That is the simplest form of updating I believe. If you work it out on paper you'll notice the player can accelerate up to a speed then it stops at a max speed. It also simulates inertia. (An object in motion tends to stay in motion). The one thing I didn't include was friction (drag) that would slow a player that isn't moving. this is accomplished by checking if acceleration.X or acceleration.Y are not 0 then apply a drag vector in the opposite direction of movement in either the forward/back or left/right directions.

I'll leave that to the OP unless you can't figure it out. (It does require using the dot product to make sure the drag doesn't reverse the direction of the player). The dot product property between two vectors in 3D that we care about is x1 * x2 + y1 * y2 + z1 * z2 will be greater than zero if they face the "same" direction.

Jumping is handled by doing
[code]if (on the ground and the key is down)
{
velocity.Z += 20;
}[/code]

Gravity is represented by the acceleration along the z axis so this will pull the value down. You'll need to set velocity.Z to 0 when the player collides with the ground.

I think that's everything. Read the Wikipedia pages about force also. It's important to understand.

Share this post


Link to post
Share on other sites
That's a very nice explanation.

Yesterday I made my first Pong game in C# XNA it really cemented updating the ball's position with a new velocity and speed.

I had the speed increasing by a factor with every bat hit.

I also had a random direction generator for when the ball was reset.

I also had the computer paddle follow the ball when it got close to bat the ball away.

I would recommend Pong as position/velocity/acceleration are fundamental to the game.

If you want I can post the Pong links, they're good one's, it was like finding a needle in a haystack.

I just don't know if your into C# XNA.

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