Monthly Archives: June 2009

QuickBox2D Skinning

Actionscript:
  1. import com.actionsnippet.qbox.*;
  2.  
  3. var sim:QuickBox2D = new QuickBox2D(this);
  4.  
  5. sim.createStageWalls({lineAlpha:0,fillColor:0x000000})
  6. sim.addBox({x:3, y:3, width:3, height:3, skin:BoxSkin});
  7. sim.addCircle({x:3, y:8,radius:1.5,  skin:CircleSkin});
  8. sim.addPoly({x:6, y:3, verts:[[1.5,0,3,3,0,3,1.5,0]], skin:TriangleSkin});
  9.  
  10. sim.addBox({x:6, y:3, width:3, height:3, skin:BoxSkin});
  11. sim.addCircle({x:6, y:8,radius:1.5,  skin:CircleSkin});
  12. sim.addPoly({x:12, y:3, verts:[[1.5,0,3,3,0,3,1.5,0]], skin:TriangleSkin});
  13.  
  14. sim.start();
  15. sim.mouseDrag();

You'll need this fla to run this snippet since the graphics are in the library. This snippet shows how to easily use linkage classes as the graphics for your rigid bodies. This was actually one of the first features I implemented in QuickBox2D.


Take a look at the swf here...

Posted in Box2D, MovieClip, QuickBox2D, motion | Tagged , , , , | 33 Comments

QuickBox2D groupIndex

Actionscript:
  1. import com.actionsnippet.qbox.*;
  2. import Box2D.Common.Math.*;
  3.  
  4. stage.frameRate = 60;
  5.  
  6. var sim:QuickBox2D = new QuickBox2D(this);
  7.  
  8. sim.createStageWalls();
  9.  
  10. createTraveler(3, 3);
  11.  
  12. addObstacles();
  13.  
  14. function addObstacles():void{
  15.     sim.setDefault({groupIndex:-1, density:0, height:0.4});
  16.     sim.addBox({x:6, y:5, width:8, angle:0.17})
  17.     sim.addBox({x:7, y:7.1, width:8, angle:-0.17})
  18.     sim.addBox({x:5, y:9.1, width:8,  angle:0.10})
  19.     sim.addBox({x:6, y:11.5, width:8.9,  angle:-0.20})
  20.     sim.addBox({x:5.5, y:16, width:9, angle:0.20})
  21.     sim.addBox({x:5.5, y:18, width:9})
  22.     sim.addCircle({x:11, y:20, radius:2.5, groupIndex:1});
  23.     sim.addBox({x:16, y:19, width:2, height:2, angle:0.0, groupIndex:1})
  24. }
  25.  
  26. function createTraveler(x:Number, y:Number):QuickObject{
  27.     var parts:Array = [];
  28.     parts[0] = sim.addCircle({x:0, y:1, radius:0.25, friction:0.01});
  29.     parts[1] = sim.addCircle({x:0, y:3, radius:0.25, friction:0.01});
  30.     parts[2] = sim.addBox({x:0, y:2, width:0.3, height:1.5 , groupIndex:-1});
  31.     return sim.addGroup({objects:parts, x:x, y:y });
  32. }
  33.  
  34. sim.start();
  35. sim.mouseDrag();

One of the more advanced and useful properties of rigid bodies is the groupIndex. It allows you to specify which rigid bodies collide with one another and which rigid bodies pass through one another. This snippit demo's the groupIndex property. For more information take a look at what the Box2D manual says.

Take a look at the swf here.

Posted in Box2D, QuickBox2D, motion | Tagged , , , , | 5 Comments

HSV Color Type

Actionscript:
  1. [SWF(width=560,height=300,backgroundColor=0x000000,frameRate=30)]
  2.  
  3. var key:Object = new Object();
  4. var alphabet:Array = "abcdefghijklmnopqrstuvwxyz".split("");
  5.  
  6. var num:Number = alphabet.length;
  7. var step:Number = 360 / num;
  8.  
  9. var colors:Object = new Object();
  10. for (var i:int  = 0; i<num; i++){
  11.     var index:String = alphabet[i];
  12.      key[index] = 65 + i;
  13.      var c:Array = hsv(i * step, 1, 1);
  14.      colors[index] = c[0] <<16 | c[1] <<8 | c[2];
  15. }
  16. alphabet.push("32");
  17. num++;
  18. key["32"] = 32;
  19. colors["32"]  = 0x333333;
  20. x = y = 10;
  21. var count:int = 0;
  22. var size:int = 20;
  23. stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyPressed);
  24. function onKeyPressed(evt:KeyboardEvent):void{
  25.     for (var i:int= 0; i<num; i++){
  26.         var index:String = alphabet[i];
  27.          if (index == "32"){
  28.             trace("hi", evt.keyCode, key[index]);
  29.          }
  30.         if (evt.keyCode == key[index]){
  31.             graphics.beginFill(colors[index]);
  32.             var xp:int = count % num * size;
  33.             var yp:int = int(count / num) * size;
  34.             graphics.drawRect(xp, yp, size, size);
  35.             count++;
  36.         }
  37.     }
  38. }
  39. // ported from here:
  40. //http://www.cs.rit.edu/~ncs/color/t_convert.html
  41. function hsv(h:Number, s:Number, v:Number):Array{
  42.     var r:Number, g:Number, b:Number;
  43.     var i:int;
  44.     var f:Number, p:Number, q:Number, t:Number;
  45.     if (s == 0){
  46.         r = g = b = v;
  47.         return [Math.round(r * 255), Math.round(g * 255), Math.round(b * 255)];
  48.     }
  49.     h /= 60;
  50.     i  = Math.floor(h);
  51.     f = h - i;
  52.     p = v *  (1 - s);
  53.     q = v * (1 - s * f);
  54.     t = v * (1 - s * (1 - f));
  55.     switch( i ) {
  56.         case 0:
  57.             r = v, g = t, b = p;
  58.             break;
  59.         case 1:
  60.             r = q, g = v, b = p;
  61.             break;
  62.         case 2:
  63.             r = p, g = v, b = t;
  64.             break;
  65.         case 3:
  66.             r = p, g = q, b = v;
  67.             break;
  68.         case 4:
  69.             r = t, g = p, b = v;
  70.             break;
  71.         default:        // case 5:
  72.             r = v, g = p, b = q;
  73.             break;
  74.     }
  75.     return [Math.round(r * 255), Math.round(g * 255), Math.round(b * 255)];
  76. }

This snippet is a typing experiment - for every letter, you type a box filled with a specific color is drawn to the stage. The color associated with each letter is determined by moving through hsv color space - so typing an alphabet will end up with something resembling a spectrum.

Posted in Graphics, UI, color, keys | Tagged , , | Leave a comment

Dot Syntax from String

Actionscript:
  1. function dotSyntax(target:*, path:String):* {
  2.     var level:Array=path.split(".");
  3.     var curr:* = target;
  4.     for (var i:int = 0; i<level.length; i++) {
  5.         curr=curr[level[i]];
  6.     }
  7.     return curr;
  8. }
  9.  
  10. trace(dotSyntax(this, "stage.stageWidth"));
  11. trace(dotSyntax(this, "graphics"));
  12. trace(dotSyntax(this, "root.loaderInfo.bytesTotal"));
  13.  
  14. /*outputs something like:
  15. 800
  16. [object Graphics]
  17. 1230
  18. */

This snippet shows how to parse dot syntax from a string. It does this by splitting the string and then using square bracket syntax. This is one of the main techniques that makes yesterdays post possible.

Posted in dynamic, string manipulation, strings | Tagged , , | Leave a comment