Sunday, August 19, 2012

Useful 3D Game Maths Equations

Calculating the Magnitude of a vector:


where:
v = vector
n = distance


Vector multiplication by scalar

Normalized Vectors


Vector Adding and Subtracting

Distance from one vector to another


Vector Dot Product


The dot product of two vectors is the sum of the products of corresponding components. This
results in a scalar:




Generally speaking, the dot product in any dimension tells how “similar” two vectors are; the
larger the dot product, the more similar the two vectors. Geometrically, we can be more precise


Projecting one vector onto another



Vector Cross Product

The other vector product, known as the cross product or outer product, applies to 3D vectors only.
Unlike the dot product, which yields a scalar and is commutative, the vector cross product yields a
3D vector and is not commutative.


Like the dot product, the term “cross product” comes from the symbol used in the notation: a×b.
We always write the cross symbol, rather than omitting it like we do with scalar multiplication.
The equation for the cross product is given by:





Complete list of linear algebra identities



Monday, August 13, 2012

Enter the Matrix - Lesson 2 - Types of Matrices

Types of Matrices

This lesson describes a few of the more important types of matrices: transpose matrices, vectors, and different kinds of square matrices.

Transpose Matrices

The transpose of one matrix is another matrix that is obtained by using by using rows from the first matrix as columns in the second matrix.

For example, it is easy to see that the transpose of matrix A is A'. Row 1 of matrix A becomes column 1 of A'; row 2 of A becomes column 2 of A'; and row 3 of A becomes column 3 of A'.

A =    
111222
333444
555666
 
A' =    
111333555
222444666
Note that the order of a matrix is reversed after it has been transposed. Matrix A is a 3 x 2 matrix, but matrix A' is a 2 x 3 matrix.

With respect to notation, this web site uses a prime to indicate a transpose. Thus, the transpose of matrix B would be written as B'.

Vectors

Vectors are a type of matrix having only one column or one row.
Vectors come in two flavors: column vectors and row vectors. For example, matrix a is a column vector, and matrix a' is a row vector.

a =    
11
12
33
 
a' =    
112233
 use lower-case, boldface letters to represent column vectors. And since the transpose of a column vector is a row vector, we use lower-case, boldface letters plus a prime to represent row vectors. Thus, vector b would be a column vector, and vector b' would be a row vector.

Square Matrices

square matrix is an n x n matrix; that is, a matrix with the same number of rows as columns. In this section, we describe several special kinds of square matrix.

Symmetric matrix. If the transpose of a matrix is equal to itself, that matrix is said to be symmetric. Two examples of symmetric matrices appear below.
A = A' =    
12
23
B = B' =    
567
632
721

Note that each of these matrices satisfy the defining requirement of a symmetric matrix: A =A' and B = B'. 

Diagonal matrix. A diagonal matrix is a special kind of symmetric matrix. It is a symmetric matrix with zeros in the off-diagonal elements. Two diagonal matrices are shown below.
A =    
10
03
B =    
500
030
001
Note that the diagonal of a matrix refers to the elements that run from the upper left corner to the lower right corner.

Scalar matrix. A scalar matrix is a special kind of diagonal matrix. It is a diagonal matrix with equal-valued elements along the diagonal. Two examples of a scalar matrix appear below.
A =    
30
03
B =    
500
050
005

These square matrices play a prominent role in the application of matrix algebra to real-world problems. For example, a scalar matrix called the identity matrix is critical to the solution of simultaneous linear equations. (We cover the identity matrix later in the tutorial.)



Matrix Notation Appendix

Introduction

Here, we describe how symbols are used in these lessons represent vectors, matricies, and other matrix algebra entities.

Vectors


Bold-face, lower-case letters refer to vectors; and italic lower-case letters refer to vector elements. For example:

  •  a and x refer to vectors a and x, respectively. 
  • aij refers to the element in row i and column j of vector a.

General Matricies

Bold-face, capital letters refer to matrices, italic capital letters refer to matrix elements, and subscripts reveal matrix dimensions. For example: 

  • A and X refer to matrices A and X, respectively. 
  • Aij refers to the element in row i and column j of matrix A
  • Aij refers to an i by j matrix A.
Spectial Matrix and Matrix Properties

