### Game Programmer’s handy maths

#### Fast 90 degrees vector rotation clockwise

`Vector2 rotated = new Vector2(-original.y, original.x);`

#### Fast 90 degrees vector rotation counterclockwise

`Vector2 rotated = new Vector2(original.y, -original.x);`

#### Fast 180 degrees vector rotation

`Vector2 rotated = new Vector2(-original.x, -original.y);`

#### Relationship between speed, distance and time

```
speed = distance / time
distance = speed * time
time = distance / speed
```

#### Dot Product

The dot product takes two equal length vectors and returns a number within the range [-1, 1]. When the two vectors are perfectly aligned the result is 1, when they are perfectly opposed the result is -1, when they are perpendicular the result is 0.

This can be used to determine if two vectors are pointing towards the same direction.

#### Cross Product

Given two vectors **a** and **b**, the cross product returns a vector that is perpendicular to both **a** and **b**, it describes the normal of the plane containing both **a** and **b**.

If the two vectors have the same direction or if one of them is zero, then their cross product is zero.

#### Linear Interpolation Formula

```
float Lerp(float a, float b, float t) {
t = Clamp01(t); // Clamp01 makes sure t is between [0, 1]
return (1.0f - t) * a + t * b;
}
```

#### Inverse Lerp Formula

The inverse lerp is the function that returns a linear value between [0, 1] by linearly interpolating between the first two values with the third.

```
float InverseLerp(float a, float b, float v) {
return Clamp01((v - a) / (b - a)); // Clamp01 makes sure the result is between [0, 1]
}
```

Examples:

```
a = 10; b = 20; v = 15; -> result = 0.5
a = 10; b = 20; v = 10; -> result = 0
a = 10; b = 20; v = 60; -> result = 1
```

#### Distance between two points

```
float Distance(Vector p1, Vector p2) {
float squaredDistance = ((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y));
return sqrt(squaredDistance);
}
```

#### Signed distance between two circles

We take the distance between the two circle centers and substract the radius of both circles from that distance.

When the two circles are far from each other the distance is positive. When the two circles touch each other the distance is zero. And when the two circles overlap each other the distance is negative.

```
float SignedDistance(Vector c1Center, float c1Radius, Vector c2Center, float c2Radius) {
return Distance(c1Center, c2Center) - (c1Radius + c2Radius);
}
```

#### Signed distance between circle and point

When the point is inside the circle the distance is negative. When the point is on the edge of the circle, the distance is zero.

This function also works to calculate the signed distance between a sphere and a 3d point.

```
float SignedDistance(Vector circleCenter, float radius, Vector point) {
return (point - circleCenter).magnitude - radius;
}
```

The following function returns the distance between a circle and a point. When the point is inside or on the edge of the circle the distance is zero.

```
float Distance(Vector circleCenter, float radius, Vector point) {
float distance = (point - circleCenter).magnitude - radius;
return (distance < 0.0f) ? 0.0f : distance;
}
```