Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Race Car AI

Discussion in 'Made With Unity' started by ikiman, Jul 27, 2008.

  1. ikiman

    ikiman

    Joined:
    Feb 19, 2008
    Posts:
    117
    This (as my other demos) is part of a much larger project involving car racing simulation.

    The project whose name is at the moment under discussion, involves the creation of a "Real Web Drive Simulator" :wink: with two different approaches, one more arcade like for simple fun, and another more simulator like, and all based in web browser play.

    The main goal is to create a growing catalog of pre made tracks, cars and gameplays, and an appplication to setup and import of gamer made cars.

    State of the Art:

    Engine to use - of course Unity (the Best).
    Car physics - under control.
    Opponents AI - under development.
    Web portal - under development.
    Gameplay, scenery, tracks, etc. - under discussion.

    Not scheduled:

    Multiplayer gaming.
    Server technology, for Hall of Fame and others.

    What I present today is an atempt of developing car AI based in the Chaos Theory that means, no pre-established conditions but initial position and same model with the same script for all cars.

    For the moment it lacks some things like direction recognition and reposition if after a big collision a car leaves the track (working now on it), so eventually you can find cars driving in opposit direction.

    For the moment all I need is test stability and performance, the demo have 5 cars, one you can drive or let the autopilot do, and four opponents. I tested with 15 cars and it works fine, I want to make testing with large amount of cars maybe 50 to 100 to see what happen.

    Although it is not perfect, I think it can give a lot of fun and still making refinements.

    The good news is that it is track independent, and only 20 lines of code.

    I know it can seem kind of obvious for some programmers, but I'm very excited and think I'm in the rigth way.

    So, please give it a try and tell me what you think. Thanks.

    http://www.3dnemo.com/AICars

    CONTROLS:
    Arrows: Drive
    Space: Autopilot On/Off
    R: Restart race
     

    Attached Files:

  2. nm8shun

    nm8shun

    Joined:
    Jul 14, 2007
    Posts:
    476
    Very cool. Very interesting. A little jerky, but certainly working....

    What are your long term plans for this? Track-independence sounds very enticing. People could plug it into their own race games.

    Do you plan to license it? Distribute it on the Wiki?
     
  3. maulongoni

    maulongoni

    Joined:
    Apr 8, 2008
    Posts:
    40
    Hi man,

    Very very nice, I know your post is about the IA system, but i must say that the car physiscs is very impressive, are you using wheel colliders from unity and wheel colliders functions to move the cars?
     
  4. MentalFish

    MentalFish

    Joined:
    Nov 2, 2005
    Posts:
    282
    That is really nice kart physics and only 20 lines of code? All included? Physics and AI? Hmmm... skeptical :)
     
  5. Ayce

    Ayce

    Joined:
    Jun 8, 2008
    Posts:
    101
    nice. I'll have to mention this to a kart racing bud of mine. :D
     
  6. ikiman

    ikiman

    Joined:
    Feb 19, 2008
    Posts:
    117
    • wadamw wrote;

      What are your long term plans for this?

    The idea is to create a base game with tracks and cars added periodically (maybe sponsored by commercial brands) and a MOD package with a Car Editor in the first step.

    About the track independence, it means you can design your own tracks and run on it, and as far as the AI is included in the Car Script you don't need nothing more than the track model made with one simple guideline, you must only have the side colliding meshes, the Car AI adjust automatically motor, brake and steer. But we must make a lot of test before thinking in licensing it, no plans by the moment for this, but, Who knows?.


    • maulongoni wrote:

      ... are you using wheel colliders from unity and wheel colliders functions to move the cars?

    Yes, all the physic behavior of the cars is WheelCollider based.

    • PetterSundnes wrote:

      ... and only 20 lines of code? All included? Physics and AI?

    Not, the 20 lines of code, refers only to the AI.

    • Ayce wrote:

      nice. I'll have to mention this to a kart racing bud of mine.

    Mention to as many people as you can, the Kart theme is only part of the project, the system now works on any kind of three or four wheeled vehicle, and now I'm working in things like Drag Racing and others.

    Examples here:
    http://forum.unity3d.com/viewtopic.php?t=12936
    http://forum.unity3d.com/viewtopic.php?t=12740

    Thank you all for your responses.
     
  7. kinl

    kinl

    Joined:
    Jun 1, 2008
    Posts:
    355
    Great great work.... :)
     
  8. Der Dude

    Der Dude

    Joined:
    Aug 7, 2006
    Posts:
    213
    How about a little Fuzzy-Logic in there? That would help keep the steering smooth.

    Also think about how you are going to let the AI make mistakes.
    The AI is ideally challenging but not unbeatable.

    Great Work!
     
  9. crockett

    crockett

    Joined:
    Apr 11, 2008
    Posts:
    264
    Nice job. I had a little fun playing with it and it will be nice to see it once you polish it off a bit. I may even be interrested in a license if it's reasonable.

    The car AI seems a bit too jerky and not as smooth as they should be IMO, but it reminds me of my days playing sega racing games.

    btw I figured I'd let you know if you didn't know already, using a joystick works with your game. Only issue is the throttle requires you to push forward on the joystick.

    It would be interesting to know if a after market steering wheel would work.

    btw something else to think about, would be giving a option to look right and left so you can get a little more driver situational awareness.
     
  10. Aubrey-Falconer

    Aubrey-Falconer

    Joined:
    Feb 13, 2008
    Posts:
    438
    It isn't that hard to beat the AI, you just have to take the smoothest possible inside path on all corners to shorten your route.

    Sorry Der, but "fuzzy logic" is a pet peeve of mine. :) The AI could use a bit of control dampening logic, but imho, there isn't anything remotely fuzzy about precisely dampening controls - and even if you added some random inputs to the logic somewhere, it wouldn't make it "fuzzy" - it would just make it less precise.

    I am yet to see an example of fuzzy logic that can't be reduced to selecting a defined logical item from several defined logical "bins" that had defined logical criteria for entry. What is "fuzzy" about that? How is that any different from "non fuzzy" logic?



    Oh... Sorry to hijack the thread :)

    Nice work Ikiman!
     
  11. crockett

    crockett

    Joined:
    Apr 11, 2008
    Posts:
    264
    Yea that's why I say he needs to smooth out the AI somehow but of course they still need to make a mistake or two.

    If they are smoothed out they won't make so many adjustments and will make them faster and harder to beat.
     
  12. Der Dude

    Der Dude

    Joined:
    Aug 7, 2006
    Posts:
    213
    I'll give you an example how he could use fuzzy logic here.

    Normal logic.

    When obstacle in front of cart closer than 20m steer right (or left). 1 or 0.

    Fuzzy logic:

    When obstacle in front of cart closer than 20m steer right (or left). How much to steer depends on the distance to the obstacle. <3m max steer (1), >=20 minimum steer (0). Interpolate steer value linearly.

    This is fuzzy beacuse it is not a question of 1 or 0, but every value in between also.
     
  13. ikiman

    ikiman

    Joined:
    Feb 19, 2008
    Posts:
    117
    • Der Dude wrote:
    • Normal logic.

      When obstacle in front of cart closer than 20m steer right (or left). 1 or 0.

      Fuzzy logic:

      When obstacle in front of cart closer than 20m steer right (or left). How much to steer depends on the distance to the obstacle. <3m max steer (1), >=20 minimum steer (0). Interpolate steer value linearly.

    Actually now I'm working and finishing that kind of Fuzzy logic not only testing distance to the track sides but also to car opponents. In addition it will have automatic recognition of "wrong direction" and recover, and realtime detection of relative position to the other cars.

    I think I can have a demo runing in a couple of weeks.

    Next step is a system of self learning for computer drived cars to better their driving skills. I have all in my head, but only two hands.

    Thank you all for your posts.
     
  14. llavigne

    llavigne

    Joined:
    Dec 27, 2007
    Posts:
    977
    Very nice, it seems the car anticipate curves, is it by design or a lucky artifact ?
    Are you using a couple raycast whiskers ?
     
  15. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    That's not fuzzy logic though...the logic is still completely precise and formulaic, and whether the result is a 1, 0, or .32774271 is irrelevant. You're just talking about analog vs. digital, which is quite a different thing. Fuzzy logic would be more like "if an obstacle is ahead, steer either right or left", except "how far ahead" would not be precisely defined and might be different depending on the circumstances (weather, light conditions, etc.), and "left" and "right" would similarly not be precise formulas but could change. "Fuzzy" in fuzzy logic refers to the logic, not the results. Just to clear things up. :)

    --Eric
     
  16. Charles Hinshaw

    Charles Hinshaw

    Joined:
    Feb 6, 2008
    Posts:
    1,070
    Since in the context of games we are almost always talking about fuzzy logic in the narrow sense, we are really talking about fuzzy sets. An example might be a state machine. In a simple finite state machine your states might define various modes for your AI driver and the driver is always in exactly one state. In a fuzzy state machine your driver would always be in all states and you could assign a grade of membership to each.

    </thread hijack>

    ikiman - this is interesting so far. Have you referenced existing sources on racecar AI? If I recall, AIGPW 2 has several good articles on AI for drivers. There is also currently a short thread on pursuit and evasion for racecar AI on the aigamedev forums that talks a little about the subject.
     
  17. AngryAnt

    AngryAnt

    Keyboard Operator

    Joined:
    Oct 25, 2005
    Posts:
    3,045
    One thought on optimisation:

    I haven't done any car AI before, but it looks like your input for the car AI unit is the distances between obstacles and opponents. Rather than spending a lot of time optimising this relationship, I imagine this would be a shortcut:

    Say one or more of the cars are player controlled. This would mean that the AI cars locations on the track would not be the same for each run as they'd have to adjust for the player factor. This means you'll need to do a lot of tuning of the relationship between track, self location and the location of the other cars.

    In stead of spending a lot of time on this, then why not focus on the one constant in the game, define behaviour from that and then adjust to the other cars? I'm thinking put some steering behaviour *in* the track - the ideal route around the track, including speed direction, breaks etc. for each N interval of the track.

    That way your scenario would be perfect driving for the AI, without all the jerkyness, when no other cars are on the track. And when other cars are introduced, the AI "just" needs to steer to avoid other cars, whilst approximating the optimal driving conditions for that track interval.

    I believe this could generate a very nice behaviour, but it would ofcourse generate more work in track creation, as the optimal driving data or "curve" needs to be added as well to track meta data.