Hello. I was wondering hwo to make something like vector in LFS? I want to make gps for my cruise InSim That shows the direction to one place. I was thought I could use Heading or X and Y to make one but I have no idea how. Hope you can help.
Thanks
The problem you're trying to solve is not really a programming problem but a mathematical one. It's usually much better to solve these problems with a pencil and a piece of paper before you start writing any code. Do you know what Cartesian coordinates system is? Do you know how some basic trigonometry works there? If you don't, you've got to do some reading on that first. If you know this, your problem is actually quite simple.
You're trying to find an angle between two vectors. Vector V is a vector between the car and the destination, vector U is the motion vector of the car (=a vector which shows which way is the car moving (and how fast, but that is not necessary to know in this case)).
Getting V is easy: V = B - A = (B.x - A.x; B.y - A.y)
Since you only need the heading of the car, not the motion vector itself, you can use car's heading from MCI packet.
Now you have to:
Use atan2(V.y, V.x) to get Alpha (in radians and in <-PI; PI) range)
Convert Alpha to degrees and to <0; 360) range
Use (Heading + 90) to get Beta which is car's heading in degrees. Heading is 0 if the car is moving North (along the Y-axis) which is why you need to add 90 (and subtract 360 if the result is greater or equals to 360)
R = Alpha - Beta. R > 0 means the destination is to the left of the car and vice versa. Also, if R > 180, R = R - 360 and if R < -180, R = R + 360
A modified Atan2 which returns angle in <0; 2*PI) range can look like this
Thank you very much. Veru nice job! Could you please explain how to get V.y and V.x ?
From your answer I understood that V is heading which I can take from MCI packet (or I`m wrong? ) Does Heading splits definition for Heading.X and Heading.Y ?
QFT
However, dot product is not the best solution in this case because you also want to know whether vector V is to the left or right of vector U. Atan2 does provide such information, dot product is always positive.
- You cannot convert Atan2 result to <0; 360) range by adding 180 the result. Google what Atan2 actually does and you'll see why...
- You're not converting LFS heading to "mathematical" angle correctly.
Seriously, if you have no idea about trigonometry, you'll probably have to suffer through some maths textbooks first... it's all in there.
Tried something like this, but all I get is minus values. Im not sure about that vector V. You wrote
What is B.x and A.x ? Where is the mistake in this?
if (Conn.Username == "jobans") { double Vx = (-211 - Conn.CompCar.X); // -211 Destination X double Vy = (-90 - Conn.CompCar.Y); // -90 Destination Y double Alpha = Math.Atan2(Vy, Vx);
Something tells me that you don't have the right coordinates. Coordinates used by various InSim apps do not correspond to coordinates reported by LFS (can't remember the conversion off the top of my head).
Atan2() returns angle in radians, but LFS reports heading in degrees. You have to use the same units.
You are missing the second part of Step 4. Without it the algorithm doesn't produce correct results if the target is behind the car.