00001
00002 #include "revolt.h"
00003 #include "coll.h"
00004 #include "geom.h"
00005 #include "3d.h"
00006 #include "sound.h"
00007 #include "input.h"
00008 #include "model.h"
00009 #include "car.h"
00010 #include "physics.h"
00011 #include "text.h"
00012
00013 extern IDirect3DDevice2 *D3Ddevice;
00014
00015 extern char Keys[];
00016 extern float TimeFactor;
00017
00018 extern CAR Car[];
00019
00020 float Velocity[3];
00021 float Friction;
00022
00023 char RevsSfx = 0;
00024 short RevsHandle;
00025
00026 char CollisionDeadLock = 0;
00027
00028 float Debug0, Debug1, Debug2;
00029
00030 void BuildCarFromWheels2(CAR *car);
00031
00032
00033
00034
00035 void ControlCar( CAR *car )
00036 {
00037
00038
00039
00040 if (RevsSfx == 0)
00041 {
00042 RevsHandle = PlaySample(2, -2500, 8000, DSBPAN_CENTER, 1);
00043 RevsSfx = 1;
00044 }
00045
00046 if (RevsSfx == 1)
00047 ChangeSample(2, RevsHandle, DSBVOLUME_MAX, (long)6000 + abs((long)(car->Revs*2.9f)), DSBPAN_CENTER);
00048
00049
00050
00051
00052
00053 float turn = 0.003f * (float)TimeFactor;
00054 char turned = 0;
00055
00056 if (Keys[DIK_LEFT])
00057 car->WheelTurn -= turn, turned = 1;
00058 if (Keys[DIK_RIGHT])
00059 car->WheelTurn += turn, turned = 1;
00060
00061
00062
00063 if (car->WheelTurn < -0.06f)
00064 car->WheelTurn = -0.06f;
00065 if (car->WheelTurn > 0.06f)
00066 car->WheelTurn = 0.06f;
00067
00068
00069
00070
00071 if( turned == 0 )
00072 {
00073 if( car->WheelTurn > 0 )
00074 {
00075 car->WheelTurn -= 0.003f*TimeFactor;
00076 if( car->WheelTurn < 0 )
00077 car->WheelTurn = 0;
00078 }
00079
00080 if( car->WheelTurn < 0 )
00081 {
00082 car->WheelTurn += 0.003f*TimeFactor;
00083 if( car->WheelTurn > 0 )
00084 car->WheelTurn = 0;
00085 }
00086 }
00087
00088
00089
00090 }
00091
00092
00093
00094
00095 void UpdateCarMotion( CAR *car )
00096 {
00097
00098 float Vect1[3], Vect2[3], Vel;
00099
00100
00101
00102
00103
00104 Vel = GetScalar( car->Matrix, car->LastVelocity );
00105
00106
00107
00108 BuildTurnMatrices(car);
00109
00110
00111
00112
00113 SetVector( Vect1, 0, 0, Vel );
00114 RotVector( car->TurnCarMatrix, Vect1, Vect2 );
00115
00116 car->Velocity[0] = Vect2[0];
00117 car->Velocity[1] = Vect2[1];
00118 car->Velocity[2] = Vect2[2];
00119
00120
00121
00122
00123 if( Keys[DIK_UP] )
00124 {
00125 SetVector( Vect1, 0, 0, 0.5f*TimeFactor );
00126 RotVector(car->TurnCarMatrix, Vect1, Vect2 );
00127 car->Velocity[0] += Vect2[0];
00128 car->Velocity[1] += Vect2[1];
00129 car->Velocity[2] += Vect2[2];
00130 }
00131
00132 if( Keys[DIK_DOWN] )
00133 {
00134 SetVector( Vect1, 0, 0, -0.5f*TimeFactor );
00135 RotVector(car->TurnCarMatrix, Vect1, Vect2 );
00136 car->Velocity[0] += Vect2[0];
00137 car->Velocity[1] += Vect2[1];
00138 car->Velocity[2] += Vect2[2];
00139 }
00140
00141
00142
00143
00144
00145 car->Gravity += (GRAVITY/100)*TimeFactor;
00146
00147
00148
00149
00150 float Drag[3];
00151
00152 Drag[0] = car->Velocity[0]*car->Velocity[0]*COEFFOFDRAG;
00153 Drag[1] = car->Velocity[1]*car->Velocity[1]*COEFFOFDRAG;
00154 Drag[2] = car->Velocity[2]*car->Velocity[2]*COEFFOFDRAG;
00155
00156 if( car->Velocity[0] > 0 )
00157 car->Velocity[0] -= Drag[0]*TimeFactor;
00158 if( car->Velocity[0] < 0 )
00159 car->Velocity[0] += Drag[0]*TimeFactor;
00160
00161 if( car->Velocity[1] > 0 )
00162 car->Velocity[1] -= Drag[1]*TimeFactor;
00163 if( car->Velocity[1] < 0 )
00164 car->Velocity[1] += Drag[1]*TimeFactor;
00165
00166 if( car->Velocity[2] > 0 )
00167 car->Velocity[2] -= Drag[2]*TimeFactor;
00168 if( car->Velocity[2] < 0 )
00169 car->Velocity[2] += Drag[2]*TimeFactor;
00170
00171
00172
00173
00174
00175
00176 car->LastPos[0] = car->WorldPos[0];
00177 car->LastPos[1] = car->WorldPos[1];
00178 car->LastPos[2] = car->WorldPos[2];
00179
00180
00181
00182 CircularMotion( car );
00183
00184
00185
00186
00187 car->WorldPos[1] += car->Gravity*TimeFactor;
00188
00189
00190
00191 CollisionMain( car );
00192
00193
00194
00195 car->LastVelocity[0] = car->Velocity[0];
00196 car->LastVelocity[1] = car->Velocity[1];
00197 car->LastVelocity[2] = car->Velocity[2];
00198
00199
00200
00201
00202 car->MasterVelocity = GetScalar( car->Matrix, car->Velocity );
00203
00204 car->WheelBL.Spin += (float)(car->MasterVelocity/DRIVEWHEELCIRC);
00205 car->WheelBR.Spin += (float)(car->MasterVelocity/DRIVEWHEELCIRC);
00206 car->WheelFL.Spin += (float)(car->MasterVelocity/DRIVEWHEELCIRC);
00207 car->WheelFR.Spin += (float)(car->MasterVelocity/DRIVEWHEELCIRC);
00208
00209 car->Revs = ( car->MasterVelocity / GEARRATIO * IDEALFRAMERATE * 60 ) / DRIVEWHEELCIRC;
00210
00211
00212 }
00213
00214
00215
00216
00217
00218
00219
00220
00221 void CircularMotion( CAR *car )
00222 {
00223 float d, r, v, w;
00224 float Temp[3], Temp2[3], Origin[3];
00225 float TempMatrix[9];
00226
00227
00228
00229 d = car->WheelFL.Offset[2] - car->WheelBL.Offset[2];
00230
00231
00232
00233
00234 double tanvalue;
00235 tanvalue = tan((1.570796327f)-(car->WheelTurn*3.14159f));
00236
00237 if( tanvalue > 0 )
00238 {
00239 if( tanvalue > 20000 )
00240 tanvalue = 20000;
00241 }
00242 else
00243 {
00244 if( tanvalue < -20000 )
00245 tanvalue = -20000;
00246 }
00247
00248 r = (float)tanvalue*d;
00249
00250
00251
00252
00253 v = GetScalar( car->TurnCarMatrix, car->Velocity );
00254
00255
00256
00257 w = (v * TimeFactor) / r;
00258
00259
00260
00261 SetVector( Temp2, -r, 0, -car->WheelBL.Offset[2] );
00262 RotVector( car->Matrix, Temp2, Temp );
00263
00264 Origin[0] = car->WorldPos[0] - Temp[0];
00265 Origin[1] = car->WorldPos[1] - Temp[1];
00266 Origin[2] = car->WorldPos[2] - Temp[2];
00267
00268
00269 TempMatrix[1] = TempMatrix[2] = TempMatrix[3] = TempMatrix[5] = TempMatrix[6] = TempMatrix[7] = 0;
00270 TempMatrix[0] = TempMatrix[4] = TempMatrix[8] = 1;
00271
00272 FBuildMatrix( 0, -w, 0, TempMatrix );
00273
00274 SetVector( Temp2, -r, 0, -car->WheelBL.Offset[2] );
00275 RotVector( TempMatrix, Temp2, Temp );
00276 RotVector( car->Matrix, Temp, Temp2 );
00277
00278
00279 car->Velocity[0] = -car->WorldPos[0];
00280 car->Velocity[1] = -car->WorldPos[1];
00281 car->Velocity[2] = -car->WorldPos[2];
00282
00283
00284 car->WorldPos[0] = Origin[0] + Temp2[0];
00285 car->WorldPos[1] = Origin[1] + Temp2[1];
00286 car->WorldPos[2] = Origin[2] + Temp2[2];
00287
00288
00289 car->Velocity[0] += car->WorldPos[0];
00290 car->Velocity[1] += car->WorldPos[1];
00291 car->Velocity[2] += car->WorldPos[2];
00292
00293 car->Velocity[0] = car->Velocity[0] / TimeFactor;
00294 car->Velocity[1] = car->Velocity[1] / TimeFactor;
00295 car->Velocity[2] = car->Velocity[2] / TimeFactor;
00296
00297
00298
00299
00300
00301 car->WorldPos[0] -= car->Velocity[0]*TimeFactor;
00302 car->WorldPos[1] -= car->Velocity[1]*TimeFactor;
00303 car->WorldPos[2] -= car->Velocity[2]*TimeFactor;
00304
00305
00306 float AccelerationVect[3];
00307 float AccLimit;
00308
00309 AccelerationVect[0] = (car->Velocity[0] - car->LastVelocity[0]);
00310 AccelerationVect[1] = (car->Velocity[1] - car->LastVelocity[1]);
00311 AccelerationVect[2] = (car->Velocity[2] - car->LastVelocity[2]);
00312
00313
00314 car->Acceleration = (float)sqrt( (AccelerationVect[0]*AccelerationVect[0])
00315 +(AccelerationVect[1]*AccelerationVect[1])
00316 +(AccelerationVect[2]*AccelerationVect[2]) );
00317
00318
00319 AccLimit = car->Acceleration;
00320
00321 if( car->Acceleration > COEFFOFFRICTION *TimeFactor )
00322 AccLimit = COEFFOFFRICTION *TimeFactor;
00323
00324
00325 if( car->Acceleration > 0 )
00326 {
00327 AccelerationVect[0] = (AccLimit*AccelerationVect[0])/car->Acceleration;
00328 AccelerationVect[1] = (AccLimit*AccelerationVect[1])/car->Acceleration;
00329 AccelerationVect[2] = (AccLimit*AccelerationVect[2])/car->Acceleration;
00330 }
00331
00332
00333 car->Velocity[0] = car->LastVelocity[0] + AccelerationVect[0];
00334 car->Velocity[1] = car->LastVelocity[1] + AccelerationVect[1];
00335 car->Velocity[2] = car->LastVelocity[2] + AccelerationVect[2];
00336
00337 car->WorldPos[0] += car->Velocity[0]*TimeFactor;
00338 car->WorldPos[1] += car->Velocity[1]*TimeFactor;
00339 car->WorldPos[2] += car->Velocity[2]*TimeFactor;
00340
00341
00342
00343
00344 FBuildMatrix( 0, -w, 0, car->Matrix );
00345
00346 }
00347
00348
00349
00350
00351
00352
00353
00354
00355 void BounceAgainstPlane( float *Approach, float *Plane, float *Exit )
00356 {
00357 float DotProduct;
00358 DotProduct = (Approach[0]*Plane[0]) + (Approach[1]*Plane[1]) + (Approach[2]*Plane[2]);
00359
00360 Exit[0] = Approach[0] - (2*Plane[0]*DotProduct);
00361 Exit[1] = Approach[1] - (2*Plane[1]*DotProduct);
00362 Exit[2] = Approach[2] - (2*Plane[2]*DotProduct);
00363
00364 }
00365
00366
00367
00368
00369
00370
00371
00372
00373 void BounceAgainstPlane2( float *Approach, float *Plane, float *Exit )
00374 {
00375 float DotProduct;
00376
00377 DotProduct = (Approach[0]*Plane[0]) + (Approach[1]*Plane[1]) + (Approach[2]*Plane[2]);
00378
00379 Exit[0] = Approach[0] - (Plane[0]*DotProduct);
00380 Exit[1] = Approach[1] - (Plane[1]*DotProduct);
00381 Exit[2] = Approach[2] - (Plane[2]*DotProduct);
00382
00383 }
00384
00385
00386
00387
00388
00389 void PhysicsCollision( CAR *car, float *Offset )
00390 {
00391
00392 float After[3];
00393 float ReturnedFriction;
00394 float Normal[3];
00395 float Exit[3];
00396 float Velocity[3];
00397 float TotalDisplacement, ActualDisplacement;
00398 float Old[3];
00399 float Current[3];
00400
00401
00402 CollisionDeadLock++;
00403
00404 if( CollisionDeadLock > 7 )
00405 return;
00406
00407
00408 Old[0] = car->LastPos[0] + Offset[0];
00409 Old[1] = car->LastPos[1] + Offset[1];
00410 Old[2] = car->LastPos[2] + Offset[2];
00411
00412 Current[0] = car->WorldPos[0] + Offset[0];
00413 Current[1] = car->WorldPos[1] + Offset[1];
00414 Current[2] = car->WorldPos[2] + Offset[2];
00415
00416 if( SphereCollTestNorm( Old, Current, 10, After, &ReturnedFriction, Normal ) )
00417 {
00418
00419
00420
00421
00422
00423
00424
00425
00426
00427
00428
00429
00430
00431
00432
00433
00434
00435
00436
00437
00438
00439
00440
00441 car->Gravity = 0;
00442
00443
00444
00445
00446 car->WorldPos[0] = After[0] - Offset[0];
00447 car->WorldPos[1] = After[1] - Offset[1];
00448 car->WorldPos[2] = After[2] - Offset[2];
00449
00450
00451
00452
00453
00454
00455
00456
00457
00458
00459
00460
00461
00462
00463
00464 PhysicsCollision( car, Offset );
00465
00466 }
00467
00468
00469 }
00470
00471
00472
00473 void CollisionMain( CAR *car )
00474 {
00475
00476
00477
00478 float Quim[3], Large[3];
00479 float WP[3], LP[3];
00480
00481
00482 Quim[0] = car->WheelFL.Offset[0] + car->WheelFL.LocalPoint[0];
00483 Quim[1] = car->WheelFL.Offset[1] + car->WheelFL.LocalPoint[1];
00484 Quim[2] = car->WheelFL.Offset[2] + car->WheelFL.LocalPoint[2];
00485
00486 RotVector( car->Matrix, Quim, Large );
00487 CollisionDeadLock = 0;
00488 WP[0] = car->WorldPos[0];
00489 WP[1] = car->WorldPos[1];
00490 WP[2] = car->WorldPos[2];
00491 LP[0] = car->LastPos[0];
00492 LP[1] = car->LastPos[1];
00493 LP[2] = car->LastPos[2];
00494
00495 PhysicsCollision( car, Large);
00496
00497 car->WheelFL.CollPoint[0] = car->WorldPos[0] + Large[0];
00498 car->WheelFL.CollPoint[1] = car->WorldPos[1] + Large[1];
00499 car->WheelFL.CollPoint[2] = car->WorldPos[2] + Large[2];
00500
00501 car->WorldPos[0] = WP[0];
00502 car->WorldPos[1] = WP[1];
00503 car->WorldPos[2] = WP[2];
00504 car->LastPos[0] = LP[0];
00505 car->LastPos[1] = LP[1];
00506 car->LastPos[2] = LP[2];
00507
00508
00509
00510
00511 Quim[0] = car->WheelFR.Offset[0] + car->WheelFR.LocalPoint[0];
00512 Quim[1] = car->WheelFR.Offset[1] + car->WheelFR.LocalPoint[1];
00513 Quim[2] = car->WheelFR.Offset[2] + car->WheelFR.LocalPoint[2];
00514
00515 RotVector( car->Matrix, Quim, Large );
00516 CollisionDeadLock = 0;
00517 WP[0] = car->WorldPos[0];
00518 WP[1] = car->WorldPos[1];
00519 WP[2] = car->WorldPos[2];
00520 LP[0] = car->LastPos[0];
00521 LP[1] = car->LastPos[1];
00522 LP[2] = car->LastPos[2];
00523
00524 PhysicsCollision( car, Large);
00525
00526 car->WheelFR.CollPoint[0] = car->WorldPos[0] + Large[0];
00527 car->WheelFR.CollPoint[1] = car->WorldPos[1] + Large[1];
00528 car->WheelFR.CollPoint[2] = car->WorldPos[2] + Large[2];
00529
00530 car->WorldPos[0] = WP[0];
00531 car->WorldPos[1] = WP[1];
00532 car->WorldPos[2] = WP[2];
00533 car->LastPos[0] = LP[0];
00534 car->LastPos[1] = LP[1];
00535 car->LastPos[2] = LP[2];
00536
00537
00538
00539
00540
00541
00542 Quim[0] = car->WheelBL.Offset[0] + car->WheelBL.LocalPoint[0];
00543 Quim[1] = car->WheelBL.Offset[1] + car->WheelBL.LocalPoint[1];
00544 Quim[2] = car->WheelBL.Offset[2] + car->WheelBL.LocalPoint[2];
00545
00546 RotVector( car->Matrix, Quim, Large );
00547 CollisionDeadLock = 0;
00548 WP[0] = car->WorldPos[0];
00549 WP[1] = car->WorldPos[1];
00550 WP[2] = car->WorldPos[2];
00551 LP[0] = car->LastPos[0];
00552 LP[1] = car->LastPos[1];
00553 LP[2] = car->LastPos[2];
00554
00555 PhysicsCollision( car, Large);
00556
00557 car->WheelBL.CollPoint[0] = car->WorldPos[0] + Large[0];
00558 car->WheelBL.CollPoint[1] = car->WorldPos[1] + Large[1];
00559 car->WheelBL.CollPoint[2] = car->WorldPos[2] + Large[2];
00560
00561 car->WorldPos[0] = WP[0];
00562 car->WorldPos[1] = WP[1];
00563 car->WorldPos[2] = WP[2];
00564 car->LastPos[0] = LP[0];
00565 car->LastPos[1] = LP[1];
00566 car->LastPos[2] = LP[2];
00567
00568
00569
00570
00571 Quim[0] = car->WheelBR.Offset[0] + car->WheelBR.LocalPoint[0];
00572 Quim[1] = car->WheelBR.Offset[1] + car->WheelBR.LocalPoint[1];
00573 Quim[2] = car->WheelBR.Offset[2] + car->WheelBR.LocalPoint[2];
00574
00575 RotVector( car->Matrix, Quim, Large );
00576 CollisionDeadLock = 0;
00577 WP[0] = car->WorldPos[0];
00578 WP[1] = car->WorldPos[1];
00579 WP[2] = car->WorldPos[2];
00580 LP[0] = car->LastPos[0];
00581 LP[1] = car->LastPos[1];
00582 LP[2] = car->LastPos[2];
00583
00584 PhysicsCollision( car, Large);
00585
00586 car->WheelBR.CollPoint[0] = car->WorldPos[0] + Large[0];
00587 car->WheelBR.CollPoint[1] = car->WorldPos[1] + Large[1];
00588 car->WheelBR.CollPoint[2] = car->WorldPos[2] + Large[2];
00589
00590 car->WorldPos[0] = WP[0];
00591 car->WorldPos[1] = WP[1];
00592 car->WorldPos[2] = WP[2];
00593 car->LastPos[0] = LP[0];
00594 car->LastPos[1] = LP[1];
00595 car->LastPos[2] = LP[2];
00596
00597
00598
00599
00600
00601
00602
00603
00604
00605
00606
00607
00608
00609
00610
00611
00612 BuildCarFromWheels2( car);
00613
00614
00615
00616
00617
00618
00619
00620
00621
00622
00623
00624
00625
00626
00627
00628
00629
00630
00631
00632
00633
00634
00635
00636
00637
00638
00639
00640 }
00641
00642
00643
00644
00645
00647
00649
00650 D3DTLVERTEX Points[8];
00651
00652 void DrawGraph( CAR *car )
00653 {
00654
00655
00656 short i;
00657
00658 D3Ddevice->SetRenderState(D3DRENDERSTATE_TEXTUREHANDLE, NULL);
00659
00660 for (i = 0 ; i < 4; i++ )
00661 {
00662 Points[i].dvSX = i;
00663 Points[i].dvSY = 100;
00664 Points[i].dvSZ = 1/(float)65536;
00665 Points[i].dcColor = RGB_MAKE( 255,255,255 );
00666 }
00667
00668 for (i = 4 ; i < 8; i++ )
00669 {
00670 Points[i].dvSX = i;
00671 Points[i].dvSY = 100-(float)(car->Revs/300);
00672 Points[i].dvSZ = 1/(float)65536;
00673 Points[i].dcColor = RGB_MAKE( 255,0,0 );
00674 }
00675
00676
00677
00678 float Vel = (float) sqrt( (car->Velocity[0]*car->Velocity[0])+
00679 (car->Velocity[1]*car->Velocity[1])+
00680 (car->Velocity[2]*car->Velocity[2]) );
00681
00682 char buff[128];
00683 wsprintf(buff, "RPM: %5d Actual MPH :%2d.%02d Acc: %d Debug: %3d,%3d,%3d",
00684 (int)car->Revs,
00685 ((int)((car->MasterVelocity*IDEALFRAMERATE*3600)/(METERSPERMILE*WORLDMETER))),
00686 abs((int)((car->MasterVelocity*IDEALFRAMERATE*3600)/(METERSPERMILE*WORLDMETER)*100) - (((int)((car->MasterVelocity*72*3600)/(METERSPERMILE*200)))*100))
00687 ,(int)((car->Acceleration*100)/TimeFactor)
00688 ,(int)Debug0,(int)Debug1,(int)Debug2
00689
00690 );
00691 DumpText(0, 16, 8, 16, 0x808080, buff);
00692
00693 D3Ddevice->DrawPrimitive(D3DPT_POINTLIST, D3DVT_TLVERTEX, &Points, 8, NULL );
00694
00695
00696 }
00697
00698
00699
00700
00701 float GetScalar( float *Matrix, float *In )
00702 {
00703
00704 float Vect[3], Vect2[3];
00705
00706 SetVector( Vect, 0, 0, 1 );
00707 RotVector( Matrix, Vect, Vect2 );
00708
00709 return ((In[0]*Vect2[0]) +(In[1]*Vect2[1]) +(In[2]*Vect2[2]));
00710
00711 }
00712
00713
00714
00715 void WholeCollision( CAR *car )
00716 {
00717
00718 float After[3];
00719 float ReturnedFriction;
00720 float Normal[3];
00721 float Exit[3];
00722 float Velocity[3];
00723 float TotalDisplacement, ActualDisplacement;
00724 float Old[3];
00725 float Current[3];
00726
00727
00728 CollisionDeadLock++;
00729
00730 if( CollisionDeadLock > 7 )
00731 return;
00732
00733
00734
00735
00736 Old[0] = car->LastPos[0];
00737 Old[1] = car->LastPos[1];
00738 Old[2] = car->LastPos[2];
00739
00740 Current[0] = car->WorldPos[0];
00741 Current[1] = car->WorldPos[1];
00742 Current[2] = car->WorldPos[2];
00743
00744 if( SphereCollTestNorm( Old, Current, 10, After, &ReturnedFriction, Normal ) )
00745 {
00746
00747
00748
00749 car->Gravity = 0;
00750
00751
00752
00753
00754
00755
00756
00757
00758 car->WorldPos[0] = After[0];
00759 car->WorldPos[1] = After[1];
00760 car->WorldPos[2] = After[2];
00761
00762
00763 WholeCollision( car );
00764
00765 }
00766
00767 Debug0 = car->WorldPos[0];
00768 Debug1 = car->WorldPos[1];
00769 Debug2 = car->WorldPos[2];
00770
00771
00772
00773 }
00774
00775
00776
00777 void GoodCollision( CAR *car )
00778 {
00779
00780 float After[3];
00781 float ReturnedFriction;
00782 float Normal[3];
00783 float Exit[3];
00784 float Velocity[3];
00785 float TotalDisplacement, ActualDisplacement;
00786 float Old[3];
00787 float Current[3];
00788
00789
00790 CollisionDeadLock++;
00791
00792 if( CollisionDeadLock > 7 )
00793 return;
00794
00795 Old[0] = car->LastPos[0];
00796 Old[1] = car->LastPos[1];
00797 Old[2] = car->LastPos[2];
00798
00799 Current[0] = car->WorldPos[0];
00800 Current[1] = car->WorldPos[1];
00801 Current[2] = car->WorldPos[2];
00802
00803 if( SphereCollTest( Old, Current, 10, After, &ReturnedFriction, Normal ) )
00804 {
00805
00806
00807
00808 TotalDisplacement = (float)sqrt( ((car->WorldPos[0]-car->LastPos[0])*(car->WorldPos[0]-car->LastPos[0])) +
00809 ((car->WorldPos[1]-car->LastPos[1])*(car->WorldPos[1]-car->LastPos[1])) +
00810 ((car->WorldPos[2]-car->LastPos[2])*(car->WorldPos[2]-car->LastPos[2]))
00811 );
00812
00813 ActualDisplacement = (float)sqrt( ((After[0]-car->LastPos[0])*(After[0]-car->LastPos[0])) +
00814 ((After[1]-car->LastPos[1])*(After[1]-car->LastPos[1])) +
00815 ((After[2]-car->LastPos[2])*(After[2]-car->LastPos[2]))
00816 );
00817
00818
00819
00820
00821 Velocity[0] = (car->WorldPos[0] - car->LastPos[0])/TimeFactor;
00822 Velocity[1] = (car->WorldPos[1] - car->LastPos[1])/TimeFactor;
00823 Velocity[2] = (car->WorldPos[2] - car->LastPos[2])/TimeFactor;
00824
00825
00826
00827
00828
00829 car->Gravity = 0;
00830
00831
00832
00833
00834 car->LastPos[0] = car->WorldPos[0] = After[0];
00835 car->LastPos[1] = car->WorldPos[1] = After[1];
00836 car->LastPos[2] = car->WorldPos[2] = After[2];
00837
00838
00839
00840
00841
00842 BounceAgainstPlane2( Velocity, Normal, Exit );
00843
00844 car->WorldPos[0] += Exit[0]*abs(TotalDisplacement-ActualDisplacement)/TotalDisplacement*TimeFactor;
00845 car->WorldPos[1] += Exit[1]*abs(TotalDisplacement-ActualDisplacement)/TotalDisplacement*TimeFactor;
00846 car->WorldPos[2] += Exit[2]*abs(TotalDisplacement-ActualDisplacement)/TotalDisplacement*TimeFactor;
00847
00848 car->Velocity[0] = Exit[0];
00849 car->Velocity[1] = Exit[1];
00850 car->Velocity[2] = Exit[2];
00851
00852 GoodCollision( car );
00853
00854
00855 }
00856
00857
00858 }
00859
00860
00861
00863
00865
00866 void BuildCarFromWheels2(CAR *car)
00867 {
00868 float f[3], b[3], l[3], r[3], forward[3], right[3], up[3];
00869
00870
00871
00872 car->WorldPos[0] = (car->WheelFL.CollPoint[0] + car->WheelFR.CollPoint[0] + car->WheelBL.CollPoint[0] + car->WheelBR.CollPoint[0]) / 4;
00873 car->WorldPos[1] = (car->WheelFL.CollPoint[1] + car->WheelFR.CollPoint[1] + car->WheelBL.CollPoint[1] + car->WheelBR.CollPoint[1]) / 4;
00874 car->WorldPos[2] = (car->WheelFL.CollPoint[2] + car->WheelFR.CollPoint[2] + car->WheelBL.CollPoint[2] + car->WheelBR.CollPoint[2]) / 4;
00875
00876
00877
00878 f[0] = (car->WheelFL.CollPoint[0] + car->WheelFR.CollPoint[0]) / 2;
00879 f[1] = (car->WheelFL.CollPoint[1] + car->WheelFR.CollPoint[1]) / 2;
00880 f[2] = (car->WheelFL.CollPoint[2] + car->WheelFR.CollPoint[2]) / 2;
00881
00882 b[0] = (car->WheelBL.CollPoint[0] + car->WheelBR.CollPoint[0]) / 2;
00883 b[1] = (car->WheelBL.CollPoint[1] + car->WheelBR.CollPoint[1]) / 2;
00884 b[2] = (car->WheelBL.CollPoint[2] + car->WheelBR.CollPoint[2]) / 2;
00885
00886 l[0] = (car->WheelFL.CollPoint[0] + car->WheelBL.CollPoint[0]) / 2;
00887 l[1] = (car->WheelFL.CollPoint[1] + car->WheelBL.CollPoint[1]) / 2;
00888 l[2] = (car->WheelFL.CollPoint[2] + car->WheelBL.CollPoint[2]) / 2;
00889
00890 r[0] = (car->WheelFR.CollPoint[0] + car->WheelBR.CollPoint[0]) / 2;
00891 r[1] = (car->WheelFR.CollPoint[1] + car->WheelBR.CollPoint[1]) / 2;
00892 r[2] = (car->WheelFR.CollPoint[2] + car->WheelBR.CollPoint[2]) / 2;
00893
00894
00895
00896 SubVector(f, b, forward);
00897 SubVector(r, l, right);
00898
00899 CrossProduct(forward, right, up);
00900 CrossProduct(up, forward, right);
00901
00902
00903
00904 Normalize(right)
00905 car->Matrix[0] = right[0];
00906 car->Matrix[1] = right[1];
00907 car->Matrix[2] = right[2];
00908
00909 Normalize(up);
00910 car->Matrix[3] = up[0];
00911 car->Matrix[4] = up[1];
00912 car->Matrix[5] = up[2];
00913
00914 Normalize(forward);
00915 car->Matrix[6] = forward[0];
00916 car->Matrix[7] = forward[1];
00917 car->Matrix[8] = forward[2];
00918 }
00919