Help with Rotator Calculations for Snapping Objects

I’m trying to create a type of interaction in VR, but I’m having trouble figuring out the necessary rotation math.

I have an object A with a child actor B, as well as another object C with child D. I want to create a “snapping” mechanic so that when a player holding A lets go of it, if B and D overlap then A will “snap” to C. This will change A’s transform so that B and D are at the same location, and B has the same world rotation as D (plus 180 on D’s Y-axis).

I have the location snapping figured out, but I haven’t been able to crack the orientation snapping. How do I rotate A so that B has a certain orientation relative to D? I’ve been trying to use the RotateAroundPivot function from the VRExpansionLibrary, if that helps.

Hey @ZapperFire44! Welcome to the forums!

So you have the snapping of location, that’s good! However the rotation is going to need a much more nuanced approach. If they’re two objects of the same size and shape with a little stick coming off of each, for instance, you could just get the vector of “B” and multiply that by -1, use MakeRot, and setRotation for A to that. Don’t forget you can rightclick to “split” a vector into X, Y, and Z, alter them and then use “MakeVector” to recombine them :slight_smile:

Hope that helps! Come back with more info if you need more help please! A screenshot could do wonders :slight_smile:

This works in some cases, but not all - I’m looking for a generalized solution.

My goal is to create snap points that I can place on different kinds of objects, so their sizes and shapes will differ. In addition, the snap points may not align with the parent’s pivot point.
snapobject

Here’s a pic of a test object I’ve got, and a video showing the intended orientation state: the two connecting snap points should have the same Y-vector and opposite X and Z-vectors.



Here’s my current attempt at aligning rotations. The held object A becomes a child to the target object C, then I attempt to set the relative rotation of A as (B relative + D relative + offset). This works in some cases (the tops or bottoms of my test objects), but in other cases (such as the sides) it fails and A has the wrong orientation.

I mentioned the RotateAroundPivot function earlier since it seems like an intuitive solution, but I haven’t figured it out. If I can use the snap point B as a pivot for A, then I should be able to rotate A by the difference between B and D plus the offset, right?

Bumping this, I could really use some advice on how to approach this

Hey again Zapper!

So If I’m being honest, this is some rather active, high level geometry. If you want to focus more on the actual gameplay development… I’d suggest using something from the marketplace to do so. There are some rather cheap (I have one for $10 USD that I love) plugins from the marketplace that do all of this so you can focus on your game instead of having to focus on the mathematical aspect of it all. :slight_smile:

If you are using actual “attachment” mechanism, start by specifying “Snap to target” in the attachment rules. This alone should make your object match the position and rotation of the snap point. Then if you need to add 180° orientation relative to the snap point’s own Y axis, just add 180 to the relative rotation Y of the attached object.


If not using attachment, then work with absolutes.
Set the world location of Self to match the world location of SnapPoint.
If you want to match their rotation, do the same with their world rotations.
If you want to rotate by 180° around snap point’s Y axis, using absolutes, the “Rotate Vector Around Axis” node is very useful :

I eventually figured out a solution on my own. Essentially, I set the rotation of A as the inverse rotation of B in the local space of D, add the offset, then use Transform Rotation/Inverse Transform Rotation nodes to convert to the local space of C. Here’s the blueprint function for anyone who needs it:

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.