The online racing simulator
InSim: detecting closest players!
Hi guys, im looking to make myself a client side program and i need a little help , i want to make it so i can find out on track who is closest to me im not botherd about contact etc etc i just wanna know how close they are. I had a little play and couldnt figure it out! Cheers guys!
in the IS_MCI packets you will receive the X and Y coords of all cars. You need to set the ISF_MCI flag in the IS_ISI packet and set the interval, then LFS will send you the IS_MCI packets.
Yes i know that and i know all about the MCI but im saying how do i calcuate the nearest car against mine.

E: ill also add im not sure weather to use Nodes or X,Y because if i use X,Y i will be getting the closest player to me "as the crow flys" therefor it could be incorrect?
I dunno how the other guys have done this, but if you know the X and Y position of two points, then it's pretty easy to figure out how far apart they are. It's been a while since I did anything like this, but I think this should do it:


double GetDistance(int aX, int aY, int bX, int bY)
{
int xDistance = bX - aX;
int yDistance = bY - aY;

return SquareRoot(xDistance * xDistance + yDistance * yDistance);
}

aX and aY being the X and Y positions of car A, with bX and bY being the X and Y positions of car B. It returns the distance in radians.

There's probably a much more elegant way of doing it though, but it seems to work for me. If you can work out the distance all you need to do is loop through each car and find which one has the shortest distance to yours, which should be very easy.
The way I detect this - is the following (iirc)

(keep a log of all positions - possible array)

Loop through connections array, if x+y difference is less than Z, add btn, if not, remove BTN :P
Ok gonna have a play and then goto some full servers and see what occours.

@ Darktimes: thanks for your help

@ Krammeh: You too cheers

Quote from DarkTimes :I dunno how the other guys have done this, but if you know the X and Y position of two points, then it's pretty easy to figure out how far apart they are. It's been a while since I did anything like this, but I think this should do it in C#:


double GetDistance(System.Drawing.Point a, System.Drawing.Point b)
{
int xDistance = b.X - a.X;
int yDistance = b.Y - a.Y;

return System.Math.Sqrt(xDistance * xDistance + yDistance * yDistance);
}

That might be totally wrong, my mind has gone hazy, but it seems to work in the few tests I've done. There's probably a much more elegant way of doing it though. If you can work out the distance all you need to do is iterate through each car and find which one has the shortest distance to yours, which should be very easy.

Would that work for calculating radii too? I mean, if you want to receive a message when a car has breached a certain radius of your XY position. I was thinking Pythagoras' Theorem, but I can't think how that could be translated into code, and how to correctly work it...
#8 - Stuff
Umm, that is pythagorean theorem. a² + b² = c² or c = sqrt(a * a + b * b). Just calculates the distance, or hypotenuse, between two points. If you always want a positive value, don't forget to take the absolute value of that. In pseudo code (maybe C/C++), something like:

int distance = abs(sqrt(a² + b²));
if (distance < MIN_DISTANCE)
send.msg("Too close! Go away");

Quote from dougie-lampkin :Pythagoras' Theorem, but I can't think how that could be translated into code, and how to correctly work it...

The C# that Darktimes posted is pythagorus.

Edit: That'll teach me to not-refresh.

Quote from Stuff :If you always want a positive value, don't forget to take the absolute value of that.

Errr... When you square a number it's always positive.. There's no need to abs() it.
Quote from Stuff :Umm, that is pythagorean theorem. a² + b² = c² or c = sqrt(a * a + b * b). Just calculates the distance, or hypotenuse, between two points. If you always want a positive value, don't forget to take the absolute value of that. In pseudo code (maybe C/C++), something like:

int distance = abs(sqrt(a² + b²));
if (distance < MIN_DISTANCE)
send.msg("Too close! Go away");


Yes, buts that the thing. I ain't good at maths (My teacher seemingly drilled into us that Pythagoras' Theorem had something to do with radii...). I'm wondering how I can get A and B...

I have 5 points to begin with (Centre of circle, 12 o clock, 3 o clock, 6 o clock and 9 o clock). But then how can i get A and B from this?

