Rotation woes with construction system

Currently I am working on a construction system which allows for the placement/attachment of objects to other objects (similar to space engineers). For the most part it is working well however I am having issues with rotations using my system.

When the user is building an attachment onto an existing object it should snap to the target and rotate the nearest construction point to face the target construction point (these are empty gameobjects placed on the object).

In the image below, the red + are the child gameobjects that the construction can snap onto (construction points), the blue arrows are the forward vectors for the construction points.

Ideally the circles in orange construction point would rotate to be facing the front face of the target construction point. How do I get this rotation and apply it to the parent object (the cube)?

Normally the objects would be snapped next to one another without the rotation but I have used this image to better show the rotation I am looking to replicate.

7436507--911222--upload_2021-8-21_17-7-48.png

You would need to get both parent object (cube) rotation values, and assuming you are only working with 4 (or 6 with top/bottom) assign Rotation of your moved object to be equal to the 2nd object.
This won’t snap the “close” side to the 2nd object, but all you would need for that is a (0, 90,180,270) degree rotation based on which would require the least amount of change. So, before snapping the object, save the rotation in a temp var. Then compare that rotation to the 2nd objects’ + (0,90,180,270) and pick the one with the least amount of change.

Well, most such construction games are voxel games for a good reason. Having a clear uniform grid simplifies many issues you might have with placement and rotation.

However if you just talk about aligning your object to the direction of the other attachment point, one of the simplest solution is to temporarily swap the order of your objects, align the attachment point and revert the ordering. What I mean is this. Usually you have a setup like this:

Cube
    - construction point 1
    - construction point 2
    - construction point 3
    - construction point 4
    - construction point 5
    - construction point 6

Say we need to align construction point 3. So we just do this:

  • unparent construction point 3
  • make the old parent a child of construction point 3
  • align construction point 3, probably using LookRotation
  • unparent the cube
  • reparent the construction point 3 to the cube

All those are relatively easy operations. To avoid slight conversion issues, you may want to ensure proper 90° alignment after reparenting. So grabbing the eulerAngles of the child, rounding all 3 values to the closest multiple of 90 and setting them back would ensure that you don’t get some drifting.

Note this is only for visually aligning the objects when they are in free move. In order to attach them together you would simply parent the objects together (usually to a logical common parent object). Again after parenting you may want to clamp / round the actual rotation to ensure proper alignment.

Of course if you want to support “arbitrarily rotated attach points” (so they are not rotated in 90° increments) things would get way more complicated. It also makes checking for overlaps way more difficult. That’s actually one of the main points why you usually want to use a regular grid.

Just to clarify: If you want to attach two construction points, you would simply use LookRotation with the inverse of the other construction points forward vector. Of course you have to select a proper up vector as well. Though there are only 4 possible ways in a 90° rotation world, so picking the one that matches the current up vector best shouldn’t be that difficult.