I’m trying to calculate the angle between two vectors and get it in degrees, and then rotate an object by this angle. Problem is that in some cases, I get an angle that is off by exactly 180 degrees. PyMEL has datatypes for Vectors and Matrices, and I would prefer to use these if possible instead of writing the formulas by hand in Python.
# Calculate vectors
vectorA = pm.datatypes.Vector(targetP0[0] - targetP1[0], targetP0[1] - targetP1[1], 0.0)
vectorB = pm.datatypes.Vector(sourceP1[0] - sourceP0[0], sourceP1[1] - sourceP0[1], 0.0)
# Returned unsigned angle in degrees
angle = math.degrees(vectorB.angle(vectorA))
# Calculate dot product
dot = (vectorB.x * vectorA.y) - (vectorB.y * vectorA.x)
if dot > 0: # Same
sign = 1
elif dot < 0: # Opposite
sign = -1
else: # Perpendicular
sign = 0
# Signed angle
angle = (angle * sign)
Gives correct rotation
targetP0: [0.674, 0.825]
targetP1: [0.557, 0.8463]
sourceP0: [0.514, 0.901]
sourceP1: [0.372, 0.901]
Does not give correct rotation (off by precisely 180 degrees)
targetP0: [0.514, 0.901]
targetP1: [0.372, 0.901]
sourceP0: [0.674, 0.825]
sourceP1: [0.557, 0.846]
It’s probably something stupid and obvious that I’m missing - but I don’t see what that would be.