0
\$\begingroup\$

I have such question:

I have a car, her coordinates (x, y, z) and z-angle (from 0 to 360 degrees).

And i have x, y, z of checkpoint.

how to calculate the angle to which need to turn the car to get to the point, taking into account its direction?

something like that:

enter image description here

Sorry for my English :D

\$\endgroup\$

1 Answer 1

0
\$\begingroup\$

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

  1. We calculate the different
    route.x = target.x - car.x; (= 45)
    route.y = target.y - car.x; (= 25)

  2. Get y when x = 1
    Y_When_X_Is_1 = route.y / route.x;

  3. Calculate z
    z = arcTan(Y_When_X_Is_1);(~ 29.055)

enter image description here

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!

\$\endgroup\$
1
  • \$\begingroup\$ You can simplify some of your case-by-case logic by using the 2-argument arctangent, Atan2(y, x) \$\endgroup\$
    – DMGregory
    Commented Aug 27, 2018 at 11:51

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .