summaryrefslogtreecommitdiffstats
path: root/game/code/worldsim/redbrick/trafficlocomotion.cpp
blob: 20bd77c01ad3730ce794be07173b3b13b9c71cee (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
//=============================================================================
// Copyright (C) 2002 Radical Entertainment Ltd.  All rights reserved.
//
// File:        trafficlocomotion.cpp
//
// Description: Blahblahblah
//
// History:     09/09/2002 + Locomote through intersections -- Dusit Eakkachaichanvet
//              04/24/2002 + Created -- Greg Mayer
//
//=============================================================================
#include <poser/poseengine.hpp>
#include <raddebugwatch.hpp>
#include <radtime.hpp>
#include <simcommon/simstatearticulated.hpp>
#include <worldsim/redbrick/trafficlocomotion.h>
#include <worldsim/redbrick/vehicle.h>

#include <debug/profiler.h>
#include <worldsim/worldphysicsmanager.h>

#include <roads/roadmanager.h>
#include <roads/road.h>
#include <roads/roadsegment.h>
#include <roads/roadsegmentdata.h>
#include <roads/lane.h>
#include <roads/intersection.h>
#include <roads/geometry.h>

const float TrafficLocomotion::SECONDS_BETW_HISTORY_UPDATES = 0.005f;
static const float SECONDS_BETW_CHECKS_FOR_FREE_LANE = 5.0f;
static const float TRAFFIC_WHEEL_GAP_HACK = 0.1f;

#define FACING_HISTORY
// Need to use pos history to smooth out motion along curve 
// Fortunately, this also causes the actual (apparent) position 
// to be located further back from the calculated position, so 
// turning looks better.
// Unfortunately, averaging means we can't stop on a dime, 
// like we want to ... 
#define POS_HISTORY 

//------------------------------------------------------------------------
TrafficLocomotion::TrafficLocomotion(Vehicle* vehicle) :
    VehicleLocomotion(vehicle),
    mMyAI( NULL )
{

    mMyAI = new TrafficAI( vehicle );
    mMyAI->AddRef();

#ifdef DEBUGWATCH
    if( vehicle && vehicle->mVehicleType == VT_TRAFFIC )
    {
        mMyAI->RegisterAI();
    }
#endif


    mVehicle = vehicle;
    mIsInIntersection = false;
    mCurrWay = -1;
    mCurrPathLength = 0.0f;
    mCurrPathLocation = 0.0f;
    mActualSpeed = 0.0f;
    mWays = NULL;
    mNumWays = 0;
    mSecondsTillCheckForFreeLane = SECONDS_BETW_CHECKS_FOR_FREE_LANE;

    ////////////////////////////////////////////////
    // HISTORY stuff
    ////////////////////////////////////////////////
#ifdef FACING_HISTORY
    rmt::Vector heading( 0.0f, 0.0f, 0.0f );
    /*
    if( mVehicle != NULL )
    {
        mVehicle->GetHeading( &heading );
    }
    */
    mFacingHistory.Init( heading );
#endif

#ifdef POS_HISTORY
    rmt::Vector pos( 0.0f, 0.0f, 0.0f );
    mPosHistory.Init( pos );
#endif
    ////////////////////////////////////////////////

    mLaneChangeProgress = 0.0f; 
    mLaneChangeDist = 0.0f;
    mLaneChangingFrom = 0; 
    mOutLaneT = 0.0f;

    mSecondsSinceLastAddToHistory = 0.0f;
}


//------------------------------------------------------------------------
TrafficLocomotion::~TrafficLocomotion()
{
    mMyAI->ReleaseVerified();
}

void TrafficLocomotion::InitPos( const rmt::Vector& pos )
{
#ifdef POS_HISTORY
    mPosHistory.Init( pos );
#endif

    mSecondsSinceLastAddToHistory = 0.0f;

    /////////////////////////////////////////////
    // Adjust ground height (unfortunately we need to do this
    // because we use pos averaging (so our y value is off)

    mPrevPos = pos;

    rmt::Vector groundPosition, outnorm;
    bool bFoundPlane = false;

    groundPosition = pos;
    outnorm.Set( 0.0f, 1.0f, 0.0f );

    GetIntersectManager()->FindIntersection(
        groundPosition,   // IN
        bFoundPlane,      // OUT
        outnorm,          // OUT
        groundPosition    // OUT
        );  

    if( bFoundPlane )
    {
        mPrevPos.y = groundPosition.y;
    }
    ///////////////////////////////////////////////

}


void TrafficLocomotion::InitFacing( const rmt::Vector& facing )
{
    rAssert( rmt::Epsilon( facing.MagnitudeSqr(), 1.0f, 0.005f ) );
#ifdef FACING_HISTORY
    mFacingHistory.Init( facing );
#endif
    mSecondsSinceLastAddToHistory = 0.0f;

}
//=============================================================================
// TrafficLocomotion::Init
//=============================================================================
// Description: Comment
//
// Parameters:  ()
//
// Return:      void 
//
//=============================================================================
void TrafficLocomotion::Init()
{
    mMyAI->Init();
}

//=============================================================================
// TrafficLocomotion::Init
//=============================================================================
// Description: Comment
//
// Parameters:  ( TrafficAI::Behaviour behaviour, 
//                unsigned int behaviourModifiers, 
//                Vehicle* vehicle, 
//                Lane* lane,
//                unsigned int laneIndex,
//                RoadSegment* segment,
//                unsigned int segmentIndex,
//                float t, 
//                float kmh  )
//
// Return:      void 
//
//=============================================================================
void TrafficLocomotion::Init( Vehicle* vehicle, 
                              Lane* lane,
                              unsigned int laneIndex,
                              RoadSegment* segment,
                              unsigned int segmentIndex,
                              float t, 
                              float mps )
{
    mMyAI->Init( mVehicle, lane, laneIndex, segment, segmentIndex, t, mps );
}

//=============================================================================
// TrafficLocomotion::InitVehicleAI
//=============================================================================
// Description: Comment
//
// Parameters:  ( Behaviour behaviour, unsigned int behaviourModifiers, Vehicle* vehicle )
//
// Return:      void 
//
//=============================================================================
void TrafficLocomotion::InitVehicleAI( Vehicle* vehicle )
{
    mMyAI->SetVehicle( vehicle );
}

//=============================================================================
// TrafficLocomotion::InitLane
//=============================================================================
// Description: Comment
//
// Parameters:  ( Lane* lane, unsigned int laneIndex, float mps )
//
// Return:      void 
//
//=============================================================================
void TrafficLocomotion::InitLane( Lane* lane, unsigned int laneIndex, float mps )
{
    mMyAI->SetLane( lane );
    mMyAI->SetLaneIndex( laneIndex );
    mMyAI->SetAISpeed( mps );
}

//=============================================================================
// TrafficLocomotion::InitSegment
//=============================================================================
// Description: Comment
//
// Parameters:  ( RoadSegment* segment, unsigned int segmentIndex, float t )
//
// Return:      void 
//
//=============================================================================
void TrafficLocomotion::InitSegment( RoadSegment* segment, unsigned int segmentIndex, float t )
{
    mMyAI->SetSegment( segment );
    mMyAI->SetSegmentIndex( segmentIndex );
    mMyAI->SetLanePosition( t );      
    mMyAI->SetIsInIntersection( false );
    mIsInIntersection = false;
}


//=============================================================================
// TrafficLocomotion::UpdateVehicleGroundPlane
//=============================================================================
// Description: Comment
//
// Parameters:  ()
//
// Return:      void 
//
//=============================================================================
void TrafficLocomotion::UpdateVehicleGroundPlane()
{
    rmt::Vector p, n;
    p.Set(0.0f, 0.0f, 0.0f);
    n.Set(0.0f, 1.0f, 0.0f);
    
    p = mVehicle->GetPosition();
    p.y -= 5.0f;     // just to be safe
    
      
    mVehicle->mGroundPlaneWallVolume->mPosition = p;
    mVehicle->mGroundPlaneWallVolume->mNormal = n;
    
    
    sim::CollisionObject* co = mVehicle->mGroundPlaneSimState->GetCollisionObject();
    co->PostManualUpdate();
    

}



//------------------------------------------------------------------------
void TrafficLocomotion::PreCollisionPrep(bool firstSubstep)
{
    UpdateVehicleGroundPlane();
}

//=============================================================================
// TrafficLocomotion::PreSubstepUpdate
//=============================================================================
// Description: Comment
//
// Parameters:  (mVehicle* mVehicle)
//
// Return:      void 
//
//=============================================================================
void TrafficLocomotion::PreSubstepUpdate()
{
    if( !mMyAI->mIsActive )
    {
        return;
    }

    // perform whatever update here
}


//=============================================================================
// TrafficLocomotion::PreUpdate
//=============================================================================
// Description: Comment
//
// Parameters:  ()
//
// Return:      void 
//
//=============================================================================
void TrafficLocomotion::PreUpdate()
{
    poser::Pose* pose = mVehicle->mPoseEngine->GetPose();
   
    mVehicle->mPoseEngine->Begin( false );
    //mVehicle->mPoseEngine->Begin(true);
   
     
    int i;
    for(i = 0; i < 4; i++)
    {
        Wheel* wheel = mVehicle->mWheels[i];

        poser::Joint* joint = pose->GetJoint(mVehicle->mWheelToJointIndexMapping[i]);
        rmt::Vector trans = joint->GetObjectTranslation();

        //trans.y -= mVehicle->mWheels[i]->mLimit;                                          
        // TODO - verify that the -= is the thing to do here
        //trans.y -= wheel->mLimit;
        trans.y += TRAFFIC_WHEEL_GAP_HACK;

        joint->SetObjectTranslation(trans);                

    }
    

}


//=============================================================================
// TrafficLocomotion::UpdateAI
//=============================================================================
// Description: Comment
//
// Parameters:  ()
//
// Return:      void 
//
//=============================================================================
void TrafficLocomotion::UpdateAI(unsigned int ms)
{
    // TrafficAI::Update takes timestep in seconds
    mMyAI->Update(float(ms)/1000.0f);
}



void TrafficLocomotion::StopSuddenly( rmt::Vector& pos, rmt::Vector& facing )
{
#ifdef POS_HISTORY
    // re-init history with pos
    mPosHistory.GetAverage( pos );
    mPosHistory.Init( pos );
#else
    pos = mPrevPos;
#endif

#ifdef FACING_HISTORY
    mFacingHistory.GetNormalizedAverage( facing );
    mFacingHistory.Init( facing );
#endif

    mSecondsSinceLastAddToHistory = 0.0f;

    // TODO:
    // When we stop suddenly, our t value is slightly ahead of 
    // us (because we've clobbered our pos history with the
    // vehicle's current position). When we accelerate again, 
    // we'll dump the t position into the history, causing 
    // the vehicle to lurch forward inertialessly, throwing off
    // the average.
    // 
    // So we reset our t value at this point to where we are.
    // This is rather tricky to do: we could be anywhere...
    // - If we're on a road segment, that's perfect. 
    // - If we're not on a road segment, but we're in 
    //    an intersection, there's no way to guarantee that
    //    our t value is still on the spline (we could already 
    //    be in the OUT lane). We need to create a new spline
    //    that still takes us from one road to another, but
    //    we didn't store all this info!! Arrgh...
    // 
    // A BETTER design (from the ground up), would have 
    // just stored splines from the beginning (even if you're
    // traversing roads), making sure that this spline is 
    // long enough to contain our vehicle's actual position.
    // As our t value crawls along a roadsegment, we add it to 
    // our spline. This way when we need to recompute t, we 
    // can just search on our spline for the closest segment
    // to our current position. The other plus side to this
    // is that we don't traverse segments differently from 
    // intersections. Everything gets stored in our spline.
    //       

}



//=============================================================================
// TrafficLocomotion::Update
//=============================================================================
// Description: Comment
//
// Parameters:  (float seconds)
//
// Return:      void 
//
//=============================================================================
void TrafficLocomotion::Update(float seconds)
{
    if( !mMyAI->mIsActive )
    {
        return;
    }

    mSecondsSinceLastAddToHistory += seconds;
    
BEGIN_PROFILE( "Traffic Locomotion" );

    //mVehicle->mPoseEngine->Begin( false );

    rmt::Vector pos, facing;

    TrafficAI::State state = mMyAI->GetState();

    switch ( state )
    {
    case TrafficAI::DEAD:
        {
            pos = mPrevPos;
        #ifdef FACING_HISTORY
            mFacingHistory.GetNormalizedAverage( facing );
        #endif
        }
        break;
    case TrafficAI::DRIVING: // fall through
    case TrafficAI::WAITING_AT_INTERSECTION:
        {
            if( mMyAI->mNeedToSuddenlyStop )
            {
                StopSuddenly( pos, facing );
                break;
            }

            bool stayDoggyStay = false;

            //================================================
            // DETERMINE t AND CURRENT WAYPOINT OR ROAD SEGMENT 
            // (whichever's applicable)
            //================================================
            float t = 0.0f;
            float pathLength = 0.0f;

            if( !mIsInIntersection )
            {
                pathLength = mMyAI->GetLaneLength();
                t = mMyAI->GetLanePosition();
            }
            else
            {
                pathLength = mCurrPathLength;
                t = mCurrPathLocation;
            }

            float adjustedDt = (GetAISpeed() * seconds) / pathLength;
            t += adjustedDt;

            while( t > 1.0f )
            {
                t -= 1.0f;

                if( !mIsInIntersection ) // we're not inside an intersection
                {
                    const Road* road = mMyAI->GetLane()->GetRoad();
                    unsigned int segmentIndex = mMyAI->GetSegmentIndex();

                    // If there are more segments on this road
                    if ( segmentIndex < (road->GetNumRoadSegments()-1) )
                    {
                        // we have to move ahead a segment
                        segmentIndex++;
                        mMyAI->SetSegmentIndex( segmentIndex ); 
                        float newLength = mMyAI->GetLaneLength();
                        t *= pathLength / newLength;
                        mMyAI->SetLanePosition(t); 
                        pathLength = newLength;

                        // ========================
                        // We just updated mMyAI:
                        // - segment & segment index updated to look at next segment
                        // - lane & lane index not updated
                        // - lane position updated with the new segment's t
                        // ========================
                    }
                    // Else we must be at an intersection
                    else 
                    {
                        // set up cubic spline
                        bool succeeded = EnterIntersection();
                        // ========================
                        // We just updated mMyAI:
                        // - segment & segment index updated to look at the OUT segment
                        // - lane & lane index updated to look at the OUT lane
                        // - lane position not updated
                        // ========================

                        // Set info of first path on spline
                        if( succeeded )
                        {
                            rAssert( mWays != NULL );

                            mMyAI->SetIsInIntersection( true );
                            mIsInIntersection = true;

                            mCurrWay = 0;
                            mCurrPath.Sub( mWays[mCurrWay+1], mWays[mCurrWay] );
                            mCurrPathLength = mCurrPath.Length();  // *** SQUARE ROOT! ***
                            t *= pathLength / mCurrPathLength;
                            pathLength = mCurrPathLength;
                        }
                        else
                        {
                            stayDoggyStay = true;
                            break;
                        }

                    }
                }
                else // we ARE inside an intersection
                {
                    // Since we never spawn in an intersection, we never
                    // get into this case unless we entered via EnterIntersection(), 
                    // and t is > 1.0f... 
                    // Meaning that at this point, mWays has been populated and
                    // mCurrWay, mCurrPath, and mCurrPathLength are set.

                    rAssert( mCurrWay < mNumWays );
                    rAssert( mCurrWay >= 0 );

                    // if we still got waypoints to traverse in intersection
                    if( mCurrWay < (mNumWays - 2) )
                    {
                        mCurrWay++;

                        rAssert( mWays != NULL );

                        mCurrPath.Sub( mWays[mCurrWay+1], mWays[mCurrWay] );
                        float newLength = mCurrPath.Length();  // *** SQUARE ROOT! ***
                        t *= pathLength / newLength;
                        pathLength = newLength;
                        mCurrPathLength = newLength;
                    }
                    else // Else going out of the intersection now
                    {
                        mIsInIntersection = false;
                        mMyAI->SetIsInIntersection( false ); 

                        // mMyAI information has already been set to look 
                        // at the out lane/segment/etc.
                        // Must move within the lane...
                        float newLength = mMyAI->GetLaneLength();
                        t *= pathLength / newLength;
                        pathLength = newLength;
                    }
                }
            }

            // if t is not a valid number, it means that we encountered
            // an error inside EnterIntersection()
            if( stayDoggyStay )
            {
                pos = mPrevPos;
            #ifdef FACING_HISTORY
                mFacingHistory.GetNormalizedAverage( facing );
            #endif                            
                break; // exit this case
            }


            //================================================
            // KNOWING t, DETERMINE pos AND facing
            //================================================
            rAssert( 0 <= t && t <= 1.0f );

            // Depending on whether we're inside an intersection,
            // we have different methods for determining pos & facing
            if( !mIsInIntersection ) 
            {
                mMyAI->SetLanePosition( t );
                RoadSegment* segment = mMyAI->GetSegment();
                rAssert( segment );

                segment->GetLaneLocation( mMyAI->GetLanePosition(), mMyAI->GetLaneIndex(), pos, facing );
                if( !rmt::Epsilon( facing.MagnitudeSqr(), 1.0f, 0.005f ) )
                {
                    facing.NormalizeSafe(); // *** SQUARE ROOT! ***
                }
                rAssert( rmt::Epsilon( facing.MagnitudeSqr(), 1.0f, 0.005f ) );
            }
            else
            {
                mCurrPathLocation = t;
                rmt::Vector temp = facing = mCurrPath;
                facing.Scale( 1.0f / mCurrPathLength );
                rAssert( rmt::Epsilon( facing.MagnitudeSqr(), 1.0f, 0.005f ) );
                temp.Scale(t);

                rAssert( mWays != NULL );
                pos.Add( mWays[mCurrWay], temp );
            }
        }
        break;

    case TrafficAI::WAITING_FOR_FREE_LANE:
        {
            mSecondsTillCheckForFreeLane -= seconds;
            if( mSecondsTillCheckForFreeLane < seconds )
            {
                mSecondsTillCheckForFreeLane = SECONDS_BETW_CHECKS_FOR_FREE_LANE;

                // Transit back to driving next frame, so we check again for 
                // lane availability
                mMyAI->SetState( TrafficAI::DRIVING );
            }

            // keep old heading & facing for now (complete and instantaneous stop)
            pos = mPrevPos;
        #ifdef FACING_HISTORY
            mFacingHistory.GetNormalizedAverage( facing );
        #endif
        }
        break;

    case TrafficAI::SPLINING: // fall thru
    case TrafficAI::LANE_CHANGING:
        {
            rAssert( !mIsInIntersection );

            // In TrafficAI, if we're SPLINING, we can be told to stop for something.
            // (when LANE_CHANGING we don't stop for anything... why? Not sure...)
            // What happens if we need to suddenly stop here?
            if( mMyAI->mNeedToSuddenlyStop )
            {
                StopSuddenly( pos, facing );
                break;
            }


            //================================================
            // DETERMINE t AND CURRENT WAYPOINT OR ROAD SEGMENT 
            // (whichever's applicable)
            //================================================
            float pathLength = mCurrPathLength;
            float t = mCurrPathLocation;

            float adjustedDt = (GetAISpeed() * seconds) / pathLength;
            t += adjustedDt;

            while( t > 1.0f )
            {
                t -= 1.0f;

                // In case we got fed a long "seconds" value and go WAY over 
                // the lane change spline. Then we're not lane changing 
                // anymore (we're on normal road now)
                if( mMyAI->GetState() != state ) 
                {
                    const Road* road = mMyAI->GetLane()->GetRoad();
                    unsigned int segmentIndex = mMyAI->GetSegmentIndex();

                    if( segmentIndex < (road->GetNumRoadSegments()-1) )
                    {
                        // we have to move ahead a segment
                        segmentIndex++;
                        mMyAI->SetSegmentIndex( segmentIndex ); 
                        float newLength = mMyAI->GetLaneLength();
                        t *= pathLength / newLength;
                        mMyAI->SetLanePosition(t); 
                        pathLength = newLength;

                        // ========================
                        // We just updated mMyAI:
                        // - segment & segment index updated to look at next segment
                        // - lane & lane index not updated
                        // - lane position updated with the new segment's t
                        // ========================
                    }
                    else // if we're out of segments.. we are at an intersection... 
                    {
                        // TODO:
                        // we should include intersection code here and deal with
                        // the t-overflow properly... but I'm thinking that's a lot 
                        // of code repeat... so I'll get around to it later... 
                        t = 0.999f;
                        mMyAI->SetLanePosition(t);
                    }
                }
                else // else we are in middle of lane changing...
                {
                    // At this point, mWays has been populated and
                    // mCurrWay, mCurrPath, and mCurrPathLength are set.

                    rAssert( mCurrWay < mNumWays );
                    rAssert( mCurrWay >= 0 );

                    // if we still got waypoints to traverse...
                    if( mCurrWay < (mNumWays - 2) )
                    {
                        mCurrWay++;

                        rAssert( mWays != NULL );

                        mCurrPath.Sub( mWays[mCurrWay+1], mWays[mCurrWay] );
                        float newLength = mCurrPath.Length();  // *** SQUARE ROOT! ***
                        t *= pathLength / newLength;
                        pathLength = newLength;
                        mCurrPathLength = newLength;
                    }
                    else // Else done lane changing now...
                    {
                        mMyAI->SetState( TrafficAI::DRIVING );

                        // mMyAI information has already been set to look 
                        // at the target lane/segment/etc.
                        // Must move within the lane...
                        float newLength = mMyAI->GetLaneLength();
                        t *= pathLength / newLength;
                        t += mOutLaneT;
                        pathLength = newLength;
                    }
                }
            }

            //================================================
            // KNOWING t, DETERMINE pos AND facing
            //================================================
            rAssert( 0 <= t && t <= 1.0f );

            // Depending on whether we're still lane changing...
            // we have different methods for determining pos & facing
            if( mMyAI->GetState() != state ) 
            {
                mMyAI->SetLanePosition( t );
                RoadSegment* segment = mMyAI->GetSegment();
                rAssert( segment );

                segment->GetLaneLocation( mMyAI->GetLanePosition(), mMyAI->GetLaneIndex(), pos, facing );
                if( !rmt::Epsilon( facing.MagnitudeSqr(), 1.0f, 0.005f ) )
                {
                    facing.NormalizeSafe(); // *** SQUARE ROOT! ***
                }
                rAssert( rmt::Epsilon( facing.MagnitudeSqr(), 1.0f, 0.005f ) );
            }
            else
            {
                mCurrPathLocation = t;
                rmt::Vector temp = facing = mCurrPath;
                facing.Scale( 1.0f / mCurrPathLength );
                rAssert( rmt::Epsilon( facing.MagnitudeSqr(), 1.0f, 0.005f ) );
                temp.Scale(t);

                rAssert( mWays != NULL );
                pos.Add( mWays[mCurrWay], temp );
            }
        }
        break;

    case TrafficAI::SWERVING:
        {
            // we should be updating TrafficLocomotion if we're swerving because
            // we should be in PhysicsLocomotion
            rAssert( false );
        }
    default:
        {
            rAssert( false );
        }
        break;
    }



    //================================================
    // KNOWING pos AND facing, UPDATE VEHICLE
    //================================================

    // only need mPrevPos for the transition into Intersection
    // since we can't rely on Vehicle->GetPosition to return us
    // the RIGHT-ON-THE-GROUND values (Vehicle class does its
    // own adjustments to bring the car up from ground level)
    rAssert( rmt::Epsilon( facing.MagnitudeSqr(), 1.0f, 0.005f ) );

#if defined(FACING_HISTORY) || defined(POS_HISTORY)

    // we need to add this many entries
    int maxCount = (int)(mSecondsSinceLastAddToHistory / SECONDS_BETW_HISTORY_UPDATES);

    if( maxCount > 0 )
    {
        float secondsConsumed = (float)(maxCount)*SECONDS_BETW_HISTORY_UPDATES;
        float tmpT = (secondsConsumed/mSecondsSinceLastAddToHistory);

        // figure out the starting and ending elements to interpolate between
    #ifdef POS_HISTORY
        rmt::Vector posStart = mPosHistory.GetLastEntry();
        rmt::Vector posDir = pos - posStart;
        rmt::Vector posEnd = posStart + posDir * tmpT;
        posDir = posEnd - posStart;
    #endif
    #ifdef FACING_HISTORY
        rmt::Vector faceStart = mFacingHistory.GetLastEntry();
        rmt::Vector faceDir = facing - faceStart;
        rmt::Vector faceEnd = faceStart + faceDir * tmpT;
        faceDir = faceEnd - faceStart;
    #endif


        float tIncrement = 1.0f / (float)(maxCount);
        tmpT = 0.0f;
        for( int count = 1; count <= maxCount; count++ )
        {
            tmpT += tIncrement;
        #ifdef POS_HISTORY
            mPosHistory.UpdateHistory( posStart + posDir * tmpT );
        #endif
        #ifdef FACING_HISTORY
            mFacingHistory.UpdateHistory( faceStart + faceDir * tmpT );
        #endif

        }
        mSecondsSinceLastAddToHistory -= secondsConsumed;

    }

#endif

    // Histories are updated, now we get the average out of them.

#ifdef FACING_HISTORY
    mFacingHistory.GetNormalizedAverage( facing );
#endif
#ifdef POS_HISTORY
    mPosHistory.GetAverage( pos );
#endif

    /////////////////////////////////////////////
    // Adjust ground height (unfortunately we need to do this
    // because we use pos averaging (so our y value is off)
    rmt::Vector groundPosition, outnorm;
    bool bFoundPlane = false;

    groundPosition = pos;
    outnorm.Set( 0.0f, 1.0f, 0.0f );

    GetIntersectManager()->FindIntersection(
        groundPosition,   // IN
        bFoundPlane,      // OUT
        outnorm,          // OUT
        groundPosition    // OUT
        );  

    if( bFoundPlane )
    {
        pos.y = groundPosition.y;
    }
    ///////////////////////////////////////////////

    // compute actual speed
    rmt::Vector oldPos;
    oldPos = mPrevPos;
    mActualSpeed = (pos - oldPos).Magnitude() / seconds;

    // Update vehicle transform...
    rmt::Matrix newTransform;
    newTransform.Identity();

    rmt::Vector target;
    target = pos;
    target.Add( facing );
    rmt::Vector up = UpdateVUP( pos, target );
    newTransform.FillTranslate(pos);
    newTransform.FillHeading(facing, up);
    mVehicle->TrafficSetTransform(newTransform);

    // Pivot the front wheels...
    PivotFrontWheels( facing );

    mPrevPos = pos;

END_PROFILE( "Traffic Locomotion" );
}

//=============================================================================
// TrafficLocomotion::PostUpdate
//=============================================================================
// Description: Comment
//
// Parameters:  ()
//
// Return:      void 
//
//=============================================================================
void TrafficLocomotion::PostUpdate()
{
    if( !mMyAI->mIsActive )
    {
        return;
    }
    // make sure values are set for wheel rendering info...

    // set 'artificial suspension point velocities
    int i;
    for(i = 0; i < 4; i++)
    {
        rmt::Vector velocity = mVehicle->mVehicleFacing;
        velocity.Scale( mActualSpeed );
        mVehicle->mSuspensionPointVelocities[i] = velocity;
    }
    mVehicle->mVelocityCM = mVehicle->mVehicleFacing;
    mVehicle->mVelocityCM.Scale( mActualSpeed );

    // update mSimStateArticulated speed here?
    // hmmm...
    rmt::Vector& linearVelocity = mVehicle->mSimStateArticulated->GetLinearVelocity();
    linearVelocity = mVehicle->mVelocityCM;

    mVehicle->mSpeed = mActualSpeed;

    
}
 
//////////////////////////////////////////////////////////////////////
//////////////////////////// PRIVATES ////////////////////////////////

void TrafficLocomotion::FindOutLane (
    const Lane* inLane, 
    unsigned int inLaneIndex,
    Lane*& outLane, 
    unsigned int& outLaneIndex )
{
    rAssert( inLane != NULL );
    
    const Road* inRoad = inLane->GetRoad();
    rAssert( inRoad != NULL );

    Intersection* intersection = 
        (Intersection*) inRoad->GetDestinationIntersection();
    rAssert( intersection );

    Road* outRoad = NULL;
    outLane = NULL;
    outLaneIndex = 0;

    switch( mMyAI->DecideTurn() )
    {
    case TrafficAI::LEFT:
        {
            intersection->GetLeftTurnForTraffic( 
                *inRoad, inLaneIndex, outRoad, outLane, outLaneIndex );
        }
        break;
    case TrafficAI::RIGHT:
        {
            intersection->GetRightTurnForTraffic( 
                *inRoad, inLaneIndex, outRoad, outLane, outLaneIndex );
        }
        break;
    case TrafficAI::STRAIGHT:
        {
            intersection->GetStraightForTraffic( 
                *inRoad, inLaneIndex, outRoad, outLane, outLaneIndex );
        }
        break;
    default:
        {
            rAssert( false );
        }
    }
}


void TrafficLocomotion::BuildCurve ( 
    RoadSegment* inSegment, 
    unsigned int inLaneIndex, 
    RoadSegment* outSegment, 
    unsigned int outLaneIndex )
{
    rAssert( inSegment != NULL );
    rAssert( outSegment != NULL );

    // get the start pos & dir
    rmt::Vector startPos, startDir;
    inSegment->GetLaneLocation( 1.0f, inLaneIndex, startPos, startDir );
    //startDir.y = 0.0f;
    if( !rmt::Epsilon( startDir.MagnitudeSqr(), 1.0f, 0.001f ) )
    {
        startDir.Normalize(); // *** SQUARE ROOT! ***
    }

    // get end pos & dir
    rmt::Vector endPos, endDir;
    outSegment->GetLaneLocation( 0.0f, outLaneIndex, endPos, endDir );
    //endDir.y = 0.0f;
    if( !rmt::Epsilon( endDir.MagnitudeSqr(), 1.0f, 0.001f ) )
    {
        endDir.Normalize(); // *** SQUARE ROOT! ***
    }

    // get intermediate points
    rmt::Vector startEndTemp, p2, p4;
    startEndTemp.Sub( endPos, startPos );
    float distFromStartOrEnd = startEndTemp.Length() / 3.0f; // *** SQUARE ROOT! ***
    p2 = startPos + startDir * distFromStartOrEnd;
    p4 = endPos - endDir * distFromStartOrEnd;

    // Now we are ready to gather together our points and send it
    // to our "curve" finder.
    rmt::Vector pts [4];
    pts[0] = startPos;
    pts[1] = p2;
    pts[2] = p4;
    pts[3] = endPos;

    mSplineMaker.SetControlPoint( pts[0], 0 );
    mSplineMaker.SetControlPoint( pts[1], 1 );
    mSplineMaker.SetControlPoint( pts[2], 2 );
    mSplineMaker.SetControlPoint( pts[3], 3 );

/*
#if defined(RAD_TUNE) || defined(RAD_DEBUG)

    const Road* inRoad = inSegment->GetRoad();
    rAssert( inRoad );

    Intersection* intersection = 
        (Intersection*) inRoad->GetDestinationIntersection();
    rAssert( intersection );

    // NOTE:
    // If you hate this assert, you can comment it out locally... FOR NOW... 
    // Put it back in once all y-value mismatch errors are gone. 
    // In other words, once hell freezes over.
    rmt::Vector intersectionLoc;
    intersection->GetLocation( intersectionLoc );
    char baseMsg[1000];
    sprintf( baseMsg, 
        "\nMismatching y-values at intersection (%f,%f,%f).\n"
        "    Check if y values are same for all IN & OUT road segments attached\n"
        "    to this intersection. Check hypergraph to see if the roadnodes leading\n"
        "    IN and OUT of this intersection contain all the proper roadsegments.\n"
        "    If you skip this, Traffic cars will \"hop\" when they transit through\n"
        "    the intersection between the road segments with mismatching y values.\n"
        "    Better to report error to me (Dusit) or Sheik. Preferrably Sheik.\n\n", 
        intersectionLoc.x, 
        intersectionLoc.y,
        -1*intersectionLoc.z );
    rAssert( strlen(baseMsg) < 1000 );

    float ep = 0.001f;
    if( !( rmt::Epsilon( pts[0].y, pts[1].y, ep ) &&
            rmt::Epsilon( pts[0].y, pts[2].y, ep ) &&
            rmt::Epsilon( pts[0].y, pts[3].y, ep ) ) )
    {
        rTunePrintf( baseMsg );
    }
#endif
*/

    mSplineMaker.GetCubicBezierCurve( mWays, mNumWays );
    rAssert( mWays != NULL );
    rAssert( mNumWays == CubicBezier::MAX_CURVE_POINTS );
}



bool TrafficLocomotion::EnterIntersection()
{
    const Lane* inLane = mMyAI->GetLane();
    rAssert( inLane != NULL );
    RoadSegment* inSegment = mMyAI->GetSegment();
    rAssert( inSegment != NULL );
    const unsigned int inLaneIndex = mMyAI->GetLaneIndex();
   
    // ==============================================
    // Find OUT LANE and OUT LANE INDEX 
    // ==============================================

    Lane* outLane = NULL;
    unsigned int outLaneIndex = 0;

    FindOutLane( inLane, inLaneIndex, outLane, outLaneIndex );

    // if absolutely can't find a road, transit to waiting state
    if( outLane == NULL ) 
    {
        // TODO: Should we slow down?
        // Right now we have complete and instantaneous stop,
        // which is needed because we are transiting to a diff state
        // and must maintain old position & facing for when we transit
        // back... 
        mMyAI->SetState( TrafficAI::WAITING_FOR_FREE_LANE );

        return false;
    }

    rAssert( outLane != NULL );


    // ================================================
    // Update IN & OUT lanes' list of traffic vehicles
    // ================================================
    UpdateLanes( mVehicle->mTrafficVehicle, (Lane*)inLane, outLane );


    // ==============================================
    // Build Cubic Spline to navigate intersection
    // ==============================================
    const Road* outRoad = outLane->GetRoad();
    rAssert( outRoad != NULL );
    unsigned int outSegmentIndex = 0;
    RoadSegment* outSegment = outRoad->GetRoadSegment(outSegmentIndex);
    rAssert( outSegment != NULL );

    // create control points, then the spline... store all in mWays
    BuildCurve( inSegment, inLaneIndex, outSegment, outLaneIndex );


    // ==================================================
    // Update mMyAI:
    //  - to look at the OUT segment & segment index
    //  - to look at the OUT lane & lane index
    //  - lane position not updated
    // ===================================================
    mMyAI->SetLane( outLane );
    mMyAI->SetLaneIndex( outLaneIndex );
    mMyAI->SetSegment( outSegment );
    mMyAI->SetSegmentIndex( outSegmentIndex );

    return true;
}

void TrafficLocomotion::PivotFrontWheels( rmt::Vector facing )
{
    float cosAlpha = 0.0f;
    rmt::Vector outPos, outFacing;
    if( mIsInIntersection )
    {
        // when we're in the intersection, the lane is the OUTLANE, so get the t=0.0f
        mMyAI->GetSegment()->GetLaneLocation( 0.0f, (int)mMyAI->GetLaneIndex(), outPos, outFacing );
        outFacing.y = 0.0f;
        outFacing.Normalize(); // *** SQUARE ROOT! ***
        facing.y = 0.0f;
        facing.Normalize(); // *** SQUARE ROOT! ***
        cosAlpha = facing.Dot( outFacing );
    }
    else
    {
        // fake wheel turning for lane change...
        if( mMyAI->GetState() == TrafficAI::LANE_CHANGING )
        {
            float progress = mLaneChangeProgress / mLaneChangeDist;
            if( progress < 0.65f )
            {
                cosAlpha = 0.9396926f; // cos20
            }
            else
            {
                cosAlpha = -0.9396926f; // cos20
            }
        }
        else
        {
            // when we're not in the intersection, the lane is the 
            // current lane, so get t=1.0f
            mMyAI->GetSegment()->GetLaneLocation( 1.0f, 
                (int)mMyAI->GetLaneIndex(), outPos, outFacing );
            outFacing.y = 0.0f;
            outFacing.Normalize(); // *** SQUARE ROOT! ***
            facing.y = 0.0f;
            facing.Normalize(); // *** SQUARE ROOT! ***
            cosAlpha = facing.Dot( outFacing );

        }
    }

    // ensure we are in bounds
    if( cosAlpha < -1.0f )
    {
        cosAlpha = -1.0f;
    }
    else if( cosAlpha > 1.0f )
    {
        cosAlpha = 1.0f;
    }
    float alpha = rmt::ACos( cosAlpha ); // *** COSINE! ***
    rAssert( !rmt::IsNan( alpha ) );

    // pivot left or right 
    if( mMyAI->GetState() != TrafficAI::LANE_CHANGING )
    {
        rmt::Vector rightOfFacing = Get90DegreeRightTurn( facing );
        if( rightOfFacing.Dot( outFacing ) < 0.0f )
        {
            alpha *= -1.0f;
        }
    }
    // Who am I? I'm Spiderman! No, I'm Dusit.
    mVehicle->SetWheelTurnAngleDirectlyInRadiansForDusitOnly( alpha );

}



void TrafficLocomotion::UpdateLanes( TrafficVehicle* tv, Lane* oldLane, Lane* newLane )
{
    rAssert( tv != NULL );
    rAssert( tv->GetIsActive() );
    rAssert( oldLane != NULL );
    rAssert( newLane != NULL );

    // Remove vehicle from IN lane
    bool found = false;
    for( int i=0; i<oldLane->mTrafficVehicles.mUseSize; i++ )
    {
        if( tv == oldLane->mTrafficVehicles[i] )
        {
            oldLane->mTrafficVehicles.Remove( i );
            found = true;
            break;
        }
    }
    //rAssert( found );

    // Add vehicle to OUT lane.
    // What happens if AddLast returns -1 because you can't add
    // another vehicle to that lane? It WON'T! Because in our
    // Intersection::Get<blah>ForTraffic, we make sure we either 
    // return a lane that can take this car or NULL. If NULL, then
    // we shouldn't be at this point in the code.
    //
    rAssert( newLane->mTrafficVehicles.mUseSize < newLane->mTrafficVehicles.mSize );
    newLane->mTrafficVehicles.Add( tv );
    tv->SetLane( newLane );
}

Lane* TrafficLocomotion::GetAIPrevLane()
{
    return mMyAI->mPrevLane;
}

// will return if there's not enough room to lanechange
bool TrafficLocomotion::BuildLaneChangeCurve( 
    RoadSegment* oldSegment, 
    const float oldT,
    unsigned int oldLaneIndex,
    unsigned int newLaneIndex,
    const float dist)
{
    rAssert( oldSegment != NULL );

    // We have to create control points
    rmt::Vector pts[4];

    // First figure out entry point
    rmt::Vector entryFacing;
    oldSegment->GetLaneLocation( oldT, oldLaneIndex, pts[0], entryFacing );
    if( !rmt::Epsilon( entryFacing.MagnitudeSqr(), 1.0f, 0.0001f ) )
    {
        entryFacing.NormalizeSafe(); // *** SQUARE ROOT! ***
    }
    rAssert( rmt::Epsilon( entryFacing.MagnitudeSqr(), 1.0f, 0.0001f ) );

    // Second, figure out the next point in line with facing, 
    // some dist ahead (percentage of lanechange dist)
    float entryOffset = 0.2f * dist;
    pts[1] = pts[0] + entryFacing * entryOffset;

    // Third, figure out the exit point.
    // we'll have to follow the road for the given lanechange distance
    RoadSegment* endSegment = oldSegment;

    float oldLaneLength = oldSegment->GetLaneLength( oldLaneIndex );
    float t = oldT;
    float adjustedDt = dist / oldLaneLength;
    t += adjustedDt;

    unsigned int endSegIndex = endSegment->GetSegmentIndex();
    while( t > 1.0f )
    {
        t -= 1.0f;
        // move on to next segment (there must be one)
        unsigned int numSegs = endSegment->GetRoad()->GetNumRoadSegments();
        endSegIndex++;

        // if we're out of bounds, it means that there weren't enough segments
        // to complete lane-change. Abort!
        if( endSegIndex < 0 || endSegIndex >= numSegs )
        {
            return false;
        }

        endSegment = endSegment->GetRoad()->GetRoadSegment( endSegIndex );
        float nextSegLen = endSegment->GetLaneLength( oldLaneIndex );

        t *= oldLaneLength / nextSegLen;

        oldLaneLength = nextSegLen;
    }

    rAssert( t <= 1.0f );

    // since the last point of spline won't be the START of the 
    // segment when we come out of LANE_CHANGING state, we need to
    // store away the t so we add it later...
    mOutLaneT = t; 

    rmt::Vector exitFacing;
    endSegment->GetLaneLocation( t, newLaneIndex, pts[3], exitFacing );
    if( !rmt::Epsilon( exitFacing.MagnitudeSqr(), 1.0f, 0.0001f ) )
    {
        exitFacing.NormalizeSafe(); // *** SQUARE ROOT! ***
    }
    rAssert( rmt::Epsilon( exitFacing.MagnitudeSqr(), 1.0f, 0.0001f ) );
    exitFacing.Scale( -1.0f );

    // Lastly, figure out the previous point, in line with facing, 
    // some dist ahead (percentage of lanechange dist)
    float exitOffset = dist * 0.5f;//0.3f;
    pts[2] = pts[3] + exitFacing * exitOffset;

    // 
    // update AI's segment info: segment, segment index, lane length
    //
    mMyAI->SetSegmentIndex( endSegment->GetSegmentIndex() );

    mSplineMaker.SetControlPoint( pts[0], 0 );
    mSplineMaker.SetControlPoint( pts[1], 1 );
    mSplineMaker.SetControlPoint( pts[2], 2 );
    mSplineMaker.SetControlPoint( pts[3], 3 );

    // use mWays to store the spline 
    // (since we're not in an intersection anyway)
    mSplineMaker.GetCubicBezierCurve( mWays, mNumWays );
    rAssert( mWays != NULL );
    rAssert( mNumWays == CubicBezier::MAX_CURVE_POINTS );

    // update the currway stuff needed to work with mWays
    mCurrWay = 0;
    mCurrPath.Sub( mWays[mCurrWay+1], mWays[mCurrWay] );
    mCurrPathLength = mCurrPath.Length();  // *** SQUARE ROOT! ***
    mCurrPathLocation = 0.0f;

    return true;
}

bool TrafficLocomotion::BuildArbitraryCurve(
    const rmt::Vector& startPos,
    const rmt::Vector& startDir, // is normalized to 1
    const rmt::Vector& endPos,
    const rmt::Vector& endDir )// is normalized to 1 
{
    rAssert( rmt::Epsilon( startDir.MagnitudeSqr(), 1.0f, 0.001f ) );
    rAssert( rmt::Epsilon( endDir.MagnitudeSqr(), 1.0f, 0.001f ) );

    float distScale = (startPos - endPos).Length(); // *** SQUARE ROOT! ***
    distScale *= 0.3f;

    rmt::Vector pts[4];
    pts[0] = startPos;
    pts[1] = startPos + startDir * distScale;
    pts[2] = endPos + endDir * -1 * distScale;
    pts[3] = endPos;

    mSplineMaker.SetControlPoint( pts[0], 0 );
    mSplineMaker.SetControlPoint( pts[1], 1 );
    mSplineMaker.SetControlPoint( pts[2], 2 );
    mSplineMaker.SetControlPoint( pts[3], 3 );

    // use mWays to store the spline 
    mSplineMaker.GetCubicBezierCurve( mWays, mNumWays );
    rAssert( mWays != NULL );
    rAssert( mNumWays == CubicBezier::MAX_CURVE_POINTS );

    // update the currway stuff needed to work with mWays
    mCurrWay = 0;
    mCurrPath.Sub( mWays[mCurrWay+1], mWays[mCurrWay] );
    mCurrPathLength = mCurrPath.Length();  // *** SQUARE ROOT! ***
    mCurrPathLocation = 0.0f;

    return true;
}

void TrafficLocomotion::GetSplineCurve( rmt::Vector*& ways, int& npts, int& currWay )
{
    if( mWays == NULL )
    {
        ways = NULL;
        npts = 0;
        currWay = -1;
    }
    else
    {
        ways = mWays;
        npts = mNumWays;
        currWay = mCurrWay;
    }
}