Game Basics Mathematical Fundamentals 6 (Dot Product)
GCC Academy
Hello everyone
I am Lee Deuk-woo for gaming maths
This session focuses on the concept of the dot product
We’ll discuss
what a dot product is and how it can be applied
Dot Product
The dot product
is a specialized operation performed on vectors
Previously, we reviewed basic vector operations
such as vector addition and scalar multiplication
These two were the basic operations
that allow us to create new vectors
in a vector space through linear combinations
However, these fundamental operations alone
may not be sufficient in using vectors
To address this, mathematicians developed special operations
that are particularly useful and widely applied
They are dot and cross products
Today, we’ll delve into the dot product
Here are some useful operations for you
The multiplication of vectors
The corresponding components are multiplied in the operation
However, this method is mainly used in contexts like mixing colors
and is rarely applied to other practical uses of vectors
On the other hand, the dot product, which we’ll discuss now
is utilized in almost all applications involving vectors
Approximately 70–80% of vector-related operations
begin with the dot product, so it is a very important operation
Another related operation is the cross product
This applies exclusively to three-dimensional vectors
We will discuss the cross product in more detail later
when we delve into three-dimensional vector operations
Let’s focus on the dot product
The dot product, as you can see
involves two two-dimensional vectors
multiplying
the x-components together and the y-components together
then summing these products
This process is not limited to two dimensions
It can extend to three, four
or even n-dimensional vectors
Multiply the corresponding components and sum the result
Regardless of the number of dimensions
the operation remains consistent
The dot product involves
multiplying corresponding components of the vectors and summing them
which always results in a scalar value
The properties of the dot product can
be summarized as follows
First, the dot product satisfies the commutative property
Since the operation involves scalar multiplication and addition
where both multiplication and addition
are commutative
the dot product naturally adheres to this property
Next is the associative property
The associative property is difficult to apply
because he operation transitions into scalar-vector multiplication
It's not an equivalent operation to the original dot product
The result involves scalar-vector multiplication
so that is the operation here
Same thing here, it's scalar-vector multiplication
So it is not an operation where identical components
are processed in a sequential manner twice
For the associative property
the operation’s nature itself is incompatible, and the resulting value is fundamentally different
So, the associative property
is not applicable to the dot product
Now, let’s consider the distributive property
In the case of the distributive property
if we first calculate the vector addition and then perform the dot product
the result will be a scalar
Similarly, if we perform the dot product on each term individually
and then sum the scalar results
the outcome will be the same
This demonstrates that the dot product
satisfies the distributive property
To summarize
the dot product satisfies the commutative property but does not satisfy the associative property
However, it does satisfy the distributive property
Now, if we examine the characteristics of the dot product
through the lens of the distributive property
The dot product applied to the addition of the same vectors
We get terms like u.u, and u.v
v.u and v.v
Due to the commutative property, u.v and v.u are equal
which simplifies the expression to u.u, v.v, e.u.v
When we calculate the dot product
of the same vector with itself
as seen here, it results to where
each component is squared
For example, the first component is squared
the second component is squared, and so on
That's what we get
This operation ultimately corresponds
to the square of the norm of the vector
Therefore, taking the dot product of a vector with itself
always results in the square of its norm
or the square of its length
Thus, the equation above can be rewritten
in terms of squared magnitudes
the square of the norm of vector u
the square of the norm of vector v
and the interaction term e.u.v
This equation is formed in this way
and since it will prove useful later
I wanted to mention it here
To summarize
the norm of vector v is calculated
as the square root of the sum of the squares of its components
When the same vector is involved in the dot product with itself, the result is always the square of this norm
For any n -dimensional vector
the dot product of the vector with itself yields the square of its norm
That's the conclusion we get
Using these properties
the dot product can be applied in various ways
First, it’s important to understand the properties of the dot product
which are closely related to trigonometric functions
particularly the cosine function
In fact, the reason the dot product
is so widely used in game production
in its application, in particular
because it relates to the cosine function
By leveraging the properties of the cosine function
the dot product can be utilized in a variety of situations
So, the formula for that is as follows
The dot product of a and b
is equal to the product of the magnitude of vector a and that of b
and the cosine of the angle theta between them
This formula is what enables this
To understand how this formula is derived, let’s briefly examine it
First, imagine an arbitrary triangle with points
point A, B, and C
Place point B at the origin
Define the vector pointing from B to A as vector C
and the vector pointing from B to C as vector a
so the side opposite point A is referred to as vector a
So the side opposite to that is vector b
When arranging the triangle in this way
we can use this triangle
to derive the cosine formula over here
Point B is set at the origin, two sides as vectors
so b defined as (0, 0)
Point C is positioned to align with the x-axis
The side opposite point A is defined as vector a
If we set vector a in the positive direction
its magnitude
corresponds to the norm of vector a
So it's the length
So it's vector a norm, 0
Now, for point A
assume the angle
between vectors c and a is beta
To determine the height
we refer to unit circle properties
For a vector with a magnitude of 1
the x-component is cosine theta
and the y-component is sine theta
If the radius is r, the components become r cosine theta and r sine theta
With vector c, whose magnitude is known
and the angle beta
it's the norm of c times cosine beta for the x-coordinate
and the norm of c times sine beta for the y-coordinate
If the positions of the points are defined as stated
we can calculate vector b
Vector b is pointing from point C to point A
or equivalently, from point A to point C
The coordinates of vector b can be determined based on earlier definitions
Next, we calculate the magnitude of vector b
and then examine the result of squaring this magnitude
Squaring each component of vector b results in an expanded form
The sum of the square of the norm of vector a, that of vector c
and twice the product of the norms of vectors a and c
multiplied by the cosine of the angle beta between them
When the magnitude of vector b is squared
it is equivalent to taking the dot product of vector b with itself
Thus, this result represents b.b
which can also be expressed in terms of vectors a and c
by rewriting the relationship
So that's the dot product
Specifically, the result becomes
the sum of the square of the norm of vector a, that of vector c
and -2a.c, where a.c is the dot product of vectors a and c
So this value is same as the one over here
From here, by canceling out
the square of the norm of a and the square
of the norm of c
we find that the dot product a.c
equals the product of the magnitudes of vectors a and c
multiplied by the cosine of the angle beta between them
So this is the conclusion we get
Using the dot product
we derived the cosine formula from trigonometric principles
If the magnitudes of the two vectors
are both 1, the formula simplifies further
reducing to the cosine of the angle
between the two vectors, the cosine of theta
This property is highly useful in various applications
We can effectively utilize proportional relationship
between the dot product and the cosine function
The cosine function’s properties can be applied in various ways
with one of the most notable applications
being orthogonality testing
This determines whether two vectors are perpendicular to each other
If the dot product of two vectors equals 0
it implies that the cosine of the angle theta between them must also be 0
For this to happen
if not 0
the cosine of theta can only be 0
for this value to be 0
So when does the cosine of theta become 0?
90 and 270 degrees
It's when the angle is 90 and -90 degrees
The condition for the dot product to be 0 directly
translates into the vectors being orthogonal
By leveraging this property of the dot product
we can tell
that the dot product of two vectors is 0
and it happens when
the two vectors are perpendicular
If we calculate the dot product of the standard basis vectors 1, 0 and 0, 1
1 by 0 by 0 by 1
it's a 0
This result indicates that the vectors are orthogonal
If we then rotate these vectors by an angle theta
so the perpendicular vector is rotated by theta
so the rotated vectors remain orthogonal
However, instead of relying on visual intuition
we can simply calculate the dot product of the rotated vectors
If the rotated vectors are represented as (cosine theta, sine theta)
and (negative sine theta, cosine theta)
their dot product becomes
cosine theta times negative sine theta
plus sine theta times cosine theta, which is 0
This confirms that
the two rotated vectors are also orthogonal
Building on this concept
we can introduce the idea of a rigid transformation
So
rigid means firm or solid
and in physics, it refers to a rigid body
which is a solid object whose shape does not deform
A rigid body
It's rigid body in physics
It can be thought of in a similar way
as in game development
Rigid transformation refers to
a type of transformation where the object’s shape
remains unchanged during the transformation
Rigid transformation
For a transformation to qualify as a rigid transformation, the following must be satisfied
After the transformation, the magnitude of all basis vectors must remain 1
All basis vectors must remain orthogonal to each other
because the original vectors in the space are orthogonal
and this property must be preserved after the transformation
The determinant of the transformation matrix must be 1
to ensure that the object does not flip
To confirm that a transformation is rigid
we can verify that the determinant of the transformation matrix is 1
Rigid transformation
It ensures that the shape of the object
remains completely unchanged
A prime example of a rigid transformation is rotation
When examining rotational transformations
we see that the magnitude of each basis vector
in the transformed space remains 1
Let's consider the basis vector represented by cosine theta and sine theta
Its norm is given by the square root of cosine squared theta plus sine squared theta
According to the Pythagorean theorem
we know this equals 1
When squared, the negative sign in the second basis vector disappears
resulting in cosine squared theta plus sine squared theta
The second basis vector also has a magnitude of 1
It hold true regardless of whether we examine the vectors horizontally or vertically
We also previously verified that the basis vectors
remain orthogonal to each other
Next, let’s examine the determinant of the transformation matrix
ad-bc
In this case, cosine squared theta plus sine squared theta
is equal to the determinant value ad - bc
This confirms that the determinant of the matrix is 1
Such a rotational transformation
ensures that the shape and structure of an object remain
unchanged after the transformation
This is the rigid transformation
Application of the Dot Product
Let’s examine how the dot product is practically applied
One prominent application is determining the front or the back
By using the cosine formula embedded in the dot product
we can evaluate the relative position of a target
For example, imagine a person facing
forward with a view vector
If there is an object in space
we can define a vector
pointing from the person to the object
When the angle between
these two vectors is theta
taking their dot product produces a value proportional to cosine theta
The graph of cosine
starts at 1 when x equals 0
and decreases as the angle moves
How does it work?
Toward 90 degrees or negative 90 degrees
reaching 0 at these limits
beyond this, the cosine value turns negative
This means that the values are positive from -90 degrees to 90 degrees
As long as the angle between the two vectors
the values remain positive until they reach plus and minus 90 degrees
Beyond this range, the cosine value becomes negative
Since the magnitude of a vector is never negative
when the dot product of two vectors yields a positive result
it indicates that the cosine of the angle is positive
Conversely, if the dot product is negative, it signifies that the cosine of the angle is negative
Therefore, if the dot product of the two vectors is positive
it implies that the object is on the same side
as the viewing direction
which we describe as being in front
On the other hand, if the dot product is negative
it indicates that the object is on the opposite side, or behind
This concept can be further applied
to determine whether an object lies within a specific field of view
Consider the following scenario
Based on the direction you’re facing
the angle between your line of sight and the target object is called alpha
You can use this to check
whether alpha falls within
a predefined viewing angle, beta
The field of view is typically defined symmetrically
around the viewing vector
so half of the field of view on one side is beta divided by 2
The task is to determine whether the target lies
within this range, meaning whether alpha is
less than beta divided by 2
If the object is detected within the field of view, it means alpha is smaller than beta divided by 2
However, while the field of view beta is known
the specific value of alpha may not be readily available
and needs to be calculated
To measure the angle
one could use inverse functions such as the arctangent
However, using the dot product is a more efficient way
to determine whether an object lies
within the field of view without needing
to calculate the actual angle alpha
Here’s how it works
First, precompute and store the value of cosine of beta divided by 2
Since the field of view
for a character typically doesn’t change often
precomputing and storing this value is advantageous
Next, normalize both the viewing vector and
the vector pointing toward the target
Normalization ensures that both vectors have a magnitude of 1
When these normalized vectors are dot-multiplied
the result is the cosine of alpha, the angle between them
Due to the properties of the cosine function
its value decreases as the angle increases from 0
all the way to 180 degrees
All the way down
The cosine function decreases in value as the angle increases
Therefore, if the cosine of alpha is greater than
the cosine of beta divided by 2
it means the angle alpha is smaller than that
By comparing the two cosine values
if the cosine of alpha is greater than the cosine of beta divided by 2
we can determine that the target is within the field of view
even without calculating the exact angle
While inverse functions can sometimes be used
leveraging the properties of the dot product
is far more efficient and practical for this purpose
Next, let’s look at shading calculations
In computer graphics, particularly in 3D rendering
the direction of light significantly
influences shading
The dot product is an essential tool here
When there is a light source
the light strikes a surface, reflects off it
and eventually reaches our eyes
The amount of light illuminating the surface
is maximized when the surface is oriented perpendicular
to the direction of the incoming light
As the angle between the light direction
and the surface normal increases
the amount of incoming light gradually decreases
This decrease is
directly proportional to the cosine of the angle between them, described in Lambert’s Cosine Law
Shading is determined based on the cosine of the angle
between the surface normal and the light direction
This simple formula allows shading to be efficiently calculated using the dot product
To apply this, first, calculate the surface normal vector
and the vector pointing from the surface to the light source
Normalize the light vector so that its magnitude becomes 1, then compute the dot product between the two vector
The result determines how much light is hitting the surface
and serves as a key factor in defining the shading
The surface normal vector is typically denoted as N
and the vector pointing from the surface to the light source is labeled as L
It's commonly paired as N.L
This basic formula for shading is
derived from Lambert’s Cosine Law
and is the foundation for what is commonly called a diffuse shader
For example, in game engines like Unity
a diffuse shader utilizes this principle
to define surface illumination
based on the angle between the light
That's the gist of this
The N.L term represents
the most fundamental lighting model
and serves as the building block
for more complex lighting calculations
All you need to understand for now
is that this N.L is at the fundamental level
Lastly, I’ll explain the projection vector formula
This formula is used
to determine the projection
of one vector onto another
This formula comes handy in getting that
When you have two vectors, u and v
and you project u onto v, the resulting vector
is what I’ll call v'
This v' is the projection onto v
So it is aligned in the same direction as v
but with a different magnitude
Different magnitude, same direction
To calculate v’
since the direction is the same and the magnitude different
we first need to normalize v
By normalizing it, we get this v-hat
Create a unit vector v-hat with a magnitude of 1
Then, by multiplying v-hat by the magnitude of v’
we can determine v’
But how do we calculate the magnitude of v’?
How do we get its norm?
That's the question here
Since we know what u is
he magnitude of v’ can be found by multiplying the magnitude of u
by the cosine of the angle, cos(theta), between u and v
So, how do we calculate cos(theta)?
Cos(theta) can be determined using the dot product
Revisiting the dot product equation
u dot v is equal to the magnitude of u times
the magnitude of v times cosine of theta
The cosine of the angle between them, cos theta
can ultimately be rewritten and calculated
using this formula
Now, if we substitute this value back into the equation
and multiply them together
we can derive the final formula for the projection vector
This v'
The final projection vector is now here
Here, for the dot product u dot v
the denominator consists of the magnitude of u by that of v
For v-hat, the denominator is the magnitude of v
and the numerator is v
Since the magnitude of v is used twice
the result becomes the square of the magnitude of v
Therefore, the expression can be written as u.v.v
Since the numerator and denominator involve u
any overlapping terms will cancel out
The final formula for the projection vector is as follows
The square of the magnitude
is equivalent to the result of multiplying the same vector by itself twice
so it can also be expressed as this
Both are considered identical expressions
If the magnitude of the vector v being projected onto is 1
there is no need to calculate the magnitude
and the formula simplifies further
Now, this projection vector formula
is significantly utilized in various vector applications
and theoretical aspects, such as equations of planes
and other mathematical theories
For now, I’ll explain that this basic formula exists
and I’ll provide a more detailed explanation of its applications
later when we discuss plane equations
This was on dot product
This concludes today's lecture
Thank you for listening
Thank you
Concept of Vector Dot Product Vector Dot Product Multiplying corresponding components and then summing them
Properties of the dot product: Satisfies the commutative property, Not satisfy the associative property, Satisfies the distributive property (a,b)x(c,d)=ac+bd
The dot product of the same n-dimensional vector results in the square of its norm
Cosine Formula of the Dot Product axb=vector a x vector b x cosine theta When the magnitudes of two vectors are 1, scalar a x scalar b = cosine theta
Concept of vector inner product Determining orthogonality of vector inner product You can prove that two vectors are orthogonal by the inner product
(cos(theta), sin(theta)) dot (-sin(theta), cos(theta)) equals to 0
Rigid transformation A transformation that maintains the shape of an object Rotation transformation does not change the shape of an object
Applications of the Dot Product Front-Back Determination If the dot product of two vectors is positive, the object is in front
If the dot product is negative, the object is behind the viewing direction
Field of View Check The inner product of the line of sight vector and the vector to the target is cosine alpha If the value of cosine alpha is greater than beta over cosine 2, the target is in the field of view
Shading Calculation Lambert’s Cosine Law: The brightness of light reflected is proportional to cos angle between surface and light direction
Shader Lighting Equation N.L The normal vector of the surface in space (N) The vector pointing from the surface to the light source (L)
Projection Vector Formula Finding a vector projected onto another vector
When the magnitude of the vector being projected is 1, vector v’ is u dot v multiplied by v