//***********************************************
//
// 2D vector class
//
// Free for non-commercial use.
// No guarantees of correctness or suitability.
// Use at your own risk :)
//
// © 2005, coded by Scott Fadick
//
//***********************************************
function vector2d(){
    
this.x = 0;
    
this.y = 0;
};
vector2d.
prototype.plus =
function(vector){
    
this.x += vector.x;
    
this.y += vector.y;
};
vector2d.
prototype.minus =
function(vector){
    
this.x -= vector.x;
    
this.y -= vector.y;
};
vector2d.
prototype.mult =
function(number){
    
this.x *= number;
    
this.y *= number;
};
vector2d.
prototype.divide =
function(number){
    
this.x /= number;
    
this.y /= number;
};
vector2d.
prototype.magnitude =
function(){
    
return Math.sqrt((
this.x*
this.x)+(
this.y*
this.y));
};
vector2d.
prototype.normalize =
function(){
    
var len =
this.magnitude();
    
if(len == 0)len = 0.0001;
    
this.divide(len);
};
vector2d.
prototype.copy =
function(vector){
    
this.x = vector.x;
    
this.y = vector.y;
};
vector2d.
prototype.output =
function(){
    
trace(
"vector: "+
this.x+
", "+
this.y);
};
// convert to left hand normal
// remember to normalize!!
vector2d.
prototype.rot_up =
function(){
    
var temp =
this.x;
    
this.x =
this.y;
    
this.y = -temp;
};
vector2d.
prototype.dot =
function(vector){
    
return ((
this.x*vector.x)+(
this.y*vector.y));
};
// for cross rotate one vector 90 deg and take dot
// if cross on normalized vectors is between 0 and 1
// then angle between is less than 180 deg
vector2d.
prototype.cross =
function(vector){
    
return ((
this.x*vector.y)-(
this.y*vector.x));
};
vector2d.
prototype.angle_between =
function(vector){
     v_temp1.x =
this.x;
     v_temp1.y =
this.y;
     v_temp2.x = vector.x;
     v_temp2.y = vector.y;
     v_temp1.normalize();
     v_temp2.normalize();
    
return Math.acos(v_temp1.dot(v_temp2));
};
// call this after a collision is detected
vector2d.
prototype.bounce =
function(vector){
     vector.normalize();
     v_temp1.x = vector.y;
     v_temp1.y = -vector.x;
     // get copy of movement
     v_temp2.x =
this.x;
     v_temp2.y =
this.y;
     // project both
     var dot =
this.dot(vector);
    
this.x = dot*vector.x;
    
this.y = dot*vector.y;
     //
     dot = v_temp2.dot(v_temp1);
     // add em up
    
this.x -= dot*v_temp1.x;
    
this.y -= dot*v_temp1.y;
};
// working vars
v_temp1 =
new vector2d;
v_temp2 =
new vector2d;