Special matrices are represented by special notation. For example,
  • A' refers to the transpose of matrix A.
  • I refers to an identity matrix.
  • In refers to an n x n identity matrix.
  • 1 refers to the sum vector, a column vector having all of its elements equal to one.
  • 1n is a 1 x n sum vector.
  • |A| refers to the determinant of matrix A.
  • x refers to a matrix of deviation scores derived from the raw scores of matrix X.
Note: Like vectors, deviation score matrices are often denoted by a lower-case, boldface letter, such as x. This can cause confusion, but usually the meaning is clear from the context.

Elementary Operations

In many references, including this site, you will encounter a compact notation to describe elementary operations. That notation is shown below.
  • Ri <--> Rj means to interchange rows i and j of a matrix.
  • sRi --> Ri means to multiply row i by s.
  • sRi + Rj --> Rj means to add s times row i to row j.
  • Ci <--> Cj means to interchange columns i and j
  • sCi --> Ci means to multiply column i by s.
  • sCi + Cj --> Cj means to add s times column i to column j.

Echleon Matrices

  • Aref denotes a row echelon form of matrix A.
  • Arref denotes the reduced row echelon form of matrix A.

Enter the Matrix - Lesson 1 - What is a Matrix


What is a Matrix

A  matrix is a rectangular  array of numbers arranged in rows and columns , and is at the centre of matrix algebra. Matrix algebra is used quite a bit in 3d game development, largely because it provides two benefits.
  1. Compact notation for describing sets of data and sets of equations.
  2. Efficient methods for manipulating sets of data and solving sets of equations.
Below shows and example of a Matrix:

21623393
44956613
77387933
The number of rows and columns that a matrix has is called its dimension or its order. By convention, rows are listed first; and columns, second. Thus, we would say that the dimension (or order) of the above matrix is 3 x 4, meaning that it has 3 rows and 4 columns.

The numbers that appear in the rows and columns of a matrix are called the elements of the matrix. In the above example, "21" is element in the first column of the first row.


Friday, August 3, 2012

Beginning Trigonometry - Lesson 2 - Arc of a Circle

Arc of a Circle

This lesson discusses the equations needed to find the length of the arc in a circle, and calculate its area.

Diagram1

Calculate the length of an arc

To calculate the length of the arc (AB), we take the following equation:

 This can also be summed up as 

(where s = LengthAB)

and refactor it to become:


In english, this means the LengthAB = Radius * theta (angle subtended from the arc - in radians)

Example

If  the radius is 5cm and the sector of AOB subtends and angle of 2 Radians, what is the length of the arc AB?

using our formula , we can work this out to be:

s = 5x2 = 10cm 

Calculate the area of a sector

To calculate the area of a sector using radians and radius. The area of the sector is directly proportional to the angle within the sector.

To calculate the area, the blow equation is used:

area = 

Example

The circle with circle O has a radius of 3, and the sector of AOB subtends and angle of 4 Radians.
Using our formula , we can work this out to be:


which will equal 18cm



Beginning Trigonometry - Lesson 1 - Radians and degrees

Introduction


It is important to understand what a radian and degree is, and how they relate to each other. This lesson explains what that are.

Radians and degrees are just different units for measuring an angle,
much like 'cm' and 'inches'. They are directly proportional to each other.

Degrees


Degrees are a unit of measurement that are used to express directionality and the size of an angle. If you stand facing directly north, you are facing the direction of zero degrees, written as 0°. If you turn yourself fully around, so you end up facing north again, you have "turned through" 360°; that is, one revolution (one circle) is 360°.

Below shows a circle split into 360 sections. Each section is 1° (one degree):
circle split into 360 degrees


You may ask yourself how it was decided that a circle should be split into 360 parts - who created this magic number, and why, and why was it called degrees?

Well we can all thank the ancient Babylonians who lived about 5000 years ago. They viewed the numbers 6, 12 and 60 as having particular religious meanings and significance. It is because of them that we have twelve-hour nights and twelve-hour days, with each hour divided into sixty minutes and each minute divided into sixty seconds. And "once around" is divided into 6×60 = 360 parts called "degrees".

So from now one, one thing to remember is when I mention 1 full revolution, I am talking about 360°, and a half revolution would be, you guessed it - 180° - also known as "about face".

