How does the tipycal building system snaps objects to objects?

I’m trying to figure how to snap objects to objects in the building system I’m working on.
I’m using a grid which helps to some extent

This is what I have right now, basically I ray cast to get the point the player is looking at and adjust that to the grid size
Still not sure how to proceed from here, for instance this always snaps the wall to the same foundation face, if I would like to do it in a different face, how would I determine which one is the player looking at? Also, having 2 snapped foundations will cause to faces pretty much in the same location…the math behind starts looking horrible to me.

I think I’m probably overcomplicating things and there should be a simpler method, is there?

There seems to be two different questions here.

The first question is: “How do I build content that’s expected to snap cleanly?”
The answer to that is one of:

  1. put sockets in the right locations on all your geometry, and match up socket-to-socket.
  2. build the geometry to a certain grid/size, such that any match-up aligned to that grid will work

The second question is: “How do I build a UI to control the snapping of objects?”
There, it really depends on what it is you want to do. There’s nothing particularly hard or magic about this, from a UI point of view. What might be hard is figuring out what it is you want to do, in the first place! Geometric thinking has a tendency to sometimes get locked up in the brain. I find that using Lego pieces or children’s wooden play blocks may help sometimes.

For the specific question of “how do I determine which face the user is looking at,” if you’re using sockets, you can get the raycast hit position, and figure out which socket is the closest to that point, for example. You may also want to compare socket orientation to the face normal of the look-at ray to filter out some sockets that “point the wrong way” if you have thin or irregularly shaped pieces.

The math is not hard at all. For geometry of a fixed size, just start with the target piece, and offset the “snapping” piece by exactly the size of the grid, in the rotated coordinate space of the target piece. For geometry with sockets, multiply the world transform of the target socket by the inverse local transform of the snapping socket, to get the world transform of the snapping piece.

Thank youjwatte, I plan to do all the pieces the same size, still can’t figure how to rotate them around the different possible snapping locations in the target object, will do some tests with sockets and see how it goes.

I’m doing fine with sockets so far.
Now I’m dealing with this issue:

wall_collisions.png

The green is the mesh and the red is the collision, in order to snap 2 walls in a 90 degrees angle I have to do the collision smaller, but then I get these “holes” when they are snapped in a straight line.
Can someone suggest an approach to deal with this problem?

When using modular geometry, you will generally end up having to very carefully build the geometry so that it interlocks in all cases.
For example, a fence might have a pillar on the right side, but not on the left side, so that when you chain many fence segments together, they end up making a long fence. Then you have a special “end cap” piece that you snap in where the fence ends.
You will have to work out where your “main” pieces go in each snapping piece, so that they all fold in neatly together.

Separately, if you say that all wall pieces have the same size, then A and B do NOT have the same size – you’d make B just contain a segment that’s the same size as one side of A, for that to work out.

I understand.

A and B are just two pieces of the same object (a wall) snapped in a different way.

I think I’ll probably be ok by adding a dedicated collision for building parts so I can use it to check if they overlap between them.

I just created a new SM by merging 2 walls in a 90º degree, seems to work so far.