Calculate car angle on the route

  • Thread starter hladon
  • Start date
  • Tags Tags
    Angle Car
2 replies · 3K views · last reply
hladon
Messages
1
Reaction score
0
I am writing program which displays car movement through recorded track(2D scene).
So I have a set of points (x1, y1), (x2, y2), (x3, y3), ...(xk, yk), from which track consits.
And I need to calculate an angle between car movement vector and OX(or OY) axis so I could then rotate car image on correct angle.


Diagram of a car travelling along a curved black route, with green car images at points and light blue rotated car outlines aligned to the route, with alpha angles marked against the route direction.
I tried to solve this problem by calculating angle as follows:

alpha = arccos( Yk / sqrt( x^2 + Yk^2 ) );

But looks like it's not the perfect solution, because if there is too much points on the route then car image starts dancing through the route:)

I also tried to compute angle between surely different points but the quantity of route points on the square inch can be changed dynamically in my program so if there's too much points, car is rotated really awful :(


Any ideas?
Thanks in advance,
Alex
 
Last edited:
hladon said:
alpha = arccos( Yk / sqrt( x^2 + Yk^2 ) )
How did you arrive at this equation?
Show your coordinate reference on your drawing.
Have you computed the angles manually? Do they match your expectations? Please show these results.

But looks like it's not the perfect solution, because if there is too much points on the route then car image starts dancing through the route:)
Not very descriptive. Are you saying it works fine with fewer points? How many?
 
Hi hladon! Welcome to PF! :smile:

arccos won't do everything you want it to do.

To calculate alpha properly, you need the atan2 function, which is defined in the various computer languages.
There are 2 variants of it: the atan2(x,y) function and the atan2(y,x) function.

Since you didn't specify which computer language you use, you'll have to find out which variant you have.

To calculate the proper angle, you'll need something like:

alpha = atan2(x2 - x1, y2 - y1)

That should stop the dancing! :wink:


Cheers! :smile: