The online racing simulator
X,Y Co-ordinates for a Square area.
Hi guys

Need a little help with a bit of coding. Im using C# in order to create a small insim application.

What i want to achieve is to create a sqaure on the track, in which if a car is in said square an event will happen.

Anyone got any ideas on how i can map a square big enough for say 4 UF1's to fit inside with X, Y Co-ordinates, instead of having thousands of lines of code?

Thanks
Dan.
You'll be best to use trial and error for the size of the square. Then simply do something like this (pseudocode):


if car has x,y greater than x1,y1 and less than x2,y2
you're in the square

#4 - nikka
Dunno how accurate you want it, but keep in mind that to get it 100% right you need to check if one of the corners of the car is inside the square, and for that you need to calculate yourself where the corners are (but thats just basic geometry actually (as soon as you have found the size of the car in question.. (and thats the tricky part here))).

X and Y in the MCI packet only tells you where the middle of the car is.
I thought the XY origin was the back left corner of the car?

What I would suggest doing is getting a UF1, and making a square on the track where you want it (to the size of a single UF1). Then reposition your car to be the next "quarter" of the square. And so on until you have all 4 quarters.

Then, you'll need to find the XY position of this square, by just making a simple insim app that sends a button with your XY co-ordinates. Then you can find the position of each corner

If it's not lining up on an axis, the next best thing is probably to use Pythagoras' theorem (see here) to get the center point of the square, and calculate how far away everyone is, then see if it's less than the distance from the center point to the edge of the square. If it is, they are in the square
Quote from dougie-lampkin :I thought the XY origin was the back left corner of the car?

Mind if i ask what planet your on today? That would make no sence what so ever. Its smack bang in the middle of the car so that they are more acurate.

---

One thing i reccomend when working with LFS X,Y,Z is converting them down to smaller values, Make them meters and they will be easier to work with.
Quote from mcgas001 :One thing i reccomend when working with LFS X,Y,Z is converting them down to smaller values, Make them meters and they will be easier to work with.

Indeed.


<?php 
# Original code by Brian J. Fox of MetaHTML.
# PHP version is not my code, that was thanks to Filur.
# I don't support this code, nor does filur, so don't ask.
function in_poly($x$y, array $polygon) {
    
$min_x = -1;
    
$max_x = -1;
    
$min_y = -1;
    
$max_y = -1;
    
$result 0;

    
$vertices count($polygon);

    foreach (
$polygon as $point) {
        if (
$min_x == -|| $point['x'] < $min_x)
            
$min_x $point['x'];
        if (
$min_y == -|| $point['y'] < $min_y)
            
$min_y $point['y'];
        if (
$point['x'] > $max_x)
            
$max_x $point['x'];
        if (
$point['y'] > $max_y)
            
$max_y $point['y'];
    }

    if (
$x $xmin_x || $x $max_x || $y $min_y || $y $max_y)
        return 
FALSE;

    
$lines_crossed 0;

    for (
$i 1$polygon[$i] != null$i++) {
        
$p1 =& $polygon[$i 1];
        
$p2 =& $polygon[$i];

        
$min_x min ($p1['x'], $p2['x']);
        
$max_x max ($p1['x'], $p2['x']);
        
$min_y min ($p1['y'], $p2['y']);
        
$max_y max ($p1['y'], $p2['y']);

        if (
$x $min_x || $x $max_x || $y $min_y || $y $max_y) {
            if (
$x $min_x && $y $min_y && $y $max_y)
                
$lines_crossed++;
            continue;
        }

        
$slope = ($p1['y'] - $p2['y']) / ($p1['x'] - $p2['x']);
        if (((
$y - ($p1['y'] - ($slope $p1['x']))) / $slope) >= $x)
            
$lines_crossed++;
    }

    if (
$lines_crossed 2)
        return 
TRUE;
    else
        return 
FALSE;
}
?>

Quote from dougie-lampkin :I thought the XY origin was the back left corner of the car?

If I recall correctly, the the centre of gravity of the vehicle is used.
Quote from Dygear :But can't that change ...

True, unless its assumed that it's the centre of gravity of an undeformed vehicle. Although that said, I don't see it being an issue if it takes deformities into account, as that would provide a fairly accurate central location.

To be fair I could be wrong. However, I'm sure that I saw it mentioned somewhere by one of the devs many moons ago
I thought it was to the fixed reference point of the car, from which all other measurements are taken? Which happens to be almost exactly at the unfueled, driverless CoG.
I have to say in the InSim app I've been working on I just presumed the XY positions were the exact centre of the car, and it's certainly been more than sufficient for my needs. That being said, I'm more concerned with the car movements looking smooth, so I might not notice subtle differences in the central position.
Quote from Bob Smith :I thought it was to the fixed reference point of the car, from which all other measurements are taken? Which happens to be almost exactly at the unfueled, driverless CoG.

Aaah. Now you say that, it sounds very familiar!

I do apologise

FGED GREDG RDFGDR GSFDG