/*Assumed Knowledge: Understanding of object and classes, a basic grasp of trigonometry and calculus. There is no kinematics or real mechanics in this tutorial, but this tute is necessary for what I plan to write soon.*/
/*Note, where needed, Vectors will be written in rows and not in column form, for obvious reasons. What a bugger. */
What is a Vector?
A Vector is a defined by a magnitude and a direction. The magnitude is the size or "length" of the Vector, the direction is an angle taken from a reference point to denote where the Vector is directed towards. Examples of Vectors are displacement, velocity, acceleration, jerk, force and momentum.
Why Use Vectors?
Vectors are essential in mathematics, but not necessarily in OOP, so why use them? Using Vectors can give you extreme power such that you can use functions enabling you to apply external forces to objects without having to interfere with the object directly. For example, you could apply a wind force to an object and the object will internally calculate the acceleration due to that force, or perhaps you want a realistic collision between two bodies, and have your system calculate the resultant velocities of each bodies via momentum.
Setting Up
class Vector {
/*------------------
Variable Declaration
------------------*/
/*Numbers*/
public var _x:Number = new Number(0);
public var _y:Number = new Number(0);
/*------------------
Function Declaration
------------------*/
/*Constructor*/
function Vector(param_x,param_y) {
_x = param_x;
_y = param_y;
}
This is a basic 2D vector set-up. The _x and _y are the two components of the vector. IE: How far the vector moves horizontally and vertically. For example, a purely horizontal vector will have no vertical component, but say, an angled force can be considered to have some of it's force directed horizontally, and some of it directed vertically.
//Add - Adds Another Vector
function Add(param_vec:Vector) {
_x += param_vec._x;
_y += param_vec._y;
}
This function will use basic vector arithmetic to add another vector to it. If you have two force vectors, acting on one body, the resulting force will be the corresponding components of each vector added together. (example: somebody pushing a car with the exact same force that the wind is pushing against the car. The resultant will be that the car will stay stationary when the components are added together.)
//Scale - Factors The Vector By A Scalar Quantity
function Scale(param_scalar:Number) {
return new Vector(_x*param_scalar,_y*param_scalar);
}
This function returns the vector with its components multiplied by a constant value. This is useful in calculating thing such as acceleration from a known force, where the mass is a scalar quantity, and a = F/M . Acceleration.AddTo(Force.Scale(1/Mass));
Using a bit of trig, we can calculate the magnitude and direction of the vector should we ever need it.
//GetMagnitude - Returns The Absolute Value Of The Vector
function GetMagnitude() { return Math.sqrt(Math.pow(_x,2)+Math.pow(_y,2)); }
//GetDirection - Return The Absolute Direction From The Horizontal
function GetDirection() { return Math.atan(_y/_x)+Math.PI*((_x<0) ? 1 : (_y<0)*2); }
GetMagnitude is simply pythagoras' theorem, GetDirection uses a trigonometric identity to find the angle of direction clockwise from the positive horizontal axis in radians. This could be useful for finding the _rotation property of a vehicle based on it's acceleration vector.
Enjoy pissing around with this. I always like to put a zero function in for frame iterative purposes, like:
//Zero - Zeroes The Vector
function Zero() {
_x = 0;
_y = 0;
}
For Flash-related reasons (to people who care) I have excluded the cross and dot product from this tutorial. I'll be following this tute up with applications of Vector Math. After I do one on kinematics I can start doing some real mechanics.
~Sekky~