- Home /
Finding flat, horizontal regions for reliable object placement?
Hello everyone. I am building a plugin for adding decorations (in form of smaller objects) to an already setup scene. I want these objects to be procedurally placed on surfaces that are big enough for them to stand on (avoiding putting a large object on an inadequately small surface area), there is also a limitation to the slope on which the objects can be placed (the angle between the surface normal and the world up vector should be under 30 degrees).
I have considered using ray-casting from above to look for points on the scene objects that are positioned on the same horizontal plane but this doesn't work with concave objects that cover up valid surfaces.
Another possible solution I thought of was to go through each scene object's mesh, looking for vertices with the correct normal values and then somehow recursively looking for equally valid neighboring vertices - but this method seems like a huge task.
I need the area of these flat, horizontal sub-meshes to tell if I can place an object there without it looking like it is disobeying gravity.
I appreciate you reading this far, thank you! I hope someone has a good idea, because my head hurts from thinking about this.
Edit: I have to add that I am looking for these interesting regions on EVERY object in the scene (perhaps with a few exceptions) NOT just the ground. I am not working with a "terrain mesh" it is an indoor environment consisting of many objects.
raycast down until you hit a surface. if flat enough, X. else, raycast down from there until you hit a surface. if flat enough, X. else, raycast...
And stop raycasting in that spot once you hit nothing. That can solve your concave-object woes.
a smart-arse solution is drop an invisible cube.
if it sits flat, you know the surface is flat to the relevant fractal dimension.
Whoa, you can't post two answers any more! Here's another answer...
the usual trivial solution is, write a routine that takes a point
imagine that to be the center of a square say a meter across. simply get the altitude of the ground at the four corners of the imaginary square
(ie, very likely cast to do that)
if the four altitudes are the same (or within a tolerance), that's good enough ... "it's flat"
I have no idea what you mean about concave etc, but the above is the simple solution. You should realise what you are asking is a near AI problem .. it depends mightily on what you want, what sizes are involved etc.
like my brother-in-law at nasa probably knows $$anonymous$$ms, dozens, of topologists and research students and AI guys who work on problems like "find flat spot on mars to land on" or some such
Whoa, you can't post three answers any more! Here's another answer...
typical code ...
private var testHit:RaycastHit;
function _groundElevationNearby(p:Vector3):float
{
p.y = HigherThanEverything;
if (Physics.Raycast( p, Vector3.down, testHit, ten$$anonymous$$eters, (1<<8) ))
return testHit.point.y;
print("WOE IN GROUND EXTRAS - could not get the ground elevation !!");
return 0.0;
// the ground is layer "8" in this example. 'ten$$anonymous$$eters' would simply be
// far enough to hit the ground from the vantage altitude
}
to check a few points around you'd just go
_elevation( p + Vector3(1,1,0) );
_elevation( p + Vector3(-1,-1,0) );
_elevation( p + Vector3(1,-1,0) );
_elevation( p + Vector3(-1,1,0) );
just get the $$anonymous$$-max gap (or whatever) of the four numbers
just TBC I think Loius means, and get the normal when you raycast (that's really obvious to guns like us :-) )
Do you guys think that I might use 4 downward directed (Physics.RaycastAll) placed in the shape of a square and look for quadruple-successes (y-axis-wise close to each other and with valid normals)?
This would probably take too much unneeded processing power and still not solve the problem of convex objects (a cube dropped over a table won't register the valid spot under the table).