Geometry and raytracing: a cookbook
From Create52 Cookbook
Contents |
Warning
This page will cause you to eat your own head.
Note on dimensions
Unless otherwise mentioned, the details below apply to any amount of dimensions. For example, the equation for rotating a vector towards another vector works just as well in 2, 3, and 57 dimensions.
Reminder of some basics
Dot product
The dot product of two vectors is calculated as the sum of the products of the two vector's corresponding components:
So, for example, for a three dimensional dot product we have:
The following useful identity holds for the dot product:
where θ is the angle between
and
.
Cross product
The cross product of two three-dimensional vectors is calculated as the following determinant:
which expands as:
The cross product is a vector which is at right angles to both
and
, and which has magnitude:
Component and perpendicular component
Component of
in direction of
(i.e. projection of
onto
) is a vector defined as:
(Derivation: draw a picture, and consider the dot product equation containing cosθ.)
Note that if there is more than 90 degrees between
and
, the
function gives a vector in the direction of
, not
.
The perpendicuar component of
in direction of
(i.e. projection of
onto plane with normal
) is a vector defined as:
Note that
and
are at right angles to each other, and together they compose
, i.e.
.
Rotating a vector towards another vector
This is accomplished by finding the component and perpendicular components and then using the 2D equation for rotation.
If vector being rotated towards is
(reference vector), and the vector being rotated is
, and the angle of rotation is θ, then let:
then find the rotated vector as:
Reflection
If
is the incident (incoming) ray and
the surface normal (which is pointing in the direction the incident ray is coming from!), the reflected ray is given by
Refraction
Snell's law of refraction:
(The refractive index, n, is a measure of the optical density of a material - i.e. it is inversely proportional to the velocity of light in the material.)
When n1 > n2, total internal reflection happens at angles greater than the critical angle, given by:
Calculating refracted ray
Firstly calculate
(due to Snell):
Note that if
, total internal reflection (T.I.R.) has occurred and you can forget about refraction. Ooh yeah.
If T.I.R. has not occurred, calculate refracted ray as:
Intersection of a line with various primitives
In the following text, we are adopting the parametric equation of a line: a line is defined by a position vector of a point on the line,
, and a direction vector,
, such that any point on the line is expressible as
whereby:
The following sections show how to find λi for the point(s) of intersection of a line with various primitives. Note that traps are listed - these are conditions where no solution can be found. The geometrical intrepretation of the trap is also given.
Plane
Plane normal is
, and
is a point on the plane.

Trap:
, which corresponds to the line direction being at right angles to the plane normal. (In this situation, the line intersects the plane in either zero or infinitely many places.)
Scope: eqn applies to any line and plane in N dimensions.
Derivation: A point
lies on the plane when the vector from
to
is at right angles to the plane normal
, hence we can write
. Then substitute in
for
and solve for λ.
Sphere
Sphere has radius r and is centered at the origin. There can be 0, 1 or 2 intersections. The intersections are found as the real solution(s) to the following quadratic:
Reminder: solutions to a quadratic are found by:
with a, b and c being the λ2, λ and constant terms respectively of aforementioned quadratic.
The amount of real solutions are dictated by the value of the discriminant, Δ = b2 − 4ac, as follows:
Trap: see comments about about the discriminant Δ.
Derivation: solve the following sphere equation for λ:
Related equations
Shortest distance from point to a line
For a point
, this is given by
Derivation 1: use dot product equation to find size of component of
perpendicular to
.
Derivation 2: Translate
such that
is at the origin, then rotate things so that
runs horizontally. Then the distance is the y coordinate of
in this system of coordinates.
Possibly unrelated
For a (hyper)sphere of any dimension, radius r, and two points on its surface
and
, the shortest distance between the points on the surface of the (hyper)sphere is:
Note that you can replace r in above equation with
(or
).






