#### Overview of this book

Physics is really important for game programmers who want to add realism and functionality to their games. Collision detection in particular is a problem that affects all game developers, regardless of the platform, engine, or toolkit they use. This book will teach you the concepts and formulas behind collision detection. You will also be taught how to build a simple physics engine, where Rigid Body physics is the main focus, and learn about intersection algorithms for primitive shapes. You’ll begin by building a strong foundation in mathematics that will be used throughout the book. We’ll guide you through implementing 2D and 3D primitives and show you how to perform effective collision tests for them. We then pivot to one of the harder areas of game development—collision detection and resolution. Further on, you will learn what a Physics engine is, how to set up a game window, and how to implement rendering. We’ll explore advanced physics topics such as constraint solving. You’ll also find out how to implement a rudimentary physics engine, which you can use to build an Angry Birds type of game or a more advanced game. By the end of the book, you will have implemented all primitive and some advanced collision tests, and you will be able to read on geometry and linear Algebra formulas to take forward to your own games!
Game Physics Cookbook
Credits
Acknowledgements
Acknowledgements
www.PacktPub.com
Customer Feedback
Preface
Free Chapter
Vectors
Matrices
Matrix Transformations
2D Primitive Shapes
2D Collisions
2D Optimizations
3D Primitive Shapes
3D Point Tests
3D Shape Intersections
3D Line Intersections
Triangles and Meshes
Models and Scenes
Camera and Frustum
Constraint Solving
Manifolds and Impulses
Springs and Joints
Index

## Normalizing

A vector with a magnitude of 1 is a normal vector, sometimes called a unit vector. Whenever a vector has a length of 1, we can say that it has unit length. A normal vector is written as the letter of the vector with a caret symbol on top instead of an arrow, . We can normalize any vector by dividing each of its components by the length of the vector:

We never implemented division operators for the vector class. We can rewrite the preceding equation as reciprocal multiplication. This means we can obtain the normal of a vector if we multiply that vector by the inverse of its length:

We are going to implement two functions, `Normalize` and `Normalized`. The first function will change the input vector to have a length of 1. The second function will not change the input vector; rather it will return a new vector with a length of 1.

### How to do it…

Follow these steps to implement functions which will make a vector unit length or return a unit length vector. These steps utilize reciprocal multiplication.

1. Declare the `Normalize` and `Normalized` functions in `vectors.h`:

```void Normalize(vec2& v);
void Normalize(vec3& v);

vec2 Normalized(const vec2& v);
vec3 Normalized(const vec3& v);```
2. Add the implementation of these functions to `vectors.cpp`:

```void Normalize(vec2& v) {
v = v * (1.0f / Magnitude(v));
}

void Normalize(vec3& v) {
v = v * (1.0f / Magnitude(v));
}

vec2 Normalized(const vec2& v) {
return v * (1.0f / Magnitude(v));
}

vec3 Normalized(const vec3& v) {
return v * (1.0f / Magnitude(v));
}```

### How it works…

Normalizing works by scaling the vector by the inverse of its length. This scale makes the vector have unit length, which is a length of 1. Unit vectors are special as any number multiplied by 1 stays the same number. This makes unit vectors ideal for representing a direction. If a direction has unit length, scaling it by some velocity becomes trivial.