Car Steering to spline path

idreamlovey 101 Jan 28, 2008 at 11:51

I got a serious problem in making my car to follow the spline path…
Vehicle is moving along the path but in zig zag way…Tel me where am i doing wrong…
If you have any Article links for Path following…That wil be really great to me…
Here is my code…

void CAIRobot::FollowPath(const D3DXVECTOR3 *m_vPos,const D3DXVECTOR3 *m_vHeading,const D3DXVECTOR3* m_vVelocity)
    const D3DXVECTOR3 *m_vWayPt = g_Profile.aGameStyle->aAIPath.vNodePoint;
    const D3DXVECTOR3 *m_vWayTan = g_Profile.aGameStyle->aAIPath.vNodeTangent;
    const float *m_fWaySpeed = g_Profile.aGameStyle->aAIPath.fWayPtSpeed;
    const int *nNumWayPt = &g_Profile.aGameStyle->aAIPath.nWayPtCount

    D3DXVECTOR3 vCurPos = *m_vPos;
//Find Out the Next Way Point
    if(m_dPrevPt == m_dCurPt)
        m_dPrevPt -= 1;
        m_dPrevPt = *nNumWayPt-1;   
    int nNextPt = m_dCurPt;
        nNextPt = 0;
    fStep = 0.2f+D3DXVec3Length(&(m_vWayPt[m_dPrevPt] - vCurPos))/D3DXVec3Length(&(m_vWayPt[m_dCurPt] - m_vWayPt[m_dPrevPt]));
    if(fStep>1)fStep = 1;
    if( fStep==1)
        m_dPrevPt = m_dCurPt;
        m_dCurPt = nNextPt+1;           

    int nPt0 = m_dPrevPt-1;
        nPt0 = *nNumWayPt-1;

    int nPt1 = m_dCurPt+1;
        nPt1 = 0;

    D3DXVECTOR3 vTarPt = CalculateSplinePt(m_vWayPt[nPt0],m_vWayPt[m_dPrevPt],m_vWayPt[m_dCurPt],m_vWayPt[nPt1],fStep);

    vTargetCross = vTarPt;
//Find Out the Steering angle
    D3DXVECTOR3 m_vTarget = vTarPt - vCurPos;
    D3DXVECTOR3 m_vSteer;
    D3DXVec3Normalize(&m_vSteer, &m_vSteer);

 // Left or right?
    if(m_vSteer.z<-0.1f)//+ve Left -ve Right
    else if((m_vSteer.z>0.1f))

6 Replies

Please log in or register to post a reply.

Nils_Pipenbrinck 101 Jan 28, 2008 at 22:22

Allright. You got your car so far that it drives somewhat into the right direction. Not bad for a beginning.

Your problem is, that the car is oscillating around the keyframe spline. Well - that’s something I can only partly help you with because it’s an effect that is inherent to the problem. Even humands oscillate around a line that they assume is the perfect line. The only thing I can help you is to make the effect so small that noone will ever notice it.

I’ve spent 5 years of my live to write race game AI, I hope I don’t sound like an ass, but I know and I can tell you how it works.

Step 1: The first thing you should change is your steering-control. You have a “button”-like steering control, e.g. either the car gets a “left” or “right” steering order. Get rid of this. You need something analog once you want to drive your car using a steering-wheel controller anyways. You need inbetweens. I suggest you change your steering to accept values between -1 (maximum left steering) to 1 (maximum right steering).

Step 2: Forget about steering for a moment and write a code that controls the gas/brake thing. Let your AI-car drive at a reasonable speed. like 50 mph or so. You’ll get faster later..

Step3: Get into the PID-controllers ( That’s the key-ingredient to get this oscillation down to a minimum. There is _no_ shortcut around this. If you don’t get the math ask someone who does. I would be glad to do it myself, but I can’t express/explain this thing in a foreign language. I even struggle with my own language when it comes down to this thing. It *is* the key ingredient to solve your problem.

Step4: Assuming your car is driving nice at a medium pace. You want to let it drive at faster and slower speeds as well. What you need to do now is to change your steering mechanism. I suggested to change your max left/right to -1 to 1. That’s fine. What you now do is to make your maximum steering relative to the current speed of the car. E.g. doing a turn at 30° is fine if you’re driving with 5mph, but it will instantly kill each one in the car if you do it on a highway. This will not only make driving easier for players but for your AI as well.

Step5: (that’s really an funny one):

Let your car drive a couple of rounds and make statistics what kind of curves it can drive with what speed and go around the corner safely. Just collect data for half a day.

Once you have collected that data: Add some prediction to your AI that it looks like 3 or 5 seconds into the future, estimates the curvature and car-speed and lower/raise it’s speed-goal for the car, so you get around the corners. You always want to be on the edge of the possible maximum that takes your cars savely through the curves. Game testes are a god-sent in this situation. Record their game inputs and adjust your PID-controller to compete with them.

Rinse, rise, repeat.

Once you’ve done all the steps I’ve described, you should have a very dumb but nearly unbeatable opponent car-AI.

Then starts fun part one: make your AI aware of other cars. Let them drive in opponents slip-stream and let them avoid obstacles and break out. That’s what make up the fun of the game: The difference between opponents that act like bots and opponents that act like humands.

How to take this into a game that makes fun for the players I may answer another day…

Hope that helps,

idreamlovey 101 Jan 29, 2008 at 06:13

Thank u so much…i just finished my 3d pipeline and landed to AI…its really 110 percent valuable information…
I am doing my AI in three layers…
In first layer…my car wil follow the path
In second layer…i made it 2 avoid ground obstacles…
In third layer..i made it to avoid collision with opponents…
I used waypoints in the spline path to let my car follow it…
Then i am using Neuro network….

Thnx a lot once again…Right now i m goin to implement it and back soon…

has981 101 Mar 14, 2008 at 11:06

hi guys.. totally agree it’s absolutly a great thread :D

I’m planning to programme something similar but I’m still in the early stages… I got a box sitting on 4 wheels which can only be controlled by the user… so I was wondering if you guys can point out to me any useful links about calculating splines which I can use as guids for the vehicles.

many thanks in advance

Nils_Pipenbrinck 101 Mar 14, 2008 at 20:03

Hi has981.

I can give no advice how to create such splines automatically.

Every time I tried that I failed. It’s not that simple. In the end I was satisfied with the solution that the spline generated by sampling and averaging a human beeing playing/driving that race-track for a day was more than good enough for my needs.

idreamlovey 101 May 31, 2008 at 14:29

I implemented PID control for the steering of my car driving at the constant 50 km/hr. But when i start using the variable speeds, sometimes the car overshoots and oscillates for a while that is not found while testing at the constant speed 50 km/hr. Do i need to precalculate different values of Kp and Kd of different speeds or there is some equation do do with or i have to derive equation for it…what could be the possible solutions for it…cause it looks me very lenghty work to precalcluate the different car’s different PID values at different speeds.

idreamlovey 101 May 31, 2008 at 18:24

Guys Help me out..i am running out of time…:(