Vector Calculation and Movement
Vectors
What are they?
A vector is a representation of a point in space relative to another. You can think of a vector as an arrow.
A vector has two properties:
- Magnitude. This is the length of the arrow
- Direction. This is the angle in which the arrow is pointing.
Another way we can represent a vector is via a pair of numbers, e.g. x and y.
Let’s say you’re at point (0,0) and you move 3 units in the x direction, and 4 units in y direction. You are now at the vector position (3, 4).
Why do vectors matter?
We use x and y to measure many properties of an object, such as its position, velocity, and acceleration. Using vectors and performing vector mathematics on these properties can be very helpful.
Vector Math
Add two vectors
What does it mean to add two vectors? We can visualize this such that we have two vectors with different magnitudes and directions.
Vector A has an (x, y) value of (3,4).
Vector B has an (x, y) value of (2,-2).
By adding one to the other we shift the starting point of one the the ending point of the other, we can the trace the vector to this new point.
The mathematics of this is very simple too. Using the vectors above, we can use the calculation:
c.x = a.x + b.x
c.y = a.y + b.y
Using the above vectors as examples. We can determine Vector C by performing:
c.x = 3 + 2 = 5
c.y = 4 + (-2) = 2
Which gives Vector C the value (5,2).
Naturally it doesn’t matter the order in which we add these vectors. This can be represented visually too:
Subtract two vectors
Subtracting vectors is very much similar to addition. We can use the equation:
c.x = a.x - b.x
c.y = a.y - b.y
We can represent this visually like so:
In this scenario, we’re subtracting b from a, which we can visualize as the b, pointing in the opposite direction being added to a.
Using our initial A and B vectors, we can calculate C when we subtract B from A.
c.x = 3 - 3 = 0
c.y = 4 - (-2) = 6
Which gives Vector C the value (0, 6).
Just like scalar (single number) subtraction, subtracting b from a gives a different result than subtracting a from b.
c.x = 3 - 3 = 0
c.y = -2 - 4 = -6
Which gives Vector C the value (0, -6).
why is this useful?
Vector multiplication
Another way of thinking about multiplying a vector is scaling a vector, which means we change the vector magnitude but maintain the direction.
To scale a vector by a given amount, we multiply it’s x and y properties by a scalar value, e.g. to multiply a vector by 2 we use the following calculation:
c.x = a.x * 2
c.y = a.y * 2
Using Vector A again as our example:
c.x = 3 * 2 = 6 c.y = 4 * 2 = 8
Which gives Vector C the value (6, 8).
Vector magnitude
We’ve talked a lot about the vector magnitude but how do we actually calculate it? You may have noticed a relationship between Vectors and right angle triangles in many of these illustrations. We can think of the magnitude of a vector as the hypotenuse of a right angle triangle, with x and y being the opposite and adjacent sides. vector represented as an x and y coordinate. Basic trigonometry teaches us that the square of the hypotenuse is equal to the square of the opposite plus the square of the adjacent (c*c = a*a + a*b), therefore we can calculate the magnitude of a vector using the following equation:
mag = sqrt(a*a + b*b)
Using Vector A again as an example, we can calculate its magnitude:
mag = sqrt(3*3 + 4*4)
mag = sqrt(9 + 16)
mag = sqrt(25)
mag = 5
Why would we want to know the magnitude of a vector? This can be useful if we want to set limitations of a vector, e.g. if the magnitude of a vector is greater than 10, we can normalize it, and then multiply it by 10.
Vector normalization
Normalizing a vector transforms the magnitude of a vector to 1, while maintaining the angle. In order to normalize a vector, we can divide the x and y values by the magnitude:
c.x = a.x / mag(a)
c.y = a.y / mag(a)
Once the vector is normalized we can multiply it by any value to set the magnitude.