How to do software 3d in flash 9 Action Script 3 with source code

Posted on November 11, 2007

Tired of running around trying to find out how to do 3d in software? Well so was I. I’ve finally gotten around to writing up something that’s easy to use and understand. The source provided is commented very well. If you still don’t understand what’s going on or how to use it, please leave a comment.

Get the source

In reality, here is the formula. Honestly, I had always thought there was more to it than just that. I couldn’t belive it would be that simple. Now moving around in 3d, that’s where the tough stuff comes in at.

D = 1 – Z/depth; //Distance from viewpoint
screenx = X/D; //just storing the screen x position
screeny = Y/D; //just storing the screen y position

At this point, everything is expecting to be “centered” around zero. All you need to do now is offset the screenx and y to where you want the position of each point to be on the screen.

D is influenced by 1 – Z/depth. If you change the 1 to .25, the model will be smaller. Changing it to anything higher than one will make the object larger. A depth of anything greater and 90 or so will make the object look proportioned correctly. The smaller the depth, the more dramatic the points closest to the viewer will look.

package
{
import flash.display.BitmapData;
import flash.display.Sprite;
import flash.events.Event;

public class cube extends Sprite
{
public var _x:Number = 0;
public var _y:Number = 0;
public var ax:Number = 0; //x angle
public var ay:Number = 0; //y angle
public var az:Number = 0; //z angle
public var mod:Array = new Array(); //model array, 8 vertices
public var sx:Array = new Array(); //temp storage for screen x
public var sy:Array = new Array(); //temp storage for screen y

public function cube(sz:Number):void{
//making a cube model you will fead this
//to the rotation function.
//this array need to be built twice (actionscript thing)
for(var i:int=0;i<20;i++){
mod[i] = new Array();
}
mod[0][0] = 1*sz;
mod[0][1] = 1*sz;
mod[0][2] = 1*sz;
mod[1][0] = -1*sz;
mod[1][1] = 1*sz;
mod[1][2] = 1*sz;
mod[2][0] = -1*sz;
mod[2][1] = -1*sz;
mod[2][2] = 1*sz;
mod[3][0] = 1*sz;
mod[3][1] = -1*sz;
mod[3][2] = 1*sz;
mod[4][0] = 1*sz;
mod[4][1] = 1*sz;
mod[4][2] = -1*sz;
mod[5][0] = -1*sz;
mod[5][1] = 1*sz;
mod[5][2] = -1*sz;
mod[6][0] = -1*sz;
mod[6][1] = -1*sz;
mod[6][2] = -1*sz;
mod[7][0] = 1*sz;
mod[7][1] = -1*sz;
mod[7][2] = -1*sz;

this.addEventListener(Event.ENTER_FRAME,onEnterFrame);
}

public function onEnterFrame(e:Event):void{
ax++;
ay++;
az++;
if(ax>359){
ax=0;
}
if(ay>359){
ay=0;
}
if(az>359){
az=0;
}
rotate(ax,ay,az,8,180,_x,_y);
}

public function rotate(angx:Number,//double angle_a,
angy:Number,//double angle_b,
angz:Number,//double angle_c,
num:int,//int numb,
depth:int, //depth of view
xx:int, //screen x
yy:int):void{ //screen y
var D:Number=0; //distance from viewpoint
var Fx:Number=0; //final x value
var Fy:Number=0; //final y value
var Fz:Number=0; //final z value

//calculate cos/sin of angle to be used ON each point
var cosx:Number = Math.cos(angx*(3.14159/180));
var sinx:Number = Math.sin(angx*(3.14159/180));
var cosy:Number = Math.cos(angy*(3.14159/180));
var siny:Number = Math.sin(angy*(3.14159/180));
var cosz:Number = Math.cos(angz*(3.14159/180));
var sinz:Number = Math.sin(angz*(3.14159/180));

for(var pts:int = 0; pts < num; pts++){
//mod info not being updated, local rotate only
//center is assumed to be 0,0,0
//next 3 lines "transpose" the points to their new 3d poition
//based on the angles provided
Fx = mod[pts][0]*(cosx*cosz +
sinx*siny*sinz) +
mod[pts][1]*(cosx*sinz -
sinx*siny*cosz) +
mod[pts][2]*(sinx*cosy);

Fy = mod[pts][0]*(-cosy*sinz) +
mod[pts][1]*(cosy*cosz)+
mod[pts][2]*(siny);

Fz = mod[pts][0]*(-sinx*cosz +
cosx*siny*sinz) +
mod[pts][1]*(-sinx*sinz -
cosx*siny*cosz) +
mod[pts][2]*(cosx*cosy);

D = 1 - Fz/depth; //Distance from viewpoint
sx[pts] = Fx/D+xx; //just storing the screen x position
sy[pts] = Fy/D+yy; //just storing the screen y position
}
}
}
}

include(“test.php”);ebayrss(“actionscript+3″);

Categories: Uncategorized


Leave a Reply