How to check if other car is left or right of me?

Discussion in 'Plugins' started by ReTin, Mar 12, 2015.

  1. ReTin

    ReTin Registered

    Joined:
    Mar 12, 2015
    Messages:
    5
    Likes Received:
    0
    Hi

    I'm working on a plugin that writes all kind of Data to a shared memory, to be used by other tools I'm working on.
    For example an Android Dash-App to display all kind of telemetry data while driving.

    Most things are fairly easy, but I have no clue how to determine if any other car is positioned left or right of my car on the Track.

    I've accomplished to check if a car is within a certain radius of my car, but how ca I know it's exact location based on in which direction my car is heading?

    I hope someone can give ma a hint on how to calculate this.



    Martin
     
  2. Lazza

    Lazza Registered

    Joined:
    Oct 5, 2010
    Messages:
    12,388
    Likes Received:
    6,602
    First, I'm sure there are very clever and concise ways to do this, if you search around enough with the right terms you might be able to find something, or maybe someone well versed in matrix manipulations can provide something.

    Putting aside the desire for cleverness :p the logical approach to me would be to:

    - get the heading from your car to the other car
    - get the heading of your own car
    - compare those two

    By heading, I of course mean a single value that represents direction around an axis, in this case the y-axis (vertical). Calculating a heading from an (x,z) offset/vector is actually easy, because you can use the built-in atan2() function. This takes (y,x) (sorry for the axis confusion) as arguments and returns the heading as a value between -pi and +pi (it works in radians; you can convert to degrees if it's easier).

    So, remembering what we're looking at here is (x,z) to ignore the vertical axis, something like this:

    - headingtocar = atan2(othercar.pos.z - mycar.pos.z, othercar.pos.x - mycar.pos.x)
    - myheading = atan2(mycar.localvel.z, mycar.localvel.x)
    - headingdiff = headingtocar - myheading

    Then you need to ensure your result is in the -pi to +pi range (or -180 to +180 if you've converted to degrees), and then tests for <0 and >0 should tell you what side they're on. Of course you would probably be better checking for 'wider' angles than that (close to zero, but also close to -pi or +pi); doesn't make sense for a car 5mm to your right but 10m away to be labelled 'on your right' :D

    You need to add some checks, for example you don't want to somehow end up calling atan2(0,0), but looking up the functions you use should highlight anything like that. And as I said above, I'm ignoring the vertical axis completely, plus your car's orientation. If you're upside down a car to your right is actually to your left, but personally I wouldn't worry about that :eek:

    Hope I'm not completely wrong, and that helps.

    *And immediately after posting I can see I've used mycar's velocity instead of orientation... so it won't work if you happen to be very sideways/spinning at the time. But hopefully it still helps lol
     
  3. smbrm

    smbrm Registered

    Joined:
    Nov 11, 2010
    Messages:
    440
    Likes Received:
    50
    I have always wondered why ISI, named the vertical axis y?
     
  4. Lazza

    Lazza Registered

    Joined:
    Oct 5, 2010
    Messages:
    12,388
    Likes Received:
    6,602
    That's a bit like asking why North isn't South :)

    Here's a single 'random' google reference... check the date...

    http://www.firebaugh.com/FDA/Courses/CS.120/Week.8/Lect16.www/Lect16.html

    Using z for height would make sense if you considered x and y to be traced out on the ground (top view). I think it makes some sense that x and y are drawn on a screen (as on paper), and z gives it depth. Apparently it makes sense to many people because that's what usually happens in computers at least.

    Ultimately all that matters, like choosing North and South, or Left and Right, is that you choose one and are consistent. But it's definitely not something ISI have done in isolation :)
     
  5. ReTin

    ReTin Registered

    Joined:
    Mar 12, 2015
    Messages:
    5
    Likes Received:
    0
    Thanks for the great input, but I just got the Idea, that there may be a much easier way to do this.

    Doing so not with car position and heading, but to check if a car is near me I can use the mLapDist (Distance around Track)
    And not sure about these values, but maybe I can use one of them to check if right or left of mycar:
    double mPathLateral; // lateral position with respect to *very approximate* "center" path
    double mTrackEdge; // track edge (w.r.t. "center" path) on same side of track as vehicle

    This would also be a lot better on performance side instead of doing all the calculations.


    Will give it a try when I get home today.
     
  6. argo0

    argo0 Registered

    Joined:
    Jan 22, 2012
    Messages:
    624
    Likes Received:
    8
    Doesn't Gerards brilliant spotter plugin have visual and aural indicators for this already?
     
  7. Lazza

    Lazza Registered

    Joined:
    Oct 5, 2010
    Messages:
    12,388
    Likes Received:
    6,602
    True, I didn't think of those. Chances are you wouldn't need to try and use telemetry on the remote cars anyway (5Hz scoring should be enough) so if they're reliable they'll do fine.

    I wouldn't be concerned about performance though... I know everything adds up, but a few atan2 calls won't add up to much.
     
  8. TechAde

    TechAde Registered

    Joined:
    Oct 13, 2010
    Messages:
    606
    Likes Received:
    38
    Because they use DirectX and +y = up in the DirectX coordinate system.
     

Share This Page