PDA

View Full Version : AI for car game



{MSX}
10-12-2004, 11:07 AM
Hi! I'm currently implementing some basic CPU-controlled cars for my game.
It's a bit harder than i thought :P
What is the preferred way to implement something like a car that follows a road ?

My try was this: i've placed some points along the track, near curves etc, and i programmed the car so that it heads to the next point, and once reached go on with the next one.
This kind of work, but there are some good problems:

-If the car for some reason miss the point (ie it is pushed by another car), it tries to go back to that point before proceeding with the following.
-The CPU cars tend to align one after the other, and all follows the very same path
-Cars don't avoid obstacles.

I don't need a to have extremely good AI, but something working and easy to implement :P
Any suggestion ?


PS how the hell does that list tag work ?!

Traveler
10-12-2004, 11:38 AM
If the car for some reason miss the point (ie it is pushed by another car), it tries to go back to that point before proceeding with the following.
Perhaps you should not think in terms of points, but rather in lines.



The CPU cars tend to align one after the other, and all follows the very same path
This could be avoided by giving each car different setups for stearing, speed (in curves or longer parts of road), breaking, pull ups etc.
Also, but this may get a bit harder. You want your oponents to act like a real racer. ie each driver wants to be first. You can't win if you stay behind other cars, so they should attempt to get past other cars.

As for avoiding obstacles, a basic detection setup, like detecting if theres something blocking your path within a few car lengths ahead, move out of the way accordingly. (You can also use this procedure to detect if an other car is ahead of you and want to go past it.)

tux
10-12-2004, 12:33 PM
you could use ray casts for the obstacles detection, that would also work for detecting other cars around you (just shoot off a few raycasts in different directions)

{MSX}
10-12-2004, 02:11 PM
you could use ray casts for the obstacles detection, that would also work for detecting other cars around you (just shoot off a few raycasts in different directions)

Ok, that would be easy (since ODE already support this), but the difficoult part comes after.
If i detect the obstacle, what can i do ? It is impossible to do a nice "turn around". It should check whether the object is moving or not, choose to go left or right and the entity of the turning, choose how much staing in "surpass" and when reentering the "normal line". Also, some part of the tracks are thight, so you should check if moving away from the "normal line" will cause the car to bump on the border..
I don't think i'm able to do all this things.

savage
17-12-2004, 10:10 AM
Hi Nicola,
Basically what I put in the email were the following links...

Craig Reynolds ( Excellent site ) - http://www.red3d.com/cwr/steer/

Steering Behaviors For Autonomous Characters - http://www.red3d.com/cwr/papers/1999/gdc99steer.html

AI used in Colin McCrae - http://www.ai-junkie.com/misc/hannan/hannan.html

AI Madness: Using AI to Bring Open-City Racing to Life - http://www.gamasutra.com/features/20010124/adzima_01.htm

OpenSteer - http://opensteer.sourceforge.net/

The BEST place to start for this sort of thing is Craig Reynolds page and he is one of the original guys ( IIRC ) who formalised steering an flocking behaviour, in terms of algorithms. The Colin McCrae AI and AI Madness articles are also very interesting.

Lastly OpenSteer is an opensource implementation of steering behaviour, I have not looked into it, but may be they have a C interface that Pascal can interface to.

I hope you find this usefull.

savage
17-12-2004, 10:36 AM
Another couple of links which you may find interesting are...
Towards More Realistic Pathfinding - http://www.gamasutra.com/features/20010314/pinter_01.htm

Polygon Soup for the Programmers Soul: 3D Pathfinding - http://www.gamasutra.com/features/20020405/smith_01.htm

Actually every single game developer on the planet ( maybe even this solar system ) should be a member of the gamasutra ( http://www.gamasutra.com/ ) site

{MSX}
17-12-2004, 10:53 AM
Thanks! :P I've lot of free time at work today... i'll study that links :P

BTW Anybody tried the last version of funkycars? What about the current AI for CPU cars ?

{MSX}
17-12-2004, 12:21 PM
Uhm i've read Craig Reinolds paper. It's path following algorithm is very interesting.. Quite better than mine! :P It's also not too difficoult to implement.
I'll surely try it.

He also describe the obstacle avoidance algorithm, but i think it will be much more difficoult for me, since he uses "actors" that have much more freedom of steering than a car. He can add "side force", while i of course can not :P

Also, i've found interesting the use of a neural network for driving a car. Does this really works ? Anyone experienced with NN here ?

savage
19-12-2004, 08:29 PM
There is an article on NN over @
http://www.ai-junkie.com/ann/evolved/nnt1.html

and it also includes some Delphi code.

Hopefully that should get you started in NN

WILL
19-12-2004, 09:03 PM
This article can also be accessed through the PGD Library aswell. ;)

