Category Archives: Vector

3D Key Controls

Actionscript:
  1. [SWF(backgroundColor=0x000000, width=500, height=500)]
  2. var hsw:Number = stage.stageWidth / 2;
  3. var hsh:Number = stage.stageHeight / 2;
  4. var pointNum:int = 300;
  5. var points3D:Vector.<Number> = new Vector.<Number>();
  6. var points2D:Vector.<Number> = new Vector.<Number>();
  7. var uvts:Vector.<Number> = new Vector.<Number>();
  8. var sorted:Array = [];
  9. var pnt:Point = new Point();
  10. var m:Matrix3D = new Matrix3D();
  11. var v:Vector3D = new Vector3D();
  12. for (var i:int = 0; i<pointNum; i++){
  13.     v.x = 300;
  14.     v.y = v.z = 0;
  15.     m.identity();
  16.     m.appendRotation(Math.random()*360, Vector3D.X_AXIS);
  17.     m.appendRotation(Math.random()*360, Vector3D.Y_AXIS);
  18.     m.appendRotation(Math.random()*360, Vector3D.Z_AXIS);
  19.     v = m.transformVector(v);
  20.     points3D.push(v.x, v.y, v.z);
  21.     points2D.push(0,0);
  22.     uvts.push(0,0,0);
  23.     sorted.push(new Vector3D());
  24. }
  25. points3D.fixed = true;
  26. points2D.fixed = true;
  27. uvts.fixed = true;
  28. var p:PerspectiveProjection = new PerspectiveProjection();
  29. var proj:Matrix3D = p.toMatrix3D();
  30. var rx:Number = 0, ry:Number = 0;
  31. addEventListener(Event.ENTER_FRAME, onLoop);
  32. function onLoop(evt:Event):void {
  33.     var i:int, j:int;
  34.     m.identity();
  35.     if (key[Keyboard.RIGHT]){
  36.         rx+=3
  37.     }else
  38.     if (key[Keyboard.LEFT]){
  39.         rx-=3
  40.     }else
  41.     if (key[Keyboard.UP]){
  42.         ry-=3
  43.     }else
  44.     if (key[Keyboard.DOWN]){
  45.         ry+=3
  46.     }
  47.     m.appendRotation(rx, Vector3D.Y_AXIS);
  48.     m.appendRotation(ry, Vector3D.X_AXIS);
  49.     m.appendTranslation(0, 0, 1000);
  50.     m.append(proj);
  51.     Utils3D.projectVectors(m, points3D, points2D, uvts);
  52.     for (i = 0, j = 0; i<points2D.length; i+=2, j++){
  53.         sorted[j].x = points2D[i] + hsw;
  54.         sorted[j].y = points2D[i + 1] + hsh;
  55.         sorted[j].z = uvts[j * 3 + 2];
  56.     }
  57.     sorted.sortOn("z", Array.NUMERIC);
  58.     graphics.clear();
  59.     graphics.lineStyle(2, 0x000000, 0.1);
  60.     for(i = 0; i<sorted.length; i++){
  61.         var zpos:Number = sorted[i].z * 12000;
  62.         var c:int = zpos * 14;
  63.         c = (c> 255) ? 255 : c;
  64.         graphics.beginFill(100<<16 | 100 <<8 |c);
  65.         graphics.drawCircle(sorted[i].x, sorted[i].y,zpos);
  66.         graphics.endFill();
  67.     }
  68. }
  69.  
  70. // permanently applies the matrix to the points3D vector
  71. function applyTransform():void{
  72.     m.identity();
  73.     m.appendRotation(rx, Vector3D.Y_AXIS);
  74.     m.appendRotation(ry, Vector3D.X_AXIS);
  75.     var temp:Vector.<Number> = new Vector.<Number>();
  76.     m.transformVectors(points3D, temp);
  77.     points3D = temp;
  78.     points3D.fixed = true;
  79.     rx = 0, ry = 0;
  80. }
  81.  
  82. // basic key setup
  83. var key:Object = new Object();
  84. stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyPressed);
  85. stage.addEventListener(KeyboardEvent.KEY_UP, onKeyReleased);
  86. function onKeyPressed(evt:KeyboardEvent):void {
  87.     key[evt.keyCode] = true;
  88.     key.keyCode = evt.keyCode;
  89. }
  90. function onKeyReleased(evt:KeyboardEvent):void {
  91.   applyTransform();
  92.   key[evt.keyCode] = false;
  93. }

