vehiclephys.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879
  1. /*
  2. ** Command & Conquer Renegade(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /***********************************************************************************************
  19. *** C O N F I D E N T I A L --- W E S T W O O D S T U D I O S ***
  20. ***********************************************************************************************
  21. * *
  22. * Project Name : WWPhys *
  23. * *
  24. * $Archive:: /Commando/Code/wwphys/vehiclephys.cpp $*
  25. * *
  26. * Original Author:: Greg Hjelstrom *
  27. * *
  28. * $Author:: Byon_g $*
  29. * *
  30. * $Modtime:: 12/12/01 10:37a $*
  31. * *
  32. * $Revision:: 26 $*
  33. * *
  34. *---------------------------------------------------------------------------------------------*
  35. * Functions: *
  36. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  37. #include "vehiclephys.h"
  38. #include "chunkio.h"
  39. #include "persistfactory.h"
  40. #include "wwphysids.h"
  41. #include "wheel.h"
  42. #include "physcontrol.h"
  43. #include "octbox.h"
  44. #include "pscene.h"
  45. #include "wwprofile.h"
  46. #include "vehicledazzle.h"
  47. // Vehicles will sit rolled over for this long before exploding!
  48. const float EXPIRE_SECONDS = 4.0f;
  49. // HACK! when the engine is off, decimate the momentum each timestep by this fraction...
  50. const float PARKING_BRAKE_DAMPING = 0.5f;
  51. // max number of auxiliary graphical bones to support (e.g. engine flames)
  52. const int MAX_CAPTURED_BONE_COUNT = 4;
  53. const char * ENGINE_FLAME_BONE_NAME = "ENGINEFLAME";
  54. // Wheel parsing constants...
  55. const char * WHEELP_BONE_NAME = "WheelP"; // Position bone (contact patch)
  56. const char * WHEELC_BONE_NAME = "WheelC"; // Center bone (rotational center of the wheel)
  57. const char * WHEELF_BONE_NAME = "WheelF"; // Fork constraint bone (e.g. back wheel of a motorcycle)
  58. const char * WHEELT_BONE_NAME = "WheelT"; // Translation constraint bone (e.g. front wheel of a motorcycle)
  59. const int WHEEL_INDEX_CHAR0 = 6;
  60. const int WHEEL_INDEX_CHAR1 = 7;
  61. const int WHEEL_FLAG_CHAR0 = 8;
  62. // Wheel flags (begin appearing at character WHEEL_FLAG_CHAR0)
  63. const char WHEEL_FLAG_STEERING = 'S'; // wheel turns with steering
  64. const char WHEEL_FLAG_INV_STEERING = 'I'; // wheel turns opposite steering
  65. const char WHEEL_FLAG_TILT_STEERING = 'T'; // wheel turns when vehicle (bike) tilts
  66. const char WHEEL_FLAG_ENGINE = 'E'; // wheel is attached to engine
  67. const char WHEEL_FLAG_LEFT_TRACK = 'L'; // wheel is part of the left track
  68. const char WHEEL_FLAG_RIGHT_TRACK = 'R'; // wheel is part of the right track
  69. const char WHEEL_FLAG_FAKE = 'F'; // wheel is fake!
  70. /***********************************************************************************************
  71. **
  72. ** VehiclePhysClass Implementation
  73. **
  74. ***********************************************************************************************/
  75. bool VehiclePhysClass::_DisableVehicleSimulation = false;
  76. bool VehiclePhysClass::_DisableVehicleRendering = false;
  77. /*
  78. ** Chunk Ids used by VehiclePhysClass
  79. */
  80. enum
  81. {
  82. VEHICLEPHYS_CHUNK_RIGIDBODY = 405001445, // parent class
  83. VEHICLEPHYS_CHUNK_VARIABLES,
  84. VEHICLEPHYS_VARIABLE_ISENGINEON = 0,
  85. };
  86. VehiclePhysClass::VehiclePhysClass(void) :
  87. IsEngineOn(false),
  88. RealWheelCount(0),
  89. DriveWheelCount(0),
  90. EngineFlameBones(MAX_CAPTURED_BONE_COUNT),
  91. LastGoodPosition(1),
  92. ExpireTimer(EXPIRE_SECONDS)
  93. {
  94. for (int i=0; i<MAX_CAPTURED_BONE_COUNT; i++) {
  95. EngineFlameBones[i] = -1;
  96. }
  97. Enable_Shadow_Generation(true);
  98. }
  99. void VehiclePhysClass::Init(const VehiclePhysDefClass & def)
  100. {
  101. RigidBodyClass::Init(def);
  102. for (int i=0; i<MAX_CAPTURED_BONE_COUNT; i++) {
  103. EngineFlameBones[i] = -1;
  104. }
  105. Enable_Shadow_Generation(true);
  106. }
  107. void VehiclePhysClass::Definition_Changed(void)
  108. {
  109. RigidBodyClass::Definition_Changed();
  110. for (int i=0; i<Wheels.Length(); i++) {
  111. const VehiclePhysDefClass * def = Get_VehiclePhysDef();
  112. Wheels[i]->Set_Spring_Constant(def->Get_Spring_Constant());
  113. Wheels[i]->Set_Damping_Coefficient(def->Get_Damping_Constant());
  114. Wheels[i]->Set_Spring_Length(def->Get_Spring_Length());
  115. }
  116. }
  117. VehiclePhysClass::~VehiclePhysClass(void)
  118. {
  119. Release_Wheels();
  120. Release_Auxiliary_Bones();
  121. Release_Dazzles();
  122. }
  123. void VehiclePhysClass::Update_Wheels (void)
  124. {
  125. /*
  126. ** update the wheels graphically
  127. */
  128. Set_Stationary_Collision_Region();
  129. Inc_Ignore_Counter();
  130. for (int i=0; i<Wheels.Length(); i++) {
  131. Wheels[i]->Update_Model();
  132. }
  133. PhysicsSceneClass::Get_Instance()->Release_Collision_Region();
  134. Dec_Ignore_Counter();
  135. return ;
  136. }
  137. void VehiclePhysClass::Non_Physical_Wheel_Update(float suspension_fraction,float rotation)
  138. {
  139. for (int i=0; i<Wheels.Length(); i++) {
  140. Wheels[i]->Non_Physical_Update(suspension_fraction,rotation);
  141. }
  142. }
  143. void VehiclePhysClass::Render(RenderInfoClass & rinfo)
  144. {
  145. const VehiclePhysDefClass * def = Get_VehiclePhysDef();
  146. /*
  147. ** update the wheels graphically
  148. */
  149. Update_Wheels();
  150. /*
  151. ** update the engine flames
  152. */
  153. Matrix3D flame_tm(1);
  154. flame_tm.Translate_Z(-Get_Normalized_Engine_Flame() * def->EngineFlameLength);
  155. for (int ibone=0; ibone<MAX_CAPTURED_BONE_COUNT; ibone++) {
  156. if (EngineFlameBones[ibone] != -1) {
  157. Model->Control_Bone(EngineFlameBones[ibone],flame_tm);
  158. }
  159. }
  160. /*
  161. ** Update the dazzles
  162. */
  163. for (int idazzle=0; idazzle<Dazzles.Length(); idazzle++) {
  164. Dazzles[idazzle]->Pre_Render_Update(this);
  165. }
  166. /*
  167. ** Pass on up!
  168. */
  169. RigidBodyClass::Render(rinfo);
  170. }
  171. void VehiclePhysClass::Set_Model(RenderObjClass * model)
  172. {
  173. Release_Wheels();
  174. Release_Auxiliary_Bones();
  175. RigidBodyClass::Set_Model(model);
  176. Update_Cached_Model_Parameters();
  177. }
  178. float VehiclePhysClass::Compute_Approximate_Ride_Height(void)
  179. {
  180. float val = 0.0f;
  181. if (ContactBox != NULL) {
  182. OBBoxClass box;
  183. ContactBox->Get_Inner_Box(&box,Quaternion(1),Vector3(0,0,0));
  184. val += (box.Extent.Z - box.Center.Z);
  185. if (Wheels.Length() > 0) {
  186. val += 0.66f * Wheels[0]->Get_Spring_Length();
  187. } else {
  188. val += 0.6f;
  189. }
  190. } else {
  191. val = 1.0f;
  192. }
  193. return val;
  194. }
  195. void VehiclePhysClass::Timestep(float dt)
  196. {
  197. WWPROFILE("VehiclePhysClass::Timestep");
  198. int i;
  199. /*
  200. ** Take a copy of our state for later "roll testing"
  201. */
  202. RigidBodyStateStruct initial_state = State;
  203. /*
  204. ** If the vehicle is not in use and it is on the ground, dampen the hell out of it
  205. */
  206. #if 0
  207. int contact_count=0;
  208. for (i=0; i<Wheels.Length(); i++) {
  209. if ((Wheels[i] != NULL) && (Wheels[i]->Get_Flag(SuspensionElementClass::INCONTACT) == true)) {
  210. contact_count++;
  211. }
  212. }
  213. if ((contact_count >= 3) && (Is_Engine_Enabled() == false)) {
  214. State.LMomentum *= PARKING_BRAKE_DAMPING;
  215. State.AMomentum *= PARKING_BRAKE_DAMPING;
  216. }
  217. #endif
  218. /*
  219. ** If there are wheels contacting the ground, turn off friction for the body of the vehicle
  220. */
  221. bool wheels_touching_ground = false;
  222. for (i=0; i<Wheels.Length(); i++) {
  223. if ((Wheels[i] != NULL) && (Wheels[i]->Get_Flag(SuspensionElementClass::INCONTACT) == true)) {
  224. wheels_touching_ground = true;
  225. }
  226. }
  227. Set_Flag(FRICTION_DISABLED,wheels_touching_ground);
  228. /*
  229. ** Call to parent class
  230. */
  231. if (Get_VehiclePhysDef()->IsFake == false) {
  232. RigidBodyClass::Timestep(dt);
  233. }
  234. /*
  235. ** See if we should be destroyed due to coming to rest upside down
  236. */
  237. float up_cos = Get_Transform().Get_Z_Vector().Z;
  238. const float MIN_Z_COSINE = 0.25f;
  239. if (up_cos < MIN_Z_COSINE) {
  240. ExpireTimer -= dt;
  241. if (ExpireTimer < 0.0f) {
  242. ExpireTimer = EXPIRE_SECONDS; // if expiration is denied, try again later.
  243. Expire();
  244. }
  245. } else {
  246. ExpireTimer = EXPIRE_SECONDS;
  247. }
  248. }
  249. SuspensionElementClass * VehiclePhysClass::Peek_Wheel(int wheel_index)
  250. {
  251. return Wheels[wheel_index];
  252. }
  253. void VehiclePhysClass::Compute_Force_And_Torque(Vector3 * force,Vector3 * torque)
  254. {
  255. {
  256. WWPROFILE("VehiclePhysClass::Compute_Force_And_Torque");
  257. /*
  258. ** Compute forces and torques for each wheel.
  259. */
  260. int goodwheels = 0;
  261. for (int iwheel = 0; iwheel<Wheels.Length(); iwheel++) {
  262. Wheels[iwheel]->Compute_Force_And_Torque(force,torque);
  263. if (Wheels[iwheel]->Get_Flag(WheelClass::INCONTACT)) {
  264. goodwheels++;
  265. }
  266. }
  267. /*
  268. ** If all of our wheels are contacting and we are mostly upright,
  269. ** remember this transform as a "good" position.
  270. */
  271. if ((goodwheels == Wheels.Length()) && (Rotation.Get_Z_Vector().Z > 0.7f)) {
  272. LastGoodPosition.Set(Rotation,State.Position);
  273. }
  274. }
  275. /*
  276. ** Let base class add in its forces
  277. */
  278. RigidBodyClass::Compute_Force_And_Torque(force,torque);
  279. }
  280. bool VehiclePhysClass::Can_Go_To_Sleep(float dt)
  281. {
  282. /*
  283. ** Vehicles go to sleep if at least three wheels are in contact and their
  284. ** velocities are below some thresh-hold and their controller isn't doing anything.
  285. */
  286. if ((Controller != NULL) && (Controller->Is_Inactive() != true)) {
  287. GoToSleepTimer = RBODY_SLEEP_DELAY;
  288. return false;
  289. }
  290. /*
  291. ** Check our velocities
  292. */
  293. const float VEL_THRESHHOLD = 0.10f;
  294. const float AVEL_THRESHHOLD = 0.10f;
  295. float max_lmomentum2 = Mass * Mass * VEL_THRESHHOLD * VEL_THRESHHOLD;
  296. float max_amomentum2 = IBody[1][1] * IBody[1][1] * AVEL_THRESHHOLD * AVEL_THRESHHOLD;
  297. bool tried_to_sleep = false;
  298. if ((State.LMomentum.Length2() < max_lmomentum2) &&
  299. (State.AMomentum.Length2() < max_amomentum2) )
  300. {
  301. /*
  302. ** Count the contacts
  303. */
  304. int contact_count = 0;
  305. for (int iwheel = 0; iwheel<Wheels.Length(); iwheel++) {
  306. if (Wheels[iwheel]->Get_Flag(SuspensionElementClass::INCONTACT)) {
  307. contact_count++;
  308. }
  309. }
  310. if ((Wheels.Length() <= 2) || (contact_count >= 3) || (ContactBox->ContactCount >= 3)) {
  311. tried_to_sleep = true;
  312. if (GoToSleepTimer < 0.0f) {
  313. return true;
  314. }
  315. }
  316. }
  317. if (tried_to_sleep) {
  318. GoToSleepTimer -= dt;
  319. } else {
  320. GoToSleepTimer = RBODY_SLEEP_DELAY;
  321. }
  322. return false;
  323. }
  324. void VehiclePhysClass::Update_Cached_Model_Parameters(void)
  325. {
  326. if (Model == NULL) return;
  327. // capture the wheels
  328. Release_Wheels();
  329. Create_Wheels();
  330. // capture auxiliary bones
  331. Release_Auxiliary_Bones();
  332. Capture_Auxiliary_Bones();
  333. // count the drive wheels and real wheels
  334. DriveWheelCount = 0;
  335. RealWheelCount = 0;
  336. for (int i=0; i<Wheels.Length(); i++) {
  337. if ( Wheels[i]->Get_Flag(SuspensionElementClass::ENGINE) ||
  338. Wheels[i]->Get_Flag(SuspensionElementClass::LEFT_TRACK) ||
  339. Wheels[i]->Get_Flag(SuspensionElementClass::RIGHT_TRACK) )
  340. {
  341. DriveWheelCount++;
  342. }
  343. // "Real" wheels are all of them except the fake ones.
  344. if (Wheels[i]->Get_Flag(SuspensionElementClass::FAKE) == false) {
  345. RealWheelCount++;
  346. }
  347. }
  348. // cache pointers to the dazzles
  349. Release_Dazzles();
  350. Capture_Dazzles();
  351. // Force the shadow manager to use a scaled version of our "blob box"
  352. ShadowManager.Enable_Force_Use_Blob_Box(true);
  353. ShadowManager.Set_Blob_Box_Projection_Scale(Vector3(1.5f,1.5f,1.5f));
  354. }
  355. void VehiclePhysClass::Release_Wheels(void)
  356. {
  357. // release the bones we "captured" and destroy the wheels
  358. for (int i=0; i<Wheels.Length(); i++) {
  359. delete Wheels[i];
  360. Wheels[i] = NULL;
  361. }
  362. Wheels.Resize(0);
  363. DriveWheelCount = 0;
  364. RealWheelCount = 0;
  365. }
  366. void VehiclePhysClass::Create_Wheels(void)
  367. {
  368. RenderObjClass * model = Peek_Model();
  369. if (model == NULL) return;
  370. const VehiclePhysDefClass * def = Get_VehiclePhysDef();
  371. WWASSERT(def != NULL);
  372. int ibone;
  373. int wheelcount = 0;
  374. // Count the wheels.
  375. for (ibone=0; ibone < model->Get_Num_Bones(); ibone++) {
  376. // search for bones named WheelP
  377. const char * bonename = model->Get_Bone_Name(ibone);
  378. if (_strnicmp(bonename,WHEELP_BONE_NAME,strlen(WHEELP_BONE_NAME)) == 0) {
  379. wheelcount++;
  380. }
  381. }
  382. if (wheelcount == 0) {
  383. return;
  384. }
  385. // Allocate the array of pointers for the wheels
  386. Wheels.Resize(wheelcount);
  387. for (int i=0; i<Wheels.Length(); i++) {
  388. Wheels[i] = NULL;
  389. }
  390. int curwheel = 0;
  391. for (ibone=0; (ibone < model->Get_Num_Bones()) && (curwheel < Wheels.Length()); ibone++) {
  392. // for each bone named WheelP
  393. const char * wpname = model->Get_Bone_Name(ibone);
  394. if (_strnicmp(wpname,WHEELP_BONE_NAME,strlen(WHEELP_BONE_NAME)) == 0) {
  395. int position_bone = ibone;
  396. int rotation_bone = Find_Rotation_Bone(model,wpname);
  397. int fork_bone = Find_Fork_Bone(model,wpname);
  398. int trans_bone = Find_Translation_Bone(model,wpname);
  399. if (position_bone != -1) {
  400. // initialize a wheel structure:
  401. SuspensionElementClass * new_wheel = Alloc_Suspension_Element();
  402. new_wheel->Init(this,position_bone,rotation_bone,fork_bone,trans_bone);
  403. new_wheel->Set_Spring_Constant(def->SpringConstant);
  404. new_wheel->Set_Damping_Coefficient(def->DampingConstant);
  405. new_wheel->Set_Spring_Length(def->SpringLength);
  406. // parse any flag characters
  407. unsigned int index = WHEEL_FLAG_CHAR0;
  408. while (index < strlen(wpname)) {
  409. switch (wpname[index]) {
  410. case WHEEL_FLAG_STEERING:
  411. new_wheel->Set_Flag(SuspensionElementClass::STEERING,true);
  412. break;
  413. case WHEEL_FLAG_INV_STEERING:
  414. new_wheel->Set_Flag(SuspensionElementClass::INV_STEERING,true);
  415. break;
  416. case WHEEL_FLAG_TILT_STEERING:
  417. new_wheel->Set_Flag(SuspensionElementClass::TILT_STEERING,true);
  418. break;
  419. case WHEEL_FLAG_ENGINE:
  420. new_wheel->Set_Flag(SuspensionElementClass::ENGINE,true);
  421. break;
  422. case WHEEL_FLAG_LEFT_TRACK:
  423. new_wheel->Set_Flag(SuspensionElementClass::LEFT_TRACK,true);
  424. break;
  425. case WHEEL_FLAG_RIGHT_TRACK:
  426. new_wheel->Set_Flag(SuspensionElementClass::RIGHT_TRACK,true);
  427. break;
  428. case WHEEL_FLAG_FAKE:
  429. new_wheel->Set_Flag(SuspensionElementClass::FAKE,true);
  430. break;
  431. }
  432. index++;
  433. }
  434. // install the wheel
  435. Wheels[curwheel++] = new_wheel;
  436. }
  437. }
  438. }
  439. }
  440. int VehiclePhysClass::Find_Fork_Bone(RenderObjClass * model,const char * wpname)
  441. {
  442. for (int ibone=0; ibone < model->Get_Num_Bones(); ibone++) {
  443. const char * wfname = model->Get_Bone_Name(ibone);
  444. if ( (_strnicmp(wfname,WHEELF_BONE_NAME,strlen(WHEELF_BONE_NAME)) == 0) &&
  445. (wfname[WHEEL_INDEX_CHAR0] == wpname[WHEEL_INDEX_CHAR0]) &&
  446. (wfname[WHEEL_INDEX_CHAR1] == wpname[WHEEL_INDEX_CHAR1]))
  447. {
  448. return ibone;
  449. }
  450. }
  451. return -1;
  452. }
  453. int VehiclePhysClass::Find_Rotation_Bone(RenderObjClass * model,const char * wpname)
  454. {
  455. for (int ibone=0; ibone < model->Get_Num_Bones(); ibone++) {
  456. const char * wcname = model->Get_Bone_Name(ibone);
  457. if ( (_strnicmp(wcname,WHEELC_BONE_NAME,strlen(WHEELC_BONE_NAME)) == 0) &&
  458. (wcname[WHEEL_INDEX_CHAR0] == wpname[WHEEL_INDEX_CHAR0]) &&
  459. (wcname[WHEEL_INDEX_CHAR1] == wpname[WHEEL_INDEX_CHAR1]))
  460. {
  461. return ibone;
  462. }
  463. }
  464. return -1;
  465. }
  466. int VehiclePhysClass::Find_Translation_Bone(RenderObjClass * model,const char * wpname)
  467. {
  468. for (int ibone=0; ibone < model->Get_Num_Bones(); ibone++) {
  469. const char * wtname = model->Get_Bone_Name(ibone);
  470. if ( (_strnicmp(wtname,WHEELT_BONE_NAME,strlen(WHEELT_BONE_NAME)) == 0) &&
  471. (wtname[WHEEL_INDEX_CHAR0] == wpname[WHEEL_INDEX_CHAR0]) &&
  472. (wtname[WHEEL_INDEX_CHAR1] == wpname[WHEEL_INDEX_CHAR1]))
  473. {
  474. return ibone;
  475. }
  476. }
  477. return -1;
  478. }
  479. void VehiclePhysClass::Release_Auxiliary_Bones(void)
  480. {
  481. // release any bones that we currently have captured
  482. if (Model != NULL) {
  483. for (int i=0;i<MAX_CAPTURED_BONE_COUNT; i++) {
  484. if (EngineFlameBones[i] != -1) {
  485. Model->Release_Bone(EngineFlameBones[i]);
  486. EngineFlameBones[i] = -1;
  487. }
  488. }
  489. }
  490. }
  491. void VehiclePhysClass::Capture_Auxiliary_Bones(void)
  492. {
  493. // search through the model for bones beginning with ENGINEFLAME
  494. int ibone = 0;
  495. int engine_bone_count = 0;
  496. for (ibone=0; (ibone < Model->Get_Num_Bones()) && (engine_bone_count < MAX_CAPTURED_BONE_COUNT); ibone++) {
  497. const char * bone_name = Model->Get_Bone_Name(ibone);
  498. if (_strnicmp(bone_name,ENGINE_FLAME_BONE_NAME,strlen(ENGINE_FLAME_BONE_NAME)) == 0) {
  499. EngineFlameBones[engine_bone_count] = ibone;
  500. Model->Capture_Bone(ibone);
  501. engine_bone_count++;
  502. }
  503. }
  504. }
  505. void VehiclePhysClass::Release_Dazzles(void)
  506. {
  507. // delete the dazzle controllers
  508. for (int i=0; i<Dazzles.Length(); i++) {
  509. delete Dazzles[i];
  510. Dazzles[i] = NULL;
  511. }
  512. Dazzles.Resize(0);
  513. }
  514. void VehiclePhysClass::Capture_Dazzles(void)
  515. {
  516. RenderObjClass * model = Peek_Model();
  517. if (model == NULL) return;
  518. const VehiclePhysDefClass * def = Get_VehiclePhysDef();
  519. WWASSERT(def != NULL);
  520. int imodel;
  521. int dazzlecount = 0;
  522. // Count the dazzles.
  523. for (imodel=0; imodel < model->Get_Num_Sub_Objects(); imodel++) {
  524. // Search for dazzle render objects whose name starts with _HLight, _TLight, or _BLight
  525. RenderObjClass * subobj = model->Get_Sub_Object(imodel);
  526. if ((subobj != NULL) && (subobj->Class_ID() == RenderObjClass::CLASSID_DAZZLE)) {
  527. if (VehicleDazzleClass::Is_Vehicle_Dazzle(subobj)) {
  528. dazzlecount++;
  529. }
  530. }
  531. REF_PTR_RELEASE(subobj);
  532. }
  533. // Return if we didn't find any dazzles that want to be controlled by the vehicle logic
  534. if (dazzlecount == 0) {
  535. return;
  536. }
  537. // Allocate the array of pointers for the wheels
  538. Dazzles.Resize(dazzlecount);
  539. for (int i=0; i<Dazzles.Length(); i++) {
  540. Dazzles[i] = NULL;
  541. }
  542. // Create dazzle controllers
  543. int curdazzle = 0;
  544. for (imodel=0; (imodel < model->Get_Num_Sub_Objects()) && (curdazzle < Dazzles.Length()); imodel++) {
  545. RenderObjClass * subobj = model->Get_Sub_Object(imodel);
  546. if (VehicleDazzleClass::Is_Vehicle_Dazzle(subobj)) {
  547. Dazzles[curdazzle] = Create_Dazzle_Controller(subobj);
  548. curdazzle++;
  549. }
  550. REF_PTR_RELEASE(subobj);
  551. }
  552. }
  553. VehicleDazzleClass * VehiclePhysClass::Create_Dazzle_Controller(RenderObjClass * obj)
  554. {
  555. WWASSERT(VehicleDazzleClass::Is_Vehicle_Dazzle(obj));
  556. VehicleDazzleClass * controller = new VehicleDazzleClass();
  557. controller->Set_Model((DazzleRenderObjClass *)obj);
  558. return controller;
  559. }
  560. void VehiclePhysClass::Teleport_To_Last_Good_Position(void)
  561. {
  562. Set_Transform(LastGoodPosition);
  563. }
  564. bool VehiclePhysClass::Is_In_Contact(void)
  565. {
  566. bool retval = false;
  567. //
  568. // Check to see if any of the wheels are on the ground
  569. //
  570. for (int index = 0; index < Wheels.Length(); index++) {
  571. //
  572. // Is this wheel on the ground?
  573. //
  574. if (Wheels[index]->Get_Flag(WheelClass::INCONTACT)) {
  575. retval = true;
  576. break;
  577. }
  578. }
  579. return retval;
  580. }
  581. bool VehiclePhysClass::Save(ChunkSaveClass &csave)
  582. {
  583. csave.Begin_Chunk(VEHICLEPHYS_CHUNK_RIGIDBODY);
  584. RigidBodyClass::Save(csave);
  585. csave.End_Chunk();
  586. csave.Begin_Chunk(VEHICLEPHYS_CHUNK_VARIABLES);
  587. WRITE_MICRO_CHUNK(csave,VEHICLEPHYS_VARIABLE_ISENGINEON,IsEngineOn);
  588. csave.End_Chunk();
  589. return true;
  590. }
  591. bool VehiclePhysClass::Load(ChunkLoadClass &cload)
  592. {
  593. while (cload.Open_Chunk()) {
  594. switch(cload.Cur_Chunk_ID())
  595. {
  596. case VEHICLEPHYS_CHUNK_RIGIDBODY:
  597. RigidBodyClass::Load(cload);
  598. break;
  599. case VEHICLEPHYS_CHUNK_VARIABLES:
  600. while (cload.Open_Micro_Chunk()) {
  601. switch(cload.Cur_Micro_Chunk_ID()) {
  602. READ_MICRO_CHUNK(cload,VEHICLEPHYS_VARIABLE_ISENGINEON,IsEngineOn);
  603. }
  604. cload.Close_Micro_Chunk();
  605. }
  606. break;
  607. default:
  608. WWDEBUG_SAY(("Unhandled Chunk: 0x%X File: %s Line: %d\r\n",cload.Cur_Chunk_ID(),__FILE__,__LINE__));
  609. break;
  610. }
  611. cload.Close_Chunk();
  612. }
  613. return true;
  614. }
  615. /***********************************************************************************************
  616. **
  617. ** VehiclePhysDefClass Implementation
  618. **
  619. ***********************************************************************************************/
  620. /*
  621. ** Declare a PersistFactory for VehiclePhysDefClass
  622. ** Dont declare a Definition Factory because we don't want users to directly create this type.
  623. */
  624. SimplePersistFactoryClass<VehiclePhysDefClass,PHYSICS_CHUNKID_VEHICLEPHYSDEF> _VehiclePhysDefFactory;
  625. /*
  626. ** Chunk ID's used by VehiclePhysDefClass
  627. */
  628. enum
  629. {
  630. VEHICLEPHYSDEF_CHUNK_RIGIDBODYDEF = 405001519, // (parent class)
  631. VEHICLEPHYSDEF_CHUNK_VARIABLES,
  632. VEHICLEPHYSDEF_VARIABLE_SPRINGCONSTANT = 0x00,
  633. VEHICLEPHYSDEF_VARIABLE_DAMPINGCONSTANT,
  634. VEHICLEPHYSDEF_VARIABLE_SPRINGLENGTH,
  635. VEHICLEPHYSDEF_VARIABLE_TRACTIONMULTIPLIER,
  636. VEHICLEPHYSDEF_VARIABLE_LATERALMOMENTARM,
  637. VEHICLEPHYSDEF_VARIABLE_TRACTIVEMOMENTARM,
  638. VEHICLEPHYSDEF_VARIABLE_ENGINEFLAMELENGTH,
  639. VEHICLEPHYSDEF_VARIABLE_ISFAKE,
  640. };
  641. VehiclePhysDefClass::VehiclePhysDefClass(void) :
  642. SpringConstant(DEFAULT_SPRING_CONSTANT),
  643. DampingConstant(DEFAULT_DAMPING_COEFFICIENT),
  644. SpringLength(DEFAULT_SPRING_LENGTH),
  645. TractionMultiplier(2.0f),
  646. LateralMomentArm(0.0f),
  647. TractiveMomentArm(0.0f),
  648. EngineFlameLength(1.0f),
  649. IsFake(false)
  650. {
  651. // make our parameters editable!
  652. EDITABLE_PARAM(VehiclePhysDefClass, ParameterClass::TYPE_BOOL, IsFake);
  653. FLOAT_UNITS_PARAM(VehiclePhysDefClass, SpringConstant, 0.0f, 100000.0f,"N/m");
  654. FLOAT_UNITS_PARAM(VehiclePhysDefClass, DampingConstant, 0.0f, 100000.0f,"N/(m/s)");
  655. FLOAT_UNITS_PARAM(VehiclePhysDefClass, SpringLength, 0.0f, 100.0f,"m");
  656. FLOAT_EDITABLE_PARAM(VehiclePhysDefClass, TractionMultiplier, 0.5f, 5.0f);
  657. FLOAT_UNITS_PARAM(VehiclePhysDefClass, LateralMomentArm, 0.0f, 10.0f,"m");
  658. FLOAT_UNITS_PARAM(VehiclePhysDefClass, TractiveMomentArm, 0.0f, 10.0f,"m");
  659. FLOAT_UNITS_PARAM(VehiclePhysDefClass, EngineFlameLength, 0.0f, 100.0f,"m");
  660. }
  661. VehiclePhysDefClass::~VehiclePhysDefClass(void)
  662. {
  663. }
  664. uint32 VehiclePhysDefClass::Get_Class_ID (void) const
  665. {
  666. return CLASSID_VEHICLEPHYSDEF;
  667. }
  668. const PersistFactoryClass & VehiclePhysDefClass::Get_Factory (void) const
  669. {
  670. return _VehiclePhysDefFactory;
  671. }
  672. bool VehiclePhysDefClass::Save(ChunkSaveClass &csave)
  673. {
  674. csave.Begin_Chunk(VEHICLEPHYSDEF_CHUNK_RIGIDBODYDEF);
  675. RigidBodyDefClass::Save(csave);
  676. csave.End_Chunk();
  677. csave.Begin_Chunk(VEHICLEPHYSDEF_CHUNK_VARIABLES);
  678. WRITE_MICRO_CHUNK(csave,VEHICLEPHYSDEF_VARIABLE_SPRINGCONSTANT,SpringConstant);
  679. WRITE_MICRO_CHUNK(csave,VEHICLEPHYSDEF_VARIABLE_DAMPINGCONSTANT,DampingConstant);
  680. WRITE_MICRO_CHUNK(csave,VEHICLEPHYSDEF_VARIABLE_SPRINGLENGTH,SpringLength);
  681. WRITE_MICRO_CHUNK(csave,VEHICLEPHYSDEF_VARIABLE_TRACTIONMULTIPLIER,TractionMultiplier);
  682. WRITE_MICRO_CHUNK(csave,VEHICLEPHYSDEF_VARIABLE_LATERALMOMENTARM,LateralMomentArm);
  683. WRITE_MICRO_CHUNK(csave,VEHICLEPHYSDEF_VARIABLE_TRACTIVEMOMENTARM,TractiveMomentArm);
  684. WRITE_MICRO_CHUNK(csave,VEHICLEPHYSDEF_VARIABLE_ENGINEFLAMELENGTH,EngineFlameLength);
  685. WRITE_MICRO_CHUNK(csave,VEHICLEPHYSDEF_VARIABLE_ISFAKE,IsFake);
  686. csave.End_Chunk();
  687. return true;
  688. }
  689. bool VehiclePhysDefClass::Load(ChunkLoadClass &cload)
  690. {
  691. while (cload.Open_Chunk()) {
  692. switch(cload.Cur_Chunk_ID()) {
  693. case VEHICLEPHYSDEF_CHUNK_RIGIDBODYDEF:
  694. RigidBodyDefClass::Load(cload);
  695. break;
  696. case VEHICLEPHYSDEF_CHUNK_VARIABLES:
  697. while (cload.Open_Micro_Chunk()) {
  698. switch(cload.Cur_Micro_Chunk_ID()) {
  699. READ_MICRO_CHUNK(cload,VEHICLEPHYSDEF_VARIABLE_SPRINGCONSTANT,SpringConstant);
  700. READ_MICRO_CHUNK(cload,VEHICLEPHYSDEF_VARIABLE_DAMPINGCONSTANT,DampingConstant);
  701. READ_MICRO_CHUNK(cload,VEHICLEPHYSDEF_VARIABLE_SPRINGLENGTH,SpringLength);
  702. READ_MICRO_CHUNK(cload,VEHICLEPHYSDEF_VARIABLE_TRACTIONMULTIPLIER,TractionMultiplier);
  703. READ_MICRO_CHUNK(cload,VEHICLEPHYSDEF_VARIABLE_LATERALMOMENTARM,LateralMomentArm);
  704. READ_MICRO_CHUNK(cload,VEHICLEPHYSDEF_VARIABLE_TRACTIVEMOMENTARM,TractiveMomentArm);
  705. READ_MICRO_CHUNK(cload,VEHICLEPHYSDEF_VARIABLE_ENGINEFLAMELENGTH,EngineFlameLength);
  706. READ_MICRO_CHUNK(cload,VEHICLEPHYSDEF_VARIABLE_ISFAKE,IsFake);
  707. }
  708. cload.Close_Micro_Chunk();
  709. }
  710. break;
  711. default:
  712. WWDEBUG_SAY(("Unhandled Chunk: 0x%X File: %s Line: %d\r\n",cload.Cur_Chunk_ID(),__FILE__,__LINE__));
  713. break;
  714. }
  715. cload.Close_Chunk();
  716. }
  717. return true;
  718. }
  719. bool VehiclePhysDefClass::Is_Type(const char * type_name)
  720. {
  721. if (stricmp(type_name,VehiclePhysDefClass::Get_Type_Name()) == 0) {
  722. return true;
  723. } else {
  724. return RigidBodyDefClass::Is_Type(type_name);
  725. }
  726. }