PDA

View Full Version : Placing objects



wkt
July 15th, 2009, 03:25 AM
Hi there

I want to place objects (attach movieclips) dynamically around a point randomly. The objects I attach should not overlap one another. Are there any similar models that I can take a look at?

Thanks in advance.

therobot
July 15th, 2009, 02:34 PM
How random do you want it to be? 8)

wkt
July 16th, 2009, 01:22 AM
Let's say placed within 300 pixels from that center point?

Gnoll
July 16th, 2009, 02:55 AM
as3 / as2?

ActionScript Code:

var left:Boolean = Math.Random()*10 > 5;
if (left)
{
obj.x = -Math.Random() * 300;
} else {
obj.x = Math.Random() * 300;
}


Then check if it is hitting another object, if it is, rinse and repeat


then the same for y, that is as3

wkt
July 16th, 2009, 03:40 AM
I'm using AS2.0.

The complicated part is the hitTesting part. Using the default hitTest function may drain too much resources, making the game lag. How should I check if it is hitting?

Or some maths may help? Like a graph which never hits itself. Graph like a spiral?

TOdorus
July 16th, 2009, 04:55 AM
Fixed distances and angles would help. Makes me think about chemistry, where electrons would be in different layers around the atom. Only so many electrons can exist per layer, the inner layers containing the lessest and the outer layers the most.

If you define a circle, a radius that goes around your objects then this should be possible. The thing is that you can only use one circle, so if you have objects of various sizes then you should pick the biggest one and use that circle on everything, to make sure they never overlap. You can easily calculate the distance each layer is from the centre point (every diameter there is a new layer). Using PI you can use this distance to get the length of the circle around the object ( a layer ), which roughly gives you how many objects could be on the layer. If you know how many objects can be on the layer you can calculate the angle between each object on that layer. 360/number of objects.

Then you know all the possible positions in your "grid" and you can pick a random gridnumber, check if it's taken and then pick another if necessary.

Grid with second layer full.
http://home.planet.nl/~bogaa096/AtomLayers.jpg

If your problem is more dynamic (like the centre moves, so the grid can move over the previously created objects), I'd suggest using a rectangular grid. Move the grid so the centre cell of the grid is on the centre object. Then assess which cells are occupied (now you can use varying sizes) and more importantly which arent. If there are a or some empty cells: Pick a random one out of these and create the object there.

wkt
July 16th, 2009, 06:55 AM
Hm... Then the look of the group of object will be like a circle, which doesn't feel like random. I guess I'll just try to use an efficient method of hitTesting to do so.

Thank you very much!