mx2k_03

Actionscript 2.0 – Vector3D class(v3d)

This class called “v3d” is meant for use in 3D Flash animations. It was written by me and Deamothul, and adapted from various AS1 prototypes(AHAB, WIS, et al). It contains most of the functions needed for 3D work. There may be a few missing. It has been tested but not to the nth degree, so if there are any mistakes , please contact us.
This class is used in the next tutorial about backface culling of 3d Objects
Usage is as follows:
var v1:v3d = new v3d(100,250,150);
var v2:v3d = new v3d(25, 60,67);
// v1 – v2
v1.minVec(v2);
v1.dot(v2);
v1.cross(v2);
v1.getNormal();
v2.normalize();

And here it is ..

class v3d {
public var x:Number;
public var y:Number;
public var z:Number;
public var vec:v3d;

// MAIN CONSTRUCTOR //

function v3d(px:Number, py:Number, pz:Number) {
x = px;
y = py;
z = pz;
}

// TOSTRING
public function toString() {
trace(“[“+x+”,”+y+”,”+z+”]”);
}

// RESET AND RESETNEW
//to reset the components of the vector we use this method:
//this resets the current vector, it doesnt return a new vector
public function reset(px:Number, py:Number, pz:Number) {
x = px;
y = py;
z = pz;
}
//to reset the components of the vector we use this method:
//this resets the current vector , it returns a new vector
public function resetNew(px:Number, py:Number, pz:Number) {
return (new v3d((x=px), (y=py), (z=pz)));
}

// CLONEVEC and CLONEVECNEW
//to clone the vector we use this method:
//this clones the current vector , it doesnt return a new vector
public function cloneVec(vec) {
x = vec.x;
y = vec.y;
z = vec.z;
}

//to clone the vector we use this method:
//this clones the current vector , and does return a new vector
public function cloneVecNew(vec) {
return (new v3d((x=vec.x), (y=vec.y), (z=vec.z)));
}

//INCREASEMENT OF THE VECTOR COMPONENTS

//increase components of the vectors by a scalar
//to increase the vectors x component:
public function incrX(xScalar:Number) {
x += xScalar;
}

//to increase the vectors y component:
public function incrY(yScalar:Number) {
y += yScalar;
}

//to increase the vectors z component:
public function incrZ(zScalar:Number) {
z += zScalar;
}

//to increase the vectors x and the y component:
public function incrXY(xScalar:Number, yScalar:Number) {
x += xScalar;
y += yScalar;
}

//to increase the vectors x and the z component:
public function incrXZ(xScalar:Number, zScalar:Number) {
x += xScalar;
z += zScalar;
}

//to increase the vectors y and z component:
public function incrYZ(yScalar:Number, zScalar:Number) {
y += yScalar;
z += zScalar;
}

//to increase the vectors x y and z components:
public function incrXYZ(xScalar:Number, yScalar:Number, zScalar:Number) {
x += xScalar;
y += yScalar;
z += zScalar;
}

//VECTOR ADDITION

//method to add vectors to eachother
//without returning a new vector from the product
public function addVec(vec:v3d) {
x += vec.x;
y += vec.y;
z += vec.z;
}

//method to add vectors to eachother ,
//this method does return a new vector from the final product
public function addVecNew(vec:v3d) {
return (new v3d((x + vec.x), (y + vec.y), (z + vec.z)));
}

// VECTOR SUBTRACTION

//method to subtract vectors from eachother
//without returning a new vector from the final result
public function minVec(vec:v3d) {
x -= vec.x;
y -= vec.y;
z -= vec.z;
}
// subtract new
//method to substract vectors from eachother ,
//this method does return a new vector from the final result
public function minVecNew(vec:v3d) {
return (new v3d((x – vec.x), (y – vec.y), (z – vec.z)));
}

// get the NEGATIVE from a vector

//method to get a negative vector from a
//vector without returning a new vector.
public function getNegativeVec(vec:v3d) {
x = -x;
y = -y;
z = -z;
}

//method to get a negative vector from a vector
//and returning a new vector.
public function getNegativeVecNew(vec:v3d) {
return (new v3d((x=-x), (y=-y), (z=-z)));
}

// scaling a vector by a scalar

//method to scale a vector by a scalar, this changes
//the current vector and does not return a new vector
public function scaleVec(scalar:Number) {
x *= scalar;
y *= scalar;
z *= scalar;
}

//method to scale a vector by a scalar,
//this does return a new vector of the final end result
public function scaleVecNew(scalar:Number) {
return (new v3d((x *= scalar), (y *= scalar), (z *= scalar)));
}

// dot product of a vector

public function dot(vec) {
return ((x*vec.x)+(y*vec.y)+(z*vec.z));
}

// cross product of a vector

public function cross(vec) {
var cross:v3d = new v3d(0, 0, 0);
cross.x = (y*vec.z)-(z*vec.y);
cross.y = (z*vec.x)-(x*vec.z);
cross.z = (x*vec.y)-(y*vec.x);
return cross;
}

//getting the normal/norm of a vector

//The normal vector, often simply called the “normal,”
//to a surface is a vector perpendicular to it.
//Often, the normal unit vector is desired,
//which is sometimes known as the “unit normal.”
//The terms “normal vector” and “normalized vector”
//should not be confused, especially since unit norm vectors
//might be called “normalized normal vectors” without redundancy.
public function getNormal() {
return (Math.sqrt((x*x)+(y*y)+(z*z)));
}

//normalising a vector

// Unit vectors have a length of one.
//If you have a particular vector v you can use it to make a unit vector.
//This is called normalizing the vector:
public function normalize() {
var norm:Number = getNormal();
//gets the norm of the vector
x /= norm;
y /= norm;
z /= norm;
}
//get the unit vector

//Unit vectors have a length of one.
//If you have a particular vector v
// you can use it to make a unit vector.
public function getUnitVec() {
var unit:v3d = new v3d(x, y, z);
var norm:Number = getNormal();
//gets the norm of the vector
unit.x /= norm;
unit.y /= norm;
unit.z /= norm;
return (unit);
}
}
//ends class

If you’re searching for a laptop pc, then search no further! Our pc store has everything you need! Take a glimpse at a desktop pc or our cheapest prices on a laser printer you will find!

.