By Zevan | December 28, 2008
Actionscript:
-
// isometric conversion
-
var centerX:Number=stage.stageWidth/2;
-
var centerY:Number=stage.stageHeight/2;
-
var theta:Number=Math.PI/4;// 45 degrees;
-
var cosX:Number=Math.cos(theta);
-
var sinX:Number=Math.sin(theta);
-
var pnt:Point = new Point();
-
function iso3D(x:Number, y:Number, z:Number):Point {
-
pnt.x = centerX + (x-z) * cosX
-
pnt.y = centerY - (x+z) * 0.5 * sinX - y;
-
return pnt;
-
}
-
// example:
-
var canvas:BitmapData=new BitmapData(stage.stageWidth,stage.stageHeight,true,0xFF000000);
-
addChild(new Bitmap(canvas,"auto",true));
-
var size:int=100;
-
var hs:int=size / 2;
-
var pen:Point = new Point();
-
var vect:Vector3D = new Vector3D();
-
// draw a few shapes with offset:
-
for (var dist:int = 10; dist <= 80; dist *= 2) {
-
// voxel space:
-
for (var i:int = 0; i<size; i++) {
-
for (var j:int = 0; j<size; j++) {
-
for (var k:int = 0; k<size; k++) {
-
vect.x=j-hs;
-
vect.y=i-hs;
-
vect.z=k-hs;
-
pen = iso3D(vect.x,vect.y,vect.z);
-
if (Math.sqrt((vect.x * vect.x) + (vect.y * vect.y) + (vect.z * vect.z)) <dist) {
-
// using Vector3D.distance() is very slow compared to above
-
// a few types of coloring:
-
var xp:Number = pen.x + (dist <<2) - 200;
-
canvas.setPixel(xp, pen.y-100, (i <<16 | j <<8 | k) <<1);
-
canvas.setPixel(xp, pen.y+100, (k <<16 | k <<8 | k+j) );
-
}
-
}
-
}
-
}
-
}
The above will draw this:

You can read more about voxels here.
This isn't the speediest way to do voxels (especially if you want to animate). This was just the first thing that came to mind.
By Zevan | December 27, 2008
Actionscript:
-
var canvas:BitmapData = new BitmapData(500, 500, true, 0xFFFFFFFF);
-
var color:BitmapData= new BitmapData(500, 500, true, 0x81666666);
-
var over:BitmapData = new BitmapData(500, 500, true, 0xFF000000);
-
addChild(new Bitmap(canvas, "auto", true));
-
var circle:Shape = new Shape();
-
circle.graphics.beginFill(0xFFFFFF,1);
-
circle.graphics.drawCircle(0,0,50);
-
var m:Matrix = new Matrix();
-
m.tx = 0;
-
m.ty = 1;
-
addEventListener(Event.ENTER_FRAME, onLoop);
-
function onLoop(evt:Event):void {
-
circle.x = mouseX;
-
circle.y = mouseY;
-
canvas.draw(circle, circle.transform.matrix);
-
canvas.copyPixels(color, color.rect, new Point(0,0), null, null, true);
-
over.copyPixels(canvas, canvas.rect, new Point(0,0), null, null, true);
-
canvas.draw(over, m, null, BlendMode.SCREEN);
-
over.applyFilter(over, over.rect, new Point(-2,-2), new BlurFilter(10,10,1));
-
canvas.draw(over, m, null, BlendMode.SUBTRACT);
-
}
Grayscale emboss technique. More info here.
Posted in Uncategorized | Also tagged flash |
By Zevan | December 26, 2008
Actionscript:
-
var vars:Dictionary = new Dictionary();
-
-
var sp:Sprite = new Sprite();
-
-
// associate variables with a sprite (or any non-dynamic class)
-
vars[sp] = {posX:100, posY:100, velX:1, velY:1};
-
-
// read
-
trace(vars[sp].posX);
I've heard people mention that they wish the sprite class were dynamic... meaning they wish they could add methods and properties to a Sprite instance at runtime. There's no way I know of to do this, however... the dictionary class can be used to associate variables with any non-dynamic class instance... as it does in this the above example.
The dictionary class is similar to an associative array except that instead of using strings for keys, dictionaries use object instances.
Posted in Dictionary, arrays, dynamic | Also tagged flash |
By Zevan | December 24, 2008
Actionscript:
-
var canvas:BitmapData=new BitmapData(400,400,true,0xFFCCCCCC);
-
addChild(new Bitmap(canvas));
-
-
var redOval:Shape = new Shape();
-
with(redOval.graphics) lineStyle(0,0), beginFill(0xFF0000), drawEllipse(0,0,10,50);
-
-
addEventListener(Event.ENTER_FRAME, onLoop);
-
function onLoop(evt:Event):void {
-
-
redOval.x = mouseX;
-
redOval.y = mouseY;
-
redOval.rotation += 10
-
// transform.matrix contains all transformation information for
-
// the redOval including scaleX, scaleY, x, y, rotation etc...
-
canvas.draw(redOval, redOval.transform.matrix);
-
}
A very easy way to create a BitmapData brush.
Posted in BitmapData | Also tagged flash |