rigidShape.cpp 61 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854
  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/rigidShape.h"
  24. #include "app/game.h"
  25. #include "math/mMath.h"
  26. #include "console/simBase.h"
  27. #include "console/console.h"
  28. #include "console/consoleTypes.h"
  29. #include "collision/clippedPolyList.h"
  30. #include "collision/planeExtractor.h"
  31. #include "T3D/gameBase/moveManager.h"
  32. #include "core/stream/bitStream.h"
  33. #include "core/dnet.h"
  34. #include "T3D/gameBase/gameConnection.h"
  35. #include "ts/tsShapeInstance.h"
  36. #include "math/mathIO.h"
  37. #include "scene/sceneRenderState.h"
  38. #include "scene/sceneManager.h"
  39. #include "T3D/fx/cameraFXMgr.h"
  40. #include "T3D/trigger.h"
  41. #include "T3D/item.h"
  42. #include "gfx/primBuilder.h"
  43. #include "gfx/gfxDrawUtil.h"
  44. #include "sfx/sfxTypes.h"
  45. #include "sfx/sfxSystem.h"
  46. #include "T3D/fx/particleEmitter.h"
  47. #include "console/engineAPI.h"
  48. #include "T3D/physics/physicsPlugin.h"
  49. #include "T3D/physics/physicsCollision.h"
  50. IMPLEMENT_CO_DATABLOCK_V1(RigidShapeData);
  51. ConsoleDocClass( RigidShapeData,
  52. "@brief Defines the physics properties for an individual RigidShapeData physics object.\n\n"
  53. "@tsexample\n"
  54. " datablock RigidShapeData( BouncingBoulder )\n"
  55. " {\n"
  56. " category = \"RigidShape\";\n"
  57. "\n"
  58. " shapeFile = \"~/data/shapes/boulder/boulder.dts\";\n"
  59. "\n"
  60. " // Rigid Body\n"
  61. " mass = 500;\n"
  62. " massCenter = \"0 0 0\"; // Center of mass for rigid body\n"
  63. " massBox = \"0 0 0\"; // Size of box used for moment of inertia,\n"
  64. " // if zero it defaults to object bounding box\n"
  65. " drag = 0.2; // Drag coefficient\n"
  66. " bodyFriction = 0.2;\n"
  67. " bodyRestitution = 0.1;\n"
  68. " minImpactSpeed = 5; // Impacts over this invoke the script callback\n"
  69. " softImpactSpeed = 5; // Play SoftImpact Sound\n"
  70. " hardImpactSpeed = 15; // Play HardImpact Sound\n"
  71. " integration = 4; // Physics integration: TickSec/Rate\n"
  72. " collisionTol = 0.1; // Collision distance tolerance\n"
  73. " contactTol = 0.1; // Contact velocity tolerance\n"
  74. "\n"
  75. " minRollSpeed = 10;\n"
  76. "\n"
  77. " maxDrag = 0.5;\n"
  78. " minDrag = 0.01;\n"
  79. "\n"
  80. " dustHeight = 10;\n"
  81. "\n"
  82. " dragForce = 0.05;\n"
  83. " vertFactor = 0.05;\n"
  84. " };\n"
  85. "@endtsexample\n\n"
  86. "@see RigidShape\n"
  87. "@see ShapeBase\n\n"
  88. "@ingroup Physics\n"
  89. );
  90. IMPLEMENT_CO_NETOBJECT_V1(RigidShape);
  91. ConsoleDocClass( RigidShape,
  92. "@brief The RigidShape class implements rigid-body physics for DTS objects in the world.\n\n"
  93. "\"Rigid body physics\" refers to a system whereby objects are assumed to have a finite size,\n"
  94. "equally distributed masses, and where deformations of the objects themselves are not accounted for.\n"
  95. "Uses the RigidShape class to control its physics.\n\n"
  96. "@tsexample\n"
  97. " datablock RigidShapeData( BouncingBoulder )\n"
  98. " {\n"
  99. " category = \"RigidShape\";\n"
  100. "\n"
  101. " shapeFile = \"~/data/shapes/boulder/boulder.dts\";\n"
  102. "\n"
  103. " // Rigid Body\n"
  104. " mass = 500;\n"
  105. " massCenter = \"0 0 0\"; // Center of mass for rigid body\n"
  106. " massBox = \"0 0 0\"; // Size of box used for moment of inertia,\n"
  107. " // if zero it defaults to object bounding box\n"
  108. " drag = 0.2; // Drag coefficient\n"
  109. " bodyFriction = 0.2;\n"
  110. " bodyRestitution = 0.1;\n"
  111. " minImpactSpeed = 5; // Impacts over this invoke the script callback\n"
  112. " softImpactSpeed = 5; // Play SoftImpact Sound\n"
  113. " hardImpactSpeed = 15; // Play HardImpact Sound\n"
  114. " integration = 4; // Physics integration: TickSec/Rate\n"
  115. " collisionTol = 0.1; // Collision distance tolerance\n"
  116. " contactTol = 0.1; // Contact velocity tolerance\n"
  117. "\n"
  118. " minRollSpeed = 10;\n"
  119. "\n"
  120. " maxDrag = 0.5;\n"
  121. " minDrag = 0.01;\n"
  122. "\n"
  123. " dustHeight = 10;\n"
  124. "\n"
  125. " dragForce = 0.05;\n"
  126. " vertFactor = 0.05;\n"
  127. " };\n"
  128. "\n"
  129. " new RigidShape()\n"
  130. " {\n"
  131. " dataBlock = \"BouncingBoulder\";\n"
  132. " parentGroup = EWCreatorWindow.objectGroup;\n"
  133. " };\n"
  134. "@endtsexample\n\n"
  135. "@see RigidShapeData\n"
  136. "@see ShapeBase\n\n"
  137. "@ingroup Physics\n"
  138. );
  139. IMPLEMENT_CALLBACK(RigidShapeData, onEnterLiquid, void, (RigidShape* obj, F32 coverage, const char* type), (obj, coverage, type),
  140. "Called when the vehicle enters liquid.\n"
  141. "@param obj the Vehicle object\n"
  142. "@param coverage percentage of the vehicle's bounding box covered by the liquid\n"
  143. "@param type type of liquid the vehicle has entered\n");
  144. IMPLEMENT_CALLBACK(RigidShapeData, onLeaveLiquid, void, (RigidShape* obj, const char* type), (obj, type),
  145. "Called when the vehicle leaves liquid.\n"
  146. "@param obj the Vehicle object\n"
  147. "@param type type of liquid the vehicle has left\n");
  148. //----------------------------------------------------------------------------
  149. namespace {
  150. static U32 sWorkingQueryBoxStaleThreshold = 10; // The maximum number of ticks that go by before
  151. // the mWorkingQueryBox is considered stale and
  152. // needs updating. Set to -1 to disable.
  153. static F32 sWorkingQueryBoxSizeMultiplier = 2.0f; // How much larger should the mWorkingQueryBox be
  154. // made when updating the working collision list.
  155. // The larger this number the less often the working list
  156. // will be updated due to motion, but any non-static shape
  157. // that moves into the query box will not be noticed.
  158. // Client prediction
  159. const S32 sMaxWarpTicks = 3; // Max warp duration in ticks
  160. const S32 sMaxPredictionTicks = 30; // Number of ticks to predict
  161. // Physics and collision constants
  162. static F32 sRestTol = 0.5; // % of gravity energy to be at rest
  163. static S32 sRestCount = 10; // Consecutive ticks before comming to rest
  164. const U32 sCollisionMoveMask = ( TerrainObjectType | PlayerObjectType |
  165. StaticShapeObjectType | VehicleObjectType |
  166. VehicleBlockerObjectType );
  167. const U32 sServerCollisionMask = sCollisionMoveMask; // ItemObjectType
  168. const U32 sClientCollisionMask = sCollisionMoveMask;
  169. void nonFilter(SceneObject* object,void *key)
  170. {
  171. SceneContainer::CallbackInfo* info = reinterpret_cast<SceneContainer::CallbackInfo*>(key);
  172. object->buildPolyList(info->context,info->polyList,info->boundingBox,info->boundingSphere);
  173. }
  174. } // namespace {}
  175. // Trigger objects that are not normally collided with.
  176. static U32 sTriggerMask = ItemObjectType |
  177. TriggerObjectType |
  178. CorpseObjectType;
  179. typedef RigidShapeData::Body::Sounds bodySounds;
  180. DefineEnumType(bodySounds);
  181. ImplementEnumType(bodySounds, "enum types.\n"
  182. "@ingroup VehicleData\n\n")
  183. { bodySounds::SoftImpactSound, "SoftImpactSound", "..." },
  184. { bodySounds::HardImpactSound, "HardImpactSound", "..." },
  185. EndImplementEnumType;
  186. typedef RigidShapeData::Sounds waterSounds;
  187. DefineEnumType(waterSounds);
  188. ImplementEnumType(waterSounds, "enum types.\n"
  189. "@ingroup RigidShapeData\n\n")
  190. { waterSounds::ExitWater, "ExitWater", "..." },
  191. { waterSounds::ImpactSoft, "ImpactSoft", "..." },
  192. { waterSounds::ImpactMedium, "ImpactMedium", "..." },
  193. { waterSounds::ImpactHard, "ImpactHard", "..." },
  194. { waterSounds::Wake, "Wake", "..." },
  195. EndImplementEnumType;
  196. //----------------------------------------------------------------------------
  197. RigidShapeData::RigidShapeData()
  198. {
  199. body.friction = 0;
  200. body.restitution = 1;
  201. minImpactSpeed = 25;
  202. softImpactSpeed = 25;
  203. hardImpactSpeed = 50;
  204. minRollSpeed = 0;
  205. cameraRoll = true;
  206. cameraLag = 0;
  207. cameraDecay = 0;
  208. cameraOffset = 0;
  209. minDrag = 0;
  210. maxDrag = 0;
  211. integration = 1;
  212. collisionTol = 0.1f;
  213. contactTol = 0.1f;
  214. massCenter.set(0,0,0);
  215. massBox.set(0,0,0);
  216. drag = 0.7f;
  217. density = 4;
  218. for (S32 i = 0; i < Body::MaxSounds; i++)
  219. INIT_SOUNDASSET_ARRAY(BodySounds, i);
  220. dustEmitter = NULL;
  221. dustID = 0;
  222. triggerDustHeight = 3.0;
  223. dustHeight = 1.0;
  224. dMemset( splashEmitterList, 0, sizeof( splashEmitterList ) );
  225. dMemset( splashEmitterIDList, 0, sizeof( splashEmitterIDList ) );
  226. splashFreqMod = 300.0;
  227. splashVelEpsilon = 0.50;
  228. exitSplashSoundVel = 2.0;
  229. softSplashSoundVel = 1.0;
  230. medSplashSoundVel = 2.0;
  231. hardSplashSoundVel = 3.0;
  232. enablePhysicsRep = true;
  233. for (S32 i = 0; i < Sounds::MaxSounds; i++)
  234. INIT_SOUNDASSET_ARRAY(WaterSounds, i);
  235. dragForce = 0;
  236. vertFactor = 0.25;
  237. dustTrailEmitter = NULL;
  238. dustTrailID = 0;
  239. _setShape(ShapeAsset::smNoShapeAssetFallback);
  240. }
  241. RigidShapeData::~RigidShapeData()
  242. {
  243. }
  244. //----------------------------------------------------------------------------
  245. bool RigidShapeData::onAdd()
  246. {
  247. if(!Parent::onAdd())
  248. return false;
  249. return true;
  250. }
  251. bool RigidShapeData::preload(bool server, String &errorStr)
  252. {
  253. if (!Parent::preload(server, errorStr))
  254. return false;
  255. // RigidShape objects must define a collision detail
  256. if (!collisionDetails.size() || collisionDetails[0] == -1)
  257. {
  258. Con::errorf("RigidShapeData::preload failed: Rigid shapes must define a collision-1 detail");
  259. errorStr = String::ToString("RigidShapeData: Couldn't load shape asset \"%s\"", mShapeAsset.getAssetId());
  260. return false;
  261. }
  262. // Resolve objects transmitted from server
  263. if (!server) {
  264. for (S32 i = 0; i < Body::MaxSounds; i++)
  265. {
  266. if (getBodySounds(i) != StringTable->EmptyString())
  267. {
  268. _setBodySounds(getBodySounds(i), i);
  269. }
  270. }
  271. for (S32 j = 0; j < Sounds::MaxSounds; j++)
  272. {
  273. if (getWaterSounds(j) != StringTable->EmptyString())
  274. {
  275. _setWaterSounds(getWaterSounds(j), j);
  276. }
  277. }
  278. }
  279. if( !dustEmitter && dustID != 0 )
  280. {
  281. if( !Sim::findObject( dustID, dustEmitter ) )
  282. {
  283. Con::errorf( ConsoleLogEntry::General, "RigidShapeData::preload Invalid packet, bad datablockId(dustEmitter): 0x%x", dustID );
  284. }
  285. }
  286. U32 i;
  287. for( i=0; i<VC_NUM_SPLASH_EMITTERS; i++ )
  288. {
  289. if( !splashEmitterList[i] && splashEmitterIDList[i] != 0 )
  290. {
  291. if( !Sim::findObject( splashEmitterIDList[i], splashEmitterList[i] ) )
  292. {
  293. Con::errorf( ConsoleLogEntry::General, "RigidShapeData::preload Invalid packet, bad datablockId(splashEmitter): 0x%x", splashEmitterIDList[i] );
  294. }
  295. }
  296. }
  297. if (dragForce <= 0.01f)
  298. {
  299. Con::warnf("RigidShapeData::preload: dragForce must be at least 0.01");
  300. dragForce = 0.01f;
  301. }
  302. if (vertFactor < 0.0f || vertFactor > 1.0f)
  303. {
  304. Con::warnf("RigidShapeData::preload: vert factor must be [0, 1]");
  305. vertFactor = vertFactor < 0.0f ? 0.0f : 1.0f;
  306. }
  307. if( !dustTrailEmitter && dustTrailID != 0 )
  308. {
  309. if( !Sim::findObject( dustTrailID, dustTrailEmitter ) )
  310. {
  311. Con::errorf( ConsoleLogEntry::General, "RigidShapeData::preload Invalid packet, bad datablockId(dustTrailEmitter): 0x%x", dustTrailID );
  312. }
  313. }
  314. return true;
  315. }
  316. //----------------------------------------------------------------------------
  317. void RigidShapeData::packData(BitStream* stream)
  318. {
  319. Parent::packData(stream);
  320. stream->write(body.restitution);
  321. stream->write(body.friction);
  322. for (U32 i = 0; i < Body::MaxSounds; ++i)
  323. {
  324. PACKDATA_SOUNDASSET_ARRAY(BodySounds, i);
  325. }
  326. stream->write(minImpactSpeed);
  327. stream->write(softImpactSpeed);
  328. stream->write(hardImpactSpeed);
  329. stream->write(minRollSpeed);
  330. stream->write(maxDrag);
  331. stream->write(minDrag);
  332. stream->write(integration);
  333. stream->write(collisionTol);
  334. stream->write(contactTol);
  335. mathWrite(*stream,massCenter);
  336. mathWrite(*stream,massBox);
  337. stream->writeFlag(cameraRoll);
  338. stream->write(cameraLag);
  339. stream->write(cameraDecay);
  340. stream->write(cameraOffset);
  341. stream->write(triggerDustHeight);
  342. stream->write(dustHeight);
  343. stream->write(exitSplashSoundVel);
  344. stream->write(softSplashSoundVel);
  345. stream->write(medSplashSoundVel);
  346. stream->write(hardSplashSoundVel);
  347. stream->write(enablePhysicsRep);
  348. // write the water sound profiles
  349. for (U32 i = 0; i < Sounds::MaxSounds; ++i)
  350. {
  351. PACKDATA_SOUNDASSET_ARRAY(WaterSounds, i);
  352. }
  353. if (stream->writeFlag( dustEmitter ))
  354. stream->writeRangedU32( dustEmitter->getId(), DataBlockObjectIdFirst, DataBlockObjectIdLast );
  355. for( U32 i = 0; i < VC_NUM_SPLASH_EMITTERS; ++ i )
  356. {
  357. if( stream->writeFlag( splashEmitterList[i] != NULL ) )
  358. stream->writeRangedU32( splashEmitterList[i]->getId(), DataBlockObjectIdFirst, DataBlockObjectIdLast );
  359. }
  360. stream->write(splashFreqMod);
  361. stream->write(splashVelEpsilon);
  362. stream->write(dragForce);
  363. stream->write(vertFactor);
  364. if (stream->writeFlag( dustTrailEmitter ))
  365. stream->writeRangedU32( dustTrailEmitter->getId(), DataBlockObjectIdFirst, DataBlockObjectIdLast );
  366. }
  367. void RigidShapeData::unpackData(BitStream* stream)
  368. {
  369. Parent::unpackData(stream);
  370. stream->read(&body.restitution);
  371. stream->read(&body.friction);
  372. for (U32 i = 0; i < Body::Sounds::MaxSounds; i++)
  373. {
  374. UNPACKDATA_SOUNDASSET_ARRAY(BodySounds, i);
  375. }
  376. stream->read(&minImpactSpeed);
  377. stream->read(&softImpactSpeed);
  378. stream->read(&hardImpactSpeed);
  379. stream->read(&minRollSpeed);
  380. stream->read(&maxDrag);
  381. stream->read(&minDrag);
  382. stream->read(&integration);
  383. stream->read(&collisionTol);
  384. stream->read(&contactTol);
  385. mathRead(*stream,&massCenter);
  386. mathRead(*stream,&massBox);
  387. cameraRoll = stream->readFlag();
  388. stream->read(&cameraLag);
  389. stream->read(&cameraDecay);
  390. stream->read(&cameraOffset);
  391. stream->read(&triggerDustHeight);
  392. stream->read( &dustHeight );
  393. stream->read(&exitSplashSoundVel);
  394. stream->read(&softSplashSoundVel);
  395. stream->read(&medSplashSoundVel);
  396. stream->read(&hardSplashSoundVel);
  397. stream->read(&enablePhysicsRep);
  398. // write the water sound profiles
  399. for (U32 i = 0; i < Sounds::MaxSounds; ++i)
  400. {
  401. UNPACKDATA_SOUNDASSET_ARRAY(WaterSounds, i);
  402. }
  403. if( stream->readFlag() )
  404. dustID = (S32) stream->readRangedU32(DataBlockObjectIdFirst, DataBlockObjectIdLast);
  405. for( U32 i = 0; i < VC_NUM_SPLASH_EMITTERS; ++ i )
  406. {
  407. if( stream->readFlag() )
  408. splashEmitterIDList[i] = stream->readRangedU32( DataBlockObjectIdFirst, DataBlockObjectIdLast );
  409. }
  410. stream->read(&splashFreqMod);
  411. stream->read(&splashVelEpsilon);
  412. stream->read(&dragForce);
  413. stream->read(&vertFactor);
  414. if( stream->readFlag() )
  415. dustTrailID = (S32) stream->readRangedU32(DataBlockObjectIdFirst, DataBlockObjectIdLast);
  416. }
  417. //----------------------------------------------------------------------------
  418. void RigidShapeData::initPersistFields()
  419. {
  420. docsURL;
  421. Parent::initPersistFields();
  422. addGroup( "Particle Effects" );
  423. addField("dustEmitter", TYPEID< ParticleEmitterData >(), Offset(dustEmitter, RigidShapeData), "Array of pointers to ParticleEmitterData datablocks which will be used to emit particles at object/terrain contact point.\n");
  424. addField("triggerDustHeight", TypeF32, Offset(triggerDustHeight, RigidShapeData), "Maximum height from the ground at which the object will generate dust.\n");
  425. addField("dustHeight", TypeF32, Offset(dustHeight, RigidShapeData), "Height of dust effects.\n");
  426. addField("dustTrailEmitter", TYPEID< ParticleEmitterData >(), Offset(dustTrailEmitter, RigidShapeData), "Particle emitter used to create a dust trail for the moving object.\n");
  427. addField("splashEmitter", TYPEID< ParticleEmitterData >(), Offset(splashEmitterList, RigidShapeData), VC_NUM_SPLASH_EMITTERS, "Array of pointers to ParticleEmitterData datablocks which will generate splash effects.\n");
  428. addField("splashFreqMod", TypeF32, Offset(splashFreqMod, RigidShapeData), "The simulated frequency modulation of a splash generated by this object. Multiplied along with speed and time elapsed when determining splash emition rate.\n");
  429. addField("splashVelEpsilon", TypeF32, Offset(splashVelEpsilon, RigidShapeData), "The threshold speed at which we consider the object's movement to have stopped when updating splash effects.\n");
  430. endGroup( "Particle Effects" );
  431. addGroup( "Sounds" );
  432. INITPERSISTFIELD_SOUNDASSET_ENUMED(BodySounds, bodySounds, Body::Sounds::MaxSounds, RigidShapeData, "Sounds for body."); INITPERSISTFIELD_SOUNDASSET_ENUMED(WaterSounds, waterSounds, Sounds::MaxSounds, RigidShapeData, "Sounds for interacting with water.");
  433. endGroup( "Sounds" );
  434. addGroup("Physics");
  435. addField("enablePhysicsRep", TypeBool, Offset(enablePhysicsRep, RigidShapeData),
  436. "@brief Creates a representation of the object in the physics plugin.\n");
  437. ("massCenter", TypePoint3F, Offset(massCenter, RigidShapeData), "Center of mass for rigid body.");
  438. addField("massBox", TypePoint3F, Offset(massBox, RigidShapeData), "Size of inertial box.");
  439. addField("bodyRestitution", TypeF32, Offset(body.restitution, RigidShapeData), "The percentage of kinetic energy kept by this object in a collision.");
  440. addField("bodyFriction", TypeF32, Offset(body.friction, RigidShapeData), "How much friction this object has. Lower values will cause the object to appear to be more slippery.");
  441. addField("maxDrag", TypeF32, Offset(maxDrag, RigidShapeData), "Maximum drag available to this object.");
  442. addField("minDrag", TypeF32, Offset(minDrag, RigidShapeData), "Minimum drag available to this object.");
  443. addField("integration", TypeS32, Offset(integration, RigidShapeData), "Number of physics steps to process per tick.");
  444. addField("collisionTol", TypeF32, Offset(collisionTol, RigidShapeData), "Collision distance tolerance.");
  445. addField("contactTol", TypeF32, Offset(contactTol, RigidShapeData), "Contact velocity tolerance.");
  446. addField("dragForce", TypeF32, Offset(dragForce, RigidShapeData), "Used to simulate the constant drag acting on the object");
  447. addField("vertFactor", TypeF32, Offset(vertFactor, RigidShapeData), "The scalar applied to the vertical portion of the velocity drag acting on a object.");
  448. endGroup("Physics");
  449. addGroup("Collision");
  450. addField("minImpactSpeed", TypeF32, Offset(minImpactSpeed, RigidShapeData),
  451. "Minimum collision speed to classify collision as impact (triggers onImpact on server object)." );
  452. addField("softImpactSpeed", TypeF32, Offset(softImpactSpeed, RigidShapeData), "Minimum speed at which this object must be travelling for the soft impact sound to be played.");
  453. addField("hardImpactSpeed", TypeF32, Offset(hardImpactSpeed, RigidShapeData), "Minimum speed at which the object must be travelling for the hard impact sound to be played.");
  454. addField("minRollSpeed", TypeF32, Offset(minRollSpeed, RigidShapeData));
  455. addField("exitSplashSoundVelocity", TypeF32, Offset(exitSplashSoundVel, RigidShapeData), "The minimum velocity at which the exit splash sound will be played when emerging from water.\n");
  456. addField("softSplashSoundVelocity", TypeF32, Offset(softSplashSoundVel, RigidShapeData),"The minimum velocity at which the soft splash sound will be played when impacting water.\n");
  457. addField("mediumSplashSoundVelocity", TypeF32, Offset(medSplashSoundVel, RigidShapeData), "The minimum velocity at which the medium splash sound will be played when impacting water.\n");
  458. addField("hardSplashSoundVelocity", TypeF32, Offset(hardSplashSoundVel, RigidShapeData), "The minimum velocity at which the hard splash sound will be played when impacting water.\n");
  459. endGroup("Collision");
  460. addGroup( "Camera" );
  461. addField("cameraRoll", TypeBool, Offset(cameraRoll, RigidShapeData), "Specifies whether the camera's rotation matrix, and the render eye transform are multiplied during camera updates.\n");
  462. addField("cameraLag", TypeF32, Offset(cameraLag, RigidShapeData), "Scalar amount by which the third person camera lags the object, relative to the object's linear velocity.\n");
  463. addField("cameraDecay", TypeF32, Offset(cameraDecay, RigidShapeData), "Scalar rate at which the third person camera offset decays, per tick.\n");
  464. addField("cameraOffset", TypeF32, Offset(cameraOffset, RigidShapeData), "The vertical offset of the object's camera.\n");
  465. endGroup( "Camera" );
  466. }
  467. //----------------------------------------------------------------------------
  468. //----------------------------------------------------------------------------
  469. //----------------------------------------------------------------------------
  470. RigidShape::RigidShape()
  471. {
  472. mNetFlags.set(Ghostable);
  473. mDustTrailEmitter = NULL;
  474. mDataBlock = 0;
  475. // [rene, 27-Apr-11] WTH is a RigidShape a vehicle???
  476. mTypeMask |= VehicleObjectType | DynamicShapeObjectType;
  477. mDelta.pos = Point3F(0,0,0);
  478. mDelta.posVec = Point3F(0,0,0);
  479. mDelta.warpTicks = mDelta.warpCount = 0;
  480. mDelta.dt = 1;
  481. mDelta.move = NullMove;
  482. mPredictionCount = 0;
  483. mDelta.cameraOffset.set(0,0,0);
  484. mDelta.cameraVec.set(0,0,0);
  485. mDelta.cameraRot.set(0,0,0);
  486. mDelta.cameraRotVec.set(0,0,0);
  487. mRigid.linPosition.set(0, 0, 0);
  488. mRigid.linVelocity.set(0, 0, 0);
  489. mRigid.angPosition.identity();
  490. mRigid.angVelocity.set(0, 0, 0);
  491. mRigid.linMomentum.set(0, 0, 0);
  492. mRigid.angMomentum.set(0, 0, 0);
  493. mContacts.clear();
  494. mCameraOffset.set(0,0,0);
  495. dMemset( mDustEmitterList, 0, sizeof( mDustEmitterList ) );
  496. dMemset( mSplashEmitterList, 0, sizeof( mSplashEmitterList ) );
  497. mDisableMove = false; // start frozen by default
  498. restCount = 0;
  499. inLiquid = false;
  500. mWorkingQueryBox.minExtents.set(-1e9f, -1e9f, -1e9f);
  501. mWorkingQueryBox.maxExtents.set(-1e9f, -1e9f, -1e9f);
  502. mWorkingQueryBoxCountDown = sWorkingQueryBoxStaleThreshold;
  503. mPhysicsRep = NULL;
  504. }
  505. RigidShape::~RigidShape()
  506. {
  507. //
  508. }
  509. U32 RigidShape::getCollisionMask()
  510. {
  511. if (isServerObject())
  512. return sServerCollisionMask;
  513. else
  514. return sClientCollisionMask;
  515. }
  516. Point3F RigidShape::getVelocity() const
  517. {
  518. return mRigid.linVelocity;
  519. }
  520. //----------------------------------------------------------------------------
  521. bool RigidShape::onAdd()
  522. {
  523. if (!Parent::onAdd())
  524. return false;
  525. mWorkingQueryBox.minExtents.set(-1e9f, -1e9f, -1e9f);
  526. mWorkingQueryBox.maxExtents.set(-1e9f, -1e9f, -1e9f);
  527. // When loading from a mission script, the base SceneObject's transform
  528. // will have been set and needs to be transfered to the rigid body.
  529. mRigid.setTransform(mObjToWorld);
  530. // Initialize interpolation vars.
  531. mDelta.rot[1] = mDelta.rot[0] = mRigid.angPosition;
  532. mDelta.pos = mRigid.linPosition;
  533. mDelta.posVec = Point3F(0,0,0);
  534. // Create Emitters on the client
  535. if( isClientObject() )
  536. {
  537. if( mDataBlock->dustEmitter )
  538. {
  539. for( U32 i=0; i<RigidShapeData::VC_NUM_DUST_EMITTERS; i++ )
  540. {
  541. mDustEmitterList[i] = new ParticleEmitter;
  542. mDustEmitterList[i]->onNewDataBlock( mDataBlock->dustEmitter, false );
  543. if( !mDustEmitterList[i]->registerObject() )
  544. {
  545. Con::warnf( ConsoleLogEntry::General, "Could not register dust emitter for class: %s", mDataBlock->getName() );
  546. delete mDustEmitterList[i];
  547. mDustEmitterList[i] = NULL;
  548. }
  549. }
  550. }
  551. for( U32 j=0; j<RigidShapeData::VC_NUM_SPLASH_EMITTERS; j++ )
  552. {
  553. if( mDataBlock->splashEmitterList[j] )
  554. {
  555. mSplashEmitterList[j] = new ParticleEmitter;
  556. mSplashEmitterList[j]->onNewDataBlock( mDataBlock->splashEmitterList[j], false );
  557. if( !mSplashEmitterList[j]->registerObject() )
  558. {
  559. Con::warnf( ConsoleLogEntry::General, "Could not register splash emitter for class: %s", mDataBlock->getName() );
  560. delete mSplashEmitterList[j];
  561. mSplashEmitterList[j] = NULL;
  562. }
  563. }
  564. }
  565. }
  566. // Create a new convex.
  567. AssertFatal(mDataBlock->collisionDetails[0] != -1, "Error, a rigid shape must have a collision-1 detail!");
  568. mConvex.mObject = this;
  569. mConvex.pShapeBase = this;
  570. mConvex.hullId = 0;
  571. mConvex.box = mObjBox;
  572. mConvex.box.minExtents.convolve(mObjScale);
  573. mConvex.box.maxExtents.convolve(mObjScale);
  574. mConvex.findNodeTransform();
  575. _createPhysics();
  576. addToScene();
  577. if( !isServerObject() )
  578. {
  579. if( mDataBlock->dustTrailEmitter )
  580. {
  581. mDustTrailEmitter = new ParticleEmitter;
  582. mDustTrailEmitter->onNewDataBlock( mDataBlock->dustTrailEmitter, false );
  583. if( !mDustTrailEmitter->registerObject() )
  584. {
  585. Con::warnf( ConsoleLogEntry::General, "Could not register dust emitter for class: %s", mDataBlock->getName() );
  586. delete mDustTrailEmitter;
  587. mDustTrailEmitter = NULL;
  588. }
  589. }
  590. }
  591. if (isServerObject())
  592. scriptOnAdd();
  593. return true;
  594. }
  595. void RigidShape::onRemove()
  596. {
  597. scriptOnRemove();
  598. removeFromScene();
  599. U32 i=0;
  600. for( i=0; i<RigidShapeData::VC_NUM_DUST_EMITTERS; i++ )
  601. {
  602. if( mDustEmitterList[i] )
  603. {
  604. mDustEmitterList[i]->deleteWhenEmpty();
  605. mDustEmitterList[i] = NULL;
  606. }
  607. }
  608. for( i=0; i<RigidShapeData::VC_NUM_SPLASH_EMITTERS; i++ )
  609. {
  610. if( mSplashEmitterList[i] )
  611. {
  612. mSplashEmitterList[i]->deleteWhenEmpty();
  613. mSplashEmitterList[i] = NULL;
  614. }
  615. }
  616. mWorkingQueryBox.minExtents.set(-1e9f, -1e9f, -1e9f);
  617. mWorkingQueryBox.maxExtents.set(-1e9f, -1e9f, -1e9f);
  618. Parent::onRemove();
  619. }
  620. void RigidShape::_createPhysics()
  621. {
  622. SAFE_DELETE(mPhysicsRep);
  623. if (!PHYSICSMGR || !mDataBlock->enablePhysicsRep)
  624. return;
  625. TSShape* shape = mShapeInstance->getShape();
  626. PhysicsCollision* colShape = NULL;
  627. colShape = shape->buildColShape(false, getScale());
  628. if (colShape)
  629. {
  630. PhysicsWorld* world = PHYSICSMGR->getWorld(isServerObject() ? "server" : "client");
  631. mPhysicsRep = PHYSICSMGR->createBody();
  632. mPhysicsRep->init(colShape, 0, PhysicsBody::BF_KINEMATIC, this, world);
  633. mPhysicsRep->setTransform(getTransform());
  634. }
  635. }
  636. //----------------------------------------------------------------------------
  637. void RigidShape::processTick(const Move* move)
  638. {
  639. PROFILE_SCOPE(RigidShape_ProcessTick);
  640. Parent::processTick(move);
  641. if ( isMounted() )
  642. return;
  643. // Warp to catch up to server
  644. if (mDelta.warpCount < mDelta.warpTicks)
  645. {
  646. mDelta.warpCount++;
  647. // Set new pos.
  648. mObjToWorld.getColumn(3,&mDelta.pos);
  649. mDelta.pos += mDelta.warpOffset;
  650. mDelta.rot[0] = mDelta.rot[1];
  651. mDelta.rot[1].interpolate(mDelta.warpRot[0],mDelta.warpRot[1],F32(mDelta.warpCount)/mDelta.warpTicks);
  652. setPosition(mDelta.pos,mDelta.rot[1]);
  653. // Pos backstepping
  654. mDelta.posVec.x = -mDelta.warpOffset.x;
  655. mDelta.posVec.y = -mDelta.warpOffset.y;
  656. mDelta.posVec.z = -mDelta.warpOffset.z;
  657. }
  658. else
  659. {
  660. if (!move)
  661. {
  662. if (isGhost())
  663. {
  664. // If we haven't run out of prediction time,
  665. // predict using the last known move.
  666. if (mPredictionCount-- <= 0)
  667. return;
  668. move = &mDelta.move;
  669. }
  670. else
  671. move = &NullMove;
  672. }
  673. // Process input move
  674. updateMove(move);
  675. // Save current rigid state interpolation
  676. mDelta.posVec = mRigid.linPosition;
  677. mDelta.rot[0] = mRigid.angPosition;
  678. // Update the physics based on the integration rate
  679. S32 count = mDataBlock->integration;
  680. --mWorkingQueryBoxCountDown;
  681. if (!mDisableMove)
  682. updateWorkingCollisionSet(getCollisionMask());
  683. for (U32 i = 0; i < count; i++)
  684. updatePos(TickSec / count);
  685. // Wrap up interpolation info
  686. mDelta.pos = mRigid.linPosition;
  687. mDelta.posVec -= mRigid.linPosition;
  688. mDelta.rot[1] = mRigid.angPosition;
  689. // Update container database
  690. setPosition(mRigid.linPosition, mRigid.angPosition);
  691. setMaskBits(PositionMask);
  692. updateContainer();
  693. //TODO: Only update when position has actually changed
  694. //no need to check if mDataBlock->enablePhysicsRep is false as mPhysicsRep will be NULL if it is
  695. if (mPhysicsRep)
  696. mPhysicsRep->moveKinematicTo(getTransform());
  697. }
  698. }
  699. void RigidShape::interpolateTick(F32 dt)
  700. {
  701. Parent::interpolateTick(dt);
  702. if ( isMounted() )
  703. return;
  704. if(dt == 0.0f)
  705. setRenderPosition(mDelta.pos, mDelta.rot[1]);
  706. else
  707. {
  708. QuatF rot;
  709. rot.interpolate(mDelta.rot[1], mDelta.rot[0], dt);
  710. Point3F pos = mDelta.pos + mDelta.posVec * dt;
  711. setRenderPosition(pos,rot);
  712. }
  713. mDelta.dt = dt;
  714. }
  715. void RigidShape::advanceTime(F32 dt)
  716. {
  717. Parent::advanceTime(dt);
  718. updateFroth(dt);
  719. if ( isMounted() )
  720. return;
  721. // Update 3rd person camera offset. Camera update is done
  722. // here as it's a client side only animation.
  723. mCameraOffset -=
  724. (mCameraOffset * mDataBlock->cameraDecay +
  725. mRigid.linVelocity * mDataBlock->cameraLag) * dt;
  726. }
  727. //----------------------------------------------------------------------------
  728. bool RigidShape::onNewDataBlock(GameBaseData* dptr, bool reload)
  729. {
  730. mDataBlock = dynamic_cast<RigidShapeData*>(dptr);
  731. if (!mDataBlock || !Parent::onNewDataBlock(dptr, reload))
  732. return false;
  733. // Update Rigid Info
  734. mRigid.mass = mDataBlock->mass;
  735. mRigid.oneOverMass = 1 / mRigid.mass;
  736. mRigid.friction = mDataBlock->body.friction;
  737. mRigid.restitution = mDataBlock->body.restitution;
  738. mRigid.setCenterOfMass(mDataBlock->massCenter);
  739. // Ignores massBox, just set sphere for now. Derived objects
  740. // can set what they want.
  741. mRigid.setObjectInertia();
  742. scriptOnNewDataBlock();
  743. return true;
  744. }
  745. //----------------------------------------------------------------------------
  746. void RigidShape::getCameraParameters(F32 *min,F32* max,Point3F* off,MatrixF* rot)
  747. {
  748. *min = mDataBlock->cameraMinDist;
  749. *max = mDataBlock->cameraMaxDist;
  750. off->set(0,0,mDataBlock->cameraOffset);
  751. rot->identity();
  752. }
  753. //----------------------------------------------------------------------------
  754. void RigidShape::getCameraTransform(F32* pos,MatrixF* mat)
  755. {
  756. // Returns camera to world space transform
  757. // Handles first person / third person camera position
  758. if (isServerObject() && mShapeInstance)
  759. mShapeInstance->animateNodeSubtrees(true);
  760. if (*pos == 0)
  761. {
  762. getRenderEyeTransform(mat);
  763. return;
  764. }
  765. // Get the shape's camera parameters.
  766. F32 min,max;
  767. MatrixF rot;
  768. Point3F offset;
  769. getCameraParameters(&min,&max,&offset,&rot);
  770. // Start with the current eye position
  771. MatrixF eye;
  772. getRenderEyeTransform(&eye);
  773. // Build a transform that points along the eye axis
  774. // but where the Z axis is always up.
  775. if (mDataBlock->cameraRoll)
  776. mat->mul(eye,rot);
  777. else
  778. {
  779. MatrixF cam(1);
  780. VectorF x,y,z(0,0,1);
  781. eye.getColumn(1, &y);
  782. mCross(y, z, &x);
  783. x.normalize();
  784. mCross(x, y, &z);
  785. z.normalize();
  786. cam.setColumn(0,x);
  787. cam.setColumn(1,y);
  788. cam.setColumn(2,z);
  789. mat->mul(cam,rot);
  790. }
  791. // Camera is positioned straight back along the eye's -Y axis.
  792. // A ray is cast to make sure the camera doesn't go through
  793. // anything solid.
  794. VectorF vp,vec;
  795. vp.x = vp.z = 0;
  796. vp.y = -(max - min) * *pos;
  797. eye.mulV(vp,&vec);
  798. // Use the camera node as the starting position if it exists.
  799. Point3F osp,sp;
  800. if (mDataBlock->cameraNode != -1)
  801. {
  802. mShapeInstance->mNodeTransforms[mDataBlock->cameraNode].getColumn(3,&osp);
  803. getRenderTransform().mulP(osp,&sp);
  804. }
  805. else
  806. eye.getColumn(3,&sp);
  807. // Make sure we don't hit ourself...
  808. disableCollision();
  809. if (isMounted())
  810. getObjectMount()->disableCollision();
  811. // Cast the ray into the container database to see if we're going
  812. // to hit anything.
  813. RayInfo collision;
  814. Point3F ep = sp + vec + offset + mCameraOffset;
  815. if (mContainer->castRay(sp, ep,
  816. ~(WaterObjectType | GameBaseObjectType | DefaultObjectType),
  817. &collision) == true)
  818. {
  819. // Shift the collision point back a little to try and
  820. // avoid clipping against the front camera plane.
  821. F32 t = collision.t - (-mDot(vec, collision.normal) / vec.len()) * 0.1;
  822. if (t > 0.0f)
  823. ep = sp + offset + mCameraOffset + (vec * t);
  824. else
  825. eye.getColumn(3,&ep);
  826. }
  827. mat->setColumn(3,ep);
  828. // Re-enable our collision.
  829. if (isMounted())
  830. getObjectMount()->enableCollision();
  831. enableCollision();
  832. // Apply Camera FX.
  833. mat->mul( gCamFXMgr.getTrans() );
  834. }
  835. //----------------------------------------------------------------------------
  836. void RigidShape::getVelocity(const Point3F& r, Point3F* v)
  837. {
  838. mRigid.getVelocity(r, v);
  839. }
  840. void RigidShape::applyImpulse(const Point3F &pos, const Point3F &impulse)
  841. {
  842. Point3F r;
  843. mRigid.getOriginVector(pos,&r);
  844. mRigid.applyImpulse(r, impulse);
  845. }
  846. //----------------------------------------------------------------------------
  847. void RigidShape::updateMove(const Move* move)
  848. {
  849. mDelta.move = *move;
  850. }
  851. //----------------------------------------------------------------------------
  852. void RigidShape::setPosition(const Point3F& pos,const QuatF& rot)
  853. {
  854. MatrixF mat;
  855. rot.setMatrix(&mat);
  856. mat.setColumn(3,pos);
  857. Parent::setTransform(mat);
  858. }
  859. void RigidShape::setRenderPosition(const Point3F& pos, const QuatF& rot)
  860. {
  861. MatrixF mat;
  862. rot.setMatrix(&mat);
  863. mat.setColumn(3,pos);
  864. Parent::setRenderTransform(mat);
  865. }
  866. void RigidShape::setTransform(const MatrixF& newMat)
  867. {
  868. mRigid.setTransform(newMat);
  869. Parent::setTransform(newMat);
  870. mRigid.atRest = false;
  871. mContacts.clear();
  872. }
  873. void RigidShape::forceClientTransform()
  874. {
  875. setMaskBits(ForceMoveMask);
  876. }
  877. //-----------------------------------------------------------------------------
  878. void RigidShape::disableCollision()
  879. {
  880. Parent::disableCollision();
  881. }
  882. void RigidShape::enableCollision()
  883. {
  884. Parent::enableCollision();
  885. }
  886. //----------------------------------------------------------------------------
  887. /** Update the physics
  888. */
  889. void RigidShape::updatePos(F32 dt)
  890. {
  891. PROFILE_SCOPE(RigidShape_UpdatePos);
  892. Point3F origVelocity = mRigid.linVelocity;
  893. // Update internal forces acting on the body.
  894. mRigid.clearForces();
  895. updateForces(dt);
  896. // Update collision information based on our current pos.
  897. bool collided = false;
  898. if (!mRigid.atRest && !mDisableMove)
  899. {
  900. collided = updateCollision(dt);
  901. // Now that all the forces have been processed, lets
  902. // see if we're at rest. Basically, if the kinetic energy of
  903. // the rigid body is less than some percentage of the energy added
  904. // by gravity for a short period, we're considered at rest.
  905. // This should really be part of the rigid class...
  906. if (mCollisionList.getCount())
  907. {
  908. F32 k = mRigid.getKineticEnergy();
  909. F32 G = mNetGravity * dt;
  910. F32 Kg = 0.5 * mRigid.mass * G * G;
  911. if (k < sRestTol * Kg && ++restCount > sRestCount)
  912. mRigid.setAtRest();
  913. }
  914. else
  915. restCount = 0;
  916. }
  917. // Integrate forward
  918. if (!mRigid.atRest && !mDisableMove)
  919. mRigid.integrate(dt);
  920. // Deal with client and server scripting, sounds, etc.
  921. if (isServerObject())
  922. {
  923. // Check triggers and other objects that we normally don't
  924. // collide with. This function must be called before notifyCollision
  925. // as it will queue collision.
  926. checkTriggers();
  927. // Invoke the onCollision notify callback for all the objects
  928. // we've just hit.
  929. notifyCollision();
  930. // Server side impact script callback
  931. if (collided)
  932. {
  933. VectorF collVec = mRigid.linVelocity - origVelocity;
  934. F32 collSpeed = collVec.len();
  935. if (collSpeed > mDataBlock->minImpactSpeed)
  936. onImpact(collVec);
  937. }
  938. // Water script callbacks
  939. if (!inLiquid && mWaterCoverage != 0.0f)
  940. {
  941. mDataBlock->onEnterLiquid_callback(this, mWaterCoverage, mLiquidType.c_str());
  942. inLiquid = true;
  943. }
  944. else if (inLiquid && mWaterCoverage == 0.0f)
  945. {
  946. mDataBlock->onLeaveLiquid_callback(this, mLiquidType.c_str());
  947. inLiquid = false;
  948. }
  949. }
  950. else {
  951. // Play impact sounds on the client.
  952. if (collided) {
  953. F32 collSpeed = (mRigid.linVelocity - origVelocity).len();
  954. S32 impactSound = -1;
  955. if (collSpeed >= mDataBlock->hardImpactSpeed)
  956. impactSound = RigidShapeData::Body::HardImpactSound;
  957. else
  958. if (collSpeed >= mDataBlock->softImpactSpeed)
  959. impactSound = RigidShapeData::Body::SoftImpactSound;
  960. if (impactSound != -1 && mDataBlock->getBodySoundsProfile(impactSound))
  961. SFX->playOnce(mDataBlock->getBodySoundsProfile(impactSound), &getTransform());
  962. }
  963. // Water volume sounds
  964. F32 vSpeed = getVelocity().len();
  965. if (!inLiquid && mWaterCoverage >= 0.8f) {
  966. if (vSpeed >= mDataBlock->hardSplashSoundVel)
  967. SFX->playOnce(mDataBlock->getWaterSoundsProfile(RigidShapeData::ImpactHard), &getTransform());
  968. else
  969. if (vSpeed >= mDataBlock->medSplashSoundVel)
  970. SFX->playOnce(mDataBlock->getWaterSoundsProfile(RigidShapeData::ImpactMedium), &getTransform());
  971. else
  972. if (vSpeed >= mDataBlock->softSplashSoundVel)
  973. SFX->playOnce(mDataBlock->getWaterSoundsProfile(RigidShapeData::ImpactSoft), &getTransform());
  974. inLiquid = true;
  975. }
  976. else
  977. if (inLiquid && mWaterCoverage < 0.8f) {
  978. if (vSpeed >= mDataBlock->exitSplashSoundVel)
  979. SFX->playOnce(mDataBlock->getWaterSoundsProfile(RigidShapeData::ExitWater), &getTransform());
  980. inLiquid = false;
  981. }
  982. }
  983. }
  984. //----------------------------------------------------------------------------
  985. void RigidShape::updateForces(F32 dt)
  986. {
  987. if (mDisableMove) return;
  988. Point3F torque(0, 0, 0);
  989. Point3F force(0, 0, mRigid.mass * mNetGravity);
  990. // Apply drag
  991. Point3F vertDrag = mRigid.linVelocity*Point3F(1, 1, mDataBlock->vertFactor);
  992. force -= vertDrag * mDataBlock->dragForce;
  993. // Add in physical zone force
  994. force += mAppliedForce;
  995. force -= mRigid.linVelocity * mDrag;
  996. torque -= mRigid.angMomentum * mDrag;
  997. mRigid.force = force;
  998. mRigid.torque = torque;
  999. // If we're still atRest, make sure we're not accumulating anything
  1000. if (mRigid.atRest)
  1001. mRigid.setAtRest();
  1002. }
  1003. //-----------------------------------------------------------------------------
  1004. /** Update collision information
  1005. Update the convex state and check for collisions. If the object is in
  1006. collision, impact and contact forces are generated.
  1007. */
  1008. bool RigidShape::updateCollision(F32 dt)
  1009. {
  1010. PROFILE_SCOPE(RigidShape_updateCollision);
  1011. if (mRigid.atRest || mDisableMove || (getVelocity().lenSquared() < mDataBlock->contactTol * mDataBlock->contactTol)) return false;
  1012. // Update collision information
  1013. MatrixF mat,cmat;
  1014. mConvex.transform = &mat;
  1015. mRigid.getTransform(&mat);
  1016. cmat = mConvex.getTransform();
  1017. mCollisionList.clear();
  1018. CollisionState *state = mConvex.findClosestState(cmat, getScale(), mDataBlock->collisionTol);
  1019. if (state && state->mDist <= mDataBlock->collisionTol)
  1020. {
  1021. //resolveDisplacement(ns,state,dt);
  1022. mConvex.getCollisionInfo(cmat, getScale(), &mCollisionList, mDataBlock->collisionTol);
  1023. }
  1024. // Resolve collisions
  1025. bool collided = resolveCollision(mRigid,mCollisionList);
  1026. resolveContacts(mRigid,mCollisionList,dt);
  1027. return collided;
  1028. }
  1029. //----------------------------------------------------------------------------
  1030. /** Resolve collision impacts
  1031. Handle collision impacts, as opposed to contacts. Impulses are calculated based
  1032. on standard collision resolution formulas.
  1033. */
  1034. bool RigidShape::resolveCollision(Rigid& ns,CollisionList& cList)
  1035. {
  1036. PROFILE_SCOPE(RigidShape_resolveCollision);
  1037. // Apply impulses to resolve collision
  1038. bool collided = false;
  1039. for (S32 i = 0; i < cList.getCount(); i++)
  1040. {
  1041. Collision& c = cList[i];
  1042. if (c.distance < mDataBlock->collisionTol)
  1043. {
  1044. // Velocity into surface
  1045. Point3F v, r;
  1046. ns.getOriginVector(c.point, &r);
  1047. ns.getVelocity(r, &v);
  1048. F32 vn = mDot(v, c.normal);
  1049. // Only interested in velocities greater than sContactTol,
  1050. // velocities less than that will be dealt with as contacts
  1051. // "constraints".
  1052. if (vn < -mDataBlock->contactTol)
  1053. {
  1054. // Apply impulses to the rigid body to keep it from
  1055. // penetrating the surface.
  1056. if (c.object->getTypeMask() & VehicleObjectType)
  1057. {
  1058. RigidShape* otherRigid = dynamic_cast<RigidShape*>(c.object);
  1059. if (otherRigid)
  1060. ns.resolveCollision(cList[i].point, cList[i].normal, &otherRigid->mRigid);
  1061. else
  1062. ns.resolveCollision(cList[i].point, cList[i].normal);
  1063. }
  1064. else ns.resolveCollision(cList[i].point, cList[i].normal);
  1065. collided = true;
  1066. // Keep track of objects we collide with
  1067. if (!isGhost() && c.object->getTypeMask() & ShapeBaseObjectType)
  1068. {
  1069. ShapeBase* col = static_cast<ShapeBase*>(c.object);
  1070. queueCollision(col, v - col->getVelocity());
  1071. }
  1072. }
  1073. }
  1074. }
  1075. return collided;
  1076. }
  1077. //----------------------------------------------------------------------------
  1078. /** Resolve contact forces
  1079. Resolve contact forces using the "penalty" method. Forces are generated based
  1080. on the depth of penetration and the moment of inertia at the point of contact.
  1081. */
  1082. bool RigidShape::resolveContacts(Rigid& ns,CollisionList& cList,F32 dt)
  1083. {
  1084. PROFILE_SCOPE(RigidShape_resolveContacts);
  1085. // Use spring forces to manage contact constraints.
  1086. bool collided = false;
  1087. Point3F t,p(0,0,0),l(0,0,0);
  1088. for (S32 i = 0; i < cList.getCount(); i++)
  1089. {
  1090. const Collision& c = cList[i];
  1091. if (c.distance < mDataBlock->collisionTol)
  1092. {
  1093. // Velocity into the surface
  1094. Point3F v,r;
  1095. ns.getOriginVector(c.point,&r);
  1096. ns.getVelocity(r,&v);
  1097. F32 vn = mDot(v,c.normal);
  1098. // Only interested in velocities less than mDataBlock->contactTol,
  1099. // velocities greater than that are dealt with as collisions.
  1100. if (mFabs(vn) < mDataBlock->contactTol)
  1101. {
  1102. collided = true;
  1103. // Penetration force. This is actually a spring which
  1104. // will seperate the body from the collision surface.
  1105. F32 zi = 2 * mFabs(mRigid.getZeroImpulse(r,c.normal));
  1106. F32 s = (mDataBlock->collisionTol - c.distance) * zi - ((vn / mDataBlock->contactTol) * zi);
  1107. Point3F f = c.normal * s;
  1108. // Friction impulse, calculated as a function of the
  1109. // amount of force it would take to stop the motion
  1110. // perpendicular to the normal.
  1111. Point3F uv = v - (c.normal * vn);
  1112. F32 ul = uv.len();
  1113. if (s > 0 && ul)
  1114. {
  1115. uv /= -ul;
  1116. F32 u = ul * ns.getZeroImpulse(r,uv);
  1117. s *= mRigid.friction;
  1118. if (u > s)
  1119. u = s;
  1120. f += uv * u;
  1121. }
  1122. // Accumulate forces
  1123. p += f;
  1124. mCross(r,f,&t);
  1125. l += t;
  1126. }
  1127. }
  1128. }
  1129. // Contact constraint forces act over time...
  1130. ns.linMomentum += p * dt;
  1131. ns.angMomentum += l * dt;
  1132. ns.updateVelocity();
  1133. return true;
  1134. }
  1135. //----------------------------------------------------------------------------
  1136. bool RigidShape::resolveDisplacement(Rigid& ns,CollisionState *state, F32 dt)
  1137. {
  1138. SceneObject* obj = (state->mA->getObject() == this)?
  1139. state->mB->getObject(): state->mA->getObject();
  1140. if (obj->isDisplacable() && ((obj->getTypeMask() & ShapeBaseObjectType) != 0))
  1141. {
  1142. // Try to displace the object by the amount we're trying to move
  1143. Point3F objNewMom = ns.linVelocity * obj->getMass() * 1.1f;
  1144. Point3F objOldMom = obj->getMomentum();
  1145. Point3F objNewVel = objNewMom / obj->getMass();
  1146. Point3F myCenter;
  1147. Point3F theirCenter;
  1148. getWorldBox().getCenter(&myCenter);
  1149. obj->getWorldBox().getCenter(&theirCenter);
  1150. if (mDot(myCenter - theirCenter, objNewMom) >= 0.0f || objNewVel.len() < 0.01)
  1151. {
  1152. objNewMom = (theirCenter - myCenter);
  1153. objNewMom.normalize();
  1154. objNewMom *= 1.0f * obj->getMass();
  1155. objNewVel = objNewMom / obj->getMass();
  1156. }
  1157. obj->setMomentum(objNewMom);
  1158. if (obj->displaceObject(objNewVel * 1.1f * dt) == true)
  1159. {
  1160. // Queue collision and change in velocity
  1161. VectorF dv = (objOldMom - objNewMom) / obj->getMass();
  1162. queueCollision(static_cast<ShapeBase*>(obj), dv);
  1163. return true;
  1164. }
  1165. }
  1166. return false;
  1167. }
  1168. //----------------------------------------------------------------------------
  1169. void RigidShape::updateWorkingCollisionSet(const U32 mask)
  1170. {
  1171. PROFILE_SCOPE( Vehicle_UpdateWorkingCollisionSet );
  1172. // First, we need to adjust our velocity for possible acceleration. It is assumed
  1173. // that we will never accelerate more than 20 m/s for gravity, plus 30 m/s for
  1174. // jetting, and an equivalent 10 m/s for vehicle accel. We also assume that our
  1175. // working list is updated on a Tick basis, which means we only expand our box by
  1176. // the possible movement in that tick, plus some extra for caching purposes
  1177. Box3F convexBox = mConvex.getBoundingBox(getTransform(), getScale());
  1178. F32 len = (mRigid.linVelocity.len() + 50) * TickSec;
  1179. F32 l = (len * 1.1) + 0.1; // fudge factor
  1180. convexBox.minExtents -= Point3F(l, l, l);
  1181. convexBox.maxExtents += Point3F(l, l, l);
  1182. // Check to see if it is actually necessary to construct the new working list,
  1183. // or if we can use the cached version from the last query. We use the x
  1184. // component of the min member of the mWorkingQueryBox, which is lame, but
  1185. // it works ok.
  1186. bool updateSet = false;
  1187. // Check containment
  1188. if ((sWorkingQueryBoxStaleThreshold == -1 || mWorkingQueryBoxCountDown > 0) && mWorkingQueryBox.minExtents.x != -1e9f)
  1189. {
  1190. if (mWorkingQueryBox.isContained(convexBox) == false)
  1191. // Needed region is outside the cached region. Update it.
  1192. updateSet = true;
  1193. }
  1194. else
  1195. {
  1196. // Must update
  1197. updateSet = true;
  1198. }
  1199. // Actually perform the query, if necessary
  1200. if (updateSet == true)
  1201. {
  1202. mWorkingQueryBoxCountDown = sWorkingQueryBoxStaleThreshold;
  1203. const Point3F lPoint( sWorkingQueryBoxSizeMultiplier * l );
  1204. mWorkingQueryBox = convexBox;
  1205. mWorkingQueryBox.minExtents -= lPoint;
  1206. mWorkingQueryBox.maxExtents += lPoint;
  1207. disableCollision();
  1208. mConvex.updateWorkingList(mWorkingQueryBox, mask);
  1209. enableCollision();
  1210. }
  1211. }
  1212. //----------------------------------------------------------------------------
  1213. /** Check collisions with trigger and items
  1214. Perform a container search using the current bounding box
  1215. of the main body, wheels are not included. This method should
  1216. only be called on the server.
  1217. */
  1218. void RigidShape::checkTriggers()
  1219. {
  1220. Box3F bbox = mConvex.getBoundingBox(getTransform(), getScale());
  1221. gServerContainer.findObjects(bbox,sTriggerMask,findCallback,this);
  1222. }
  1223. /** The callback used in by the checkTriggers() method.
  1224. The checkTriggers method uses a container search which will
  1225. invoke this callback on each obj that matches.
  1226. */
  1227. void RigidShape::findCallback(SceneObject* obj,void *key)
  1228. {
  1229. RigidShape* shape = reinterpret_cast<RigidShape*>(key);
  1230. U32 objectMask = obj->getTypeMask();
  1231. // Check: triggers, corpses and items, basically the same things
  1232. // that the player class checks for
  1233. if (objectMask & TriggerObjectType) {
  1234. Trigger* pTrigger = static_cast<Trigger*>(obj);
  1235. pTrigger->potentialEnterObject(shape);
  1236. }
  1237. else if (objectMask & CorpseObjectType) {
  1238. ShapeBase* col = static_cast<ShapeBase*>(obj);
  1239. shape->queueCollision(col,shape->getVelocity() - col->getVelocity());
  1240. }
  1241. else if (objectMask & ItemObjectType) {
  1242. Item* item = static_cast<Item*>(obj);
  1243. if (shape != item->getCollisionObject())
  1244. shape->queueCollision(item,shape->getVelocity() - item->getVelocity());
  1245. }
  1246. }
  1247. //----------------------------------------------------------------------------
  1248. void RigidShape::writePacketData(GameConnection *connection, BitStream *stream)
  1249. {
  1250. Parent::writePacketData(connection, stream);
  1251. mathWrite(*stream, mRigid.linPosition);
  1252. mathWrite(*stream, mRigid.angPosition);
  1253. mathWrite(*stream, mRigid.linMomentum);
  1254. mathWrite(*stream, mRigid.angMomentum);
  1255. stream->writeFlag(mRigid.atRest);
  1256. stream->writeFlag(mContacts.getCount() == 0);
  1257. stream->writeFlag(mDisableMove);
  1258. stream->setCompressionPoint(mRigid.linPosition);
  1259. }
  1260. void RigidShape::readPacketData(GameConnection *connection, BitStream *stream)
  1261. {
  1262. Parent::readPacketData(connection, stream);
  1263. mathRead(*stream, &mRigid.linPosition);
  1264. mathRead(*stream, &mRigid.angPosition);
  1265. mathRead(*stream, &mRigid.linMomentum);
  1266. mathRead(*stream, &mRigid.angMomentum);
  1267. mRigid.atRest = stream->readFlag();
  1268. if (stream->readFlag())
  1269. mContacts.clear();
  1270. mRigid.updateInertialTensor();
  1271. mRigid.updateVelocity();
  1272. mDisableMove = stream->readFlag();
  1273. stream->setCompressionPoint(mRigid.linPosition);
  1274. }
  1275. //----------------------------------------------------------------------------
  1276. U32 RigidShape::packUpdate(NetConnection *con, U32 mask, BitStream *stream)
  1277. {
  1278. U32 retMask = Parent::packUpdate(con, mask, stream);
  1279. // The rest of the data is part of the control object packet update.
  1280. // If we're controlled by this client, we don't need to send it.
  1281. if (stream->writeFlag(getControllingClient() == con && !(mask & InitialUpdateMask)))
  1282. return retMask;
  1283. mDelta.move.pack(stream);
  1284. if (stream->writeFlag(mask & PositionMask))
  1285. {
  1286. stream->writeFlag(mask & ForceMoveMask);
  1287. stream->writeCompressedPoint(mRigid.linPosition);
  1288. mathWrite(*stream, mRigid.angPosition);
  1289. mathWrite(*stream, mRigid.linMomentum);
  1290. mathWrite(*stream, mRigid.angMomentum);
  1291. stream->writeFlag(mRigid.atRest);
  1292. }
  1293. if(stream->writeFlag(mask & FreezeMask))
  1294. stream->writeFlag(mDisableMove);
  1295. return retMask;
  1296. }
  1297. void RigidShape::unpackUpdate(NetConnection *con, BitStream *stream)
  1298. {
  1299. Parent::unpackUpdate(con,stream);
  1300. if (stream->readFlag())
  1301. return;
  1302. mDelta.move.unpack(stream);
  1303. if (stream->readFlag())
  1304. {
  1305. // Check if we need to jump to the given transform
  1306. // rather than interpolate to it.
  1307. bool forceUpdate = stream->readFlag();
  1308. mPredictionCount = sMaxPredictionTicks;
  1309. F32 speed = mRigid.linVelocity.len();
  1310. mDelta.warpRot[0] = mRigid.angPosition;
  1311. // Read in new position and momentum values
  1312. stream->readCompressedPoint(&mRigid.linPosition);
  1313. mathRead(*stream, &mRigid.angPosition);
  1314. mathRead(*stream, &mRigid.linMomentum);
  1315. mathRead(*stream, &mRigid.angMomentum);
  1316. mRigid.atRest = stream->readFlag();
  1317. mRigid.updateVelocity();
  1318. if (!forceUpdate && isProperlyAdded())
  1319. {
  1320. // Determine number of ticks to warp based on the average
  1321. // of the client and server velocities.
  1322. Point3F cp = mDelta.pos + mDelta.posVec * mDelta.dt;
  1323. mDelta.warpOffset = mRigid.linPosition - cp;
  1324. // Calc the distance covered in one tick as the average of
  1325. // the old speed and the new speed from the server.
  1326. F32 dt,as = (speed + mRigid.linVelocity.len()) * 0.5 * TickSec;
  1327. // Cal how many ticks it will take to cover the warp offset.
  1328. // If it's less than what's left in the current tick, we'll just
  1329. // warp in the remaining time.
  1330. if (!as || (dt = mDelta.warpOffset.len() / as) > sMaxWarpTicks)
  1331. dt = mDelta.dt + sMaxWarpTicks;
  1332. else
  1333. dt = (dt <= mDelta.dt)? mDelta.dt : mCeil(dt - mDelta.dt) + mDelta.dt;
  1334. // Adjust current frame interpolation
  1335. if (mDelta.dt)
  1336. {
  1337. mDelta.pos = cp + (mDelta.warpOffset * (mDelta.dt / dt));
  1338. mDelta.posVec = (cp - mDelta.pos) / mDelta.dt;
  1339. QuatF cr;
  1340. cr.interpolate(mDelta.rot[1],mDelta.rot[0],mDelta.dt);
  1341. mDelta.rot[1].interpolate(cr,mRigid.angPosition,mDelta.dt / dt);
  1342. mDelta.rot[0].extrapolate(mDelta.rot[1],cr,mDelta.dt);
  1343. }
  1344. // Calculated multi-tick warp
  1345. mDelta.warpCount = 0;
  1346. mDelta.warpTicks = (S32)(mFloor(dt));
  1347. if (mDelta.warpTicks)
  1348. {
  1349. mDelta.warpOffset = mRigid.linPosition - mDelta.pos;
  1350. mDelta.warpOffset /= mDelta.warpTicks;
  1351. mDelta.warpRot[0] = mDelta.rot[1];
  1352. mDelta.warpRot[1] = mRigid.angPosition;
  1353. }
  1354. }
  1355. else
  1356. {
  1357. // Set the shape to the server position
  1358. mDelta.dt = 0;
  1359. mDelta.pos = mRigid.linPosition;
  1360. mDelta.posVec.set(0,0,0);
  1361. mDelta.rot[1] = mDelta.rot[0] = mRigid.angPosition;
  1362. mDelta.warpCount = mDelta.warpTicks = 0;
  1363. setPosition(mRigid.linPosition, mRigid.angPosition);
  1364. }
  1365. }
  1366. if(stream->readFlag())
  1367. mDisableMove = stream->readFlag();
  1368. }
  1369. //----------------------------------------------------------------------------
  1370. //----------------------------------------------------------------------------
  1371. void RigidShape::consoleInit()
  1372. {
  1373. Con::addVariable("$rigidPhysics::workingQueryBoxStaleThreshold", TypeS32, &sWorkingQueryBoxStaleThreshold,
  1374. "@brief The maximum number of ticks that go by before the mWorkingQueryBox is considered stale and needs updating.\n\n"
  1375. "Other factors can cause the collision working query box to become invalidated, such as the rigid body moving far "
  1376. "enough outside of this cached box. The smaller this number, the more times the working list of triangles that are "
  1377. "considered for collision is refreshed. This has the greatest impact with colliding with high triangle count meshes.\n\n"
  1378. "@note Set to -1 to disable any time-based forced check.\n\n"
  1379. "@ingroup GameObjects\n");
  1380. Con::addVariable("$rigidPhysics::workingQueryBoxSizeMultiplier", TypeF32, &sWorkingQueryBoxSizeMultiplier,
  1381. "@brief How much larger the mWorkingQueryBox should be made when updating the working collision list.\n\n"
  1382. "The larger this number the less often the working list will be updated due to motion, but any non-static shape that "
  1383. "moves into the query box will not be noticed.\n\n"
  1384. "@ingroup GameObjects\n");
  1385. }
  1386. void RigidShape::initPersistFields()
  1387. {
  1388. docsURL;
  1389. Parent::initPersistFields();
  1390. }
  1391. //----------------------------------------------------------------------------
  1392. void RigidShape::updateLiftoffDust( F32 dt )
  1393. {
  1394. Point3F offset( 0.0, 0.0, mDataBlock->dustHeight );
  1395. emitDust( mDustEmitterList[ 0 ], mDataBlock->triggerDustHeight, offset,
  1396. ( U32 )( dt * 1000 ) );
  1397. }
  1398. //--------------------------------------------------------------------------
  1399. void RigidShape::updateFroth( F32 dt )
  1400. {
  1401. // update bubbles
  1402. Point3F moveDir = getVelocity();
  1403. Point3F contactPoint;
  1404. F32 speed = moveDir.len();
  1405. if( speed < mDataBlock->splashVelEpsilon ) speed = 0.0;
  1406. U32 emitRate = (U32)(speed * mDataBlock->splashFreqMod * dt);
  1407. U32 i;
  1408. for( i=0; i<RigidShapeData::VC_NUM_SPLASH_EMITTERS; i++ )
  1409. {
  1410. if( mSplashEmitterList[i] )
  1411. {
  1412. mSplashEmitterList[i]->emitParticles( contactPoint, contactPoint, Point3F( 0.0, 0.0, 1.0 ),
  1413. moveDir, emitRate );
  1414. }
  1415. }
  1416. }
  1417. //--------------------------------------------------------------------------
  1418. // Returns true if shape is intersecting a water surface (roughly)
  1419. //--------------------------------------------------------------------------
  1420. bool RigidShape::collidingWithWater( Point3F &waterHeight )
  1421. {
  1422. Point3F curPos = getPosition();
  1423. F32 height = mFabs( mObjBox.maxExtents.z - mObjBox.minExtents.z );
  1424. RayInfo rInfo;
  1425. if( gClientContainer.castRay( curPos + Point3F(0.0, 0.0, height), curPos, WaterObjectType, &rInfo) )
  1426. {
  1427. waterHeight = rInfo.point;
  1428. return true;
  1429. }
  1430. return false;
  1431. }
  1432. void RigidShape::setEnergyLevel(F32 energy)
  1433. {
  1434. Parent::setEnergyLevel(energy);
  1435. setMaskBits(EnergyMask);
  1436. }
  1437. void RigidShape::prepBatchRender( SceneRenderState *state, S32 mountedImageIndex )
  1438. {
  1439. Parent::prepBatchRender( state, mountedImageIndex );
  1440. if ( !gShowBoundingBox )
  1441. return;
  1442. ObjectRenderInst *ri = state->getRenderPass()->allocInst<ObjectRenderInst>();
  1443. ri->renderDelegate.bind( this, &RigidShape::_renderMassAndContacts );
  1444. ri->type = RenderPassManager::RIT_Editor;
  1445. state->getRenderPass()->addInst( ri );
  1446. }
  1447. void RigidShape::_renderMassAndContacts( ObjectRenderInst *ri, SceneRenderState *state, BaseMatInstance *overrideMat )
  1448. {
  1449. // Box for the center of Mass
  1450. GFXStateBlockDesc desc;
  1451. desc.setBlend(false, GFXBlendSrcAlpha, GFXBlendInvSrcAlpha);
  1452. desc.setZReadWrite(false);
  1453. desc.fillMode = GFXFillWireframe;
  1454. GFX->getDrawUtil()->drawCube( desc, Point3F(0.1f,0.1f,0.1f), mDataBlock->massCenter, ColorI(255, 255, 255), &mRenderObjToWorld );
  1455. // Collision points...
  1456. for (S32 i = 0; i < mCollisionList.getCount(); i++)
  1457. {
  1458. const Collision& collision = mCollisionList[i];
  1459. GFX->getDrawUtil()->drawCube( desc, Point3F(0.05f,0.05f,0.05f), collision.point, ColorI(0, 0, 255) );
  1460. }
  1461. // Render the normals as one big batch...
  1462. PrimBuild::begin(GFXLineList, mCollisionList.getCount() * 2);
  1463. for (S32 i = 0; i < mCollisionList.getCount(); i++)
  1464. {
  1465. const Collision& collision = mCollisionList[i];
  1466. PrimBuild::color3f(1, 1, 1);
  1467. PrimBuild::vertex3fv(collision.point);
  1468. PrimBuild::vertex3fv(collision.point + collision.normal * 0.05f);
  1469. }
  1470. PrimBuild::end();
  1471. // Build and render the collision polylist which is returned
  1472. // in the server's world space.
  1473. ClippedPolyList polyList;
  1474. polyList.mPlaneList.setSize(6);
  1475. polyList.mPlaneList[0].set(getWorldBox().minExtents,VectorF(-1,0,0));
  1476. polyList.mPlaneList[1].set(getWorldBox().minExtents,VectorF(0,-1,0));
  1477. polyList.mPlaneList[2].set(getWorldBox().minExtents,VectorF(0,0,-1));
  1478. polyList.mPlaneList[3].set(getWorldBox().maxExtents,VectorF(1,0,0));
  1479. polyList.mPlaneList[4].set(getWorldBox().maxExtents,VectorF(0,1,0));
  1480. polyList.mPlaneList[5].set(getWorldBox().maxExtents,VectorF(0,0,1));
  1481. Box3F dummyBox;
  1482. SphereF dummySphere;
  1483. buildPolyList(PLC_Collision, &polyList, dummyBox, dummySphere);
  1484. //polyList.render();
  1485. }
  1486. void RigidShape::reset()
  1487. {
  1488. mRigid.clearForces();
  1489. mRigid.setAtRest();
  1490. }
  1491. void RigidShape::freezeSim(bool frozen)
  1492. {
  1493. mDisableMove = frozen;
  1494. setMaskBits(FreezeMask);
  1495. }
  1496. DefineEngineMethod( RigidShape, reset, void, (),,
  1497. "@brief Clears physic forces from the shape and sets it at rest.\n\n"
  1498. "@tsexample\n"
  1499. "// Inform the RigidShape object to reset.\n"
  1500. "%thisRigidShape.reset();\n"
  1501. "@endtsexample\n\n"
  1502. "@see ShapeBaseData")
  1503. {
  1504. object->reset();
  1505. }
  1506. DefineEngineMethod( RigidShape, freezeSim, void, (bool isFrozen),,
  1507. "@brief Enables or disables the physics simulation on the RigidShape object.\n\n"
  1508. "@param isFrozen Boolean frozen state to set the object.\n"
  1509. "@tsexample\n"
  1510. "// Define the frozen state.\n"
  1511. "%isFrozen = \"true\";\n\n"
  1512. "// Inform the object of the defined frozen state\n"
  1513. "%thisRigidShape.freezeSim(%isFrozen);\n"
  1514. "@endtsexample\n\n"
  1515. "@see ShapeBaseData")
  1516. {
  1517. object->freezeSim(isFrozen);
  1518. }
  1519. DefineEngineMethod( RigidShape, forceClientTransform, void, (),,
  1520. "@brief Forces the client to jump to the RigidShape's transform rather then warp to it.\n\n")
  1521. {
  1522. if(object->isServerObject())
  1523. {
  1524. object->forceClientTransform();
  1525. }
  1526. }