views:

84

answers:

1

. . Hi there, everybody.

. . I have a canvas defined by many points (x,y)--it's not rectangular, but at least it will follow some almost-hexagonal shape (like a distorted hexagon, with angles that are always < 180 degrees). I also have a big collection of different rectangles (more or less 140 of them, with different widths and heights) and I need to position them inside the shape. They must be positioned as if they were being pushed by gravity (I mean... the top of the shape doesn't needs to be filled, but the bottom needs). . . Right now my only idea is to store the min and max "x" position for every "y" position (something like: limits[300] = [30,30]; limits[299] = [29, 32]; ...), loop through the rectangles, get their area and then compare the values to find the best position. The biggest issued I have right now is that they need to be organized in a "organic" way, not like a table (I can't have rows and columns and position them inside cells. they need to be positioned as good as they can be, like fluidly, side by side, one above other...). I'm not sure how to manage the positioning. . . I know some good examples of "auto-balanced" objects (a good one is the "Ball Pool" Chrome Experiment: http://mrdoob.com/projects/chromeexperiments/ball_pool/), but they use rectangular canvas and circular objects, which are much easier to compare the limits. . . Right now my best idea is to check the objects for every possible x/y combination, from bottom to up, from left to right, checking if any area would overlap, but this would probably lock the whole computer for the calculations for minutes (the more itens on the screen, the more calculations would be needed).

. . The project is running on Flash, but any example, tip, algorithm, paper or pseudo-code will be of great help.

A: 

Hi,

I think you can get all done using a physics simulation. It might sound complicated, but it's not as hard as it sounds. There are several physics APIs for actionscript 3. I've used the as3 port of Box2D and I'm pretty happy with the results.

The way I would go about it is:

  • define shapes for the walls ( the lines that make your polygonal shape )
  • define the boxes(rectangles)
  • create the physics bodies with the proper display objects information(linkage id , etc. )
  • add them to the physics world and simulate.

I don't know what your experience with actionscript 3 is, but have a look at the World Construction Kit. It should allow you to get on track fast.

George Profenza