Search Unity

Dead Reckoning for racing games - What are some good ideas?

Discussion in 'Scripting' started by Rush3fan, Jan 18, 2012.

  1. Rush3fan

    Rush3fan

    Joined:
    Nov 2, 2011
    Posts:
    87
    Hi all,

    I'm finally at the stage where I get to implement dead reckoning in my game..

    I have tried a few things already, like projecting a vector in front of the car's trajectory and then mirroring it along the car's local x and y. Previously, I have tried graphing a catmull spline curve, but I have no idea how I should position the curve, or what to do with it, etc..

    I suppose there are not a lot of people who have experience in this, but if you have any good ideas, that would sure help.

    My goal is to smooth out a very fast moving vehicle, also projecting it slightly into the future.
     
  2. fholm

    fholm

    Joined:
    Aug 20, 2011
    Posts:
    2,052
    Are we talking networking or just in general?
     
  3. Rush3fan

    Rush3fan

    Joined:
    Nov 2, 2011
    Posts:
    87
    Networking.
     
  4. fholm

    fholm

    Joined:
    Aug 20, 2011
    Posts:
    2,052
    Forgive me for being thick, but isn't it just as simple as doing forward euler integration? As in, something like this:

    You receive a position (P) + velocity (V) for T0
    The position at T1 is (P+V*(T0-T1))
    And then correct the position as need be when you get updates

    I'm sure there are more fancy things you can do.
     
  5. Rush3fan

    Rush3fan

    Joined:
    Nov 2, 2011
    Posts:
    87
    Right.. but then I probably need to smooth it out. The car is after all traveling around 100+ MPH on average. Any slight lag is going to throw the car off by a few feet.
     
  6. fholm

    fholm

    Joined:
    Aug 20, 2011
    Posts:
    2,052
    Yes true, the smoothing out part can be done like this (and how you do most smoothing out when doing lag compensation).

    P = position
    E = estimated position
    V = velocity

    1. Receive P0 and V0 for T0
    2. Calculate E1 from (P0+V0*(T0-T1))
    3. Store E1 somewhere (array, local variable, etc.)
    4. Smooth from P0 to E1 over (T0-T1)
    5. When you receive P1 and V1 for T1 you smooth from E1 to P1 to correct your prediction
    6. Calculate E2 from (smooth(E1, P1) + V1 * (T1-T2))
    7. Start over from step 2

    And the smooth algorithm can be whatever you want, linear, cubic, spherical, etc. whatever looks best for your game.

    Edit: if this is stuff you already know, say so, so I can stop responding ;p
     
    Last edited: Jan 18, 2012
  7. Rush3fan

    Rush3fan

    Joined:
    Nov 2, 2011
    Posts:
    87
    The smoothing algorithm isn't just Vector3.Lerp right? How would you make a cubic smoothing algorithm?
     
  8. fholm

    fholm

    Joined:
    Aug 20, 2011
    Posts:
    2,052
    Well, that's just a normal cubic interpolation, for example a bi-cubic interpolation like this: http://www.paulinternet.nl/?page=bicubic
    Honestly though, for this stuff linear interpolation works pretty well in my experience.
     
  9. Rush3fan

    Rush3fan

    Joined:
    Nov 2, 2011
    Posts:
    87
    Thanks fholm! I'm going to try something like you were saying, as it seems to make a lot more sense than what I was trying before.. And I'll check out those linear and cubic formulas and see how they work.
     
  10. fholm

    fholm

    Joined:
    Aug 20, 2011
    Posts:
    2,052
    Glad I could be of help, I corrected my post with the steps outlined in it as I had said V0 where it was supposed to say V1.
     
  11. Rush3fan

    Rush3fan

    Joined:
    Nov 2, 2011
    Posts:
    87
    I think I got it now.. See, I never understood how to use a spline graph for smoothing algorithms. The link you gave me helped a lot. I now have a nice catmull-rom interpolate script working. Next, I'll be fine tuning the leading vector to make better predictions.
     
  12. zoultrex

    zoultrex

    Joined:
    Mar 11, 2010
    Posts:
    63
    Would you mind sharing(if you can) that script, the scritp that I am working on is too basic and results in movement chopping.