Sign in to follow this  
Rajveer

Splines for Racing AI

Recommended Posts

Rajveer    250
Hi guys, I'm deciding how to control my AI for a racing game. It's essential that the computer stays on course, as they can fall off the edge otherwise. I was thinking of having a general Bezier curve that would go through the middle of the track, and storing a width with each node so the computer can "see" how thick the track is. Then I thought what if I have multiple curves: one for the centre and two for the edges of the course? I would first calculate where on the central path the computer is, then project this onto the edge splines and see how far from the edge they are. It might all get a bit complicated though, as I want to be able to branch some tracks. I just want some input and advice, what would you guys recommend for this problem? [Edited by - Rajveer on November 25, 2007 12:42:41 PM]

Share this post


Link to post
Share on other sites
yahastu    154
Keep it simple, use a single bezier curve (technically, its a separate bezier curve between each 2 points) to represent the center of the track. At each node, store the width of the track. You will also need to store the normal at each node, otherwise you wouldn't know how it was oriented (this way, your track can have a slope to it, or even go upside down in loop-de-loops).

Share this post


Link to post
Share on other sites
Rajveer    250
I think you're right, simple is generally better. I'm a bit confused though, when you say store the normal is this because the polygon data is calculated in real-time? What I'm doing is storing the spline and polygon data separately: I want as little calculation as possible as I'm trying this on a handheld. How intense would calculating the polygons on-the-fly be? Or would this be calculated before-hand during loading?

Another problem with calculating during the game would be how to handle branches. I would want some kind of slope on the edges of the track to help the player to stay on, and interfering slopes would be a problem.

Share this post


Link to post
Share on other sites
yahastu    154
The bezier spline just tells you the location of the center of the track...to get the width of the track, you need to store the width at each node (and I assume do linear interpolation inbetween).

Likewise, the spline does not tell you anything about the slope of the track. Unless your track is always perfectly horizontal, you will need to know how it is sloped. The simplest way to keep track of slope is by the surface normal, which you should store at each node as well.

For example, lets so that one of the race cars wants to move from the left lane into the right lane. It will need to be able to move with the slope of the road.

What direction is that?

First compute the direction that the road is going:

vector3 road[t] = normalize(spline[t+1] - spline[t])

Then you want it to move tangent to the road, so:

vector3 tangent[t] = road[t] cross normal[t]

Also, the boundaries of the track will be at:

right_boudnary[t] = spline[t] + tangent[t] * width[t]/2
left_boundary[t] = spline[t] - tangent[t] * width[t]/2

Share this post


Link to post
Share on other sites
Rajveer    250
Cheers for going into detail :) I don't think I need to use the spline nodes normal though. The 3D model of the track is what I'm using for collision detection and this is what the player orientation depends on, so all slopes are stored as polygon normals anyways.

The player's racer moves along the polygons and changes its orientation depending on the polygon's normal. To see which polygon the racer is over I fire a ray down and find which one it is intersecting, I then rotate the racers orientation according to the polygon's normal.

I'm thinking of then using the point where the ray intersects the polygon's plane and also finding the closest point on the spline, and comparing these two to see if the AI is on the path, on its left or on its right. Is this right?

Also, just to make sure, what data needs to be stored with the nodes? I'm thinking obviously it's coordinates, width, 2 control points for it's tangent, maybe normal is what I said is wrong, anything else?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this