Vector Maths

Vectors & Points

A vector/point represents a displacement in 3D space. Vectors have no central displacement point (origin) whilst points do. For basic concepts, the idea of a vector and a point are the same.

Mathematical operations on vectors are performed along each axis. E.g. (a) + (b) = (ax+bx, ay+by)

Magnitude

Magnitude (|v|) = the squareroot of the sum of the squares

Normalised vector = vector/vector magnitude. The magnitude of a normal = 1

Dot Product

Dot product of a and b = the sum of ai*bi for each i

Angle Between 2 Vectors

The angle is arccos of the cross product (of the normalised vectors).

Cross Product

Only 3D vectors have cross products.

Cross products are cool - the magnitude of the cross product of two vectors is also the area of the parallelogram they form.

Polygon Area

The area of a polygon is just the sum of each triangle’s area (pi, pi+1 and p0) minus any negative space.

Finding Points in Polygons

To find whether a point is in a polygon, we have to draw a line from the polygon out to the far far right, and count whether it crosses any of the polygon edges (edge or bottom vertex). If it crosses an odd number then it’s inside it

Planes

Where C = origin, a and b are axes. Similar to lerping (chuck in a position along, get out at value at whatever resolution you want).

Point Normals basically says because any vector is on the (flat) plane then the normal can be worked out using any point.

Ray Colliding with Points

Remembering that normals of scaled vectors are the same (e.g. normal (vector) = normal (scale(vector)) ray tracing works by checking n·(Vector-B)=0 and that means the ray has hit the line.

Parametric ray:
R(t) = A+ct
Point normal segment:

Collide when:

Also to work out which way you are entering via the ray you can check if n . c < 0 for entering and vice versa for exiting. where n is normal and c is the line generated

Lerping

If a is the value at the first point, and b the value at the second point, and f the percentage along the lines, then the value = a -af + bf

E.g. p(1) = 3, p(3) = 5 therefore p(4.5) = 3 - (3*.75) + (5*.75) = 4.5

Bilinear Interpolation

Bilinear interpolation is just linear interpolation in 3D space. Instead of passing in x coords and getting y values using 2 points, we have to consider 4 points of x and z and get y values.

E.g. p(0,0) = 2, p(0,1) = 3, p(1,0) = 3, p(1,1) = 4
p(0.5, 0.6) = ?

1. First we find the points above - p(0,0) and p(1,0) - we lerp between these points to find the value at x = 0.5
• p(0.5, 0)
• = 2 - (2*.5) + (3 *.5)
• = 2 - 1 + 1.5
• = 2.5
2. Secondly we find the points below - p(0,1) and p(1,1) - we lerp between these points to find the value at x = 0.5
• p(0.5, 1)
• = 3 - (3*.5) + (4*.5)
• = 3 - 1.5 + 2
• = 3.5
3. Thirdly we lerp between the two points we just explored - x on the top z line, and x on the bottom z line, using the above two values
• p(0.5, 0.5)
• = 2.5 - (2.5 * 0.5) + (3.5 * 0.5)
• = 2.5 - 1.25 + 1.75
• = 3

This is less nice if we don’t have an axis-aligned rectangle, this is Malcolm’s guide

page revision: 4, last edited: 13 Aug 2014 04:51