In this collision tutorial, we will take more in detail with how exact we get the normals and how we get the intervals. However, I will not be going in detail regarding vector math needed to understand this, although it is simple and I will eventually post blogs regarding them.

Down to business. Our shape is composed of vertices. In the case of this rectangle, there is four vertices, in red, and four normals, in brown. The vertices are self explanatory and the normals describe the orientation or the way a side of the triangle is facing. Take note that the vertices are defined such that the center of the triangle, not indicated in the picture, is at position(0,0) and all vertices defining the vertex is relative to this location; just like in a normal coordinate system.

Now, to get the normal for a side of the rectangle we take two vertices defining an edge of the rectangle and subtract them. Further, we swap the x, and y components and negate the new y component. Then we normalize that normal so that it has a length of one.

Now, to get the normal for a side of the rectangle we take two vertices defining an edge of the rectangle and subtract them. Further, we swap the x, and y components and negate the new y component. Then we normalize that normal so that it has a length of one.

some pseudo-code:

Vertex{ double x, double y}

Normal{ double x, double y}

Rectangle{

Vertex vtx[4];//four vtx define a rectangle

Normal normal[4];

}

for(int i=0,j=0; i<4; i++){ //loop through all vertices for

if(i == j) {

j++; //used to ensure that our indices are not referencing the same vtx

if(j > 3) j=0; //make sure we are not about of bounds to prevent memory issues

}

Vertex a = Rectangle.vtx[i];

Vertex b = Rectangle.vtx[j];

Vertex c = b - a; //the normal is the difference of a and b resuting in c

Normal N = Normal(c.y, -c.x); //the component of the normal must be swapped in 2D

N.normalize();

N.normalize();

Rectangle.normal[i] = N;

}

In the next part, we will talk about the getting the intervals.