Tag Archives: isometric

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 | Also tagged , | Leave a comment

Isometric Voxels

Actionscript:
  1. // isometric conversion
  2. var centerX:Number=stage.stageWidth/2;
  3. var centerY:Number=stage.stageHeight/2;
  4. var theta:Number=Math.PI/4;// 45 degrees;
  5. var cosX:Number=Math.cos(theta);
  6. var sinX:Number=Math.sin(theta);
  7. var pnt:Point = new Point();
  8. function iso3D(x:Number, y:Number, z:Number):Point {
  9.     pnt.x = centerX + (x-z) *  cosX
  10.     pnt.y = centerY -  (x+z) * 0.5 * sinX - y;
  11.     return pnt;
  12. }
  13. // example:
  14. var canvas:BitmapData=new BitmapData(stage.stageWidth,stage.stageHeight,true,0xFF000000);
  15. addChild(new Bitmap(canvas,"auto",true));
  16. var size:int=100;
  17. var hs:int=size / 2;
  18. var pen:Point = new Point();
  19. var vect:Vector3D = new Vector3D();
  20. // draw a few shapes with offset:
  21. for (var dist:int = 10; dist <= 80; dist *= 2) {
  22.     // voxel space:
  23.     for (var i:int = 0; i<size; i++) {
  24.         for (var j:int = 0; j<size; j++) {
  25.             for (var k:int = 0; k<size; k++) {
  26.                 vect.x=j-hs;
  27.                 vect.y=i-hs;
  28.                 vect.z=k-hs;
  29.                 pen = iso3D(vect.x,vect.y,vect.z);
  30.                 if (Math.sqrt((vect.x * vect.x) + (vect.y * vect.y) + (vect.z * vect.z)) <dist) {
  31.                     // using Vector3D.distance() is very slow compared to above
  32.                     // a few types of coloring:
  33.                     var xp:Number = pen.x + (dist <<2) - 200;
  34.                     canvas.setPixel(xp, pen.y-100, (i <<16 | j <<8 | k) <<1);
  35.                     canvas.setPixel(xp, pen.y+100, (k <<16 | k <<8 | k+j)  );
  36.                 }
  37.             }
  38.         }
  39.     }
  40. }

The above will draw this:

You can read more about voxels here.

This isn't the speediest way to do voxels (especially if you want to animate). This was just the first thing that came to mind.

Posted in 3D, BitmapData, color, pixel manipulation, setPixel | Also tagged , , | 3 Comments

Isometric Box

Actionscript:
  1. stage.frameRate = 30;
  2.  
  3. for (var i:int = 0; i<100; i++){
  4.     makeBoxSegment(200, 200 - i, i * 2);
  5. }
  6.  
  7. function makeBoxSegment(xp:Number, yp:Number, col:uint):Sprite {
  8.     var isoBox:Sprite = Sprite(addChild(new Sprite()));
  9.     with (isoBox) scaleY = .5, y = yp, x = xp;
  10.     var box:Shape = Shape(isoBox.addChild(new Shape()));
  11.     box.rotation = 45;
  12.     with (box.graphics) beginFill(col), drawRect(-50,-50,100,100);
  13.     isoBox.addEventListener(Event.ENTER_FRAME, onRotate);
  14.     return isoBox;
  15. }
  16.  
  17. function onRotate(evt:Event):void {
  18.     evt.currentTarget.getChildAt(0).rotation = mouseX;
  19. }

An isometric box that rotates with the mouseX.

Posted in DisplayObject, Graphics | Also tagged , , | 2 Comments