I recently have been experimenting with X-27's excellent answer on how to place objects dynamically in the BGE. I have been trying to figure out how to do the same thing, only make it possible to snap the next block to previously added ones as well as the ground. Is there a simple way to do this? And if not, what sorts of workarounds/complex solutions are there?
Starting with the rig that is in this answer, Making a grid snapping system is actually very simple.
First, select the ground plane and scale it to be relatively small.
Replace the script on the ground plane to this
from bge import logic def main(): scene = logic.getCurrentScene() cont = logic.getCurrentController() own = cont.owner mouse_over = cont.sensors["Mouse_Over"] if mouse_over.positive: tracker = scene.objects["Tracker"] tracker.worldPosition = own.worldPosition tracker.worldPosition.z += 1
You need to also use this logic setup on your plane
Now, when the mouse is over that plane, it will move our arrow to the world position of the plane.
However, this is not much of a grid - yet.
First we should move the plane to one corner. Your current setup should look like this.
To create the grid, we can use the array modifier on the ground plane.
Enter edit mode by pressing tab
With all of the faces selected, we can press p Note that we must be In edit mode, if we were in object mode, this would start the game engine. Select separate by loose parts
We now have all of the planes now as different objects. If we press play we can see how it will work so far.
For some reason it is not working. Good thing is that I'm writing this answer and I happen to know why it isn't working. (This is really easy to fix :-)
Our problem is that all of the origins are in the same place. The system actually Is working, but quite simply, the world positions of all the objects are in the exact same location so there will be no noticeable change.
However, with all (most) problems, there is a solution. Select all of the grid objects, and press Ctrl+Shift+Alt+C This will allow you to change the origins of all the grid objects.
You could select Origin to Center of Mass or Origin to Geometry
Since they are infinity square planes there is no difference.
Now, Each grid section has it's origin centered over itself, so when it snaps the arrow to it's origin, it will work like a grid snapping system Translation: everything works and everyone is happy
Now we have a basic grid snapping system.
The basic idea for making a world building system is being able to stack cubes and place them next to each other. This is not Very hard to do, we only need to add the Boolean property Ground to the cube that is being added (Building), and we also need that cube (still named Building) to be running the script.
There are a few more minor adjustments we need to make to the grid objects. First, because our cubes are 2x2x2 the grid planes should all also be 2x2 . Also, If two cubes are stacked, there is a space of Two between their origins. we must adjust the script.
Also, because the cubes are being added Two above the plane, we need to move the origins down one unit.
The end result can be seen working awesomely.
Working blend with world building system.