9
$\begingroup$

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?

$\endgroup$
  • $\begingroup$ do you want a snapping system to place cubes along a 3D grid or other cubes, or objects on any face? $\endgroup$ – X-27 Dec 23 '15 at 15:05
  • $\begingroup$ @X-27 On any face of a cube. $\endgroup$ – VRM Dec 23 '15 at 16:08
13
+50
$\begingroup$

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

enter image description here

Now, when the mouse is over that plane, it will move our arrow to the world position of the plane.

enter image description here

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.

enter image description here

To create the grid, we can use the array modifier on the ground plane.

enter image description here

Enter edit mode by pressing tab

enter image description here

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.

enter image description here

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 :-)

enter image description here

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.

enter image description here

You could select Origin to Center of Mass or Origin to Geometry

Since they are infinity square planes there is no difference.

enter image description here

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

enter image description here

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.

enter image description here

There are a few more minor adjustments we need to make to the grid objects. First, because our cubes are 2x2x2 Blender units, the grid planes should all also be 2x2 Blender units. Also, If two cubes are stacked, there is a space of Two Blender units between their origins. we must adjust the script.

enter image description here

Also, because the cubes are being added Two Blender units above the plane, we need to move the origins down one unit.

The end result can be seen working awesomely.

enter image description here

Working blend with world building system.

$\endgroup$
  • $\begingroup$ Works great, but I wanted to snap to the previously added objects as well. How hard is that? $\endgroup$ – VRM Apr 22 '15 at 0:36
  • $\begingroup$ so you want a minecraft type building system? (that is relatively easy) add the Ground property to the cubes that you are adding, and also have them running the grid placement script. You can also adjust the size of the grid to be the same size as the cubes. $\endgroup$ – X-27 Apr 22 '15 at 0:38
  • $\begingroup$ Sort of, I want to be able to build buildings adjacent to or on top of each other $\endgroup$ – VRM Apr 22 '15 at 0:39
  • 1
    $\begingroup$ I'll edit the answer with a new gif. $\endgroup$ – X-27 Apr 22 '15 at 0:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.