I'll be putting up it's sister tutorial up today so it will hopefully shed some light on Genetic Algorythms for you aswell. Fupster's theories involve the use of GAs and ANNs together.

{MSX}
20-12-2004, 01:39 PM
There is an article on NN over @
http://www.ai-junkie.com/ann/evolved/nnt1.html


Umm i've tried the example program and it works good. It's not too hard to implement too. ( The delphi code didn't work for me, anyway)..
I wonder if it could work for my game.. wouldn't it be cool to have cars driven by neural networks ? :P

savage
20-12-2004, 06:46 PM
I had not played with the Delphi code, only looked through the article. Does your version of the Delphi code work?

PS. it woulc be super cool if the cars used NN. Apparently that is what they used in Colin McRae 2.

{MSX}
20-12-2004, 07:17 PM
I had not played with the Delphi code, only looked through the article. Does your version of the Delphi code work?


I've not tryed to compile it, since it is windows only. But i've tried the executable at work (where i don't have delphi), and it runs but doesn't work (the agents of the simulation don't seems to get updated). I think there is some error in the sources.



PS. it woulc be super cool if the cars used NN. Apparently that is what they used in Colin McRae 2.

Well the demo of that article is quite effective. In say 10 minutes the agents go straight to the targets. That's similar to what i want, even if more simple.
If i find some time, i'll try :P

BTW that demo uses genetic algorithms to train the NN. Do any of you know of a good article about the classical train method, that is feed forward (if i'm correct) ?

savage
20-04-2005, 03:04 PM
Another NN article you might find interesting...
http://www.onlamp.com/pub/a/onlamp/2004/09/30/AIforGameDev.html

JSoftware
20-04-2005, 04:37 PM
if you compile the delphi nn example then it works. though there are some errors in the code

{MSX}
20-04-2005, 05:59 PM
Another NN article you might find interesting...
http://www.onlamp.com/pub/a/onlamp/2004/09/30/AIforGameDev.html

Umm i've read it but i found it quite superficial. It tells almost nothing practically useful.
:D

savage
20-04-2005, 06:45 PM
I agree, I thought the links it provides might be more usefull.

lief
16-06-2005, 01:09 AM
Heres how i might do it...

Set up your AI driving points around the track - these are joined by lines, and you need functions do be able to get the position on the line at a given distance from each point. You will also need to be able to get the distance from each point and the distance from the desired position (on the ideal driving line).

What do drivers do? Steer, brake and accelerate... these functions need to be accessible by the AI.

Calculate which way they need to steer to move closer to the desired position (which will change every time they move)...
Desired position is the distance from the next AI point to the car, but measured along the ideal racing line (and maybe altered to 'look ahead' slightly, reducing the distance measured)
Direction can be calculated via a 'get angle to point' function or dot-product or something similar.

The steering factor should be given in an amount (x > 0 < 1) or something so you can tell how hard the car has to steer (further away = more steering needed).

You then need to find out the steering factor for: obstacles, other cars, and desired road position (to allow overtaking )

Finally, add the steering inputs together, weighting each input according to its importance (if a car is within 2 car lengths, car collision input might be more important than track steering input, likewise if the car is near a AI point, track steering maybe more important than the others)

This will result in a VERY simple neural net type AI. Further improvements may include 'fuzzy' logic, where at distance the desired points and input become 'blurred' - instead of steer left 0.21 units, it may be 'steer left a little bit', leaving it up to the AI to randomly decide how much a little bit is, this will introduce humanlike behaviour in the driving. also, when calculating AI point position, add a random factor influenced by distance... the more distance there is from the point concerned, the more inaccurate the returned point maybe.

Sorry if all this is confusing, and sorry if the topic is dead :)
I love AI stuff however, and often the concepts are hard to put in clear words.

Oh yes, and to add uniqueness to each driver, try altering the following variables (much like real drivers) - reflexes, accuracy, aggression, and the weighting of each input.

Mix that with cars with different abilities (Acceleration, braking, steering, grip, top speed) and you should have a wide variety of racers!

Oh yes (another one...) swap the collision steering factor for cars behind the driver and the AI drivers will try to not let you pass!

lief