The online racing simulator
Theory needed
(5 posts, started )
Theory needed
Hi, i need the theory or math calculations for a insim im currently doing...
So, the situation:
I'm in black car, i write "!check" or something, that doesnt matter... and now i want to know the first car that is in the line in front of my car,but just the first. Here is the drawing:


So blue yes, pink and green not. I need just the theory.
I hope you understand and im sorry for my english Thanks in advance.
So the program will "return" the car that is nearer to the red line, or the car that is more in front of you than the others right? if i knew how the coordinate system works for lfs i could help with the theory..
#3 - Stuff
Well, if the black car is at x,y and you're facing down the positive y-axis (like the picture) and using LFS units (1m = 65536) the code that first comes to mind is something like:


static const int MAX = 20;
static const int SCALE = 65536;

Car findForwardCar(x, y) {
Car forward = 0;

int distance = 0, xTemp = x, yTemp = y;
while (distance <= MAX) {
++distance; //increment distance
yTemp += (distance * SCALE); //increment y with real distance
for_each(car in Cars) {
if (pointInRect(xTemp, yTemp, car.rect) {
forward = car;
break; //exit for loop
}
}
}

return forward;
}

Hopefully that makes sense It just increments 1 meter at a time until the new point is within the rectangle of a car. Once found its returned. Of course, this example assumes you facing directly down that positive y-axis. If not you'll need the angle and have to use some trig to find the proper point.
You do not have pitch or roll attributes in InSim so all your calculations will have to assume the car is on all four wheels. An upside down car or on a 45 degree tilt will be the same as being flat.

However you still have car heading direction, X,Y,Z co-ords and also you have an approximate car length and width. You should use the car's heading, not the car's heading of travel which is easy to confuse the two.



Use this diagram:


Use the car's X,Y,Z co-ordinate and direction of heading to figure out the four verticies of each corner of the vehicle. The car's X,Y,Z co-ordinate is the approximate centre of mass.

Giving points A,B,C & D

Shift A & B using the direction of heading axis by an equal distance to give points A & B
(use sine sin/cos/tan trig AND car heading to figure out the new A & B points)

Now you have a really long pink rectangle A,B,C & D. Use an intersecting rectangle formula to figure out if the pink rectangle intersects each of the other red rectangles individually. (You must pre-calculate each other car's vertices first)
Here is a thread to working out intersecting rectangles.

Once you find which red rectangles are intersecting the pink one, use pythag to calculate which is the closest red one to the blue car to see which one is in-line of the blue car "first".

In the cases where hairpins are going to give incorrect results, use the cars nodes to figure out which are closest in relation to the track terrain. Keep in mind that a spinning car would give results of cars behind it's position as in-line. So you must keep an eye on that event.

I can't help more now, I need to go. But the theory should be apparent.


edit: ah well someone else posted a solution. I thought of that first, but wrote up this method because I thought that incremental method might take too long to scan 50 meters or 100 meters, it would take 100 calculations cycles for 100 meters for example. Probably wouldn't make much difference with todays CPU's. But another reason why I didn't opt for that is because the 1 meter points might skip over the 'car in front's corner, but if you're scanning fast enough, then you would catch it in another cycle.
Attached images
rects.png
Thanks for answers, i will try both methods.

Theory needed
(5 posts, started )
FGED GREDG RDFGDR GSFDG