Please explain slowly, I'm struggling with this!


Quote from the_angry_angel :The C# that Darktimes posted is pythagorus.

Ah...so could I use that for radii?
Quote from dougie-lampkin :Ah...so could I use that for radii?

Yes, simply take the output value from the pythag. and compare it to the value of the radius you want them within (just as Stuff/RayOK says).

Don't forget that if you want it in metres, you'll have to convert things from the units that LFS uses.
Quote from the_angry_angel :Yes, simply take the output value from the pythag. and compare it to the value of the radius you want them within (just as Stuff/RayOK says).

Don't forget that if you want it in metres, you'll have to convert things from the units that LFS uses.

Sorry if appear slow at this, but what's A and B?
A would be the difference between the last known X coordinates of each vehicle, and B would be the difference in the last known Y coordinates of each vehicle.
Ah OK, thanks
Yay i done it, Cheers for all the help
Hello,

Yes but with this method, you can't say if car are left, right, before or behind you. For this, you must do a rotation of each coordonate car to Know relatie position from your car to another.

Quote :
# CVHAngRad = Heading of your car in insim packet
# CVX = Your car X Coordonat
# CVY = Your car Y Coordonat
# currMci.x = Other car X coord
# currMci.y = Other car Y coord
# x = distance between your car ( you car is a zero axis now, x = 0, y = 0 )
# y = Distance between your car
# z = Distance between your car z axis
# x1 = Rotation for the relative position to your car
# y1 = Rotation for the relative position to your car
# Now x1 and y1 relative to a zero point ( your car ) positive of negative number determine the position relative to your car in X axis and Y axis

CVcos = (long)((Math.Cos(CVHAngRad)) * 65536);
CVsin = (long)((Math.Sin(CVHAngRad)) * 65536);

int x = currMci.x - CVX;
int y = currMci.y - CVY;
int z = currMci.z - CVZ;
// Make a rotation to put on axis y
long x1 = (x * CVcos - y * CVsin) / 65536;
long y1 = (x * CVsin + y * CVcos) / 65536;


Look at source (main.cs) from LFSRelax to know how do it.

Gai-Luron
:jawdrop: I wish I was good at maths...
Quote from dougie-lampkin ::jawdrop: I wish I was good at maths...

Its not acutally that hard if you look closely and read it and make sure you understand it bit by bit
I understand it, but I don't think I could code that!
Hello,

It's no so difficult, test collision on "pit Spotter" in LFSRelax take only 100 lines code ( little part of LFSRelax ). Look at source, i use only this mathematicel formula for rotation

x' = x cos T - y sin T
y' = x sin T + y cos T

Where:
  • T is the angle for the rotation. positive or negative
  • x,y coordonate of point to rotate.
  • x',y' coordonat of new point.
#21 - sun
Detecting the nearest people to you
Hi, i tried looking for Seans post about this but couldnt find it any where...

I'am trying to detect the nearest people to you, is this possiable ?

if any one can help, please post.
Yes you can do this. But what would you define as someone close to you? Someone in the same split as you, someone in the same turn or straight, how about some one within 10 meters of you?.

All of these definitions have there own challenges. Let's start with the hardest, or just the most time consumeing first. Someone in the same turn or stright as you, for this one you would have to make a polygon and find if two cars are then in that same area you would also have to define each straight and turn for each track. That's alot of turns and strights.
With the split you would have to define an area as split1, split2, split3 and so on, while that's not so bad over all, it would also be unique for each track, atlest for the most part, some tracks do share split sections, so you could use that and define it across tracks. You could also do the same for the turns and straights. While that would save space overall, in memory, it would be a real bitch to program for.
Now the last one is within X meters from one user. For this you would have to find if one user is infringing on another user's area. You would have to do this with a circle around the point for the car, so that could be a bit of an issue. But over all this would be the easiest to construct.
#24 - sun
lol i looked through the whole lfs programmer forum, lol
Merged.

FGED GREDG RDFGDR GSFDG