actionscript 3 - Calculating column and row number in an isometric rectangular shaped grid -
i'm trying extend isometric flash game based on as3isolib. game support diamond shaped grids have implement possibility have rectangular shaped grids, too. please see example grid.
i managed create grid. here code:
private function createrectangulargrid():void { var c:int, r:int; var node:datanode; var pt:pt; var nodepos:point; var isevenrow:boolean; (c=0; c<cols; c++) { nodepos = new point(c*_cellsize, -c*_cellsize); (r=0; r<rows; r++) { node = new datanode(); node.col = c; node.row = r; node.x = nodepos.x; node.y = nodepos.y; node.z = z; node.width = _cellsize; node.length = _cellsize; node.height = 0; pt = new pt(node.x, node.y, node.z); isomath.isotoscreen(pt); node.screenx = pt.x; node.screeny = pt.y; _nodes.set(c, r, node); isevenrow = r % 2 == 0; if (isevenrow) nodepos.x += _cellsize; else nodepos.y += _cellsize; } } }
with cellsize (isometric width , length) of 20px, isometric positions of grid cells shown above are:
- [datanode (col:0, row:0, x:0, y:0)]
- [datanode (col:0, row:1, x:20, y:0)]
- [datanode (col:0, row:2, x:20, y:20)]
- [datanode (col:0, row:3, x:40, y:20)]
- [datanode (col:0, row:4, x:40, y:40)]
- [datanode (col:0, row:5, x:60, y:40)]
- [datanode (col:0, row:6, x:60, y:60)]
- [datanode (col:1, row:0, x:20, y:-20)]
- [datanode (col:1, row:0, x:20, y:-20)]
- [datanode (col:1, row:1, x:40, y:-20)]
- [datanode (col:1, row:2, x:40, y:0)]
- [datanode (col:1, row:3, x:60, y:0)]
- [datanode (col:1, row:4, x:60, y:20)]
- [datanode (col:1, row:5, x:80, y:20)]
- [datanode (col:1, row:6, x:80, y:40)]
- [datanode (col:2, row:0, x:40, y:-40)]
- [datanode (col:2, row:1, x:60, y:-40)]
- [datanode (col:2, row:2, x:60, y:-20)]
- [datanode (col:2, row:3, x:80, y:-20)]
- [datanode (col:2, row:4, x:80, y:0)]
- [datanode (col:2, row:5, x:100, y:0)]
- [datanode (col:2, row:6, x:100, y:20)]
the problem objects , avatars still placed grid diamond shaped. because formula calculating column , row number based on isometric x/y position working diamond grids:
var isopt:pt = isomath.screentoiso(new pt(avatar.x, avatar.y)); var col:uint = math.floor(isopt.x / cellsize); var row:uint = math.floor(isopt.y / cellsize);
does know how formula should rectangular shaped grid?
i figured out. following method return column , row number of tile lying under isometric x/y position passed parameters.
public function getnodebyisopos(x:number, y:number):datanode { var c:uint, r:uint; if (_shape == staggered_shape) { // calculates column , row rectangular map (also called staggered map or block map) c = math.floor((math.floor(x / _cellsize) - math.floor(y / _cellsize)) / 2); r = math.floor(x / _cellsize) + math.floor(y / _cellsize); } else { // calculates column , row diamond map c = math.floor(x / _cellsize); r = math.floor(y / _cellsize); } return getnode(c, r); }
works perfectly. please let me know if there more elegant way.
Comments
Post a Comment