The online racing simulator
#1 - nikka
Insim collision/contact detection
Heya all!

I'm working on an insim app where I have to find a way to detect a collision or contact between cars. I assume this can be done by looking at mci packets and calculating distance between cars (using X and Y), but that will (for several reasons) never be 100% accurate (or at least thats what I think...).

I hate to re-invent the wheel, so have any of you done something like this already, and would like to share this with me?

Or would anyone of you like to help me out with this? Any ideas are more than welcome.

Or maybe even Scawen would be so nice to add a insim packet that notifies you when there is contact between two cars?

Many thanks,
nikka.
I did some work a while back doing a blindspot checker, which created an overlap of the cars around you. Of course it had to be a lot less accurate since it was really trying prevent collisions not detect them. I just dealt with a collision circle around the car rather than a rectangle, but even with this, the different sizes of different cars had effects on accuracy.

One thing i'm playing with is doing collision by looking at large changes in velocity and direction. This will probably not show small bumps, but should be a cheap and easy way to detect a vehicle that crashed. And once you have made that detection, you can check cars in its proximity so see if they were involved in the crash.
I think with X, Y need use Heading.

Description cars using 4 point (like rectangle) and check lie everything peak of rectangle first car into square second car and retrogradely.



- MCI get position of car relative to center (Xnc,Ync).
- you need have a peaks (Xn1,Yn1;Xn2,Yn2;Xn3,Yn3;Xn4,Yn4) relative to center.
- using trigonometry will help you get absolute coordinate of peaks.
- if one of top of one machine appertained in square another => collision
Attached images
collision.jpg
BTW, just for information: a simple way to detect if a car is flying is to calculate the vertical acceleration. If it is 9.5 or higher, then the car is flying. If there is no acceleration (afair, there are only position XYZ), then use z(t)-2z(t-1)+z(t-2).

a(t) = v(t)-v(t-1)
v(t) = z(t)-z(t-1) => a(t) = z(t)-2z(t-1)+z(t-2)
if point(xa,ya) in square area then true or else false


function vectors(x1,y1,x2,y2,x3,y3,x4,y4, xa,ya)

local s = math.abs(1/2*((x1-x3)*(y2-y3)-(x2-x3)*(y1-y3))) + math.abs(1/2*((x3-x1)*(y4-y1)-(x4-x1)*(y3-y1)))

local sa = math.abs(1/2*((x1-xa)*(y2-ya)-(x2-xa)*(y1-ya))) + math.abs(1/2*((x2-xa)*(y3-ya)-(x3-xa)*(y2-ya))) + math.abs(1/2*((x3-xa)*(y4-ya)-(x4-xa)*(y3-ya))) + math.abs(1/2*((x4-xa)*(y1-ya)-(x1-xa)*(y4-ya)))

if (s ~= sa) then
return false
elseif (s == sa) then
return true
end
end

#6 - nikka
Thanks A.Fedorov, that was very helpfull indeed!

But there is one big problems with using the "rectangle-overlap-detection" method only, and that is the rate of the MCI packets. With shortest delay (50 ms) and 32 cars on track (4 packets between each cars MCI), you get a 200 ms interval between one cars MCI... so when cars are moving fast, a lot of things can happen in 200 ms that this method wont detect.

Also there could in theory be up to 100 ms between two close cars' MCI packets, so even though you detect overlapping rectangles, it doesn'h have to mean contact, it's just one car chasing another at high speed.

(and, but not that important, car sizes are not constant, a crashed car for example could be shorter than a repaired one)

So in addition to this method I guess I have to look at change in velocity and direction (as sdether mentioned), and maybe use that when cars are moving fast, and rectangle-overlapping for slow moving cars?

But thanks for pointing me in the right direction, more ideas and thoughts are more than welcome
maybe the LFS devs should add a packet IS_COL or something, whenever the LFS knows its made contact, it says info

unique_id - obvious
conn_num - obvious
power - how hard it hit
object - what it hit (wall, fence, object, car)
#8 - nikka
Quote from A.Fedorov :if point(xa,ya) in square area then true or else false


function vectors(x1,y1,x2,y2,x3,y3,x4,y4, xa,ya)

local s = math.abs(1/2*((x1-x3)*(y2-y3)-(x2-x3)*(y1-y3))) + math.abs(1/2*((x3-x1)*(y4-y1)-(x4-x1)*(y3-y1)))

local sa = math.abs(1/2*((x1-xa)*(y2-ya)-(x2-xa)*(y1-ya))) + math.abs(1/2*((x2-xa)*(y3-ya)-(x3-xa)*(y2-ya))) + math.abs(1/2*((x3-xa)*(y4-ya)-(x4-xa)*(y3-ya))) + math.abs(1/2*((x4-xa)*(y1-ya)-(x1-xa)*(y4-ya)))

if (s ~= sa) then
return false
elseif (s == sa) then
return true
end
end


Just FYI: This algorithm didn't work for me at all, dunno why.
But I searched the net and found this algorithm.. (look at "Code Sample"). The "polygon" in my case is simply a rectangle (the car).

This seems to be spot on... tested with the UF1, and it's close to 90% perfect, the reason why it's not 100% is because it's hard to find the excact length and width of the car, and where the "centre" x,y of the car (reported by mci) is. I assumed it was in the middle of the car, but not sure if that's right

Anyways, for my app this is good enough. I was looking for a way to detect contact between UF1 at relative slow speeds, and that's what I got. So I'm happy.. for now

But does anyone know the excact length and width of all or some cars? And where the centre point of the cars are located? I've only figured out the UF1, and the length seems to be 2,84 meters and width 1,47 meters, but I doubt these are the excact figures.
Quote from Krammeh :maybe the LFS devs should add a packet IS_COL or something, whenever the LFS knows its made contact, it says info

unique_id - obvious
conn_num - obvious
power - how hard it hit
object - what it hit (wall, fence, object, car)

+1 This would be really useful!
Quote from nikka :Just FYI: This algorithm didn't work for me at all, dunno why.
But I searched the net and found this algorithm.. (look at "Code Sample"). The "polygon" in my case is simply a rectangle (the car).

I use it algorithm into last script.
Quote :
This seems to be spot on... tested with the UF1, and it's close to 90% perfect, the reason why it's not 100% is because it's hard to find the excact length and width of the car, and where the "centre" x,y of the car (reported by mci) is. I assumed it was in the middle of the car, but not sure if that's right

Never mind where center. It need for know peaks. We may get coords item of Layout. Put cone (you know coords it) and move car each of side. I think if use this method may be get more or less peaks of car.
I think it should just be built into the insim protocol.

Having a packet, saying who hit what, and at what speed/strength.

that'd be fun to mess about with!

FGED GREDG RDFGDR GSFDG