Showing posts with label Dot Product. Show all posts
Showing posts with label Dot Product. Show all posts

Wednesday, January 23, 2019

Matrices Pt 2

If you haven't read part one, it might be a good idea as it goes over the basics of matrices.

Multiplication
You can think of calculating the matrices multiplication by thinking in terms of vectors and dot products, For example, you have matrix A and matrix B, both are a 2x2 matrix, you want to multiply the two together to get matrix C. The top left value of matrix C is equal to the dot product of the first row of matrix A and the first column of matrix B. Then to get the top right value of matrix C, you do the same but using the second column of matrix B. The bottom two values of matrix C can be calculated in the same before but using the second row of matrix A.


Of course this scales upwards so if you have two 4x4 matrices, the working out is still the same as before. Multiplication doesn't require both matrices to have the same dimensions, although it does require the number of columns in matrix A to be the same as the number of rows in matrix B.


It should be noted that matrix multiplication is note commutative, however, it is both associative and distributive over addition. Also if a matrix is multiplied by the identity matrix then the matrix stays the same. You can also calculate the identity matrix by multiplying a matrix by its inverse.

Matrix Multiplication Properties
  • A * B ≠ B * A - This means that it isn't communative
  • A * (B + C) = A * B + A * C  - This means that it is distributive
  • A * (B * C) = C * (A * B) - This means that it is associative
  • A = A * I 
  • I = A * A^-1
Matrix Multiplication Dimension
The dimensions of the end matrix of a matrix multiplication are the number of rows in the first matrix and the number of columns in the second matrix. So if we had a 3x2 matrix and a 2x5 matrix, the dimension of the final matrix is 3x5.

Transpose
When transposing a matrix, you convert each row of the matrix to its corresponding column, so the first row becomes the first column,etc. Transposing is denoted by a T.


Transforming 3D Vectors by Matrices
So you need to transform a vector by a specific matrix, well the first thing is to convert the vector into a matrix. There are two ways to do this, row major and column major.

For example, Vector A = {1, 2, 3} can either look like:

Row Major Version

Column Major Version
Depending on the kind of matrix you are multiplying by it makes sense to convert to that type of major. So if a matrix is intended to be multiplied by a row major matrix then convert it to that type of major. However, if that is not possible you can transpose the multiplaction matrix to multiply with the opposite major type.


x^1 = x * a + y * b + z * c
y^1 = x * d + y * e + z * f
z^1 = x * g + y * h + z * i


x^1 = a * x + b * y + c *z
y^1 = d * x + e * y + f * z
z^1 = g * x + h * y + i * z

So it actually doesn't matter which major is used but it is best to be consistent with which one is used throughout a game.

All images were created using https://www.codecogs.com/latex/eqneditor.php

Wednesday, January 16, 2019

Sample Problem: Rotating a 2D Character

The Problem
In a top down shooter, there is a scripted in game cutscene where an explosion happens and the player must turn to face the explosion. The turn to face the explosion must be a smoothed transition and since the cutscene can be triggered in a variety of ways, the initial direction of the player can be different between playthroughs. We need to know the angle the player needs to rotate and whether the direction they need to rotate is clockwise or anticlockwise.

The Variables
The current variables we have access to is the initial direction the player is facing (Vector C) , the position of the player (P) and the position of the explosion (E).

The Solution
The first step is to calculate the direction towards the explosion from the player, which can be done by subtracting P from E. This gives us Vector N and since we don't care about the distance between the two positions, we can normalise Vector N.

Since we now have two unit vectors (Vector N and Vector C), we can calculate the angle between them by using the dot product calculation. So the angle is equal to arccos(dotProduct(Vector C, Vector N)). However, this doesn't tell us if the direction we need to rotate is clockwise or anti clockwise.

This can be done with the cross product. However, since both vectors are in 2D space, they need to be converted into 3D vectors. Once the cross product is calculated we can determine the direction of the rotation needed. If the z component of the resultant vector is positive then the rotation is anti clockwise, which means if the z component is negative then the rotation is clockwise.

Thursday, January 10, 2019

Dot Product

The dot product describes the relation between two vectors with a single number. The single number can mean three possible things about the angle between the two vectors.

  • If the value was 0 then the angle between the two vectors is 90°
  • If the value is positive so greater than 0, then the angle between the two vectors is less than 90°
  • If the value is negative so less than 0, then the angle between the two vectors is greater than 90°
Pseudo code
float DotProduct(Vector2 _vectorA, Vector2 _vectorB)
{
    return _vectorA.x * _vectorB.x + _vectorA.y * _vectorB.y;
}

The dot product can be used for more though as using the dot product with the unit vectors of any two given vectors can result in the actual angle between the two vectors.

  1. First find the unit vector of both given vectors
  2. Calculate the dot product of the two unit vectors
  3. Use arc cosine (dot product) to calculate the angle in radians
  4. Convert angle to degrees if needed

Also the dot product of two vectors that are the same equals the length of the vector squared. This can be used for geometric calculations. Another side note if you have a unit vector and a non unit vector you can perform scalar projects, which returns the length of the extended unit vector (Unit Vector * Non Unit Vector).