The online racing simulator
Direction/Vectors.
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
Hello,

Try to read [iC] Inline Cruise gps code and understand it
Thanks, but it works only on distance. I want something like a panel which shows direction of that place. Like this

X X X
X .. >
X X X
Maybe this awesome pic help you

calculate difference between angle of heading and (current to desired destination) angle
Attached images
Untitled.png
Nice pic
How can I calculate angle of destination?
... part of old code... maybe it works
fi =atan2( car[i].position.y-destination.y, car[i].position.x-destination.x )*180/3.1415+180;

also note that heading can have different angles than 0°.
what is atan2?
Well I did this
double test = Math.Atan2(Conn.CompCar.Y - (-90), Conn.CompCar.X - (-211)) * 180 / 3.1415 + 180;

Not sure what does it give to me and no idea what to do next.
that -90 and -211 is corrdinate to a place which I want it to show. Can you please explain what does this do? And what to do next? Waiting for help :/

Sorry for double post.
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:
  1. Use atan2(V.y, V.x) to get Alpha (in radians and in <-PI; PI) range)
  2. Convert Alpha to degrees and to <0; 360) range
  3. 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)
  4. 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

<?php 
static double Atan2Positive(double ydouble x)
   { 
     
double angle Math.Atan2(yx);
     if (
0)
     {
       return 
Math.PI angle;
     }
     else
     {
       return 
angle;
     }
   }
?>

Thank you very much. Veru nice job! Could you please explain how to get V.y and V.x ?
Quote :Use atan2(V.y, V.x) to get Alpha (in radians and in <-PI/2; PI/2) range)

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 ?
I made something like this:
double test = Math.Atan2(Conn.CompCar.Y - (-90), Conn.CompCar.X - (-211)) * 180 / 3.1415 + 180; // -90 = Destination Y and -211 = Destination X

double test2 = Conn.CompCar.Heading + 90;
InSim.Send_BTN_CreateButton("^1" + (test - test2), Flags.ButtonStyles.ISB_DARK, 4, 25, 40, 60, 51, C.UniqueID, 2, false);

Is anything of it right? Really confused about all this :/
Anyone?
Dot products! School IS useful in real life!!!
Quote from Stang70Fastback :Dot products! School IS useful in real life!!!

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.
And what about those calculations above? Cant get them to work right could you tell me whats wrong?
Still waiting :/
MadCatX please answer
- 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
Quote :Getting V is easy: V = B - A = (B.x - A.x; B.y - A.y)

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);

if (Vy < 0)
{
Alpha = 2 * Math.PI + Alpha;
}
else
{
Alpha = Alpha;
}

double Beta = Conn.CompCar.Heading + 90;
if (Beta >= 360)
{
Beta = Beta - 360;
}
double R = Alpha - Beta;



InSim.Send_BTN_CreateButton("^1" + R, Flags.ButtonStyles.ISB_DARK, 4, 25, 40, 60, 51, C.UniqueID, 2, false);
}

Thank you.
  1. 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).
  2. Atan2() returns angle in radians, but LFS reports heading in degrees. You have to use the same units.
  3. 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.
Other than that it looks fine to me...
Added this to convert Alpha to degrees
Alpha = Alpha * 3.14 / 180;

Results look the same, will check those coordinates tomorrow.
Thanks for help

EDIT: Results are about from -65 000 to 0.
You got it backwards, this formula is for conversion FROM degrees to radians.
This was right formula, wasnt it?
Alpha = Alpha * 180 / Math.PI;

Still get same results. I think I just need some sleep. Goodnight
1

FGED GREDG RDFGDR GSFDG