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...

This entry was posted in 3D, Graphics, Vector, sortOn and tagged , , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

4 Comments

  1. Posted August 29, 2009 at 11:54 am | Permalink

    I’m trying to implement keyboard usage (actually, not trying, i did) with this snippet, the problem is that when you rotate it on the y axis, the circles don’t rotate on the x axis (they do, but not from the front view). how do i get the circles to spin left and right with the left and right arrows, and then be able to spin then up and down with the up and down arrow keys?

  2. Posted August 29, 2009 at 11:56 am | Permalink

    woops, i mean after you rotate on the y axis, then try to rotate on the x axis, it rotates based on the rotation of the y, and I’d like to rotate based on the screen pose.

  3. Posted August 30, 2009 at 12:46 am | Permalink

    Hey Thomas, just posted a demo to answer your question:
    http://actionsnippet.com/?p=2064

  4. Posted August 31, 2009 at 8:08 am | Permalink

    pretty sweat, might be useful soon - thanks :)

One Trackback

  1. [...] Simple z-sorting [...]

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*