# Traversing a racetrack (Car AI)

This topic is 3769 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hello there, I'm currently trying to implement a simple piece of driving AI that can drive around a given track. My track is defined as a series of "nodes" that are aligned in the centre and after some help from a guy off these forums, I've managed to gain the full picture of the track (with the left/right extents) that looks a little like this: - The green points represent the centre of the track. Each "node" is stored in a Vector called track, effectively defines the racetrack. My question is, how do I traverse these nodes? To put it clearly, how do I check when the car has reached/passed the last node so I can find out where to move next? A lot of people on here have directed others to this: - Steering Behaviours for Autonomous Characters, while this is very useful information, it just tells you how to reach a target and not what happens after it gets there. Any help would be appreciated

##### Share on other sites
I wonder if it is really necassery to have that many nodes on your race track. I would argue that you only need a node at the positions where a race track 'changes' in some way i.e. at a corner or if the race track narrows / widens. This may help lower the run-time complexity and reduce memory requirements.

I assume you're thinking about having a position along the race line that is someway in front of the car (distance dependent on the current car speed and if there are any pending corners) and that the car will drive towards that position. And I also assume that you have some method of working out when to brake for the next corner.

But given the link to Steering Behaviours, I'm not entirely sure what it is you still need to know? You say you don't know what to do once you have reached a target, but in truth you should never reach a target because the target should be constantly moving along the race track in proportion to how the car is moving along the race track. Are you assuming that the target is a fixed location?

##### Share on other sites
Quote:
 Original post by makarI assume you're thinking about having a position along the race line that is someway in front of the car (distance dependent on the current car speed and if there are any pending corners) and that the car will drive towards that position.

Pretty much yeah, what I was thinking of doing was this: -

The yellow box represents the AI "looking ahead" to check the direction of the nodes so it can determine when to turn etc. Do you think this is a reasonable solution?

Quote:
 Original post by makar And I also assume that you have some method of working out when to brake for the next corner.

Not yet unfortunately, I just want the car to calculate it's way around the track, then I can go ahead and implement braking and so forth.

Quote:
 Original post by makarBut given the link to Steering Behaviours, I'm not entirely sure what it is you still need to know? You say you don't know what to do once you have reached a target, but in truth you should never reach a target because the target should be constantly moving along the race track in proportion to how the car is moving along the race track. Are you assuming that the target is a fixed location?

I guess I got a little confused. What I mean is, when the car passes a node, I want it to register that it's passed that node so it can look ahead to the next nodes. Or am I getting the wrong idea? I suppose in reality the car should be always looking ahead continuously. Well, to be fair I need to workout where the car is in relation to the track so it knows where to look ahead.

If you catch my drift? Perhaps I have the wrong idea I'm not sure. Thanks for the reply by the way.

##### Share on other sites

well if you know the index of the next node you're heading towards then to find out if you have crossed it or not is a simple line intersection test. something like:

Vector3 vCarStart = car.GetPosition();
Vector3 vCarEnd = vCarStart + car.GetVelocity();

Vector3 vNodeLeft = node[m_uNextNode].GetLeft();
Vector3 vNodeRight = node[m_uNextNode].GetRight();

if (GetDoLinesIntersect(vCarStart,vCarEnd, vNodeLeft, vNodeRight)
{
// crossed the node!
m_uNextNode = (m_uNextNode+1) % ms_kuMaxNodes;
}

You will have to take care to ensure that it is not possible to leave the race track and go around the node-line.

If you need help with the intersection test, it is discussed in this forum:

http://www.gamedev.net/community/forums/topic.asp?topic_id=380428

Good luck

##### Share on other sites
Firstly, I'd just like to point out that the racing line is different for every car (probably best to make each car the same to begin with).

The racing line is obviously the fastest speed at which a car can handle around the track. Each car has different speed, acceleration, handling etc...

Secondly, I think the idea with the sight area is not the best decision because in real life the driver has complete knowledge of the track even though he/she cannot see the entire track. To implement this I'd give each node a "recommended speed" for the car to follow, this means the car doesn't have to figure it out for itself!

Idea: once a car has successfully traversed a node, it may chose to increase or decrease the nodes "recommended speed". Smart. I want to implement my own racing game now :p

Hope this helps, you should find it far simpler to code. pm me if it works.

##### Share on other sites
Hi there, sorry for the late replies.

Thanks for your help, makar I managed to implement your solution with success!

fajitsu - Thank you for your input, however I want to view my project as the driver having no prior knowledge of the track. As if he/she had just climbed in the car at the starting grid and was given the task of driving around it.

I liked your recommended speed idea.

At the moment my car driving algorithm isn't very realistic, it drives around the track, yes, but all it does at the moment is get the direction of the next node and steer in that direction. It doesn't take into account a racing line or where the walls of the track lie, or even what speed to go at, it just goes around at one speed. If it leaves the track then it'll just steer off.

I was wondering if a solution could be implemented where the driver "plots" his own racing line as he goes along, for example the AI could look ahead, "plot" a point at a certain distance to the right/left of the centre node and drive towards it, thus eventually plotting a line as it moves around the track. Then this could be modified on the next lap until eventually it learns the best route around the track.

You reckon this is a good idea? I'm not really sure how to steer "towards" this points however, this is where those steering algorithm behaviours may come in useful.

##### Share on other sites
To decide if you have passed a node or not.

Take the dot product of (cars position relative to current node) vs (position of next node relative to current node). When it's negative you're still behind the current node, when it's positive you have passed it.

Basically the difference in position of next vs current node defines a forward direction vector. Then the position of the car is compared to that to see if its forward of the node or still behind it.

This should be a lot cheaper than doing line intersection tests, and give probably the same result. (note, this is based off the centerline nodes only, dont care about the left/right wall nodes)
wall avoidance is a separate issue

1. 1
2. 2
Rutin
19
3. 3
4. 4
5. 5

• 14
• 12
• 9
• 12
• 37
• ### Forum Statistics

• Total Topics
631428
• Total Posts
3000027
×