_x = 200;
_y = 200;
The above sets the centre of the stage as 0,0
post = new Array();
post = ["p", 0, 2, 3, 0, "p", 0, 2, 1, 0, "p", 1, 2, 4, 1, "p", 4, 2, 3, 4];
vectorx = new Array();
vectorx = [0, 0, 50, 100, 100];
vectory = new Array();
vectory = [1100, 1200, 1150, 1100, 1200];
vectorz = new Array();
vectorz = [0, 0, -50, 0, 0];
Ok. The vector arrays store coordinates (five coordinates in a pyramid, five numbers in each array)
“post” stores the POSTition of the coordinates (the order needed to draw the pyramid). ”p” means start drawing a new polygon.
var slope:Number = 0;
var rotXY:Number = 0;
var rotZY:Number = 0;
var xplus:Number = 0;
var yplus:Number = 0;
var zplus:Number = 0;
Variables related to functions, I’ll go over this soon.
var neg:Number = 0;
var drawit:Boolean = true;
Variables related to drawing.
getScalerX = function () {
newPostXR = (150/vectory[i]);
xpostr.push(vectorx[i]*newPostXR);
};
getScalerZ = function () {
newPostZR = (150/vectory[i]);
zpostr.push(vectorz[i]*newPostZR);
};
Ignore this for now, we’ll come to it in a minute.
getRotXY = function () {
The function for rotation around the z axis
delete (xpostr);
xpostr = new Array();
xpostr is the position array, it is modified in the part I told you to ignore, we WILL get to it.
for (i=0; i<vectorx.length; i++) {
We want to modify every coordinate in the vectorx array
if (vectorx[i] == 0) {
vectorx[i] = 0.0001;
}
If vector x=0, program=dead. You CANNOT devide by zero.
if (vectory[i] == 0) {
vectory[i] = 0.0001;
}
Same as above.
hyp = Math.sqrt((vectorx[i]*vectorx[i])+(vectory
[i]*vectory[i]));
angle = Math.atan(vectory[i]/vectorx[i])/(Math.PI/
180);
if (vectory[i]<0 && vectorx[i]<0 or vectory[i]>0 && vectorx[i]<0) {
angle += 180;
}
This gets the angle between 0,0 and the coordinate on a 2d plane. If you don’t understand this, learn trig then return.
newY = hyp*(Math.sin((rotXY+angle)*(Math.PI/180))
);
newX = hyp*(Math.cos((rotXY+angle)*(Math.PI/180))
);
Finds the new coordinates through trig. Takes the global angle and the coordinates angle and adds them together to get the new coordinate. If you are going to say something like “this is unnecessary, matrixes work faster” yes they do, but the hypotenuse and angles are needed for shading and culling-which will be explored in the next tutorial.
vectorx[i] = newX;
vectory[i] = newY;
Changes the values of vectorx and vectory. Btw I know that a name like “coordx” makes more sence, but I don’t care.
getScalerX();
Muw ha ha the part I put off. Go up and look at the code then come back. Ok? Good. That code basically makes it so if the polygon is far away, it is small, if you don’t understand the specifics, try harder. It also adds the modified coordinates to the drawing array, which is in perspective.
}
};
getRotZY = function () {
Basically the same as above, I am not going over it.
delete (zpostr);
zpostr = new Array();
for (i=0; i<vectorx.length; i++) {
if (vectorz[i] == 0) {
vectorz[i] = 0.0001;
}
if (vectory[i] == 0) {
vectory[i] = 0.0001;
}
hyp = Math.sqrt((vectorz[i]*vectorz[i])+(vectory
[i]*vectory[i]));
angle = Math.atan(vectory[i]/vectorz[i])/(Math.PI/
180);
if (vectory[i]<0 && vectorz[i]<0 or vectory[i]>0 && vectorz[i]<0) {
angle += 180;
}
newY = hyp*(Math.sin((angle+rotZY)*(Math.PI/180))
);
newZ = hyp*(Math.cos((angle+rotZY)*(Math.PI/180))
);
vectorz[i] = newZ;
vectory[i] = newY;
getScalerZ();
}
};
getMove = function () {
This function controls the global movement. Its not that hard to figure out so just look over it.
if (Key.isDown(Key.UP)) {
yplus = -3;
} else {
if (Key.isDown(Key.DOWN)) {
yplus = 3;
} else {
yplus = 0;
}
}
if (Key.isDown(Key.RIGHT)) {
xplus = -3;
} else {
if (Key.isDown(Key.LEFT)) {
xplus = 3;
} else {
xplus = 0;
}
}
if (Key.isDown(90)) {
zplus = 3;
} else {
if (Key.isDown(88)) {
zplus = -3;
} else {
zplus = 0;
}
}
if (Key.isDown(65)) {
rotXY = 1;
} else {
if (Key.isDown(83)) {
rotXY = -1;
} else {
rotXY = 0;
}
}
for (i=0; i<vectory.length; i++) {
vectorx[i] += xplus;
vectory[i] += yplus;
vectorz[i] += zplus;
}
};
getWire = function () {
This is the drawing function. In the next tutorial I will expand it to include back-face culling and shading.
startoff = 0;
How many polygons there are at the beginning of the draw function, none.
for (ii=0; ii<post.length; ii++) {
“post” holds the shape of the polygon, so we have to draw every entry in it.
if (post[ii] == "p") {
If we need to draw a new polygon.
startoff++;
# of polygons goes up
drawit = true;
We assume that the polygon can be drawn
delete (poly);
poly = new Array();
for (neg=(ii+1); neg<(ii+4); neg++) {
trace(vectory[post[neg]]);
if (vectory[post[neg]]<0) {
drawit = false;
}
}
Tests to see if all the polygons coordinates are positive.
_root["square"+startoff].removeMovieClip()
;
_root.createEmptyMovieClip("square"+starto
ff, startoff);
with (_root["square"+startoff]) {
lineStyle(2, 0x000000, 100);
moveTo(xpostr[post[ii+1]], zpostr[post[ii+1]]);
}
}
Starts the new shape.
if (post[ii] !== "p") {
We don’t want to draw “p”’s.
with (_root["square"+startoff]) {
if (drawit) {
lineTo(xpostr[post[ii]], zpostr[post[ii]]);
Ok this is pretty cool. Xpostr contains the coordinates, but we are examining “post”. “post”’s entries correspond with xpost’s coordinates, so xpostr[post[ii]] gives you the next drawing coordinate.
}
}
}
}
};
onEnterFrame = function () {
getMove();
getRotXY();
getRotZY();
getWire();
};
Order in which things are done
Thanks for reading my tutorial, I hope it helps you. NG needs some 3d games. If you use this tutorial to create a game, please include me in the credits.