x


Avoiding overlap between randomly placed object

Is there any smart and efficient methods that could be used to avoid collision and overlap between gameobjects generated and placed during runtime and already existing objects in the scene?

more ▼

asked Nov 26 '09 at 01:32 PM

torg gravatar image

torg
97 3 3 7

(comments are locked)
10|3000 characters needed characters left

5 answers: sort voted first

Take a look at Physics.CheckSphere: http://unity3d.com/support/documentation/ScriptReference/Physics.CheckSphere.html/ Physics.CheckSphere

First you generate a random position, then you use CheckSphere to find out if there is an object there already, if it is, generate a new position, if not, place the object there.

This won't look artificial and the objects wont overlap.

more ▼

answered Nov 26 '09 at 02:59 PM

TowerOfBricks gravatar image

TowerOfBricks
3.2k 17 25 50

FYI, The CheckSphere link has an extra slash at the end.

Dec 10 '09 at 06:29 PM Ehren
(comments are locked)
10|3000 characters needed characters left

Make sure there is enough space to start with.

Also, if you're instansiating alot of object inside a volume you can use cells to place objects semi-randomly.

Imagine you've got a grid x*y*z large, and each grid might be like 10x10x10 units. Then randomly select n amount of cells to generate items into, and randomize the position of the object inside that cell. This will greatly aid instansiating objects somewhat randomly that won't overlap as long the object fit inside the cell. Remember you still have to make sure that none of the objects extents (bounding box) pass over to neighboring cells.

In a dense grid, this effect will look very artificial but in a sparse grid this is a good approach.

Note that if you register existing game objects as occupants of cells, then you'll also gurantee that you aren't instansiating objects into preexisting objects.

more ▼

answered Nov 26 '09 at 01:45 PM

Statement gravatar image

Statement ♦♦
20.1k 35 70 175

(comments are locked)
10|3000 characters needed characters left

I used Check Sphere and it works fine, make sure you have an exit strategy if somehow there is no space to place the object, avoiding an infinate loop!

more ▼

answered Nov 26 '09 at 03:53 PM

Ash gravatar image

Ash
23 3 3 4

(comments are locked)
10|3000 characters needed characters left

I've had some time to play with this now and must say I'm a little lost with how this exactly works. I'm using Physics.CheckCapusle as a capsule like shape is more in line with my objects, which are sometimes long and sometimes not.

static function CheckCapsule (start : Vector3, end : Vector3, radius : float, layermask : int = kDefaultRaycastLayers) : bool

I'm assuming that the "start" vector would be the position of one end of my object and "end" the other. The radius is self explanatory, and as I understand it the layermask is an option to ignore collisions with objects on said layer? My object are being placed on a terrain made of a planes so naturally I put these planes on this layer, which gives me only false results (even if the object are clearly colliding. And if I don't set this layermask I get only positives regardless of object collision. Could anyone point me in the right direction? :) Btw. Each object is made up of several children, sort of on a row of different length, so the way I figure out start and end positions is through the position of the first and last child object.

more ▼

answered Nov 26 '09 at 08:54 PM

torg gravatar image

torg
97 3 3 7

You can iterate through all the child objects and test which one is placed most to the right for example, or you can use the collider.bounds and figure out which direction is the longest, and from that generate a start and end value.

Regarding the layer mask. The best way is to define it in the editor, and make sure both the objects and the plane ground are set to different layers.

Nov 27 '09 at 07:09 AM TowerOfBricks
(comments are locked)
10|3000 characters needed characters left

Thanks for some excellent suggestions. Already using a grid system of sorts, but the objects in question are fairly large and of different sizes so its difficult to fit them perfectly into a grid without making the spacing excessive. I'm also giving them a random rotation, which makes things even trickier, but the Physics.CheckSphere looks like a good solution to my problem :)

more ▼

answered Nov 26 '09 at 03:14 PM

torg gravatar image

torg
97 3 3 7

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x2083
x572

asked: Nov 26 '09 at 01:32 PM

Seen: 3087 times

Last Updated: Nov 26 '09 at 01:32 PM