package { import flash.display.Graphics; /** * * Petri Leskinen, Finland, Espoo, 7th June 2009 * leskinen[dot]petri[at]luukku[dot]com * pixelero.wordpress.com * */ public class GridItem { public var // coordinates in 3D x:Number, y:Number, z:Number, // texture coordinates u:Number = 0.0, v:Number = 0.0, // normal vector for shading normalx:Number = 0.0, normaly:Number = 0.0, normalz:Number = 1.0; public var id:int; public var // connections for looping the grid itemRight:GridItem = null, itemBelow:GridItem = null; public var // values for physics demo mass:Number = 1.0, // acceleration az:Number = 0.0, // velocity vz:Number = 0.0, // does this point move fixed:Boolean=false; public function GridItem(_x:Number = 0.0, _y:Number=0.0, _z:Number =0.0, _id:int = -1) { x = _x; y = _y; z = _z; id = _id; } public static function resetNormal(i:GridItem):void { i.normalx = i.normaly = 0.0; i.normalz = 1.0; } public static function setNormal(i:GridItem):void { /* * cross product = * i j k * 1 0 normalx * 0 1 normaly * * = -normalx i -normaly j + k */ var tmp:Number = 1.0 / Math.sqrt(i.normalx * i.normalx + i.normaly * i.normaly +1.0); i.normalx *= tmp; i.normaly *= tmp; i.normalz = tmp; } // texture coordinate u is used for shading // nb: lenght of vectors 'light' and 'normal' must be both 1.0 public static function updateUVT(i:GridItem, light:Object):void { var cosangle:Number = i.normalx * light.x + i.normaly * light.y + i.normalz * light.z; i.u = 0.5 + 0.45 * cosangle; } // for mouse interaction in animation public function bounce(value:Number):void { // vx = tmp*particles[i].x; // vy = tmp*particles[i].y; vz = value; } // some useless functions for testing public function draw(g:Graphics):void { var itemY:GridItem = this; do { g.moveTo(itemY.x, itemY.y); var itemX:GridItem = itemY; do { g.lineStyle(1, Math.random()*0xFFFFFF, 1); g.lineTo(itemX.x, itemX.y); if (itemX.itemBelow) { g.lineTo(itemX.itemBelow.x, itemX.itemBelow.y); g.moveTo(itemX.x, itemX.y); } } while (itemX = itemX.itemRight) } while (itemY = itemY.itemBelow) } public function lineTo(g:Graphics):void {xy(g.lineTo)} public function moveTo(g:Graphics):void {xy(g.moveTo)} private function xy(f:Function):void { f(10 * x, 10 * (y + z / 2)); } } }