Now when the movie loads, we need to set some variables so lets start off by setting the origin
QUOTE
origin = new Object();
origin.x = 300;
origin.y = 200;
origin.x = 300;
origin.y = 200;
What this does, is it defines the origin to be an object with physical properties such as X and Y coordinates and it then sets those coodinates to 300 and 200, which is the center of the movie. Now we want to set what is called the 'focalLength'.
QUOTE
focalLength = 500;
What the focal length does, is it helps to define how much z will offset the object. The higher the number, the smaller the offset. We dont want it to be too big, but not too small so we will set it at 500 for the time being.
Every figure whether it be 2 or 3 demensions has to have some coordinates, so lets go ahead and define the coordinates for our box shall we:
QUOTE
boxpoints = [
Make3D(50, -50, -50),
Make3D(150, -50, -50),
Make3D(150, -50, 50),
Make3D(50, -50, 50),
Make3D(50, 50, -50),
Make3D(150, 50, -50),
Make3D(150, 50, 50),
Make3D(50, 50, 50)
];
Make3D(50, -50, -50),
Make3D(150, -50, -50),
Make3D(150, -50, 50),
Make3D(50, -50, 50),
Make3D(50, 50, -50),
Make3D(150, 50, -50),
Make3D(150, 50, 50),
Make3D(50, 50, 50)
];
Make3D is a function that we will define a little later on, but what matters now is what is inside it. inside each function call there are 3 numbers, representing x, y, and z. these coordinates will make a box 100 x 100 x 100 at a little less than center. Now i know your eager to make the function Make3d so lets get to it:
QUOTE
function Make3D(x,y,z) {
var point = new Object();
point.x = x;
point.y = y;
point.z = z;
return point;
}
I know its not quite what you expected BUT this is a very important part. What it does is takes those 3 coordinates and assigns them to an object. Just like with the origin this object has X and Y values, but this one also has a Z value. Then it returns the point object back to the array.
and then we should make an empty movie clip to store it in:
QUOTE
this.createEmptyMovieClip("box",1);
Ok were done with the easy part. Now comes the a lil harder but still pretty easy. I say this because it would be EXTREMELY difficult to draw a 3d object without it. In computer land, they dont have a Z plane like we do in the real world. All they have is X and Y. So what we have to do is make a function that will convert 2d coordinates to 3d by offsetting the X and Y values. I know its sounds like gibberish but bear with me... lets take a look at the function:
QUOTE
function convert3d(pointIn3D) {
var pointIn2D = new Object();
var scaleRatio = focalLength/(focalLength + pointIn3D.z);
pointIn2D.x = pointIn3D.x * scaleRatio;
pointIn2D.y = pointIn3D.y * scaleRatio;
return pointIn2D;
}
var pointIn2D = new Object();
var scaleRatio = focalLength/(focalLength + pointIn3D.z);
pointIn2D.x = pointIn3D.x * scaleRatio;
pointIn2D.y = pointIn3D.y * scaleRatio;
return pointIn2D;
}
see that wasnt that hard, what this does is when passed in a 3d point (which was originally made by Make3d()
now we have to draw it. But these coordinates are just the virtual coordinates because we still have to make each point relative to the origin... Lets look at the code:
QUOTE
function draw3d(item) {
//Make Real Points ^.^
var realpoints = new Array();
for (i=0; i < boxpoints.length; i++) {
var itemPoint = boxpoints[i];
realpoints[i] = convert3d(itemPoint);
realpoints[i].x += origin.x;
realpoints[i].y += origin.y;
}
// draw
item.clear();
item.lineStyle(2,0x000fff,25);
//bottom
item.moveTo(realpoints[0].x, realpoints[0].y);
item.lineTo(realpoints[1].x, realpoints[1].y);
item.lineTo(realpoints[2].x, realpoints[2].y);
item.lineTo(realpoints[3].x, realpoints[3].y);
item.lineTo(realpoints[0].x, realpoints[0].y);
//top
item.moveTo(realpoints[4].x, realpoints[4].y);
item.lineTo(realpoints[5].x, realpoints[5].y);
item.lineTo(realpoints[6].x, realpoints[6].y);
item.lineTo(realpoints[7].x, realpoints[7].y);
item.lineTo(realpoints[4].x, realpoints[4].y);
// connecting bottom and top
item.moveTo(realpoints[0].x, realpoints[0].y);
item.lineTo(realpoints[4].x, realpoints[4].y);
item.moveTo(realpoints[1].x, realpoints[1].y);
item.lineTo(realpoints[5].x, realpoints[5].y);
item.moveTo(realpoints[2].x, realpoints[2].y);
item.lineTo(realpoints[6].x, realpoints[6].y);
item.moveTo(realpoints[3].x, realpoints[3].y);
item.lineTo(realpoints[7].x, realpoints[7].y);
}
//Make Real Points ^.^
var realpoints = new Array();
for (i=0; i < boxpoints.length; i++) {
var itemPoint = boxpoints[i];
realpoints[i] = convert3d(itemPoint);
realpoints[i].x += origin.x;
realpoints[i].y += origin.y;
}
// draw
item.clear();
item.lineStyle(2,0x000fff,25);
//bottom
item.moveTo(realpoints[0].x, realpoints[0].y);
item.lineTo(realpoints[1].x, realpoints[1].y);
item.lineTo(realpoints[2].x, realpoints[2].y);
item.lineTo(realpoints[3].x, realpoints[3].y);
item.lineTo(realpoints[0].x, realpoints[0].y);
//top
item.moveTo(realpoints[4].x, realpoints[4].y);
item.lineTo(realpoints[5].x, realpoints[5].y);
item.lineTo(realpoints[6].x, realpoints[6].y);
item.lineTo(realpoints[7].x, realpoints[7].y);
item.lineTo(realpoints[4].x, realpoints[4].y);
// connecting bottom and top
item.moveTo(realpoints[0].x, realpoints[0].y);
item.lineTo(realpoints[4].x, realpoints[4].y);
item.moveTo(realpoints[1].x, realpoints[1].y);
item.lineTo(realpoints[5].x, realpoints[5].y);
item.moveTo(realpoints[2].x, realpoints[2].y);
item.lineTo(realpoints[6].x, realpoints[6].y);
item.moveTo(realpoints[3].x, realpoints[3].y);
item.lineTo(realpoints[7].x, realpoints[7].y);
}
all we do to convert it to real coordinates is add the origin x and y to the point. Then we draw it by first clearing everything inside the movie clip, setting the line style then drawing the points. I wont get into the details about the moveTo and lineTo functions but what happens next is it draws the bottom. Then it draws the top.
now we need to make sure it does this on every frame, so in the onenterframe command you would put:
QUOTE
draw3d(box);
And your done! feel free to edit the coordinates and such to make new figures once you get the hang of it ^.^


