|
||||||||||||||
. . . . . |
Flash 3d engine Polygone deconstruction - Test first
After the class descriptions , its time to start testing and writing some code to see how it fits together. var v1 = new Vector3d(10,10,10); v1.toString(); Nothing happens. Then write your class. Start off with declaring the member variables and then write the constructor:
class Vector3d{
var x;
var y;
var z;
// Constructor
function Vector3d(px,py,pz:Number){
x = px;
y = py;
z = pz;
}
//
// test
function toString():Void{
trace("["+x+","+y+","+z+"]");
}
}
Then compile it again. Your output should be: Now go onto the first method, addVecNew. Write the test code first: var v2 = new Vector3d(20,10,40); var dest = v1.addVecNew(v2); dest.toString();Then write the code to make the test work..
// add 2 vectors
function addVecNew(vec:Vector3d):Vector3d {
return(new Vector3d((x + vec.x),(y+vec.y),(z+vec.z)));
}
then continue on with the rest of the methods..
class Vector3d{
var x;
var y;
var z;
// Constructor
function Vector3d(px,py,pz:Number){
x = px;
y = py;
z = pz;
}
//
// test
function toString():Void{
trace("["+x+","+y+","+z+"]");
}
//
// add 2 vectors
function addVecNew(vec:Vector3d):Vector3d {
return (new Vector3d((x+vec.x),(y+vec.y),(z+vec.z)));
}
//
// minus 2 vectors dest = v2 - v1
function minVecNew(vec:Vector3d):Vector3d {
return (new Vector3d((x-vec.x),(y-vec.y),(z-vec.z)));
}
//
// length or magnitude of vec
function magnitude():Number {
return (Math.sqrt((x*x)+(y*y)+(z*z)));
}
//
// Normalize the vector
public function normalize() {
var len:Number = this.magnitude();
//gets the norm of the vector
x /= len;
y /= len;
z /= len;
}
//
// dot product of a vector
public function dot(vec):Number {
return ((x*vec.x)+(y*vec.y)+(z*vec.z));
}
//
// cross product of a vector
public function cross(vec):Vector3d {
var cross:Vector3d = new Vector3d(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;
}
//
// distance between 2 vectors
function distance(v2:Vector3d):Number {
return (Math.sqrt((x-v2.x)*(x-v2.x)
+(y-v2.y)*(y-v2.y)+(z-v2.z)*(z-v2.z)));
}
// end class
}
And here is the test code. It is not done properly but it will do me. To do it properly you would have to count the number of tests and compute by hand what the answer should be, and check against the value pumped out by your code. But anyway, this is what i did, in my test fla: var v1 = new Vector3d(10,10,10); //v1.toString(); var v2 = new Vector3d(20,10,40); var dest = v1.addVecNew(v2); dest = v1.minVecNew(v2); dest.toString(); trace(v2.magnitude()); // v2.normalize(); v2.toString(); var dotp = v1.dot(v2); trace(dotp + " = dot "); var v3 = v1.cross(v2); v3.toString(); var dis = v1.distance(v2); trace(dis + " = ditacne");
Flash Tutorials in Video Format -
Watch them now at LearnFlash.com Flash 3D Matrix classThis is my code for the completed Matrix class:
class Matrix{
var m:Array
// Constructor
function Matrix(){
m = [3]
//this is the multiplication identity mat.mrix
m[0] = [1,0,0];
m[1] = [0,1,0];
m[2] = [0,0,1];
}
//
// test
public function toString(){
trace(
"[" + m[0][0] + "," + m[0][1]
+ "," + m[0][2] + "]" + '\n' +
"[" + m[1][0] + "," + m[1][1]
+ "," + m[1][2] + "]" + '\n' +
"[" + m[2][0] + "," + m[2][1]
+ "," + m[2][2] + "]"
);
};
//
// load the identity matrix
public function ident():Void{
m[0][0] = 1;
m[1][0] = 0;
m[2][0] = 0;
m[0][1] = 0;
m[1][1] = 1;
m[2][1] = 0;
m[0][2] = 0;
m[1][2] = 0;
m[2][2] = 1;
}
//
// copy this to destination
function copyMatrix(dest:Matrix):Void{
for(var i =0;i<3;i++){
for(var j=0;j<3;j++){
dest.m[j][i] = m[j][i];
}
}
}
//
public function mult(matrix:Matrix):Matrix{
var productMatrix:Matrix = new Matrix();
productMatrix.m[0][0] = matrix.m[0][0] * this.m[0][0]
+ matrix.m[1][0] * this.m[0][1]
+ matrix.m[2][0] * this.m[0][2];
productMatrix.m[0][1] = matrix.m[0][1] * this.m[0][0]
+ matrix.m[1][1] * this.m[0][1]
+ matrix.m[2][1] * this.m[0][2];
productMatrix.m[0][2] = matrix.m[0][2] * this.m[0][0]
+ matrix.m[1][2] * this.m[0][1]
+ matrix.m[2][2] * this.m[0][2];
productMatrix.m[1][0] = matrix.m[0][0] * this.m[1][0]
+ matrix.m[1][0] * this.m[1][1]
+ matrix.m[2][0] * this.m[1][2];
productMatrix.m[1][1] = matrix.m[0][1] * this.m[1][0]
+ matrix.m[1][1] * this.m[1][1]
+ matrix.m[2][1] * this.m[1][2];
productMatrix.m[1][2] = matrix.m[0][2] * this.m[1][0]
+ matrix.m[1][2] * this.m[1][1]
+ matrix.m[2][2] * this.m[1][2];
productMatrix.m[2][0] = matrix.m[0][0] * this.m[2][0]
+ matrix.m[1][0] * this.m[2][1]
+ matrix.m[2][0] * this.m[2][2];
productMatrix.m[2][1] = matrix.m[0][1] * this.m[2][0]
+ matrix.m[1][1] * this.m[2][1]
+ matrix.m[2][1] * this.m[2][2];
productMatrix.m[2][2] = matrix.m[0][2] * this.m[2][0]
+ matrix.m[1][2] * this.m[2][1]
+ matrix.m[2][2] * this.m[2][2];
return (productMatrix);
};
// temp for testing
//x rotation matrix;
public function rotationX(sin:Number,cos:Number){
m[0][0] = 1;
m[0][1] = 0;
m[0][2] = 0;
m[1][0] = 0;
m[1][1] = cos;
m[1][2] = -sin;
m[2][0] = 0;
m[2][1] = sin;
m[2][2] = cos;
}
}
And here is my test code in the fla. It is very crap :
///////////////////////////////////
// Test matrix //
//////////////////////////////////
// constructor
var m1 = new Matrix();
// looad identity matrix
m1.ident();
//mat.toString();
// copy method
var mat:Matrix = new Matrix() ;
mat.rotationX(Math.sin(.1), Math.cos(.1));
//mat.toString();
//trace("afterrotx");
mat.copyMatrix(m1);
//m1.toString();
var m3:Matrix;
var matx:Matrix = new Matrix() ;
matx.rotationX(Math.sin(.1), Math.cos(.1));
m3 = mat.mult(matx);
m3.toString();
Brapppp....... abandoned.. I gave it away.. This engine was written in procedural programming and go too
convoluted to be worth it.. But anyway.. the structure is there.. and it is worth having a look at the class diagrams
and descriptions.. I am going on to design my own from the ground up.. see next article... |
|
||||||||||||
|
. | Home | Flash MX | Actionscript 2.0 | Flash 3D | Flash 8 | Flash Database | Flash Mobile | Flash CS3 | Java For Kids | Video Course | General Video | Photoshop | Web Design | Digital Photography | Games | free backgrounds | Resume | Streaming Video | Students Work | Links | Contact me | sitemap | reviews | . . |
||||||||||||||