Versions Compared

Key

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

...

There are a number of ways to manipulate and combine vectors with simple operations called vector maths.

Addition and Subtraction

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.

...

Diagram 4.  Vector addition

 


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.

...

Diagram 5.  Vector multiplication

Finding the magnitude

You can find the magnitude (the length) using Pythagorean theorem. In a car game, you might need to find this value show show the speed of an object on the HUD display. 

c = (4, 5) 

sqrt(4^2 + 5^2))

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. 

...

2 Divide each vector components by the magnitude

PVector

Processing has a vector class called ‘PVector‘, which can be two or three dimensions. Basic vector maths are included in a number of methods of the PVector class, so processing can do all the hard work for you. 


Code Block
PVector vec1 = new PVector(1,2);
PVector vec2 = new PVector(3,4);
 
// read out components of vec1 
println(vec1.x);
println(vec1.y);
println(vec1.z);
 
println(vec1);
println(vec2);
 
// set new components of vector
vec1.set(10,20);
println(vec1);
 
// vector addition when creating a new vector
println("-- Vec Add --");
PVector resVec = PVector.add(vec1,vec2);
println(resVec);
 
// adding a vector to an existing vector  
vec1.add(vec2);
println(vec1);
 
// vector subtraction  
println("-- Vec Sub --");
resVec = PVector.sub(vec1,vec2);
println(resVec);
 
vec1.sub(vec2);
println(vec1);
 
// vector multiplication   
println("-- Vec Mult --");
resVec = PVector.mult(vec1,2);
println(resVec);
 
vec1.mult(2);
println(vec1);
 
// find vector magnitude    
println("-- Vec Length --");
println(vec1.mag());
 
// find angle between two vectors    
println("-- Vec Angle Between --");
println(degrees(PVector.angleBetween(vec1, vec2)));
 
// normalise a vector    
println("-- Vec Normalise --");
vec1.normalize();
println(vec1);
println(vec1.mag());