In response to Thomas Francis's question, this snippet takes the Simple z-sorting snippet and shows how one might go about adding some basic key controls to it. It works by permanently applying x and y rotation transformations to the set of 3D points every time a key is released.

Check out the swf...

If you need to be able to rotate on both the x and y axis at the same time - and just need more flexibility... one way to do it would be to use quaternions - which may be tricky - but there are plenty of examples out there in java, processing and C/C++ just waiting to be ported (been on the todo list for some time actually).

Haven't been posting every day because I've been out of the country and away from my computer... have a backlog of snippets that need to be cleaned up and put in the pipeline...

Also posted in 3D, Graphics, keys | Tagged , , | 3 Comments

Simple z-sorting

Actionscript:
  1. [SWF(backgroundColor=0x444444, width=500, height=500)]
  2. var hsw:Number = stage.stageWidth / 2;
  3. var hsh:Number = stage.stageHeight / 2;
  4.  
  5. var pointNum:int = 200;
  6. var points3D:Vector.<Number> = new Vector.<Number>();
  7. var points2D:Vector.<Number> = new Vector.<Number>();
  8. var uvts:Vector.<Number> = new Vector.<Number>();
  9. var sorted:Array = [];
  10.  
  11. var pnt:Point = new Point();
  12. var m:Matrix3D = new Matrix3D();
  13. var v:Vector3D = new Vector3D();
  14. for (var i:int = 0; i<pointNum; i++){
  15.     v.x = Math.random()*400-200;
  16.     m.identity();
  17.     m.appendRotation(Math.random()*360, Vector3D.X_AXIS);
  18.     m.appendRotation(Math.random()*360, Vector3D.Y_AXIS);
  19.     m.appendRotation(Math.random()*360, Vector3D.Z_AXIS);
  20.     v = m.transformVector(v);
  21.     points3D.push(v.x, v.y, v.z);
  22.     points2D.push(0,0);
  23.     uvts.push(0,0,0);
  24.     sorted.push(new Vector3D());
  25. }
  26. points3D.fixed = true;
  27. points2D.fixed = true;
  28. uvts.fixed = true;
  29.  
  30. var p:PerspectiveProjection = new PerspectiveProjection();
  31. var proj:Matrix3D = p.toMatrix3D();
  32.  
  33. var dx:Number = 0, dy:Number = 0;
  34. addEventListener(Event.ENTER_FRAME, onLoop);
  35. function onLoop(evt:Event):void {
  36.     var i:int, j:int;
  37.     dx += (mouseX - dx) / 4;
  38.     dy += (mouseY - dy) / 4;
  39.     m.identity();
  40.     m.appendRotation(dx, Vector3D.Y_AXIS);
  41.     m.appendRotation(dy, Vector3D.X_AXIS);
  42.     m.appendTranslation(0, 0, 1000);
  43.     m.append(proj);
  44.    
  45.     Utils3D.projectVectors(m, points3D, points2D, uvts);
  46.    
  47.     for (i = 0, j = 0; i<points2D.length; i+=2, j++){
  48.         sorted[j].x = points2D[i] + hsw;
  49.         sorted[j].y = points2D[i + 1] + hsh;
  50.         sorted[j].z = uvts[j * 3 + 2];
  51.     }
  52.     sorted.sortOn("z", Array.NUMERIC);
  53.    
  54.     graphics.clear();
  55.     for(i = 0; i<sorted.length; i++){
  56.         var zpos:Number = sorted[i].z * 12000;
  57.         var c:int = zpos * 14;
  58.         graphics.beginFill(c <<16 | c <<8 | c);
  59.         graphics.drawCircle(sorted[i].x, sorted[i].y,zpos);
  60.         graphics.endFill();
  61.     }
  62. }

Here is an easy way to do z-sorting on a cluster of circles.


Have a look at the swf over at wonderfl...

Also posted in 3D, Graphics, sortOn | Tagged , , | 5 Comments

BitmapData Static

