# Linear motion

What is the difference between speed and velocity?

To move a ball (or anything else) then we need to know how fast to move the ball and the direction to move it. So to answer the question –

• speed = says how fast the ball is moving.
• velocity = says how fast the ball is moving and the direction it is moving.

So in our program we need to think in terms of velocity and there are two ways we can represent velocity.

### Speed and angle

The ball is moving at 100 pixels per second at 30°.

In mathematics 0° starts along the hrorizontal axis and increases anti-clockwise but on most computer displays it increases clockwise. This is because the positive Y axis is reversed and y increases as we move down the display.

### Vectors

Instead of using the angle we can simply specify the amount the ball should travel in both  the X and Y directions and record this in a vector. In Processing we should use the PVector class to do this.

The PVector class has three fields [x, y, z] but since we are moving in 2D we can ignore the z value. As well as velocity the PVector should be used to remember the position of the ball.

###### Converting between speed/angle and vector representations

There are many situations where you might use both representations so it is important to be able to convert between representations. So using –

```a = the movement angle measured in radians
s = the movement speed
vx = the amount to move in the x direction
vy = the amount to move in the y direction
vel = a PVector object that represents the velocity [vx, vy]```
###### Speed/angle to vector
```vx = s * cos(a)
vy = s * sin(a)```

and using the PVector object

`vel.set(s * cos(a), s * sin(a))`
###### Vector to speed/angle
```s = sqrt(vx * vx + vy + vy)
a = atan2(vy, vx)```

and using the PVector object

```s = vel.mag()
a = atan2(vel.y, vel.x)
```

### Updating the balls position

Lets assume that we are using a PVector to store the ball’s current position. We can update the balls position every frame using the elapsed time between frames.

###### Speed and angle
```// pos is a PVector of the ball's current position
// the ball is traveling at 'speed'
// angle = direction the ball is traveling in radians
// speed = the ball's speed in pixels per second
// elapsedTime = time since the last frame in seconds
speed * sin(angle) * elapsedTime); ```
###### Vector
```// pos is a PVector of the ball's current position
// vel is the velocity where the fields are
//     x = the horizontal speed in pixels per second
//     y = the vertical speed in pixels per second
// elapsedTime = time since the last frame in seconds
pos.add(vel.x * elapsedTime, vel.y * elapsedTime);
```

### Which representation to use?

It depends on what you are trying to do, but generally the vector representation is preferred because it reduces the need to use the processor intensive trigonometric functions.

Next: The post Cannon Fire shows how this information can be used to simulate a medieval cannon.