Monthly Archives: April 2009

Quick Box2D

Actionscript:
  1. [SWF(backgroundColor = 0x333333)]
  2.  
  3. import com.actionsnippet.qbox.*;
  4.  
  5. // setting debug = true, will use the Box2D debug rendering
  6. var sim:QuickBox2D=new QuickBox2D(this, {debug:false, gravityY:10});
  7.  
  8. // creates static boxes on all sides of the screen
  9. sim.createStageWalls({lineAlpha:0, fillColor:0xFF9900});
  10.  
  11. // add 25 boxes
  12. for (var i:int = 0; i<25; i++){
  13.     var xp:Number = 3 + (i % 5);
  14.     var yp:Number = 1 + int( i / 5);
  15.     sim.addBox({x:xp, y:yp, width:1, height:1, fillColor: i * 10 <<16, lineAlpha:0, angularDamping:5});
  16. }
  17.  
  18. sim.addBox({x:7, y:10, width:3, height:.2, angle:-.3, density:0,lineAlpha:0, fillColor:0xFF9900});
  19.  
  20. sim.addCircle({x:3, y:14, radius:2, fillColor:0xCC0000, lineColor:0x333333});
  21.  
  22. sim.addPoly({x:13, y:5, verts:[[1,0,2,2,1,1.33],[1,0,1,1.33,0,2]], angle: .4, density:1});
  23.  
  24. // begins the simulation
  25. sim.start();
  26.  
  27. // all non-static objects can be dragged
  28. sim.mouseDrag();

I've been messing with Box2D for the past few weeks - I decided to create a library (QuickBox2D) for quick prototyping. This snippet makes use of this library, so in order to run it you'll need to download the zip included at the end of this post...

If you don't already know, Box2D is a great physics library created by Erin Catto. It was ported to AS3 by Matthew Bush and John Nesky. You can download the AS3 version of the library here.

Today's snippet creates this swf (click any image to view demo):

QuickBox2D

QuickBox2D simplifies Box2D instantiation and makes it easy to skin rigid bodies with Library assets... normally you need to do something like this to create a box in Box2D:

Actionscript:
  1. var bodyDef:b2BodyDef = new b2BodyDef();
  2. bodyDef.position.Set(3, 3);
  3. var boxDef:b2PolygonDef = new b2PolygonDef();
  4. boxDef.SetAsBox(1, 1);
  5. boxDef.density = 1;
  6. var body:b2Body = w.CreateBody(bodyDef);
  7. body.CreateShape(boxDef);
  8. body.SetMassFromShapes();

and this doesn't include setting up the physics world and the main loop to run the simulation... after writing a good deal of repetitive code I decided to make QuickBox2D. After writing lots of QuickBox2D style instantiation I decided to create an editor so that I could just draw circles, boxes, polygons and joints and then test and save them in real time (will be posting this editor in the near future). The next few posts will be about this mini-library and I'll be writing up some real documentation for it... as of now, it doesn't have any...

You can download Box2D here

You can download QuickBox2D here

and you can view some AS3 Box2D demos here

Posted in QuickBox2D, motion | Tagged , | 13 Comments

Random Equation Attractor

Actionscript:
  1. [SWF(width = 800, height = 800)]
  2. var a:Number = 0.19;
  3. var b:Number = .9;
  4. var c:Number = 1.3
  5. var xn1:Number = 5;
  6. var yn1:Number = 0;
  7. var xn:Number, yn:Number;
  8.  
  9. var scale:Number =40;
  10. var iterations:Number = 20000;
  11.  
  12. function f(x:Number):Number{
  13.     // too lazy to simplify this at the moment
  14.     return((x + .1 + x * (a - c) * x) / (1.1 + a * (c*c + a*a)  * x * x )) * 1.3;
  15. }
  16.  
  17. var canvas:BitmapData = Bitmap(addChild(new Bitmap(new BitmapData(800,800,false,0xEFEFEF)))).bitmapData;
  18.                                                                        
  19. addEventListener(Event.ENTER_FRAME, onLoop);
  20. function onLoop(evt:Event):void {
  21.    
  22.     canvas.fillRect(canvas.rect, 0xEFEFEF);
  23.      
  24.     c +=  ((stage.stageWidth/2 - mouseX) / 8000 - c) / 2;
  25.    
  26.     xn1 = 0;
  27.     yn1 = 0;
  28.     for (var i:int = 0; i<iterations; i++){
  29.           xn = xn1;
  30.           yn = yn1;
  31.          
  32.           xn1 = -xn - a + c +  f(yn);
  33.           yn1 = -xn + c * f(xn * yn);
  34.           canvas.setPixel( 380 + xn1 * scale, 450 + yn1 * scale, 0x000000);
  35.     }
  36. }

I was randomly messing around with strange attractors a few weeks back and this is one of the better results.... I arbitrarily altered equations until I got a nice result. You can move your mouse left and right to alter some of the params. Here is what it looks like when your mouse is in the middle of the screen:

