How to rotate/snap 2D objects together based upon normal vectors

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By jarlowrey
:warning: Old Version Published before Godot 3 was released.

Given some 2D objects with corresponding normal vectors I’d like to snap them together such that their normal vectors become opposites. So it’d look something like the following:

enter image description here

enter image description here

Unfortunately I can’t get them to stick from all rotations/orientations and am not sure what I’m doing wrong. Hopefully someone can help out. Here’s the malfunctioning code:

var my_ang = cnct_joints.mine.normal_angle + cnct_joints.mine.get_global_rotd()
var their_ang = cnct_joints.theirs.normal_angle + cnct_joints.theirs.get_global_rotd()
my_ang -= 180 if abs(round(my_ang)) >= 180 else 0
their_ang -= 180 if abs(round(their_ang)) >= 180 else 0

var diff = my_ang - their_ang
var diff_sign = 1 if my_ang < their_ang else -1
diff -= 180 if abs(round(diff)) >= 180 else 0

set_global_rotd(get_global_rotd() + diff_sign * diff)
:bust_in_silhouette: Reply From: jarlowrey

I had to change the joint’s normal_angle to a normal vector. But this seems to be working! Only took a week or more to figure out…

var start = cnct_joints.mine.normal
var end = -cnct_joints.theirs.normal.rotated(cnct_joints.theirs.get_global_rot())
var ang = start.angle_to(end)