Racing Game AI And Gameplay Programming

Post your questions, suggestions and experiences regarding game design, integration of external libraries here. For irrEdit, irrXML and irrKlang, see the
ambiera forums
Post Reply
mubashar
Posts: 63
Joined: Wed Sep 07, 2011 7:20 pm

Racing Game AI And Gameplay Programming

Post by mubashar »

Working on small racing game project ... Implemented Ray cast Vehicles, Camera, GUI, Some 2D Stuff, and Now i want to Implement AI for Computer Controlled Vehicles i am sure its not very easy but i am here to get some ideas what i want do is as .

I have some 4 to 5 Opponent Cars i want them to be self controlled (defiantly AI)
cars must follow some path But How ???
and if car is going in opposite direction/hit to another car and rotated how i want it will realign itself to correct path.
how cars will know they have to follow this path (>> the right one)
how to count laps (i mean its not based on distance or some static object) ...

i am so confused at that stage its my first experiment of game development i need your help ... Please suggest me some ideas any AI library/Class Code Snippet, Tutorial or Any type of help .... :)

- Mubashar
When it's all over, it's not who you were. It's whether you made a difference
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: Racing Game AI And Gameplay Programming

Post by CuteAlien »

Oh well, I can tell you how to create a cheating AI ...

First you code a recorder. It records position + rotation + speed of the player vehicle.
Drive 2 rounds (or maybe a few) and record the second one.
Now just interpolate your bots to that line which you have recorded (same position + rotation + speed) and you have nearly realistic looking movement.
Whenever a bot collides then disable his follow-the-line mode for... lets say 3 seconds ... in that time physic takes over, then slowly start the interpolation again (not teleporting, just slightly sliding back to the line).
Congratz - you now got an AI which is very far from perfect, but actually works good enough that playing against it can be some fun ;-)

Now as next step you might consider some more features ... for example you can start playing with the speeds at different positions (for example be slower the first seconds when starting). Or you might consider ways to get a better line to follow. Or you might consider - and there it get's hard - better way to follow the line itself, for example real steering AI. Or you might even leave the line to allow for maneuvers like taking over (using 2 lines might be the simple way to do that for a start).

But maybe take a look at SuperTuxCart - they have a nice AI which is probably working in a way nicer style than what I described here.
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
Virion
Competition winner
Posts: 2148
Joined: Mon Dec 18, 2006 5:04 am

Re: Racing Game AI And Gameplay Programming

Post by Virion »

I use waypoints to do that. each way point links to 2-3 other points which give more options for the AI to choose from. everytime the AI is near to the target point it will randomly pick the next point from the list which stores in the current target point. inexpensive and working pretty well for most of the cases.
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: Racing Game AI And Gameplay Programming

Post by CuteAlien »

Virion - you do that in a racing AI? I guess I would rather use a few lines between which they can switch.
Also note that the hard case in racing isn't so much finding the lines, but actually following them in a way that is physically realistic.
The trick with recording is that the data then is automatically already behaving correct (as a player using real physics is recorded). Small interpolations are easier to hide (although still noticeable unfortunately sometimes). While if you just try following a line using real physics you have a way harder task in front of you - then you have to write AI which does correct steering (which is certainly also a lot cooler, but it will take way(!) longer to code).
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
Virion
Competition winner
Posts: 2148
Joined: Mon Dec 18, 2006 5:04 am

Re: Racing Game AI And Gameplay Programming

Post by Virion »

ahh sounds good. i used that for relatively simple racing game (no 'real' physics) so it worked well for me.

EDIT: I do know that SEGA and CodeMasters used something like polygon soup for their AI's pathfinding alongside with ray casts. (last time I had access to their inhouse engines but couldn't look at the code though)
mubashar
Posts: 63
Joined: Wed Sep 07, 2011 7:20 pm

Re: Racing Game AI And Gameplay Programming

Post by mubashar »

@CuteAlien well i have recorded position and rotation and interpolated it works like a charm ... i think its quite easy but it limits to simple AI ... i have seen some games using way points As Virion told above i don't know how to create way points and use them for AI i have also quite poor 3D Vectors concepts is there any example available that uses way points or anything that describes way points concept.

well also i need help with certain things

how can i calculate LAPS
and how i can identify my cars position with respect to AI cars
how i can identify i am going in right direction
and how to create 2D map for navigation

i just need logic and ideas .... kindly guide
When it's all over, it's not who you were. It's whether you made a difference
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: Racing Game AI And Gameplay Programming

Post by CuteAlien »

Hm, there are different ways to solve all that. Maybe it gives you an idea looking at a screenshot of an editor I wrote for an racing game a few years ago: http://www.michaelzeilfelder.de/Pics/editor.jpg
The outer lines are planes around the whole track to find out when a player left the track completely. So I do checks if the players are inside of all those planes all the time.
The dark blue lines give me the width of the track - calculating those was a little bit tricky, so maybe easier to write an editor where you can manipulate those vertices if you have no idea how to do that automatic.
Finding the position can be done either by checking how many blue lines have been passed. Or, probably better, finding the closest point to the center-lines of the road for each player. And then you need a function that tells you on which road-part that part of the center-line is (note center-lines are just an array of lines-segments - Irrlicht's line classes have functions helping you to find collisions and nearest points).
Finding out right direction is basically that you never should pass planes behind you again - or never go backwards on the center-lines.
Lap-start is just a special line. But make sure you also check that a sufficient number of other lines has been passed between (but also allow some shortcuts maybe). It's a little playing with numbers to get that right, just avoid that people simply circle around the start-line :-)
The pink arrows in that shot are something like save-points. Once you pass one of those and crash you will be put back on those.
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
Virion
Competition winner
Posts: 2148
Joined: Mon Dec 18, 2006 5:04 am

Re: Racing Game AI And Gameplay Programming

Post by Virion »

@cutealien SEG@'s engine uses similar technique like yours. :) they also added an up vector to the centre line so the cars can move in a vertical loop like this:
Image
Post Reply