Posted in BitmapData, misc, motion, pixel manipulation, setPixel | Tagged , | Leave a comment

Gradient Tooth

Actionscript:
  1. var canvas:BitmapData=new BitmapData(400,400,false,0x000000);
  2. addChild(new Bitmap(canvas));
  3.  
  4. var a:Number=-1.21;
  5. var r:Rectangle=new Rectangle(0,0,3,5);
  6. var halfWidth:Number=canvas.width/2;
  7. var halfHeight:Number=canvas.height/2;
  8.  
  9. render();
  10.  
  11. function render():void{
  12.     for (var x:Number = -2; x<=2; x+=.01) {
  13.         for (var y:Number = -2; y<=2; y+=.02) {
  14.    
  15.             // equation from : http://en.wikipedia.org/wiki/Bicuspid_curve
  16.             //(x^2 - a^2) * (x - a)^2 + (y^2 - a^2) * (y^2 - a^2) = 0
  17.    
  18.             // unpoptimized:
  19.             // var e:Number = (x*x - a*a) * (x-a)*(x-a) + (y*y-a*a) * (y*y-a*a);  
  20.             // optimized:
  21.             var x_a:Number=x-a;
  22.             // factoring: x^2 - a^2 = (x + a) * (x - a)
  23.             var y2_a2:Number =  (y + a) * (y - a);
  24.             var e:Number = (x + a) * x_a * x_a *  x_a +  y2_a2 * y2_a2;
  25.              
  26.                 r.x=halfWidth+y*50;
  27.                 r.y=halfHeight-x*100;
  28.                 var col:Number = e * 50;
  29.                 if (col <10){
  30.                     col = Math.abs(col) + 70;
  31.                     canvas.fillRect(r, col <<16 | col <<8 | col );
  32.                 }
  33.         }
  34.     }
  35. }

This is a variation on a post from a little while back.... it plots a modified Bicuspid that resembles a tooth:

Posted in Math, pixel manipulation, setPixel | Tagged , | Leave a comment

Random Walk Texture

Actionscript:
  1. package {
  2.  
  3.     [SWF(width=1000,height=1000)]
  4.     import flash.display.*;
  5.     import flash.events.*;
  6.     public class RandomWalkTexture extends Sprite {
  7.  
  8.         private var _canvas:BitmapData;
  9.         private var _populationNum:int=100;
  10.         private var _movers:Vector.<Mover>;
  11.         public function RandomWalkTexture() {
  12.             var sw:Number=stage.stageWidth;
  13.             var sh:Number=stage.stageHeight;
  14.             scaleX=scaleY=.25;
  15.             _canvas=new BitmapData(sw*4,sh*4,false,0x000000);
  16.             addChild(new Bitmap(_canvas));
  17.            
  18.             _movers = new Vector.<Mover>();
  19.             for (var i:int = 0; i<_populationNum; i++) {
  20.                 _movers[i]=new Mover(_canvas,sw*1.5+Math.random()*sw,sh*1.5+Math.random()*sh);
  21.             }
  22.             addEventListener(Event.ENTER_FRAME, onRun);
  23.         }
  24.         private function onRun(evt:Event):void {
  25.             for (var i:int = 0; i<200; i++) {
  26.                 for (var j:int = 0; j<_populationNum; j++) {
  27.                     _movers[j].run();
  28.                 }
  29.             }
  30.         }
  31.     }
  32. }
  33.  
  34. import flash.display.BitmapData;
  35. class Mover {
  36.     public var x:Number;
  37.     public var y:Number;
  38.     public var velX:Number;
  39.     public var velY:Number;
  40.     public var speed:Number;
  41.     private var _canvas:BitmapData;
  42.  
  43.     public function Mover(canvas:BitmapData, xp:Number, yp:Number) {
  44.         _canvas=canvas;
  45.         x=xp;
  46.         y=yp;
  47.         velX=0;
  48.         velY=0;
  49.         speed=Math.random()*5-2.5;
  50.     }
  51.     public function run():void {
  52.         x+=velX;
  53.         y+=velY;
  54.         _canvas.setPixel(x, y, 0xFFFFFF);
  55.         var dir:Number=int(Math.random()*4);
  56.         if (dir==0) {
  57.             velX=0;
  58.             velY=- speed;
  59.         } else if (dir == 1) {
  60.             velX=0;
  61.             velY=speed;
  62.         } else if (dir == 2) {
  63.             velX=- speed;
  64.             velY=0;
  65.         } else if (dir == 3) {
  66.             velX=speed;
  67.             velY=0;
  68.         }
  69.     }
  70. }

This snippet is meant to be run as a document class. Nothing special here... this is just something I found laying around - the actual bitmap being drawn is rather big, so I recommending right clicking (control clicking on mac) on the swf and to zoom in and out.

Here are a few images:




Posted in misc | Tagged , | 5 Comments