Monthly Archives: January 2009

x2

Actionscript:
  1. var c:Number = 1;
  2. addEventListener(Event.ENTER_FRAME, function(){c *= 2,  trace(c)});

and....
2
4
8
16
32
64
128
256
512
1024
2048
4096
8192
16384
32768
65536
131072
262144
524288
1048576
2097152
4194304
8388608
16777216
33554432
67108864
134217728
268435456
536870912
1073741824
2147483648
4294967296
8589934592
17179869184
34359738368
68719476736
137438953472
274877906944
549755813888
1099511627776
2199023255552
4398046511104
8796093022208
17592186044416
35184372088832
70368744177664
140737488355328
281474976710656
562949953421312
1125899906842624
2251799813685248
4503599627370496
9007199254740992
18014398509481984
36028797018963970
72057594037927940
144115188075855870
288230376151711740
576460752303423500
1152921504606847000
2305843009213694000
4611686018427388000
9223372036854776000
18446744073709552000
36893488147419103000
73786976294838210000
147573952589676410000
295147905179352830000
590295810358705700000

etc...

Posted in misc | Tagged , | Leave a comment

Infinity, PI, phi and i

Actionscript:
  1. trace(1 / 0);
  2.  
  3. trace( 22 / 7 );
  4.  
  5. trace((1 + Math.sqrt(5)) / 2);
  6.  
  7. trace(Math.sqrt(-1));

Posted in misc | Tagged , , , , | 2 Comments

Isometric to Cartesian

Actionscript:
  1. var centerX:Number=stage.stageWidth/2;
  2. var centerY:Number=stage.stageHeight/2;
  3. // 1 to 2 ratio
  4. // try others 1 / 1.5 etc...
  5. var theta:Number = Math.atan(1 / 2);
  6.  
  7. var cosX:Number=Math.cos(theta);
  8. var sinX:Number=Math.sin(theta);
  9. var pnt:Point = new Point();
  10.  
  11. function iso3D(x:Number, y:Number, z:Number):Point {
  12.     pnt.x = centerX + (x-z) *  cosX;
  13.     pnt.y = centerY -  (x+z) * sinX - y;
  14.     return pnt;
  15. }
  16.  
  17. var p:Point = iso3D(0,0,0);
  18.  
  19. graphics.beginFill(0x000000);
  20. graphics.drawCircle(p.x, p.y, 2);
  21.  
  22. // x axis positive
  23. trace("x = red");
  24. for (var i:int = 1; i<10; i++){
  25.     graphics.beginFill(0xFF0000);
  26.     p = iso3D(i*10, 0, 0);
  27.     graphics.drawCircle(p.x, p.y, 2);
  28. }
  29.  
  30. // y axis positive
  31. trace("y = green");
  32. for (i= 1; i<10; i++){
  33.     graphics.beginFill(0x00FF00);
  34.     p = iso3D(0, i * 10, 0);
  35.     graphics.drawCircle(p.x, p.y, 2);
  36. }
  37.  
  38. // z axis positive
  39. trace("z = blue");
  40. for (i= 1; i<10; i++){
  41.     graphics.beginFill(0x0000FF);
  42.     p = iso3D(0, 0, i * 10);
  43.     graphics.drawCircle(p.x, p.y, 2);
  44. }

The above code demos a conversion from isometric coordinates to cartesian coordinates. It draws out part of the x, y and z axis using the iso3D() function.

I've always faked isometric conversion by just tweaking numbers. A few years ago I created some strange isometric engines (here's another example). The other day when I wrote the isometric voxels snippet I just created the iso3D() function with a little trial and error. As you'll see it's not exactly the same as the one here. This new conversion is the result of some googling. The updated conversion is pretty close to the one that I wrote for the voxel post, but has one less multiplication... and a clearer place to control the scale ratio ...

Posted in 3D, misc | Tagged , , | Leave a comment

Matrix.transformPoint()

Actionscript:
  1. var circle:Shape = Shape(addChild(new Shape()));
  2. with(circle.graphics) beginFill(0x00000), drawCircle(0,0,10);
  3.  
  4. var point:Point = new Point();
  5. var trans:Matrix = new Matrix();
  6. trans.rotate(Math.PI);
  7. trans.scale(.5, .5);
  8. trans.tx = stage.stageWidth / 2;
  9. trans.ty = stage.stageHeight / 2;
  10.  
  11. addEventListener(Event.ENTER_FRAME, onLoop);
  12.  
  13. function onLoop(evt:Event):void {
  14.     point.x = mouseX - trans.tx;
  15.     point.y = mouseY - trans.ty;
  16.    
  17.     point = trans.transformPoint(point);
  18.    
  19.     circle.x = point.x;
  20.     circle.y = point.y;
  21. }

If you don't feel like rolling your own transformations Matrix.transformPoint() is a very powerful method. It simply applies the tranformations of a given matrix to a Point. The above example scales rotates and translates a point which is then used to position a circle Shape.

Matrix.transformPoint() is well suited for creating orbiting behavior:

Actionscript:
  1. var circle:Shape = Shape(addChild(new Shape()));
  2. with(circle.graphics) beginFill(0x00000), drawCircle(0,0,10);
  3.  
  4. var point:Point = new Point(100,0);
  5. var trans:Matrix = new Matrix();
  6.  trans.rotate(.1);
  7.  trans.scale(.99,.99);
  8.  
  9. addEventListener(Event.ENTER_FRAME, onLoop);
  10.  
  11. function onLoop(evt:Event):void {
  12.      
  13.       point = trans.transformPoint(point);
  14.      
  15.       circle.x = point.x + stage.stageWidth / 2;
  16.       circle.y = point.y + stage.stageHeight / 2;
  17. }

This code will cause the circle Shape to move in a spiral formation.

Posted in misc, motion | Tagged , , | Leave a comment