Actionscript:
-
stage.frameRate = 30;
-
var centerX:Number = 200, centerY:Number = 200, zpos:Number, xpos:Number, ypos:Number, depth:Number;
-
var rotX:Number = 0, rotY:Number = 0, px:Number, py:Number, pz:Number;
-
var cosx:Number, cosy:Number, sinx:Number, siny:Number;
-
-
var canvas:BitmapData = new BitmapData(400,400,true,0xFF000000);
-
addChild(new Bitmap(canvas));
-
-
addEventListener(Event.ENTER_FRAME, onLoop);
-
-
function onLoop(evt:Event):void {
-
canvas.fillRect(canvas.rect, 0xFF000000);
-
rotX += (mouseX / 50 - rotX)/12;
-
rotY += (mouseY / 50 - rotY)/12;
-
-
cosx = Math.cos(rotX);
-
cosy = Math.cos(rotY);
-
sinx = Math.sin(rotX);
-
siny = Math.sin(rotY);
-
for (var a:Number =0; a <6.28; a+=.1){
-
for (var b:Number =0; b <6.28; b+=.05){
-
px = 100 * Math.cos(a) * Math.cos(b) * Math.cos(b);
-
py = 100 * Math.sin(a) * Math.cos(b)
-
pz = 100 * Math.sin(b);
-
zpos= pz*cosx - px*sinx ;
-
xpos= pz*sinx +px*cosx ;
-
ypos= py*cosy - zpos*siny ;
-
zpos= py*siny+ zpos*cosy ;
-
depth = 1/((zpos/340)+1);
-
canvas.setPixel((xpos * depth) + centerX, (ypos * depth) + centerY, 0xFFFFFF);
-
}
-
}
-
}
Renders a rotating 3D shape.
4 Comments
this is pretty neat looking. when you’re writing code like this, how do you know all these random numbers like 6.28? is it through experimentation or are you finding formulas somewhere and converting them to AS?
also, i’ve been meaning to ask you, you talk alot in your posts about your students, where do you teach? when i was in college we were doing director even then, though, with the little flash we did, we didn’t really do much AS, mostly timeline and simple AS stuff.
The 6.28 is a lazy way of writing (2 * Math.PI)… that’s 360 degrees in radians. For the above code I just used the equation for a sphere:
x = r *cos(a)*cos(b)
y = r * sin(a)*cos(b)
z = r * sin(b)
and then tweaked the equation a bit to change the shape.
I teach at the School of Visual Arts in NYC.
why do you need calculate zpos again ?
zpos= py*siny+ zpos*cosy ;
It’s pretty complex, but there is a good explanation here …
One Trackback
[...] A 3D shape created with setPixel(). [...]