actionscript 3 - Connecting blocks together but prevent intersection -
edit have had rethink: going use easier implement tree structure grid each block can have 4 neighbours.
i trying attach blocks on stalk (series of blocks) of flower. green blocks existing stalk , blue block 1 attached mouse.
the block attached mouse correctly snap nearest edges (i'm allowing diagonals now) blocks able go 'inside' stalk (image 2) snapping block above or below.
my question is, how can stop this? considered
- iterating list again ignoring block tried attach to; think result in same problem attaching block 'inside' stalk.
- find block intersecting , connection point it, repeat until there no intersections; seems better option messy when intersecting more 1 block @ time
i should note plan on having smoother snap, not arbitrarily edge, grid pretty out of question. pretty confident there must elegant solution, i'm not seeing it!
here snapping code stands
var mousepos:point = new point(mousex, mousey);// new point(e.stagex, e.stagey); var nearestpoint:point = null; var nearestdistance:number = 0; (var i:int = 0; < mplant.length; ++i) { var part:plantpart = mplant[i]; if (part stalk) { var connectionpoint:point = (part stalk).getnearestconnectionpoint(mousepos); var distance:number = point.distance(mousepos, connectionpoint); if (nearestpoint == null || distance < nearestdistance) { nearestpoint = connectionpoint; nearestdistance = distance; } } } if (nearestpoint != null) { mmousepointer.x = nearestpoint.x; mmousepointer.y = nearestpoint.y; }
you can pre-calculate bounding rectangle stalk, , snapping check against rectangle.
the rectangle can calculated using (un-tested code, though)
var _bounding:rectangle = new rectangle(int.max_value, int.max_value,0,0); each( var part:plantpart in mplant) { if(part stalk) { _bounding.width = part.width; // width same _bounding.height = math.max( _bounding.height, part.y ); _bounding.x = math.min( _bounding.x, part.x ); _bounding.y = math.min( _bounding.y, part.y ); } }
Comments
Post a Comment