Actionscript:
  1. var canvas:BitmapData = new BitmapData(500,500,false, 0x000000);
  2. addChild(new Bitmap(canvas));
  3.  
  4. var pix:Vector.<uint> = new Vector.<uint>(canvas.width * canvas.height, true);
  5.  
  6. addEventListener(Event.ENTER_FRAME, onLoop);
  7. function onLoop(evt:Event):void {
  8.     canvas.lock();
  9.     var i:int = pix.length;
  10.     while(--i> -1){
  11.         var c:uint = uint(Math.random() * 255);
  12.         pix[i] = c <<16 | c <<8 | c;
  13.     }
  14.     canvas.setVector(canvas.rect, pix);
  15.     canvas.unlock();
  16. }

This is an easy way to fill a BitmapData Object up with a bunch of TV-style static....

This post is meant to serve as filler while I work to get a good solid SIMPLE fp10, drawing api z-sorting example going... There seem to be 3 main ways of doing z-sorting all of which don't seem right to me for some reason... they all work... but I have a gut feeling there is a faster way... The winner so far (unexpectedly) is using Array.sortOn

Also posted in BitmapData, misc | Tagged , , | 6 Comments

Sierpiński Glitch Texture #3

Actionscript:
  1. [SWF(frameRate=60, backgroundColor=0x000000, width=500, height=500)]
  2.  
  3. var canvas:BitmapData = new BitmapData(500,500,false, 0x000000);
  4. addChild(new Bitmap(canvas));
  5. var clone:BitmapData = new BitmapData(500,500,false, 0x000000);
  6. var canvasRect:Rectangle = canvas.rect;
  7. var w:int = canvas.width;
  8. var w2:Number = 1/w;
  9. var w10:Number = 1/(w * 80);
  10. var convert:Number = Math.PI/180;
  11. var size:int = canvas.width * canvas.height;
  12. var pix:Vector.<uint> = new Vector.<uint>(size, true);
  13. var m:Matrix = new Matrix();
  14. m.scale(1,-1);
  15. m.translate(0,canvas.height);
  16. var sin:Number = 0, cos:Number = 0;
  17. var dx:Number = 0, dy:Number = 0;
  18. var pnt:Point = new Point();
  19. var blur:BlurFilter = new BlurFilter(10,10,1);
  20. addEventListener(Event.ENTER_FRAME, onLoop);
  21. function onLoop(evt:Event):void {
  22.     canvas.lock();
  23.     dx +=  (mouseX * 10 - 3000 - dx) / 8;
  24.     dy +=  (mouseY * 4 - dy) / 8;
  25.     for (var i:int = 0; i<size; i++){
  26.         var xp:int = i % w;
  27.         var yp:int = int(i * w2);
  28.         var xp2:int = xp <<1;
  29.         var t:Number;
  30.         t = ((yp|(xp + yp * 0.1)) * (xp + dx) *w10) % 6.14687;
  31.         //compute sine
  32.         // technique from http://lab.polygonal.de/2007/07/18/fast-and-accurate-sinecosine-approximation/
  33.         // by Michael Baczynski
  34.         if (t<0) {
  35.             sin=1.27323954*t+.405284735*t*t;
  36.         } else {
  37.             sin=1.27323954*t-0.405284735*t*t;
  38.         }
  39.         // compute cosine
  40.         t = (xp2|(yp+xp*0.1) + dy) * convert % 6.28;
  41.         t+=1.57079632;
  42.         if (t>3.14159265) {
  43.             t-=6.28318531;
  44.         }
  45.         if (t<0) {
  46.             cos=1.27323954*t+0.405284735*t*t;
  47.         } else {
  48.             cos=1.27323954*t-0.405284735*t*t;
  49.         }
  50.         var c1:int = 31 * (sin - cos);
  51.         if (c1 <0) c1 = 256 - c1;
  52.         c1 = (c1 <<4 | c1) ;
  53.         pix[i] = c1 <<16 | c1 <<8 | c1;
  54.     }
  55.     canvas.setVector(canvasRect, pix);
  56.     clone.copyPixels(canvas, canvasRect, pnt);
  57.     canvas.draw(clone, m, null, BlendMode.SUBTRACT);
  58.     clone.copyPixels(canvas, canvasRect, pnt);
  59.     clone.applyFilter(clone, canvasRect, pnt, blur);
  60.     canvas.draw(clone, null, null, BlendMode.SCREEN);
  61.     canvas.unlock();
  62. }

Decided to do a grayscale version ... also rendered it out large and posted it on flickr...

Check out the swf...


Check out the large flickr version...

Also posted in BitmapData, Operators, pixel manipulation | Tagged , , | 2 Comments