...
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 ot to simulate wind resistance or drag. If an object in a game collides with wall for example, we may want to multiply the objects vector by -1, to reverse it's direction so that it bounces off the surface.
Diagram 5. Vector multiplication
Diagram 6c = (4, 5)
2c = (8, 10)
Diagram 5. Vector negative multiplication
Normalising
...
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);
println(vec1.x);
println(vec1.y);
println(vec1.z);
println(vec1);
println(vec2);
vec1.set(10,20);
println(vec1);
println("-- Vec Add --");
PVector resVec = PVector.add(vec1,vec2);
println(resVec);
vec1.add(vec2);
println(vec1);
println("-- Vec Sub --");
resVec = PVector.sub(vec1,vec2);
println(resVec);
vec1.sub(vec2);
println(vec1);
println("-- Vec Mult --");
resVec = PVector.mult(vec1,2);
println(resVec);
vec1.mult(2);
println(vec1);
println("-- Vec Length --");
println(vec1.mag());
println("-- Vec Angle Between --");
println(degrees(PVector.angleBetween(vec1, vec2)));
println("-- Vec Normalize --");
vec1.normalize();
println(vec1);
println(vec1.mag()); |