Hi All,
I have two objects on the Z=0 plane. I want to calculate the heading between them, relative to the “forward()” direction (the Y-axis grows towards angle 0, and the X-axis grows towards the 90 degrees direction).
Based on @brian and @rdb help in the Discord channel I wrote a simple function using the angle_deg method.
The problem with the function (see below): The results when the target is to the left (Xp1>Xp2) are between 0 and 180 degrees, similar to the points on the right. I expect them to be negative, or between 180 and 360. What is the cause of this? I can always add another if
, but I want to understand whether I’m doing something wrong, or maybe missing a simple solution.
def heading(p1, p2):
zero_heading = Vec3(0, 0, 0).forward()
relative_vector = Vec3(p2 - p1).normalized()
return zero_heading.angle_deg(relative_vector)
for p in [(110, 10, 0), (90, 10, 0), (90, -10, 0), (110, -10, 0),
(100, 5, 0), (105, 0, 0), (95, 0, 0), (100, -5, 0), ]:
p1 = LPoint3f(100, 0, 0)
p2 = LPoint3f(p)
print(f"heading from (100, 0, 0) to {p}: {heading(p1, p2)} deg")
resulting in:
heading from (100, 0, 0) to (110, 10, 0): 45.0 deg
heading from (100, 0, 0) to (90, 10, 0): 45.0 deg # I expect -45 or 315
heading from (100, 0, 0) to (90, -10, 0): 135.0 deg # I expect -135 or 225
heading from (100, 0, 0) to (110, -10, 0): 135.0 deg
heading from (100, 0, 0) to (100, 5, 0): 0.0 deg
heading from (100, 0, 0) to (105, 0, 0): 90.0 deg
heading from (100, 0, 0) to (95, 0, 0): 90.0 deg # I expect -90 or 270
heading from (100, 0, 0) to (100, -5, 0): 180.0 deg
- What am I missing? Any simpler way to implement this?
- What is a common way to represent “left” angles - as negative or between 180 and 360?