Search
Games / Projects

Categories
3D Vector Math

## 3D Vector Math

These are just some basic examples of the mathematical operations normally used on/with 3d vectors.

#### Length

Get the length of a vector.

`length = sqrt((X * X) + (Y * Y) + (Z * Z))`

#### Scale

Scale a vector by a magnitude (m).

```X = X*m
Y = Y*m
Z = Z*m```

#### Negate

Negate a vector.

```X = -X
Y = -Y
Z = -Z```

#### Subtract

Subtract one vector (left) from another (right).

```X = left.X - right.X
Y = left.Y - right.Y
Z = left.Z - right.Z```

Add one vector (left) to another (right).

```X = left.X + right.X
Y = left.Y + right.Y
Z = left.Z + right.Z```

#### Normalization

Normalize a vector (make its length equal one).

#### ```length = sqrt((X * X) + (Y * Y) + (Z * Z)) X = X/length Y = Y/length Z = Z/length```Cross

Get the cross product of one vector (left) and another (right).

#### ```X = (left.Y * right.Z) - (left.Z * right.Y) Y = (left.Z * right.X) - (left.X * right.Z) Z = (left.X * right.Y) - (left.Y * right.X)```Dot

Get the dot product of one vector (left) and another (right).

`dotProduct = left.X * right.X + left.Y * right.Y + left.Z * right.Z`

#### Example Vector Class

Here is an example 3d vector class incorporating all these methods in C++.

Vector3f.h