Advertisement

Drag racer AI [edit - code included]

Started by August 07, 2010 12:01 PM
2 comments, last by lefthandman 14 years, 3 months ago
I have some problems with making an AI script for a drag racer game. There are some obstacles placed all over the track(e.g. stones) So far I haven't been successful in making the opponents avoid those obstacles.
At first, I took a simple approach: The AI checks if it's going to hit a stone. If it is, then checks if it's safe to turn to the left or to the right. If neither of the options is available, then it stops accelerating until reaching a small speed which makes the impact insignificant.
Unfortunately, that wasn't enough - it avoided some obstacles, but not all of them and even completely ignored other ones. Now I'm developing a more advanced algorithm which checks when and how the vehicles should behave based on the surrounding in the folowing one hundred meters or so. But I wonder should I continue and rewrite everything or it's better to improve the previous algorithm? I have no experience with AI at all.

Also if you want to know, here's the second algorithm:
The vehicle object upon reaching a x value which is dividable by 100 creates a test object. This test object calls a recursive script which checks is the current position collision free and if it is, creates another test object further away from the vehicle. When it reaches certain distance, the recursion stops and the AI deals with the gathered data.
Thanks in advance.

edit: the code:
Quote:
obstacle=checkforobstacle(x,y,distance*5,50);
flag=0;
if obstacle
{
if isleftturnpossible
{
flag=1;
if !checkforobstacle(x,y-30,distance*3,40){leftturn=1;rightturn=0;}
else flag=0;
}
else flag=0;
if isrightturnpossible&&!flag
{
if !checkforobstacle(x,y+30,distance*3,40){leftturn=0;rightturn=1;flag=1;}
else flag=0;
}
if !flag decelerates=1;
}

//script checkforobstacle
var xx,yy,g,uuu;
xx=argument0;//x coordinate to check
yy=argument1;//y coordinate to check
g=argument2;//the distance
if argument3==0 uuu=30;else uuu=argument3;//the width of the obstacle
if xx<control.tracklength
{
//the obstacle data is stored in an array
for(i=xx;i<xx+g;i+=50)
if control.obstaclex[ceil(i/100)]==1 //1 means it exists; 0 - it doesnt
if control.obstacley[ceil(i/100)]>yy-uuu&&control.obstacley[ceil(i/100)]<yy+uuu
{return 1;exit;}
return 0;
}
else return 0;


[Edited by - Plusekwal on August 9, 2010 6:33:29 AM]
Ok, so I'll start off this post with links to two websites that may be of interest to you.
http://www.red3d.com/cwr/boids/
http://gamma.cs.unc.edu/RVO/
The above two may be a little advanced for you, but they are the current industry standard. If you want your script to work, check to make sure that:
a. Your obstacles that it isn't avoiding are stored in your scene graph or whatever you are looking at when you're checking if an obstacle is in front of the player.
b. You aren't explicitly stating to check for one kind of obstacle. Make sure you use the highest level class that describes "something to avoid" as what you're checking for.
c. You don't have rounding errors anywhere near that section of the code. Make sure you always give coordinates the same type.
d. IF none of these help you to solve your problem, post your code for us to look at.
Now for the second algo. My basic understanding of what you've described is you're using recursive circles of perception to get from one 100 m line to the next. The thing is, if you want that to work, you'll have to know why the first one isn't working.
ROFLMAO-GG-HF-GL-LOL-TTYL-BRB-GTG
Advertisement
Quote: Original post by lefthandman
Ok, so I'll start off this post with links to two websites that may be of interest to you.
http://www.red3d.com/cwr/boids/
http://gamma.cs.unc.edu/RVO/
The above two may be a little advanced for you, but they are the current industry standard. If you want your script to work, check to make sure that:
a. Your obstacles that it isn't avoiding are stored in your scene graph or whatever you are looking at when you're checking if an obstacle is in front of the player.
b. You aren't explicitly stating to check for one kind of obstacle. Make sure you use the highest level class that describes "something to avoid" as what you're checking for.
c. You don't have rounding errors anywhere near that section of the code. Make sure you always give coordinates the same type.
d. IF none of these help you to solve your problem, post your code for us to look at.
Now for the second algo. My basic understanding of what you've described is you're using recursive circles of perception to get from one 100 m line to the next. The thing is, if you want that to work, you'll have to know why the first one isn't working.

Thanks for the links =)
Only c might be causing trouble, but it's unlikely, since I'm using rounding functions all the time. Also I think the problem is that AI is "seeing" some obstacles in the last possible moment, but it's very hard to check is it really so.
I'll look at those two websites and I'll post the code if they don't help.

edit: See the first post for the first algorithm, the second one is still under development.

[Edited by - Plusekwal on August 9, 2010 6:42:33 AM]
Ok, first off I'm going to clean up your code a bit. This will help both of us look at it and make determining what is causing your current problem a little bit easier.
obstacle=checkforobstacle(x,y,distance*5,50);//idk if the flag is being used later on, so I'm simply going to make it perform the same exact way as it did previously...flag = 1;if obstacle{if isleftturnpossible&&!checkforobstacle(x,y-30,distance*3,40){leftturn=1;rightturn=0;}else if isrightturnpossible&&!checkforobstacle(x,y+30,distance*3,40){leftturn=0;rightturn=1;}else{flag=0; decelerates=1;}}

Now about that leftturn = 1, rightturn = 0 part. Well, don't you only need one turn variable and a flag that states whether or not you're turning. Therefore it can be changed to lrturn(1 = left, 0 = right).
Now about the mystery constants being used on checkforobstacle. If it's possible to decelerate, then why will distance*3 always be appropriate? And will you always be moving 30 (tiles?) at a time? Also, will you always be heading the same direction or will it be a circular track? Because if it's anything but straight, you might actually need to subtract 30 where you're adding it and visa versa. It gets even more complex if your turns change the direction you're headed which means you'll have to deal with angles.
So in closing, I suggest the following:
a) Clean up your code!
b) Take a good look at your checkforobstacle function and make sure your coordinate system isn't screwing you over. Remember, three rights makes a left(once you've turned, turning again won't cause you to head in the same direction as the first turn).
c) Post your checkforobstacle code, and maybe even your turning code.
Anyways, here's the code I recommend you use...
//idk if the flag is being used later on, so I'm simply going to make it perform the same exact way as it did previously...flag = 1;if checkforobstacle(x,y,distance*5,50);{if isleftturnpossible&&!checkforobstacle(x,y-30,distance*3,40){lrturn = 1;}else if isrightturnpossible&&!checkforobstacle(x,y+30,distance*3,40){lrturn=0;}else{flag=0; decelerates=1;}}
ROFLMAO-GG-HF-GL-LOL-TTYL-BRB-GTG

This topic is closed to new replies.

Advertisement