vehicle.cpp 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "platform/platform.h"
  23. #include "T3D/vehicles/vehicle.h"
  24. #include "math/mMath.h"
  25. #include "console/simBase.h"
  26. #include "console/console.h"
  27. #include "console/consoleTypes.h"
  28. #include "console/engineAPI.h"
  29. #include "collision/clippedPolyList.h"
  30. #include "collision/planeExtractor.h"
  31. #include "core/stream/bitStream.h"
  32. #include "core/dnet.h"
  33. #include "T3D/gameBase/gameConnection.h"
  34. #include "T3D/fx/cameraFXMgr.h"
  35. #include "ts/tsShapeInstance.h"
  36. #include "T3D/fx/particleEmitter.h"
  37. #include "sfx/sfxSystem.h"
  38. #include "sfx/sfxProfile.h"
  39. #include "sfx/sfxSource.h"
  40. #include "math/mathIO.h"
  41. #include "scene/sceneRenderState.h"
  42. #include "T3D/trigger.h"
  43. #include "T3D/item.h"
  44. #include "gfx/primBuilder.h"
  45. #include "gfx/gfxDrawUtil.h"
  46. #include "materials/materialDefinition.h"
  47. #include "T3D/physics/physicsPlugin.h"
  48. #include "T3D/physics/physicsBody.h"
  49. #include "T3D/physics/physicsCollision.h"
  50. namespace {
  51. static U32 sWorkingQueryBoxStaleThreshold = 10; // The maximum number of ticks that go by before
  52. // the mWorkingQueryBox is considered stale and
  53. // needs updating. Set to -1 to disable.
  54. static F32 sWorkingQueryBoxSizeMultiplier = 2.0f; // How much larger should the mWorkingQueryBox be
  55. // made when updating the working collision list.
  56. // The larger this number the less often the working list
  57. // will be updated due to motion, but any non-static shape
  58. // that moves into the query box will not be noticed.
  59. // Client prediction
  60. const S32 sMaxWarpTicks = 3; // Max warp duration in ticks
  61. const S32 sMaxPredictionTicks = 30; // Number of ticks to predict
  62. // Physics and collision constants
  63. static F32 sRestTol = 0.5; // % of gravity energy to be at rest
  64. static S32 sRestCount = 10; // Consecutive ticks before comming to rest
  65. } // namespace {}
  66. // Trigger objects that are not normally collided with.
  67. static U32 sTriggerMask = ItemObjectType |
  68. TriggerObjectType |
  69. CorpseObjectType;
  70. IMPLEMENT_CONOBJECT(VehicleData);
  71. ConsoleDocClass( VehicleData,
  72. "@brief Base properties shared by all Vehicles (FlyingVehicle, HoverVehicle, "
  73. "WheeledVehicle).\n\n"
  74. "This datablock defines properties shared by all Vehicle types, but should "
  75. "not be instantiated directly. Instead, set the desired properties in the "
  76. "FlyingVehicleData, HoverVehicleData or WheeledVehicleData datablock.\n"
  77. "@section VehicleData_damage Damage\n\n"
  78. "The VehicleData class extends the basic energy/damage functionality provided "
  79. "by ShapeBaseData to include damage from collisions, as well as particle "
  80. "emitters activated automatically when damage levels reach user specified "
  81. "thresholds.\n\n"
  82. "The example below shows how to setup a Vehicle to:\n"
  83. "<ul>\n"
  84. " <li>take damage when colliding with another object\n"
  85. " <li>emit gray smoke particles from two locations on the Vehicle when damaged above 50%</li>\n"
  86. " <li>emit black smoke particles from two locations on the Vehicle when damaged above 85%</li>\n"
  87. " <li>emit bubbles when any active damage emitter point is underwater</li>\n"
  88. "</ul>\n\n"
  89. "@tsexample\n"
  90. "// damage from collisions\n"
  91. "collDamageMultiplier = 0.05;\n"
  92. "collDamageThresholdVel = 15;\n\n"
  93. "// damage levels\n"
  94. "damageLevelTolerance[0] = 0.5;\n"
  95. "damageEmitter[0] = GraySmokeEmitter; // emitter used when damage is >= 50%\n"
  96. "damageLevelTolerance[1] = 0.85;\n"
  97. "damageEmitter[1] = BlackSmokeEmitter; // emitter used when damage is >= 85%\n"
  98. "damageEmitter[2] = DamageBubbleEmitter; // emitter used instead of damageEmitter[0:1]\n"
  99. " // when offset point is underwater\n"
  100. "// emit offsets (used for all active damage level emitters)\n"
  101. "damageEmitterOffset[0] = \"0.5 3 1\";\n"
  102. "damageEmitterOffset[1] = \"-0.5 3 1\";\n"
  103. "numDmgEmitterAreas = 2;\n"
  104. "@endtsexample\n"
  105. "@ingroup Vehicles\n"
  106. );
  107. //----------------------------------------------------------------------------
  108. VehicleData::VehicleData()
  109. {
  110. shadowSize = 256;
  111. shadowProjectionDistance = 14.0f;
  112. maxSteeringAngle = M_PI_F/4.0f; // 45 deg.
  113. jetForce = 500;
  114. jetEnergyDrain = 0.8f;
  115. minJetEnergy = 1;
  116. steeringReturn = 0.0f;
  117. steeringReturnSpeedScale = 0.01f;
  118. powerSteering = false;
  119. dMemset( damageEmitterList, 0, sizeof( damageEmitterList ) );
  120. dMemset( damageEmitterOffset, 0, sizeof( damageEmitterOffset ) );
  121. dMemset( damageEmitterIDList, 0, sizeof( damageEmitterIDList ) );
  122. dMemset( damageLevelTolerance, 0, sizeof( damageLevelTolerance ) );
  123. numDmgEmitterAreas = 0;
  124. collDamageThresholdVel = 20;
  125. collDamageMultiplier = 0.05f;
  126. enablePhysicsRep = true;
  127. }
  128. //----------------------------------------------------------------------------
  129. bool VehicleData::preload(bool server, String &errorStr)
  130. {
  131. if (!Parent::preload(server, errorStr))
  132. return false;
  133. // Vehicle objects must define a collision detail
  134. if (!collisionDetails.size() || collisionDetails[0] == -1)
  135. {
  136. Con::errorf("VehicleData::preload failed: Vehicle models must define a collision-1 detail");
  137. errorStr = String::ToString("VehicleData: Couldn't load shape asset \"%s\"", mShapeAsset.getAssetId());
  138. return false;
  139. }
  140. U32 i;
  141. for( i=0; i<VC_NUM_DAMAGE_EMITTERS; i++ )
  142. {
  143. if( !damageEmitterList[i] && damageEmitterIDList[i] != 0 )
  144. {
  145. if( !Sim::findObject( damageEmitterIDList[i], damageEmitterList[i] ) )
  146. {
  147. Con::errorf( ConsoleLogEntry::General, "VehicleData::preload Invalid packet, bad datablockId(damageEmitter): 0x%x", damageEmitterIDList[i] );
  148. }
  149. }
  150. }
  151. return true;
  152. }
  153. //----------------------------------------------------------------------------
  154. void VehicleData::packData(BitStream* stream)
  155. {
  156. S32 i;
  157. Parent::packData(stream);
  158. stream->write(maxSteeringAngle);
  159. stream->write(jetForce);
  160. stream->write(jetEnergyDrain);
  161. stream->write(minJetEnergy);
  162. stream->write(steeringReturn);
  163. stream->write(steeringReturnSpeedScale);
  164. stream->writeFlag(powerSteering);
  165. stream->write( numDmgEmitterAreas );
  166. stream->write(enablePhysicsRep);
  167. for (i = 0; i < VC_NUM_DAMAGE_EMITTERS; i++)
  168. {
  169. if( stream->writeFlag( damageEmitterList[i] != NULL ) )
  170. {
  171. stream->writeRangedU32( damageEmitterList[i]->getId(), DataBlockObjectIdFirst, DataBlockObjectIdLast );
  172. }
  173. }
  174. for (S32 j = 0; j < VC_NUM_DAMAGE_EMITTER_AREAS; j++)
  175. {
  176. stream->write( damageEmitterOffset[j].x );
  177. stream->write( damageEmitterOffset[j].y );
  178. stream->write( damageEmitterOffset[j].z );
  179. }
  180. for (S32 k = 0; k < VC_NUM_DAMAGE_LEVELS; k++)
  181. {
  182. stream->write( damageLevelTolerance[k] );
  183. }
  184. stream->write(collDamageThresholdVel);
  185. stream->write(collDamageMultiplier);
  186. }
  187. void VehicleData::unpackData(BitStream* stream)
  188. {
  189. Parent::unpackData(stream);
  190. S32 i;
  191. stream->read(&maxSteeringAngle);
  192. stream->read(&jetForce);
  193. stream->read(&jetEnergyDrain);
  194. stream->read(&minJetEnergy);
  195. stream->read(&steeringReturn);
  196. stream->read(&steeringReturnSpeedScale);
  197. powerSteering = stream->readFlag();
  198. stream->read( &numDmgEmitterAreas );
  199. stream->read(&enablePhysicsRep);
  200. for (i = 0; i < VC_NUM_DAMAGE_EMITTERS; i++)
  201. {
  202. if( stream->readFlag() )
  203. {
  204. damageEmitterIDList[i] = stream->readRangedU32( DataBlockObjectIdFirst, DataBlockObjectIdLast );
  205. }
  206. }
  207. for( S32 j=0; j<VC_NUM_DAMAGE_EMITTER_AREAS; j++ )
  208. {
  209. stream->read( &damageEmitterOffset[j].x );
  210. stream->read( &damageEmitterOffset[j].y );
  211. stream->read( &damageEmitterOffset[j].z );
  212. }
  213. for( S32 k=0; k<VC_NUM_DAMAGE_LEVELS; k++ )
  214. {
  215. stream->read( &damageLevelTolerance[k] );
  216. }
  217. stream->read(&collDamageThresholdVel);
  218. stream->read(&collDamageMultiplier);
  219. }
  220. //----------------------------------------------------------------------------
  221. void VehicleData::initPersistFields()
  222. {
  223. docsURL;
  224. Parent::initPersistFields();
  225. addGroup("Particle Effects");
  226. addField( "damageEmitter", TYPEID< ParticleEmitterData >(), Offset(damageEmitterList, VehicleData), VC_NUM_DAMAGE_EMITTERS,
  227. "@brief Array of particle emitters used to generate damage (dust, smoke etc) "
  228. "effects.\n\n"
  229. "Currently, the first two emitters (indices 0 and 1) are used when the damage "
  230. "level exceeds the associated damageLevelTolerance. The 3rd emitter is used "
  231. "when the emitter point is underwater.\n\n"
  232. "@see damageEmitterOffset" );
  233. addField( "damageEmitterOffset", TypePoint3F, Offset(damageEmitterOffset, VehicleData), VC_NUM_DAMAGE_EMITTER_AREAS,
  234. "@brief Object space \"x y z\" offsets used to emit particles for the "
  235. "active damageEmitter.\n\n"
  236. "@tsexample\n"
  237. "// damage levels\n"
  238. "damageLevelTolerance[0] = 0.5;\n"
  239. "damageEmitter[0] = SmokeEmitter;\n"
  240. "// emit offsets (used for all active damage level emitters)\n"
  241. "damageEmitterOffset[0] = \"0.5 3 1\";\n"
  242. "damageEmitterOffset[1] = \"-0.5 3 1\";\n"
  243. "numDmgEmitterAreas = 2;\n"
  244. "@endtsexample\n" );
  245. addFieldV( "damageLevelTolerance", TypeRangedF32, Offset(damageLevelTolerance, VehicleData), &CommonValidators::PositiveFloat, VC_NUM_DAMAGE_LEVELS,
  246. "@brief Damage levels (as a percentage of maxDamage) above which to begin "
  247. "emitting particles from the associated damageEmitter.\n\n"
  248. "Levels should be in order of increasing damage.\n\n"
  249. "@see damageEmitterOffset" );
  250. addFieldV( "numDmgEmitterAreas", TypeRangedF32, Offset(numDmgEmitterAreas, VehicleData), &CommonValidators::PositiveFloat,
  251. "Number of damageEmitterOffset values to use for each damageEmitter.\n\n"
  252. "@see damageEmitterOffset" );
  253. endGroup("Particle Effects");
  254. addGroup("Physics");
  255. addField("enablePhysicsRep", TypeBool, Offset(enablePhysicsRep, VehicleData),
  256. "@brief Creates a representation of the object in the physics plugin.\n");
  257. endGroup("Physics");
  258. addGroup("Collision");
  259. addFieldV( "collDamageThresholdVel", TypeRangedF32, Offset(collDamageThresholdVel, VehicleData), &CommonValidators::PositiveFloat,
  260. "Minimum collision velocity to cause damage to this vehicle.\nCurrently unused." );
  261. addFieldV( "collDamageMultiplier", TypeRangedF32, Offset(collDamageMultiplier, VehicleData), &CommonValidators::PositiveFloat,
  262. "@brief Damage to this vehicle after a collision (multiplied by collision "
  263. "velocity).\n\nCurrently unused." );
  264. endGroup("Collision");
  265. addGroup("Steering");
  266. addFieldV( "jetForce", TypeRangedF32, Offset(jetForce, VehicleData), &CommonValidators::PositiveFloat,
  267. "@brief Additional force applied to the vehicle when it is jetting.\n\n"
  268. "For WheeledVehicles, the force is applied in the forward direction. For "
  269. "FlyingVehicles, the force is applied in the thrust direction." );
  270. addFieldV( "jetEnergyDrain", TypeRangedF32, Offset(jetEnergyDrain, VehicleData), &CommonValidators::PositiveFloat,
  271. "@brief Energy amount to drain for each tick the vehicle is jetting.\n\n"
  272. "Once the vehicle's energy level reaches 0, it will no longer be able to jet." );
  273. addFieldV( "minJetEnergy", TypeRangedF32, Offset(minJetEnergy, VehicleData), &CommonValidators::PositiveFloat,
  274. "Minimum vehicle energy level to begin jetting." );
  275. addFieldV( "maxSteeringAngle", TypeRangedF32, Offset(maxSteeringAngle, VehicleData), &CommonValidators::PositiveFloat,
  276. "Maximum yaw (horizontal) and pitch (vertical) steering angle in radians." );
  277. endGroup("Steering");
  278. addGroup("AutoCorrection");
  279. addField( "powerSteering", TypeBool, Offset(powerSteering, VehicleData),
  280. "If true, steering does not auto-centre while the vehicle is being steered by its driver." );
  281. addFieldV( "steeringReturn", TypeRangedF32, Offset(steeringReturn, VehicleData), &CommonValidators::PositiveFloat,
  282. "Rate at which the vehicle's steering returns to forwards when it is moving." );
  283. addFieldV( "steeringReturnSpeedScale", TypeRangedF32, Offset(steeringReturnSpeedScale, VehicleData), &CommonValidators::PositiveFloat,
  284. "Amount of effect the vehicle's speed has on its rate of steering return." );
  285. endGroup("AutoCorrection");
  286. }
  287. //----------------------------------------------------------------------------
  288. //----------------------------------------------------------------------------
  289. //----------------------------------------------------------------------------
  290. IMPLEMENT_CONOBJECT(Vehicle);
  291. ConsoleDocClass( Vehicle,
  292. "@brief Base functionality shared by all Vehicles (FlyingVehicle, HoverVehicle, "
  293. "WheeledVehicle).\n\n"
  294. "This object implements functionality shared by all Vehicle types, but should "
  295. "not be instantiated directly. Create a FlyingVehicle, HoverVehicle, or "
  296. "WheeledVehicle instead.\n"
  297. "@note The model used for any Vehicle must include a collision mesh at detail "
  298. "size -1.\n"
  299. "@ingroup Vehicles\n"
  300. );
  301. Vehicle::Vehicle()
  302. {
  303. mDataBlock = 0;
  304. mTypeMask |= VehicleObjectType | DynamicShapeObjectType;
  305. mDelta.pos = Point3F(0,0,0);
  306. mDelta.posVec = Point3F(0,0,0);
  307. mDelta.warpTicks = mDelta.warpCount = 0;
  308. mDelta.dt = 1;
  309. mDelta.move = NullMove;
  310. mPredictionCount = 0;
  311. mDelta.cameraOffset.set(0,0,0);
  312. mDelta.cameraVec.set(0,0,0);
  313. mDelta.cameraRot.set(0,0,0);
  314. mDelta.cameraRotVec.set(0,0,0);
  315. mRigid.linPosition.set(0, 0, 0);
  316. mRigid.linVelocity.set(0, 0, 0);
  317. mRigid.angPosition.identity();
  318. mRigid.angVelocity.set(0, 0, 0);
  319. mRigid.linMomentum.set(0, 0, 0);
  320. mRigid.angMomentum.set(0, 0, 0);
  321. mContacts.clear();
  322. mSteering.set(0,0);
  323. mThrottle = 0;
  324. mJetting = false;
  325. mCameraOffset.set(0,0,0);
  326. dMemset( mDamageEmitterList, 0, sizeof( mDamageEmitterList ) );
  327. mDisableMove = false;
  328. restCount = 0;
  329. inLiquid = false;
  330. mWakeSound = NULL;
  331. mWorkingQueryBox.minExtents.set(-1e9f, -1e9f, -1e9f);
  332. mWorkingQueryBox.maxExtents.set(-1e9f, -1e9f, -1e9f);
  333. mWorkingQueryBoxCountDown = sWorkingQueryBoxStaleThreshold;
  334. mPhysicsRep = NULL;
  335. }
  336. U32 Vehicle::getCollisionMask()
  337. {
  338. AssertFatal(false, "Vehicle::getCollisionMask is pure virtual!");
  339. return 0;
  340. }
  341. //----------------------------------------------------------------------------
  342. bool Vehicle::onAdd()
  343. {
  344. if (!Parent::onAdd())
  345. return false;
  346. mWorkingQueryBox.minExtents.set(-1e9f, -1e9f, -1e9f);
  347. mWorkingQueryBox.maxExtents.set(-1e9f, -1e9f, -1e9f);
  348. // When loading from a mission script, the base SceneObject's transform
  349. // will have been set and needs to be transfered to the rigid body.
  350. mRigid.setTransform(mObjToWorld);
  351. // Initialize interpolation vars.
  352. mDelta.rot[1] = mDelta.rot[0] = mRigid.angPosition;
  353. mDelta.pos = mRigid.linPosition;
  354. mDelta.posVec = Point3F(0,0,0);
  355. // Create Emitters on the client
  356. if( isClientObject() )
  357. {
  358. U32 j;
  359. for( j=0; j<VehicleData::VC_NUM_DAMAGE_EMITTERS; j++ )
  360. {
  361. if( mDataBlock->damageEmitterList[j] )
  362. {
  363. mDamageEmitterList[j] = new ParticleEmitter;
  364. mDamageEmitterList[j]->onNewDataBlock( mDataBlock->damageEmitterList[j], false );
  365. if( !mDamageEmitterList[j]->registerObject() )
  366. {
  367. Con::warnf( ConsoleLogEntry::General, "Could not register damage emitter for class: %s", mDataBlock->getName() );
  368. delete mDamageEmitterList[j];
  369. mDamageEmitterList[j] = NULL;
  370. }
  371. }
  372. }
  373. }
  374. // Create a new convex.
  375. AssertFatal(mDataBlock->collisionDetails[0] != -1, "Error, a vehicle must have a collision-1 detail!");
  376. mConvex.mObject = this;
  377. mConvex.pShapeBase = this;
  378. mConvex.hullId = 0;
  379. mConvex.box = mObjBox;
  380. mConvex.box.minExtents.convolve(mObjScale);
  381. mConvex.box.maxExtents.convolve(mObjScale);
  382. mConvex.findNodeTransform();
  383. _createPhysics();
  384. return true;
  385. }
  386. void Vehicle::onRemove()
  387. {
  388. SAFE_DELETE(mPhysicsRep);
  389. U32 i=0;
  390. for( i=0; i<VehicleData::VC_NUM_DAMAGE_EMITTERS; i++ )
  391. {
  392. if( mDamageEmitterList[i] )
  393. {
  394. mDamageEmitterList[i]->deleteWhenEmpty();
  395. mDamageEmitterList[i] = NULL;
  396. }
  397. }
  398. mWorkingQueryBox.minExtents.set(-1e9f, -1e9f, -1e9f);
  399. mWorkingQueryBox.maxExtents.set(-1e9f, -1e9f, -1e9f);
  400. Parent::onRemove();
  401. }
  402. //----------------------------------------------------------------------------
  403. void Vehicle::processTick(const Move* move)
  404. {
  405. PROFILE_SCOPE( Vehicle_ProcessTick );
  406. ShapeBase::processTick(move);
  407. if ( isMounted() )
  408. return;
  409. // Warp to catch up to server
  410. if (mDelta.warpCount < mDelta.warpTicks)
  411. {
  412. mDelta.warpCount++;
  413. // Set new pos.
  414. mObjToWorld.getColumn(3,&mDelta.pos);
  415. mDelta.pos += mDelta.warpOffset;
  416. mDelta.rot[0] = mDelta.rot[1];
  417. mDelta.rot[1].interpolate(mDelta.warpRot[0],mDelta.warpRot[1],F32(mDelta.warpCount)/mDelta.warpTicks);
  418. setPosition(mDelta.pos,mDelta.rot[1]);
  419. // Pos backstepping
  420. mDelta.posVec.x = -mDelta.warpOffset.x;
  421. mDelta.posVec.y = -mDelta.warpOffset.y;
  422. mDelta.posVec.z = -mDelta.warpOffset.z;
  423. }
  424. else
  425. {
  426. if (!move)
  427. {
  428. if (isGhost())
  429. {
  430. // If we haven't run out of prediction time,
  431. // predict using the last known move.
  432. if (mPredictionCount-- <= 0)
  433. return;
  434. move = &mDelta.move;
  435. }
  436. else
  437. move = &NullMove;
  438. }
  439. // Process input move
  440. updateMove(move);
  441. // Save current rigid state interpolation
  442. mDelta.posVec = mRigid.linPosition;
  443. mDelta.rot[0] = mRigid.angPosition;
  444. // Update the physics based on the integration rate
  445. S32 count = mDataBlock->integration;
  446. --mWorkingQueryBoxCountDown;
  447. if (!mDisableMove)
  448. updateWorkingCollisionSet(getCollisionMask());
  449. for (U32 i = 0; i < count; i++)
  450. updatePos(TickSec / count);
  451. // Wrap up interpolation info
  452. mDelta.pos = mRigid.linPosition;
  453. mDelta.posVec -= mRigid.linPosition;
  454. mDelta.rot[1] = mRigid.angPosition;
  455. // Update container database
  456. setPosition(mRigid.linPosition, mRigid.angPosition);
  457. setMaskBits(PositionMask);
  458. updateContainer();
  459. //TODO: Only update when position has actually changed
  460. //no need to check if mDataBlock->enablePhysicsRep is false as mPhysicsRep will be NULL if it is
  461. if (mPhysicsRep)
  462. mPhysicsRep->moveKinematicTo(getTransform());
  463. }
  464. }
  465. void Vehicle::advanceTime(F32 dt)
  466. {
  467. PROFILE_SCOPE( Vehicle_AdvanceTime );
  468. Parent::advanceTime(dt);
  469. updateLiftoffDust( dt );
  470. updateDamageSmoke( dt );
  471. }
  472. //----------------------------------------------------------------------------
  473. bool Vehicle::onNewDataBlock(GameBaseData* dptr,bool reload)
  474. {
  475. mDataBlock = dynamic_cast<VehicleData*>(dptr);
  476. if (!mDataBlock || !Parent::onNewDataBlock(dptr, reload))
  477. return false;
  478. // Update Rigid Info
  479. mRigid.mass = mDataBlock->mass;
  480. mRigid.oneOverMass = 1 / mRigid.mass;
  481. mRigid.friction = mDataBlock->body.friction;
  482. mRigid.restitution = mDataBlock->body.restitution;
  483. mRigid.setCenterOfMass(mDataBlock->massCenter);
  484. // Ignores massBox, just set sphere for now. Derived objects
  485. // can set what they want.
  486. mRigid.setObjectInertia();
  487. if (isGhost())
  488. {
  489. // Create the sound ahead of time. This reduces runtime
  490. // costs and makes the system easier to understand.
  491. SFX_DELETE( mWakeSound );
  492. }
  493. return true;
  494. }
  495. //----------------------------------------------------------------------------
  496. void Vehicle::getCameraParameters(F32 *min,F32* max,Point3F* off,MatrixF* rot)
  497. {
  498. *min = mDataBlock->cameraMinDist;
  499. *max = mDataBlock->cameraMaxDist;
  500. off->set(0,0,mDataBlock->cameraOffset);
  501. rot->identity();
  502. }
  503. //----------------------------------------------------------------------------
  504. void Vehicle::getCameraTransform(F32* pos, MatrixF* mat)
  505. {
  506. // Returns camera to world space transform
  507. // Handles first person / third person camera position
  508. if (isServerObject() && mShapeInstance)
  509. mShapeInstance->animateNodeSubtrees(true);
  510. if (*pos == 0)
  511. {
  512. getRenderEyeTransform(mat);
  513. }
  514. else
  515. {
  516. // Get the shape's camera parameters.
  517. F32 min, max;
  518. MatrixF rot;
  519. Point3F offset;
  520. getCameraParameters(&min, &max, &offset, &rot);
  521. // Start with the current eye position
  522. MatrixF eye;
  523. getRenderEyeTransform(&eye);
  524. // Build a transform that points along the eye axis
  525. // but where the Z axis is always up.
  526. if (mDataBlock->cameraRoll)
  527. mat->mul(eye, rot);
  528. else
  529. {
  530. MatrixF cam(1);
  531. VectorF x, y, z(0, 0, 1);
  532. eye.getColumn(1, &y);
  533. mCross(y, z, &x);
  534. x.normalize();
  535. mCross(x, y, &z);
  536. z.normalize();
  537. cam.setColumn(0, x);
  538. cam.setColumn(1, y);
  539. cam.setColumn(2, z);
  540. mat->mul(cam, rot);
  541. }
  542. // Camera is positioned straight back along the eye's -Y axis.
  543. // A ray is cast to make sure the camera doesn't go through
  544. // anything solid.
  545. VectorF vp, vec;
  546. vp.x = vp.z = 0;
  547. vp.y = -(max - min) * *pos;
  548. eye.mulV(vp, &vec);
  549. // Use the camera node as the starting position if it exists.
  550. Point3F osp, sp;
  551. if (mDataBlock->cameraNode != -1)
  552. {
  553. mShapeInstance->mNodeTransforms[mDataBlock->cameraNode].getColumn(3, &osp);
  554. getRenderTransform().mulP(osp, &sp);
  555. }
  556. else
  557. eye.getColumn(3, &sp);
  558. // Make sure we don't hit ourself...
  559. disableCollision();
  560. if (isMounted())
  561. getObjectMount()->disableCollision();
  562. // Cast the ray into the container database to see if we're going
  563. // to hit anything.
  564. RayInfo collision;
  565. Point3F ep = sp + vec + offset + mCameraOffset;
  566. if (mContainer->castRay(sp, ep,
  567. ~(WaterObjectType | GameBaseObjectType | DefaultObjectType | sTriggerMask),
  568. &collision) == true) {
  569. // Shift the collision point back a little to try and
  570. // avoid clipping against the front camera plane.
  571. F32 t = collision.t - (-mDot(vec, collision.normal) / vec.len()) * 0.1;
  572. if (t > 0.0f)
  573. ep = sp + offset + mCameraOffset + (vec * t);
  574. else
  575. eye.getColumn(3, &ep);
  576. }
  577. mat->setColumn(3, ep);
  578. // Re-enable our collision.
  579. if (isMounted())
  580. getObjectMount()->enableCollision();
  581. enableCollision();
  582. }
  583. // Apply Camera FX.
  584. mat->mul( gCamFXMgr.getTrans() );
  585. }
  586. //----------------------------------------------------------------------------
  587. void Vehicle::updateMove(const Move* move)
  588. {
  589. PROFILE_SCOPE( Vehicle_UpdateMove );
  590. mDelta.move = *move;
  591. // Image Triggers
  592. if (mDamageState == Enabled) {
  593. setImageTriggerState(0,move->trigger[0]);
  594. setImageTriggerState(1,move->trigger[1]);
  595. }
  596. // Throttle
  597. if(!mDisableMove)
  598. mThrottle = move->y;
  599. // Steering
  600. if (move != &NullMove) {
  601. F32 y = move->yaw;
  602. mSteering.x = mClampF(mSteering.x + y,-mDataBlock->maxSteeringAngle,
  603. mDataBlock->maxSteeringAngle);
  604. F32 p = move->pitch;
  605. mSteering.y = mClampF(mSteering.y + p,-mDataBlock->maxSteeringAngle,
  606. mDataBlock->maxSteeringAngle);
  607. }
  608. else {
  609. mSteering.x = 0;
  610. mSteering.y = 0;
  611. }
  612. // Steering return
  613. if(mDataBlock->steeringReturn > 0.0f &&
  614. (!mDataBlock->powerSteering || (move->yaw == 0.0f && move->pitch == 0.0f)))
  615. {
  616. Point2F returnAmount(mSteering.x * mDataBlock->steeringReturn * TickSec,
  617. mSteering.y * mDataBlock->steeringReturn * TickSec);
  618. if(mDataBlock->steeringReturnSpeedScale > 0.0f)
  619. {
  620. Point3F vel;
  621. mWorldToObj.mulV(getVelocity(), &vel);
  622. returnAmount += Point2F(mSteering.x * vel.y * mDataBlock->steeringReturnSpeedScale * TickSec,
  623. mSteering.y * vel.y * mDataBlock->steeringReturnSpeedScale * TickSec);
  624. }
  625. mSteering -= returnAmount;
  626. }
  627. // Jetting flag
  628. if (move->trigger[3]) {
  629. if (!mJetting && getEnergyLevel() >= mDataBlock->minJetEnergy)
  630. mJetting = true;
  631. if (mJetting) {
  632. F32 newEnergy = getEnergyLevel() - mDataBlock->jetEnergyDrain;
  633. if (newEnergy < 0) {
  634. newEnergy = 0;
  635. mJetting = false;
  636. }
  637. setEnergyLevel(newEnergy);
  638. }
  639. }
  640. else
  641. mJetting = false;
  642. }
  643. //----------------------------------------------------------------------------
  644. /** Update the physics
  645. */
  646. void Vehicle::updatePos(F32 dt)
  647. {
  648. PROFILE_SCOPE( Vehicle_UpdatePos );
  649. Point3F origVelocity = mRigid.linVelocity;
  650. // Update internal forces acting on the body.
  651. mRigid.clearForces();
  652. updateForces(dt);
  653. // Update collision information based on our current pos.
  654. bool collided = false;
  655. if (!mRigid.atRest && !mDisableMove)
  656. {
  657. collided = updateCollision(dt);
  658. // Now that all the forces have been processed, lets
  659. // see if we're at rest. Basically, if the kinetic energy of
  660. // the vehicles is less than some percentage of the energy added
  661. // by gravity for a short period, we're considered at rest.
  662. // This should really be part of the rigid class...
  663. if (mCollisionList.getCount())
  664. {
  665. F32 k = mRigid.getKineticEnergy();
  666. F32 G = mNetGravity* dt * TickMs / mDataBlock->integration;
  667. F32 Kg = 0.5 * mRigid.mass * G * G;
  668. if (k < sRestTol * Kg && ++restCount > sRestCount)
  669. mRigid.setAtRest();
  670. }
  671. else
  672. restCount = 0;
  673. }
  674. // Integrate forward
  675. if (!mRigid.atRest && !mDisableMove)
  676. mRigid.integrate(dt);
  677. // Deal with client and server scripting, sounds, etc.
  678. if (isServerObject()) {
  679. // Check triggers and other objects that we normally don't
  680. // collide with. This function must be called before notifyCollision
  681. // as it will queue collision.
  682. checkTriggers();
  683. // Invoke the onCollision notify callback for all the objects
  684. // we've just hit.
  685. notifyCollision();
  686. // Server side impact script callback
  687. if (collided) {
  688. VectorF collVec = mRigid.linVelocity - origVelocity;
  689. F32 collSpeed = collVec.len();
  690. if (collSpeed > mDataBlock->minImpactSpeed)
  691. onImpact(collVec);
  692. }
  693. // Water script callbacks
  694. if (!inLiquid && mWaterCoverage != 0.0f) {
  695. mDataBlock->onEnterLiquid_callback( this, mWaterCoverage, mLiquidType.c_str() );
  696. inLiquid = true;
  697. }
  698. else if (inLiquid && mWaterCoverage == 0.0f) {
  699. mDataBlock->onLeaveLiquid_callback( this, mLiquidType.c_str() );
  700. inLiquid = false;
  701. }
  702. }
  703. else {
  704. // Play impact sounds on the client.
  705. if (collided) {
  706. F32 collSpeed = (mRigid.linVelocity - origVelocity).len();
  707. S32 impactSound = -1;
  708. if (collSpeed >= mDataBlock->hardImpactSpeed)
  709. impactSound = RigidShapeData::Body::HardImpactSound;
  710. else
  711. if (collSpeed >= mDataBlock->softImpactSpeed)
  712. impactSound = RigidShapeData::Body::SoftImpactSound;
  713. if (impactSound != -1 && mDataBlock->getBodySoundsProfile(impactSound) != NULL)
  714. SFX->playOnce( mDataBlock->getBodySoundsProfile(impactSound), &getTransform() );
  715. }
  716. // Water volume sounds
  717. F32 vSpeed = getVelocity().len();
  718. if (!inLiquid && mWaterCoverage >= 0.8f) {
  719. if (vSpeed >= mDataBlock->hardSplashSoundVel)
  720. SFX->playOnce( mDataBlock->getWaterSoundsProfile(RigidShapeData::ImpactHard), &getTransform() );
  721. else
  722. if (vSpeed >= mDataBlock->medSplashSoundVel)
  723. SFX->playOnce( mDataBlock->getWaterSoundsProfile(RigidShapeData::ImpactMedium), &getTransform() );
  724. else
  725. if (vSpeed >= mDataBlock->softSplashSoundVel)
  726. SFX->playOnce( mDataBlock->getWaterSoundsProfile(RigidShapeData::ImpactSoft), &getTransform() );
  727. inLiquid = true;
  728. }
  729. else
  730. if(inLiquid && mWaterCoverage < 0.8f) {
  731. if (vSpeed >= mDataBlock->exitSplashSoundVel)
  732. SFX->playOnce( mDataBlock->getWaterSoundsProfile(RigidShapeData::ExitWater), &getTransform() );
  733. inLiquid = false;
  734. }
  735. }
  736. }
  737. //----------------------------------------------------------------------------
  738. void Vehicle::updateForces(F32 /*dt*/)
  739. {
  740. // Nothing here.
  741. }
  742. /** The callback used in by the checkTriggers() method.
  743. The checkTriggers method uses a container search which will
  744. invoke this callback on each obj that matches.
  745. */
  746. void Vehicle::findCallback(SceneObject* obj,void *key)
  747. {
  748. Vehicle* vehicle = reinterpret_cast<Vehicle*>(key);
  749. U32 objectMask = obj->getTypeMask();
  750. // Check: triggers, corpses and items, basically the same things
  751. // that the player class checks for
  752. if (objectMask & TriggerObjectType) {
  753. Trigger* pTrigger = static_cast<Trigger*>(obj);
  754. pTrigger->potentialEnterObject(vehicle);
  755. }
  756. else if (objectMask & CorpseObjectType) {
  757. ShapeBase* col = static_cast<ShapeBase*>(obj);
  758. vehicle->queueCollision(col,vehicle->getVelocity() - col->getVelocity());
  759. }
  760. else if (objectMask & ItemObjectType) {
  761. Item* item = static_cast<Item*>(obj);
  762. if (vehicle != item->getCollisionObject())
  763. vehicle->queueCollision(item,vehicle->getVelocity() - item->getVelocity());
  764. }
  765. }
  766. //----------------------------------------------------------------------------
  767. void Vehicle::writePacketData(GameConnection *connection, BitStream *stream)
  768. {
  769. Parent::writePacketData(connection, stream);
  770. mathWrite(*stream, mSteering);
  771. }
  772. void Vehicle::readPacketData(GameConnection *connection, BitStream *stream)
  773. {
  774. Parent::readPacketData(connection, stream);
  775. mathRead(*stream, &mSteering);
  776. }
  777. //----------------------------------------------------------------------------
  778. U32 Vehicle::packUpdate(NetConnection *con, U32 mask, BitStream *stream)
  779. {
  780. U32 retMask = Parent::packUpdate(con, mask, stream);
  781. stream->writeFlag(mJetting);
  782. // The rest of the data is part of the control object packet update.
  783. // If we're controlled by this client, we don't need to send it.
  784. if (stream->writeFlag(getControllingClient() == con && !(mask & InitialUpdateMask)))
  785. return retMask;
  786. F32 yaw = (mSteering.x + mDataBlock->maxSteeringAngle) / (2 * mDataBlock->maxSteeringAngle);
  787. F32 pitch = (mSteering.y + mDataBlock->maxSteeringAngle) / (2 * mDataBlock->maxSteeringAngle);
  788. stream->writeFloat(yaw,9);
  789. stream->writeFloat(pitch,9);
  790. mDelta.move.pack(stream);
  791. stream->writeFloat(mClampF(getEnergyValue(), 0.f, 1.f), 8);
  792. return retMask;
  793. }
  794. void Vehicle::unpackUpdate(NetConnection *con, BitStream *stream)
  795. {
  796. Parent::unpackUpdate(con,stream);
  797. mJetting = stream->readFlag();
  798. if (stream->readFlag())
  799. return;
  800. F32 yaw = stream->readFloat(9);
  801. F32 pitch = stream->readFloat(9);
  802. mSteering.x = (2 * yaw * mDataBlock->maxSteeringAngle) - mDataBlock->maxSteeringAngle;
  803. mSteering.y = (2 * pitch * mDataBlock->maxSteeringAngle) - mDataBlock->maxSteeringAngle;
  804. mDelta.move.unpack(stream);
  805. setEnergyLevel(stream->readFloat(8) * mDataBlock->maxEnergy);
  806. }
  807. void Vehicle::setControllingClient(GameConnection* client)
  808. {
  809. ShapeBase::setControllingClient(client);
  810. }
  811. //----------------------------------------------------------------------------
  812. void Vehicle::consoleInit()
  813. {
  814. Con::addVariable("$vehicle::workingQueryBoxStaleThreshold",TypeS32,&sWorkingQueryBoxStaleThreshold,
  815. "@brief The maximum number of ticks that go by before the mWorkingQueryBox is considered stale and needs updating.\n\n"
  816. "Other factors can cause the collision working query box to become invalidated, such as the vehicle moving far "
  817. "enough outside of this cached box. The smaller this number, the more times the working list of triangles that are "
  818. "considered for collision is refreshed. This has the greatest impact with colliding with high triangle count meshes.\n\n"
  819. "@note Set to -1 to disable any time-based forced check.\n\n"
  820. "@ingroup GameObjects\n");
  821. Con::addVariable("$vehicle::workingQueryBoxSizeMultiplier",TypeF32,&sWorkingQueryBoxSizeMultiplier,
  822. "@brief How much larger the mWorkingQueryBox should be made when updating the working collision list.\n\n"
  823. "The larger this number the less often the working list will be updated due to motion, but any non-static shape that "
  824. "moves into the query box will not be noticed.\n\n"
  825. "@ingroup GameObjects\n");
  826. }
  827. void Vehicle::initPersistFields()
  828. {
  829. docsURL;
  830. Parent::initPersistFields();
  831. }
  832. void Vehicle::mountObject(SceneObject *obj, S32 node, const MatrixF &xfm )
  833. {
  834. Parent::mountObject( obj, node, xfm );
  835. // Clear objects off the working list that are from objects mounted to us.
  836. // (This applies mostly to players...)
  837. for ( CollisionWorkingList* itr = mConvex.getWorkingList().wLink.mNext;
  838. itr != &mConvex.getWorkingList();
  839. itr = itr->wLink.mNext)
  840. {
  841. if (itr->mConvex->getObject() == obj)
  842. {
  843. CollisionWorkingList* cl = itr;
  844. itr = itr->wLink.mPrev;
  845. cl->free();
  846. }
  847. }
  848. }
  849. //----------------------------------------------------------------------------
  850. void Vehicle::updateLiftoffDust( F32 dt )
  851. {
  852. Point3F offset( 0.0, 0.0, mDataBlock->dustHeight );
  853. emitDust( mDustEmitterList[ 0 ], mDataBlock->triggerDustHeight, offset,
  854. ( U32 )( dt * 1000 ) );
  855. }
  856. //----------------------------------------------------------------------------
  857. void Vehicle::updateDamageSmoke( F32 dt )
  858. {
  859. for( S32 j=VehicleData::VC_NUM_DAMAGE_LEVELS-1; j>=0; j-- )
  860. {
  861. F32 damagePercent = mDamage / mDataBlock->maxDamage;
  862. if( damagePercent >= mDataBlock->damageLevelTolerance[j] )
  863. {
  864. for( S32 i=0; i<mDataBlock->numDmgEmitterAreas; i++ )
  865. {
  866. MatrixF trans = getTransform();
  867. Point3F offset = mDataBlock->damageEmitterOffset[i];
  868. trans.mulP( offset );
  869. Point3F emitterPoint = offset;
  870. if( pointInWater(offset ) )
  871. {
  872. U32 emitterOffset = VehicleData::VC_BUBBLE_EMITTER;
  873. if( mDamageEmitterList[emitterOffset] )
  874. {
  875. mDamageEmitterList[emitterOffset]->emitParticles( emitterPoint, emitterPoint, Point3F( 0.0, 0.0, 1.0 ), getVelocity(), (U32)( dt * 1000 ) );
  876. }
  877. }
  878. else
  879. {
  880. if( mDamageEmitterList[j] )
  881. {
  882. mDamageEmitterList[j]->emitParticles( emitterPoint, emitterPoint, Point3F( 0.0, 0.0, 1.0 ), getVelocity(), (U32)(dt * 1000));
  883. }
  884. }
  885. }
  886. break;
  887. }
  888. }
  889. }
  890. //--------------------------------------------------------------------------
  891. void Vehicle::updateFroth( F32 dt )
  892. {
  893. // update bubbles
  894. Point3F moveDir = getVelocity();
  895. Point3F contactPoint;
  896. if( !collidingWithWater( contactPoint ) )
  897. {
  898. if ( mWakeSound )
  899. mWakeSound->stop();
  900. return;
  901. }
  902. F32 speed = moveDir.len();
  903. if( speed < mDataBlock->splashVelEpsilon ) speed = 0.0;
  904. U32 emitRate = (U32)(speed * mDataBlock->splashFreqMod * dt);
  905. U32 i;
  906. if ( mWakeSound )
  907. {
  908. if ( !mWakeSound->isPlaying() )
  909. mWakeSound->play();
  910. mWakeSound->setTransform( getTransform() );
  911. mWakeSound->setVelocity( getVelocity() );
  912. }
  913. for( i=0; i<VehicleData::VC_NUM_SPLASH_EMITTERS; i++ )
  914. {
  915. if( mSplashEmitterList[i] )
  916. {
  917. mSplashEmitterList[i]->emitParticles( contactPoint, contactPoint, Point3F( 0.0, 0.0, 1.0 ),
  918. moveDir, emitRate );
  919. }
  920. }
  921. }
  922. void Vehicle::prepBatchRender( SceneRenderState *state, S32 mountedImageIndex )
  923. {
  924. Parent::prepBatchRender( state, mountedImageIndex );
  925. if ( !gShowBoundingBox )
  926. return;
  927. if ( mountedImageIndex != -1 )
  928. {
  929. ObjectRenderInst *ri = state->getRenderPass()->allocInst<ObjectRenderInst>();
  930. ri->renderDelegate.bind( this, &Vehicle::_renderMuzzleVector );
  931. ri->objectIndex = mountedImageIndex;
  932. ri->type = RenderPassManager::RIT_Editor;
  933. state->getRenderPass()->addInst( ri );
  934. return;
  935. }
  936. ObjectRenderInst *ri = state->getRenderPass()->allocInst<ObjectRenderInst>();
  937. ri->renderDelegate.bind( this, &Vehicle::_renderMassAndContacts );
  938. ri->type = RenderPassManager::RIT_Editor;
  939. state->getRenderPass()->addInst( ri );
  940. }
  941. void Vehicle::_renderMassAndContacts( ObjectRenderInst *ri, SceneRenderState *state, BaseMatInstance *overrideMat )
  942. {
  943. GFXStateBlockDesc desc;
  944. desc.setBlend(false, GFXBlendSrcAlpha, GFXBlendInvSrcAlpha);
  945. desc.setZReadWrite(false,true);
  946. desc.fillMode = GFXFillWireframe;
  947. // Render the mass center.
  948. GFX->getDrawUtil()->drawCube(desc, Point3F(0.1f,0.1f,0.1f),mDataBlock->massCenter, ColorI(255, 255, 255), &mRenderObjToWorld);
  949. // Now render all the contact points.
  950. for (S32 i = 0; i < mCollisionList.getCount(); i++)
  951. {
  952. const Collision& collision = mCollisionList[i];
  953. GFX->getDrawUtil()->drawCube(desc, Point3F(0.05f,0.05f,0.05f),collision.point, ColorI(0, 0, 255));
  954. }
  955. // Finally render the normals as one big batch.
  956. PrimBuild::begin(GFXLineList, mCollisionList.getCount() * 2);
  957. for (S32 i = 0; i < mCollisionList.getCount(); i++)
  958. {
  959. const Collision& collision = mCollisionList[i];
  960. PrimBuild::color3f(1, 1, 1);
  961. PrimBuild::vertex3fv(collision.point);
  962. PrimBuild::vertex3fv(collision.point + collision.normal * 0.05f);
  963. }
  964. PrimBuild::end();
  965. // Build and render the collision polylist which is returned
  966. // in the server's world space.
  967. ClippedPolyList polyList;
  968. polyList.mPlaneList.setSize(6);
  969. polyList.mPlaneList[0].set(getWorldBox().minExtents,VectorF(-1,0,0));
  970. polyList.mPlaneList[1].set(getWorldBox().minExtents,VectorF(0,-1,0));
  971. polyList.mPlaneList[2].set(getWorldBox().minExtents,VectorF(0,0,-1));
  972. polyList.mPlaneList[3].set(getWorldBox().maxExtents,VectorF(1,0,0));
  973. polyList.mPlaneList[4].set(getWorldBox().maxExtents,VectorF(0,1,0));
  974. polyList.mPlaneList[5].set(getWorldBox().maxExtents,VectorF(0,0,1));
  975. Box3F dummyBox;
  976. SphereF dummySphere;
  977. buildPolyList(PLC_Collision, &polyList, dummyBox, dummySphere);
  978. //polyList.render();
  979. }
  980. void Vehicle::_renderMuzzleVector( ObjectRenderInst *ri, SceneRenderState *state, BaseMatInstance *overrideMat )
  981. {
  982. const U32 index = ri->objectIndex;
  983. AssertFatal( index > 0 && index < MaxMountedImages, "Vehicle::_renderMuzzleVector() - Bad object index!" );
  984. AssertFatal( mMountedImageList[index].dataBlock, "Vehicle::_renderMuzzleVector() - Bad object index!" );
  985. Point3F muzzlePoint, muzzleVector, endpoint;
  986. getMuzzlePoint(index, &muzzlePoint);
  987. getMuzzleVector(index, &muzzleVector);
  988. endpoint = muzzlePoint + muzzleVector * 250;
  989. if (mSolidSB.isNull())
  990. {
  991. GFXStateBlockDesc desc;
  992. desc.setBlend(false, GFXBlendSrcAlpha, GFXBlendInvSrcAlpha);
  993. desc.setZReadWrite(false);
  994. mSolidSB = GFX->createStateBlock(desc);
  995. }
  996. GFX->setStateBlock(mSolidSB);
  997. PrimBuild::begin(GFXLineList, 2);
  998. PrimBuild::color4f(0, 1, 0, 1);
  999. PrimBuild::vertex3fv(muzzlePoint);
  1000. PrimBuild::vertex3fv(endpoint);
  1001. PrimBuild::end();
  1002. }