JayTracer Mathematical Background

Notation
DEFINITION:                                                        EXAMPLE: all scalars are in normal letters                                  t all vectors are in capitals                                         V the coordinates of vectors are named after the vector, suffixed with x, y and z                                                V = [Vx, Vy, Vz] dot product of vectors X and Y                                     XY dot product of vector X with itself                                 X^2 length of vector X = sqrt(X^2) = sqrt(Xx^2 + Xy^2 + Xz^2)          |X| expressions in text areas are enclosed in single quotes when it   is not obvious for the reader, what is math and what isn't      we see that 'X = tV', which...

Algebraic Properties of the Dot Product
I'll just remind you of the algebraic properties of the dot product:

AB = AxBx + AyBy + AzBz AA = |A|^2 = sqrt( Ax^2 + Ay^2 + Az^2 )^2 = Ax^2 + Ay^2 + Az^2 AB = BA A(B+C) = AB+AC (rA)B = r(AB) = A(rB)

Ray
a ray (a directed line) is given by the following equation.

X = S + tV

S is the starting point and V is the direction.

Plane
a plane is given by the following equation:

N (X - P) = 0 N is the normal and P is a point on the plane. The plane is given by all vectors PX, who are orthogonal to N (e.g. their dot-product is 0).

The intersection between the ray and the plane is the point(s) where the free vectors X are equal. In the ray X is given by S + tV, which we substitute for X in the plane equation:

N (S + tV - P) = 0 NS + tNV - NP = 0 tNV = NP - NS              tNV = N(P-S)

Easily solved for t:                    N(P-S) t = --- NV

If NV = 0, the plane normal and the ray are orthogonal, which means that the ray and the plane are parallel, i.e. no intersection.

Sphere
a sphere is given by the following equation:

(X - C)^2 = r^2

C is the center and r is the radius.

The intersection between the ray and the sphere is the point(s) where the free vectors X are equal. In the ray X is given by S + tV, which we substitute for X in the sphere equation:

(S + tV - C)^2 = r^2

We introduce D = S - C, the vector from the sphere center to the ray starting point, which gives us:

(tV + D)^2 = r^2

This can also be written as 'sqrt((tV + D)^2) = r' or '|tV + D| = r' which makes sense: the ray plus the vector from the sphere center to the ray starting point and the radius of the sphere must have equal length.

We expand:

(tVx + Dx)^2 + (tVy + Dy)^2 + (tVz + Dz)^2 = r^2 tVx^2 + Dx^2 + 2tVxDx +  tVy^2 + Dy^2 + 2tVyDy  +  tVz^2 + Dz^2 + 2tVzDz  = r^2

'Dx^2 + Dy^2 + Dz^2 = D^2' and 'VxDx + VyDy + VzDz = VD' which leaves:

tVx^2 + tVy^2 + tVz^2 +  D^2 + 2tVD = r^2

also, tVx^2 + tVy^2 + tVz^2 = t^2V^2, which leaves:

t^2V^2 + D^2 + 2tVD = r^2 V^2t^2 + 2VDt + D^2 - r^2 = 0          (2nd degree polynomial of t)

Which we solve for t:                 _______________________          /             _________  \ -2VD +- \/(2VD)^2 - 4V^2(D^2-r^2)        |      -B +- \/B^2 - 4AC   | t = -        |   x =    | 2V^2                         \              2A        / _______________________       -2VD +- \/4(VD)^2 - 4V^2(D^2-r^2) = -                    2V^2 _____________________       -2VD +- 2\/(VD)^2 - V^2(D^2-r^2) =                     2V^2 _____________________       -VD +- \/(VD)^2 - V^2(D^2-r^2) = --                    V^2

If the discriminant '(VD)^2 - V^2(D^2-r^2)' is < 0, there are no solutions, i.e. no intersections. Otherwise, there are 1 or 2 solutions.