Breaking News

World Building – Snapping

With the Maze VR world builder constantly learning new tricks, I had somehow forgotten about one really important time-saving feature. All elements that can be put into the world start at their local (0,0,0) and then expand towards z to accommodate different thicknesses of objects. A poster is for example very thing, a wall on the other hand uses up more space.

I somehow overlooked that if you want to now put the poster onto the wall, you cannot foresee how thick of a wall you will put it on, so this needs to be a run-time setting instead of a design-time one.

Snapping to the rescue

My line of thinking was, that objects should somehow glue themselves onto their counter-parts. It turns out this is relatively easy to do. Instead of placing them at the (0,0,0) they were created for, I move them halfway into the grid, away from the direction they are facing. Then I shoot a ray to find the nearest surface and move the object back into the original direction by distance of the ray.

Four tiles in mid-air and a blue ray

The tiles correctly glued onto the next wall

Of course the obligatory pitfalls do not wait long. The main one is: from which origin point should I shoot the ray exactly? At some point in time I want to stack multiple objects, with some being bigger, some smaller. Shooting the way from the local pivot (0,0,0) will then not hit smaller objects in-between.

The best solution I could come up with so far and which I will go for (right now I still use a somehow shifted pivot) is to calculate a compound bounding box of the object, shoot the ray from the center and then do some magic to somehow subtract the object width so that it is not half-way in the wall.

Another tricky issue is that a ray needs to have a target, so either I have to wait for the whole world to finish building and do a second pass or ensure everything to snap onto was already built. I decided for the second way for now, as the build order is the order of the layers in the Tiled map file and therefore gives me potentially the freedom easier support stackable objects this way.

3 thoughts on “World Building – Snapping

Leave a Reply

Your email address will not be published. Required fields are marked *