If you draw a line between point b
the target point and the x-axis you can see we always have a Triangle. When you now draw a circle which meets the new 90° corner of the triangle. The idea comes to use tan and arctan.
Is y (25) = tan(z)
so z = arcTan(y)
The last Problem is that we need y when x = 1. We achieve this by dividing y and x: y / x
;
So when our car is on position x = 15, y = 50 and his target is on x = 60 and y = 75
We calculate the different
route.x = target.x - car.x; (= 45)
route.y = target.y - car.x; (= 25)
Get y when x = 1
Y_When_X_Is_1 = route.y / route.x;
Calculate z
z = arcTan(Y_When_X_Is_1);(~ 29.055)
Now you must watch of some special cases:
if route.y < 0 then z = 360 - z
if route.x < 0 then z = 180 - z
if route.x < 0 && route.y < 0 then z += 180
I hope this will help you!