90° represents a quarter of a circle.

Understanding initial and terminal angles meaning, and what an arc is


Run the following following scenario in you mind:

1) You are facing north holding your arm out in front of you
2) you turn 90° to face west - sweeping your arm across with you. 

When people talk about the "initial angle" this was your arms starting position (north).
Where your arm ended facing west - that is the "termainal angle"
The action of your arm sweeping from the initial angle to the terminal angle is known to have "swept out" a 90° angle.
If you imaging the line that your fingertips would have draw - the curve - is known as the "arc", and the angle that was turned through is said to "subtended" that arc.


Decimal Degrees and "Degrees, Minutes and Seconds"


When you work with degrees, you'll almost always be working with decimal degrees; that is, with degrees expressed as decimal numbers such as 43.1025°. But just as "1.75" hours can be expressed as "1 hour and 45 minutes", so also "degrees" can be expressed in terms of smaller units. These units, just as for "hours", are called "minutes" and "seconds". Just as "hours" can be expressed as decimals or else as hours - minutes - seconds, so also "degrees" can be expressed as decimals or else as degrees - minutes - seconds, denoted as "DMS".

  • Convert 43.1025° to DMS form.
I can see that I have 43°, but what do I do with the "0.1025" part? I treat it like a percentage of the sixty minutes in one degree, and find out how many minutes this is:
(0.1025 degrees)(60 minutes / 1 degree) = 6.15 minutes
...or 6 minutes and 0.15 of a minute. Each minute has sixty seconds, so:
(0.15 minutes)(60 seconds / 1 minute) = 9 seconds
Then 43.1025° = 43° 6' 9"
Notice the symbols: A single quote-mark (an apostrophe) indicates "minutes" and a double quote-mark indicates "seconds". This is similar to the notation (in Imperial measurements) for "feet" and "inches": the smaller unit gets the more-substantial mark.
  • Convert 102° 45' 54" to decimal form.
Clearly, I've got 102°, but how do I convert the minutes and seconds to decimal form? By using the definitions and doing the divisions. The 45' means 45/60 of a degree, since each degree contains sixty minutes. Simplification and long division gives me 45/60 = 3/4 = 0.75. So the 45' is 0.75°.
Now I need to deal with the 54". Since each minute is sixty seconds, then I get 54/60 = 9/10 = 0.9. But this is minutes. Now I need to convert the 0.9 of a minute to degrees:
(0.9 minutes)(1 degree / 60 minutes) = 0.015 degrees
So 102° 45' 54" = 102° + 0.75° + 0.015° = 102.765°.

Radians 


Why do we have to learn radians, when we already have perfectly good degrees? Because degrees, technically speaking, are not actually numbers, and we can only do math with numbers. This is somewhat similar to the difference between decimals and percentages. Yes, "83%" has a clear meaning, but to do mathematical computations, you first must convert to the equivalent decimal form, 0.83. Something similar is going on here (which will make more sense as you progress further into calculus, etc).

The 360° for one revolution ("once around") is messy enough. Why is the value for one revolution in radians the irrational value ? Because this value makes the math work out right. You know that the circumference C of a circle with radius r is given by C = 2πr. If r = 1, then C = 2π. For reasons you'll learn later, mathematicians like to work with the "unit" circle, being the circle with r = 1. For the math to make sense, the "numerical" value corresponding to 360° needed to be defined as (that is, needed to be invented having the property of) " is the numerical value of 'once around'."


Converting Between Radians and Degrees


We know that the circumference of a circle is 2pie r. When we are measuring the angle in radians, we are trying to find the ratio of the arc to the radius.
As a result, the angle subtended at O = 
We also know that the number of degrees in a circle is 360°
Hence, 

or 


It's not necessary to remember what 1 degree or 1 radian is. More importantly, you should understand the concept and derive it on the spot.
Scholar's Tip: When the unit of an angle is not specified, it usually means that the angle is in radians.

Examples

Radians into degrees

a) 2.63 
So we simply multiply 2.63 with  to obtain
157.7°  (correct to the nearest 0.1 degrees) .

Degrees into Radians

b) 37°
We multiply     with 37 to obtain 0.64565   .