Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

If you add to vectors together, the resulting vector is the equivalent of stacking the vectors end to end. The maths behind it is simple, just add the x positions of each vector together and then add the y positions together. Adding vectors together can be used to simulate simple physics like the effect of wind or gravity, so for every step forward a game character might have a wind vector added to their movement vector. If a character in a game jumps while moving forward, then we also want to add the vectors of the upward movement with the forward motion, and then in reverse when they fall back down (or by adding a gravity vector which points down). 


a = ( 3, 6)  

b = ( 7, 2)

a + b = (10, 5)

...

Diagram 5.  Vector addition


Subtraction is a useful way of finding a vector between two points. If I subtract two points (x1,y1) from (x2,y2) the result is the vector between the two points.

Scaler Multiplication

Scaler Multiplication only effects the magnitude of the vector, leaving the direction unchanged. However, if the vector is multiplied by a negative, then the direction is reversed! Scaler Multiplication can be used to control the acceleration of space ship or to simulate wind resistance or drag. If an object in a game collides with wall for example, we could multiply the objects vector by -1, to reverse it's direction so that it bounces off the surface.

...

c = (4, 5) 

sqrt(4^2 + 5^2))

The PVector class in processing has a method that returns this value: mag(); 

Normalising

A normalised vector or a unit vector has had its magnitude set to one, with the direction left unchanged. A unit vector shows us a direction alone, without a magnitude. Sometimes we are only interested in direction, and removing magnitude can make many calculations much simpler. 

...