physicsShape.cpp 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173
  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/physics/physicsShape.h"
  24. #include "console/consoleTypes.h"
  25. #include "core/stream/bitStream.h"
  26. #include "core/resourceManager.h"
  27. #include "math/mathIO.h"
  28. #include "T3D/physics/physicsPlugin.h"
  29. #include "T3D/physics/physicsBody.h"
  30. #include "T3D/physics/physicsWorld.h"
  31. #include "T3D/physics/physicsCollision.h"
  32. #include "collision/concretePolyList.h"
  33. #include "ts/tsShapeInstance.h"
  34. #include "scene/sceneRenderState.h"
  35. #include "gfx/gfxTransformSaver.h"
  36. #include "T3D/physics/physicsDebris.h"
  37. #include "T3D/fx/explosion.h"
  38. #include "T3D/containerQuery.h"
  39. #include "lighting/lightQuery.h"
  40. #include "console/engineAPI.h"
  41. using namespace Torque;
  42. bool PhysicsShape::smNoCorrections = false;
  43. bool PhysicsShape::smNoSmoothing = false;
  44. ImplementEnumType( PhysicsSimType,
  45. "How to handle the physics simulation with the client's and server.\n"
  46. "@ingroup Physics\n\n")
  47. { PhysicsShapeData::SimType_ClientOnly, "ClientOnly", "Only handle physics on the client.\n" },
  48. { PhysicsShapeData::SimType_ServerOnly, "ServerOnly", "Only handle physics on the server.\n" },
  49. { PhysicsShapeData::SimType_ClientServer, "ClientServer", "Handle physics on both the client and server.\n" }
  50. EndImplementEnumType;
  51. IMPLEMENT_CO_DATABLOCK_V1( PhysicsShapeData );
  52. ConsoleDocClass( PhysicsShapeData,
  53. "@brief Defines the properties of a PhysicsShape.\n\n"
  54. "@see PhysicsShape.\n"
  55. "@ingroup Physics"
  56. );
  57. PhysicsShapeData::PhysicsShapeData()
  58. : shapeName( NULL ),
  59. mass( 1.0f ),
  60. dynamicFriction( 0.0f ),
  61. staticFriction( 0.0f ),
  62. restitution( 0.0f ),
  63. linearDamping( 0.0f ),
  64. angularDamping( 0.0f ),
  65. linearSleepThreshold( 1.0f ),
  66. angularSleepThreshold( 1.0f ),
  67. waterDampingScale( 1.0f ),
  68. buoyancyDensity( 0.0f ),
  69. simType( SimType_ClientServer )
  70. {
  71. }
  72. PhysicsShapeData::~PhysicsShapeData()
  73. {
  74. }
  75. void PhysicsShapeData::initPersistFields()
  76. {
  77. Parent::initPersistFields();
  78. addGroup("Media");
  79. addField( "shapeName", TypeShapeFilename, Offset( shapeName, PhysicsShapeData ),
  80. "@brief Path to the .DAE or .DTS file to use for this shape.\n\n"
  81. "Compatable with Live-Asset Reloading. ");
  82. addField( "debris", TYPEID< SimObjectRef<PhysicsDebrisData> >(), Offset( debris, PhysicsShapeData ),
  83. "@brief Name of a PhysicsDebrisData to spawn when this shape is destroyed (optional)." );
  84. addField( "explosion", TYPEID< SimObjectRef<ExplosionData> >(), Offset( explosion, PhysicsShapeData ),
  85. "@brief Name of an ExplosionData to spawn when this shape is destroyed (optional)." );
  86. addField( "destroyedShape", TYPEID< SimObjectRef<PhysicsShapeData> >(), Offset( destroyedShape, PhysicsShapeData ),
  87. "@brief Name of a PhysicsShapeData to spawn when this shape is destroyed (optional)." );
  88. endGroup("Media");
  89. addGroup( "Physics" );
  90. addField( "mass", TypeF32, Offset( mass, PhysicsShapeData ),
  91. "@brief Value representing the mass of the shape.\n\n"
  92. "A shape's mass influences the magnitude of any force exerted on it. "
  93. "For example, a PhysicsShape with a large mass requires a much larger force to move than "
  94. "the same shape with a smaller mass.\n"
  95. "@note A mass of zero will create a kinematic shape while anything greater will create a dynamic shape.");
  96. addField( "friction", TypeF32, Offset( dynamicFriction, PhysicsShapeData ),
  97. "@brief Coefficient of kinetic %friction to be applied to the shape.\n\n"
  98. "Kinetic %friction reduces the velocity of a moving object while it is in contact with a surface. "
  99. "A higher coefficient will result in a larger velocity reduction. "
  100. "A shape's friction should be lower than it's staticFriction, but larger than 0.\n\n"
  101. "@note This value is only applied while an object is in motion. For an object starting at rest, see PhysicsShape::staticFriction");
  102. addField( "staticFriction", TypeF32, Offset( staticFriction, PhysicsShapeData ),
  103. "@brief Coefficient of static %friction to be applied to the shape.\n\n"
  104. "Static %friction determines the force needed to start moving an at-rest object in contact with a surface. "
  105. "If the force applied onto shape cannot overcome the force of static %friction, the shape will remain at rest. "
  106. "A larger coefficient will require a larger force to start motion. "
  107. "This value should be larger than zero and the physicsShape's friction.\n\n"
  108. "@note This value is only applied while an object is at rest. For an object in motion, see PhysicsShape::friction");
  109. addField( "restitution", TypeF32, Offset( restitution, PhysicsShapeData ),
  110. "@brief Coeffecient of a bounce applied to the shape in response to a collision.\n\n"
  111. "Restitution is a ratio of a shape's velocity before and after a collision. "
  112. "A value of 0 will zero out a shape's post-collision velocity, making it stop on contact. "
  113. "Larger values will remove less velocity after a collision, making it \'bounce\' with a greater force. "
  114. "Normal %restitution values range between 0 and 1.0."
  115. "@note Values near or equaling 1.0 are likely to cause undesirable results in the physics simulation."
  116. " Because of this it is reccomended to avoid values close to 1.0");
  117. addField( "linearDamping", TypeF32, Offset( linearDamping, PhysicsShapeData ),
  118. "@brief Value that reduces an object's linear velocity over time.\n\n"
  119. "Larger values will cause velocity to decay quicker.\n\n" );
  120. addField( "angularDamping", TypeF32, Offset( angularDamping, PhysicsShapeData ),
  121. "@brief Value that reduces an object's rotational velocity over time.\n\n"
  122. "Larger values will cause velocity to decay quicker.\n\n" );
  123. addField( "linearSleepThreshold", TypeF32, Offset( linearSleepThreshold, PhysicsShapeData ),
  124. "@brief Minimum linear velocity before the shape can be put to sleep.\n\n"
  125. "This should be a positive value. Shapes put to sleep will not be simulated in order to save system resources.\n\n"
  126. "@note The shape must be dynamic.");
  127. addField( "angularSleepThreshold", TypeF32, Offset( angularSleepThreshold, PhysicsShapeData ),
  128. "@brief Minimum rotational velocity before the shape can be put to sleep.\n\n"
  129. "This should be a positive value. Shapes put to sleep will not be simulated in order to save system resources.\n\n"
  130. "@note The shape must be dynamic.");
  131. addField( "waterDampingScale", TypeF32, Offset( waterDampingScale, PhysicsShapeData ),
  132. "@brief Scale to apply to linear and angular dampening while underwater.\n\n "
  133. "Used with the waterViscosity of the "
  134. "@see angularDamping linearDamping" );
  135. addField( "buoyancyDensity", TypeF32, Offset( buoyancyDensity, PhysicsShapeData ),
  136. "@brief The density of the shape for calculating buoyant forces.\n\n"
  137. "The result of the calculated buoyancy is relative to the density of the WaterObject the PhysicsShape is within.\n\n"
  138. "@see WaterObject::density");
  139. endGroup( "Physics" );
  140. addGroup( "Networking" );
  141. addField( "simType", TYPEID< PhysicsShapeData::SimType >(), Offset( simType, PhysicsShapeData ),
  142. "@brief Controls whether this shape is simulated on the server, client, or both physics simulations.\n\n" );
  143. endGroup( "Networking" );
  144. }
  145. void PhysicsShapeData::packData( BitStream *stream )
  146. {
  147. Parent::packData( stream );
  148. stream->writeString( shapeName );
  149. stream->write( mass );
  150. stream->write( dynamicFriction );
  151. stream->write( staticFriction );
  152. stream->write( restitution );
  153. stream->write( linearDamping );
  154. stream->write( angularDamping );
  155. stream->write( linearSleepThreshold );
  156. stream->write( angularSleepThreshold );
  157. stream->write( waterDampingScale );
  158. stream->write( buoyancyDensity );
  159. stream->writeInt( simType, SimType_Bits );
  160. stream->writeRangedU32( debris ? debris->getId() : 0, 0, DataBlockObjectIdLast );
  161. stream->writeRangedU32( explosion ? explosion->getId() : 0, 0, DataBlockObjectIdLast );
  162. stream->writeRangedU32( destroyedShape ? destroyedShape->getId() : 0, 0, DataBlockObjectIdLast );
  163. }
  164. void PhysicsShapeData::unpackData( BitStream *stream )
  165. {
  166. Parent::unpackData(stream);
  167. shapeName = stream->readSTString();
  168. stream->read( &mass );
  169. stream->read( &dynamicFriction );
  170. stream->read( &staticFriction );
  171. stream->read( &restitution );
  172. stream->read( &linearDamping );
  173. stream->read( &angularDamping );
  174. stream->read( &linearSleepThreshold );
  175. stream->read( &angularSleepThreshold );
  176. stream->read( &waterDampingScale );
  177. stream->read( &buoyancyDensity );
  178. simType = (SimType)stream->readInt( SimType_Bits );
  179. debris = stream->readRangedU32( 0, DataBlockObjectIdLast );
  180. explosion = stream->readRangedU32( 0, DataBlockObjectIdLast );
  181. destroyedShape = stream->readRangedU32( 0, DataBlockObjectIdLast );
  182. }
  183. bool PhysicsShapeData::onAdd()
  184. {
  185. if ( !Parent::onAdd() )
  186. return false;
  187. ResourceManager::get().getChangedSignal().notify( this, &PhysicsShapeData::_onResourceChanged );
  188. return true;
  189. }
  190. void PhysicsShapeData::onRemove()
  191. {
  192. ResourceManager::get().getChangedSignal().remove( this, &PhysicsShapeData::_onResourceChanged );
  193. Parent::onRemove();
  194. }
  195. void PhysicsShapeData::_onResourceChanged( const Torque::Path &path )
  196. {
  197. if ( path != Path( shapeName ) )
  198. return;
  199. // Reload the changed shape.
  200. Resource<TSShape> reloadShape;
  201. PhysicsCollisionRef reloadcolShape;
  202. reloadShape = ResourceManager::get().load( shapeName );
  203. if ( !bool(reloadShape) )
  204. {
  205. Con::warnf( ConsoleLogEntry::General, "PhysicsShapeData::_onResourceChanged: Could not reload %s.", path.getFileName().c_str() );
  206. return;
  207. }
  208. // Reload the collision shape.
  209. reloadcolShape = shape->buildColShape( false, Point3F::One );
  210. if ( bool(reloadShape) && bool(colShape))
  211. {
  212. shape = reloadShape;
  213. colShape = reloadcolShape;
  214. }
  215. mReloadSignal.trigger();
  216. }
  217. bool PhysicsShapeData::preload( bool server, String &errorBuffer )
  218. {
  219. if ( !Parent::preload( server, errorBuffer ) )
  220. return false;
  221. // If we don't have a physics plugin active then
  222. // we have to fail completely.
  223. if ( !PHYSICSMGR )
  224. {
  225. errorBuffer = "PhysicsShapeData::preload - No physics plugin is active!";
  226. return false;
  227. }
  228. if ( !shapeName || !shapeName[0] )
  229. {
  230. errorBuffer = "PhysicsShapeData::preload - No shape name defined.";
  231. return false;
  232. }
  233. // Load the shape.
  234. shape = ResourceManager::get().load( shapeName );
  235. if ( bool(shape) == false )
  236. {
  237. errorBuffer = String::ToString( "PhysicsShapeData::preload - Unable to load shape '%s'.", shapeName );
  238. return false;
  239. }
  240. // Prepare the shared physics collision shape.
  241. if ( !colShape )
  242. {
  243. colShape = shape->buildColShape( false, Point3F::One );
  244. // If we got here and didn't get a collision shape then
  245. // we need to fail... can't have a shape without collision.
  246. if ( !colShape )
  247. {
  248. errorBuffer = String::ToString( "PhysicsShapeData::preload - No collision found for shape '%s'.", shapeName );
  249. return false;
  250. }
  251. }
  252. // My convex demcomposion test
  253. /*
  254. // Get the verts and triangles for the first visible detail.
  255. ConcretePolyList polyList;
  256. polyList.setTransform( &MatrixF::Identity, Point3F::One );
  257. TSShapeInstance shapeInst( shape, false );
  258. shapeInst.animate(0);
  259. if ( !shapeInst.buildPolyList( &polyList, 0 ) )
  260. return false;
  261. // Gah... Ratcliff's lib works on doubles... why, oh why?
  262. Vector<F64> doubleVerts;
  263. doubleVerts.setSize( polyList.mVertexList.size() * 3 );
  264. for ( U32 i=0; i < polyList.mVertexList.size(); i++ )
  265. {
  266. doubleVerts[ ( i * 3 ) + 0 ] = (F64)polyList.mVertexList[i].x;
  267. doubleVerts[ ( i * 3 ) + 1 ] = (F64)polyList.mVertexList[i].y;
  268. doubleVerts[ ( i * 3 ) + 2 ] = (F64)polyList.mVertexList[i].z;
  269. }
  270. using namespace ConvexDecomposition;
  271. class ConvexBuilder : public ConvexDecompInterface
  272. {
  273. public:
  274. ConvexBuilder() { }
  275. ~ConvexBuilder()
  276. {
  277. for ( U32 i=0; i < mHulls.size(); i++ )
  278. delete mHulls[i];
  279. }
  280. virtual void ConvexDecompResult( ConvexResult &result )
  281. {
  282. FConvexResult *hull = new FConvexResult( result );
  283. mHulls.push_back( hull );
  284. }
  285. Vector<FConvexResult*> mHulls;
  286. };
  287. DecompDesc d;
  288. d.mVcount = polyList.mVertexList.size();
  289. d.mVertices = doubleVerts.address();
  290. d.mTcount = polyList.mIndexList.size() / 3;
  291. d.mIndices = polyList.mIndexList.address();
  292. d.mDepth = 3;
  293. d.mCpercent = 20.0f;
  294. d.mPpercent = 30.0f;
  295. d.mMaxVertices = 32;
  296. d.mSkinWidth = 0.05f; // Need to expose this!
  297. ConvexBuilder builder;
  298. d.mCallback = &builder;
  299. if ( performConvexDecomposition( d ) < 1 || builder.mHulls.empty() )
  300. return false;
  301. // Add all the convex hull results into the collision shape.
  302. colShape = PHYSICSMGR->createCollision();
  303. for ( U32 i=0; i < builder.mHulls.size(); i++ )
  304. colShape->addConvex( (const Point3F*)builder.mHulls[i]->mHullVertices,
  305. builder.mHulls[i]->mHullVcount,
  306. MatrixF::Identity );
  307. */
  308. return true;
  309. }
  310. IMPLEMENT_CO_NETOBJECT_V1(PhysicsShape);
  311. ConsoleDocClass( PhysicsShape,
  312. "@brief Represents a destructible physical object simulated through the plugin system.\n\n"
  313. "@see PhysicsShapeData.\n"
  314. "@ingroup Physics"
  315. );
  316. PhysicsShape::PhysicsShape()
  317. : mPhysicsRep( NULL ),
  318. mWorld( NULL ),
  319. mShapeInst( NULL ),
  320. mResetPos( MatrixF::Identity ),
  321. mDestroyed( false ),
  322. mPlayAmbient( false ),
  323. mAmbientThread( NULL ),
  324. mAmbientSeq( -1 )
  325. {
  326. mNetFlags.set( Ghostable | ScopeAlways );
  327. mTypeMask |= DynamicShapeObjectType;
  328. }
  329. PhysicsShape::~PhysicsShape()
  330. {
  331. }
  332. void PhysicsShape::consoleInit()
  333. {
  334. Con::addVariable( "$PhysicsShape::noCorrections", TypeBool, &PhysicsShape::smNoCorrections,
  335. "@brief Determines if the shape will recieve corrections from the server or "
  336. "will instead be allowed to diverge.\n\n"
  337. "In the event that the client and server object positions/orientations "
  338. "differ and if this variable is true, the server will attempt to \'correct\' "
  339. "the client object to keep it in sync. Otherwise, client and server objects may fall out of sync.\n\n");
  340. Con::addVariable( "$PhysicsShape::noSmoothing", TypeBool, &PhysicsShape::smNoSmoothing,
  341. "@brief Determines if client-side shapes will attempt to smoothly transition to "
  342. "their new position after reciving a correction.\n\n"
  343. "If true, shapes will immediately render at the position they are corrected to.\n\n");
  344. Parent::consoleInit();
  345. }
  346. void PhysicsShape::initPersistFields()
  347. {
  348. addGroup( "PhysicsShape" );
  349. addField( "playAmbient", TypeBool, Offset( mPlayAmbient, PhysicsShape ),
  350. "@brief Enables or disables playing of an ambient animation upon loading the shape.\n\n"
  351. "@note The ambient animation must be named \"ambient\"." );
  352. endGroup( "PhysicsShape" );
  353. Parent::initPersistFields();
  354. removeField( "scale" );
  355. }
  356. void PhysicsShape::inspectPostApply()
  357. {
  358. Parent::inspectPostApply();
  359. setMaskBits( InitialUpdateMask );
  360. }
  361. U32 PhysicsShape::packUpdate( NetConnection *con, U32 mask, BitStream *stream )
  362. {
  363. U32 retMask = Parent::packUpdate( con, mask, stream );
  364. if ( stream->writeFlag( mask & InitialUpdateMask ) )
  365. {
  366. stream->writeAffineTransform( getTransform() );
  367. stream->writeFlag( mPlayAmbient );
  368. stream->writeFlag( mDestroyed );
  369. return retMask;
  370. }
  371. // If we got here its not an initial update. So only send
  372. // the least amount of data possible.
  373. if ( stream->writeFlag( mask & StateMask ) )
  374. {
  375. // This will encode the position relative to the control
  376. // object position.
  377. //
  378. // This will compress the position to as little as 6.25
  379. // bytes if the position is within about 30 meters of the
  380. // control object.
  381. //
  382. // Worst case its a full 12 bytes + 2 bits if the position
  383. // is more than 500 meters from the control object.
  384. //
  385. stream->writeCompressedPoint( mState.position );
  386. // Use only 3.5 bytes to send the orientation.
  387. stream->writeQuat( mState.orientation, 9 );
  388. // If the server object has been set to sleep then
  389. // we don't need to send any velocity.
  390. if ( !stream->writeFlag( mState.sleeping ) )
  391. {
  392. // This gives me ~0.015f resolution in velocity magnitude
  393. // while only costing me 1 bit of the velocity is zero length,
  394. // <5 bytes in normal cases, and <8 bytes if the velocity is
  395. // greater than 1000.
  396. AssertWarn( mState.linVelocity.len() < 1000.0f,
  397. "PhysicsShape::packUpdate - The linVelocity is out of range!" );
  398. stream->writeVector( mState.linVelocity, 1000.0f, 16, 9 );
  399. // For angular velocity we get < 0.01f resolution in magnitude
  400. // with the most common case being under 4 bytes.
  401. AssertWarn( mState.angVelocity.len() < 10.0f,
  402. "PhysicsShape::packUpdate - The angVelocity is out of range!" );
  403. stream->writeVector( mState.angVelocity, 10.0f, 10, 9 );
  404. }
  405. }
  406. if ( stream->writeFlag( mask & DamageMask ) )
  407. stream->writeFlag( mDestroyed );
  408. return retMask;
  409. }
  410. void PhysicsShape::unpackUpdate( NetConnection *con, BitStream *stream )
  411. {
  412. Parent::unpackUpdate( con, stream );
  413. if ( stream->readFlag() ) // InitialUpdateMask
  414. {
  415. MatrixF mat;
  416. stream->readAffineTransform( &mat );
  417. setTransform( mat );
  418. mPlayAmbient = stream->readFlag();
  419. if ( isProperlyAdded() )
  420. _initAmbient();
  421. if ( stream->readFlag() )
  422. {
  423. if ( isProperlyAdded() )
  424. {
  425. // Destroy immediately if we've already been added
  426. // to the scene.
  427. destroy();
  428. }
  429. else
  430. {
  431. // Indicate the shape should be destroyed when the
  432. // shape is added.
  433. mDestroyed = true;
  434. }
  435. }
  436. return;
  437. }
  438. if ( stream->readFlag() ) // StateMask
  439. {
  440. PhysicsState state;
  441. // Read the encoded and compressed position... commonly only 6.25 bytes.
  442. stream->readCompressedPoint( &state.position );
  443. // Read the compressed quaternion... 3.5 bytes.
  444. stream->readQuat( &state.orientation, 9 );
  445. state.sleeping = stream->readFlag();
  446. if ( !state.sleeping )
  447. {
  448. stream->readVector( &state.linVelocity, 1000.0f, 16, 9 );
  449. stream->readVector( &state.angVelocity, 10.0f, 10, 9 );
  450. }
  451. if ( !smNoCorrections && mPhysicsRep && mPhysicsRep->isDynamic() && !mDestroyed )
  452. {
  453. // Set the new state on the physics object immediately.
  454. mPhysicsRep->applyCorrection( state.getTransform() );
  455. mPhysicsRep->setSleeping( state.sleeping );
  456. if ( !state.sleeping )
  457. {
  458. mPhysicsRep->setLinVelocity( state.linVelocity );
  459. mPhysicsRep->setAngVelocity( state.angVelocity );
  460. }
  461. mPhysicsRep->getState( &mState );
  462. }
  463. // If there is no physics object then just set the
  464. // new state... the tick will take care of the
  465. // interpolation and extrapolation.
  466. if ( !mPhysicsRep || !mPhysicsRep->isDynamic() )
  467. mState = state;
  468. }
  469. if ( stream->readFlag() ) // DamageMask
  470. {
  471. if ( stream->readFlag() )
  472. destroy();
  473. else
  474. restore();
  475. }
  476. }
  477. bool PhysicsShape::onAdd()
  478. {
  479. if ( !Parent::onAdd() )
  480. return false;
  481. // If we don't have a physics plugin active then
  482. // we have to fail completely.
  483. if ( !PHYSICSMGR )
  484. {
  485. Con::errorf( "PhysicsShape::onAdd - No physics plugin is active!" );
  486. return false;
  487. }
  488. //
  489. if ( !mPhysicsRep && !_createShape() )
  490. {
  491. Con::errorf( "PhysicsShape::onAdd() - Shape creation failed!" );
  492. return false;
  493. }
  494. // The reset position is the transform on the server
  495. // at creation time... its not used on the client.
  496. if ( isServerObject() )
  497. {
  498. storeRestorePos();
  499. PhysicsPlugin::getPhysicsResetSignal().notify( this, &PhysicsShape::_onPhysicsReset );
  500. }
  501. // Register for the resource change signal.
  502. //ResourceManager::get().getChangedSignal().notify( this, &PhysicsShape::_onResourceChanged );
  503. // Only add server objects and non-destroyed client objects to the scene.
  504. if ( isServerObject() || !mDestroyed)
  505. addToScene();
  506. if ( isClientObject() && mDestroyed )
  507. {
  508. // Disable all simulation of the body... no collision or dynamics.
  509. if ( mPhysicsRep )
  510. mPhysicsRep->setSimulationEnabled( false );
  511. // Stop doing tick processing for this SceneObject.
  512. setProcessTick( false );
  513. }
  514. return true;
  515. }
  516. void PhysicsShape::onRemove()
  517. {
  518. removeFromScene();
  519. SAFE_DELETE( mPhysicsRep );
  520. SAFE_DELETE( mShapeInst );
  521. mAmbientThread = NULL;
  522. mAmbientSeq = -1;
  523. mWorld = NULL;
  524. if ( isServerObject() )
  525. {
  526. PhysicsPlugin::getPhysicsResetSignal().remove( this, &PhysicsShape::_onPhysicsReset );
  527. if ( mDestroyedShape )
  528. mDestroyedShape->deleteObject();
  529. }
  530. // Remove the resource change signal.
  531. //ResourceManager::get().getChangedSignal().remove( this, &PhysicsShape::_onResourceChanged );
  532. Parent::onRemove();
  533. }
  534. bool PhysicsShape::onNewDataBlock( GameBaseData *dptr, bool reload )
  535. {
  536. if ( !Parent::onNewDataBlock( dptr, reload ) )
  537. return false;
  538. if ( !isProperlyAdded() )
  539. return true;
  540. // If we don't have a physics plugin active then
  541. // we have to fail completely.
  542. if ( !PHYSICSMGR )
  543. {
  544. Con::errorf( "PhysicsShape::onNewDataBlock - No physics plugin is active!" );
  545. return false;
  546. }
  547. //
  548. if ( !_createShape() )
  549. {
  550. Con::errorf( "PhysicsShape::onNewDataBlock() - Shape creation failed!" );
  551. return false;
  552. }
  553. return true;
  554. }
  555. bool PhysicsShape::_createShape()
  556. {
  557. SAFE_DELETE( mPhysicsRep );
  558. SAFE_DELETE( mShapeInst );
  559. mAmbientThread = NULL;
  560. mWorld = NULL;
  561. mAmbientSeq = -1;
  562. PhysicsShapeData *db = getDataBlock();
  563. if ( !db )
  564. return false;
  565. // Set the world box.
  566. mObjBox = db->shape->bounds;
  567. resetWorldBox();
  568. // If this is the server and its a client only simulation
  569. // object then disable our tick... the server doesn't do
  570. // any work for this shape.
  571. if ( isServerObject() &&
  572. db->simType == PhysicsShapeData::SimType_ClientOnly )
  573. {
  574. setProcessTick( false );
  575. return true;
  576. }
  577. // Create the shape instance.
  578. mShapeInst = new TSShapeInstance( db->shape, isClientObject() );
  579. if ( isClientObject() )
  580. {
  581. mAmbientSeq = db->shape->findSequence( "ambient" );
  582. _initAmbient();
  583. }
  584. // If the shape has a mass then its dynamic... else
  585. // its a kinematic shape.
  586. //
  587. // While a kinematic is less optimal than a static body
  588. // it allows for us to enable/disable collision and having
  589. // all dynamic actors react correctly... waking up.
  590. //
  591. const bool isDynamic = db->mass > 0.0f;
  592. // If we aren't dynamic we don't need to tick.
  593. setProcessTick( isDynamic || mPlayAmbient );
  594. // If this is the client and we're a server only object then
  595. // we don't need any physics representation... we're done.
  596. if ( isClientObject() &&
  597. db->simType == PhysicsShapeData::SimType_ServerOnly )
  598. return true;
  599. mWorld = PHYSICSMGR->getWorld( isServerObject() ? "server" : "client" );
  600. mPhysicsRep = PHYSICSMGR->createBody();
  601. mPhysicsRep->init( db->colShape,
  602. db->mass,
  603. isDynamic ? 0 : PhysicsBody::BF_KINEMATIC,
  604. this,
  605. mWorld );
  606. mPhysicsRep->setMaterial( db->restitution, db->dynamicFriction, db->staticFriction );
  607. if ( isDynamic )
  608. {
  609. mPhysicsRep->setDamping( db->linearDamping, db->angularDamping );
  610. mPhysicsRep->setSleepThreshold( db->linearSleepThreshold, db->angularSleepThreshold );
  611. }
  612. mPhysicsRep->setTransform( getTransform() );
  613. return true;
  614. }
  615. void PhysicsShape::_initAmbient()
  616. {
  617. if ( isServerObject() )
  618. return;
  619. bool willPlay = mPlayAmbient && mAmbientSeq != -1;
  620. if ( willPlay )
  621. {
  622. // Create thread if we dont already have.
  623. if ( mAmbientThread == NULL )
  624. mAmbientThread = mShapeInst->addThread();
  625. // Play the sequence.
  626. mShapeInst->setSequence( mAmbientThread, mAmbientSeq, 0);
  627. setProcessTick(true);
  628. }
  629. else
  630. {
  631. if ( mAmbientThread != NULL )
  632. {
  633. mShapeInst->destroyThread( mAmbientThread );
  634. mAmbientThread = NULL;
  635. }
  636. }
  637. }
  638. void PhysicsShape::_onPhysicsReset( PhysicsResetEvent reset )
  639. {
  640. if ( reset == PhysicsResetEvent_Store )
  641. mResetPos = getTransform();
  642. else if ( reset == PhysicsResetEvent_Restore )
  643. {
  644. setTransform( mResetPos );
  645. // Restore to un-destroyed state.
  646. restore();
  647. // Cheat and reset the client from here.
  648. if ( getClientObject() )
  649. {
  650. PhysicsShape *clientObj = (PhysicsShape*)getClientObject();
  651. clientObj->setTransform( mResetPos );
  652. clientObj->restore();
  653. }
  654. }
  655. }
  656. void PhysicsShape::setTransform( const MatrixF &newMat )
  657. {
  658. Parent::setTransform( newMat );
  659. // This is only called to set an absolute position
  660. // so we discard the delta state.
  661. mState.position = getPosition();
  662. mState.orientation.set( newMat );
  663. mRenderState[0] = mRenderState[1] = mState;
  664. setMaskBits( StateMask );
  665. if ( mPhysicsRep )
  666. mPhysicsRep->setTransform( newMat );
  667. }
  668. void PhysicsShape::setScale( const VectorF &scale )
  669. {
  670. // Cannot scale PhysicsShape.
  671. return;
  672. }
  673. void PhysicsShape::storeRestorePos()
  674. {
  675. mResetPos = getTransform();
  676. }
  677. F32 PhysicsShape::getMass() const
  678. {
  679. const PhysicsShapeData *db = const_cast<PhysicsShape*>( this )->getDataBlock();
  680. return db->mass;
  681. }
  682. void PhysicsShape::applyImpulse( const Point3F &pos, const VectorF &vec )
  683. {
  684. if ( mPhysicsRep && mPhysicsRep->isDynamic() )
  685. mPhysicsRep->applyImpulse( pos, vec );
  686. }
  687. void PhysicsShape::applyRadialImpulse( const Point3F &origin, F32 radius, F32 magnitude )
  688. {
  689. if ( !mPhysicsRep || !mPhysicsRep->isDynamic() )
  690. return;
  691. // TODO: Find a better approximation of the
  692. // force vector using the object box.
  693. VectorF force = getWorldBox().getCenter() - origin;
  694. F32 dist = force.magnitudeSafe();
  695. force.normalize();
  696. if ( dist == 0.0f )
  697. force *= magnitude;
  698. else
  699. force *= mClampF( radius / dist, 0.0f, 1.0f ) * magnitude;
  700. mPhysicsRep->applyImpulse( origin, force );
  701. // TODO: There is no simple way to really sync this sort of an
  702. // event with the client.
  703. //
  704. // The best is to send the current physics snapshot, calculate the
  705. // time difference from when this event occured and the time when the
  706. // client recieves it, and then extrapolate where it should be.
  707. //
  708. // Even then its impossible to be absolutely sure its synced.
  709. //
  710. // Bottom line... you shouldn't use physics over the network like this.
  711. //
  712. // Cheat for single player.
  713. //if ( getClientObject() )
  714. //((PhysicsShape*)getClientObject())->mPhysicsRep->applyImpulse( origin, force );
  715. }
  716. void PhysicsShape::interpolateTick( F32 delta )
  717. {
  718. AssertFatal( !mDestroyed, "PhysicsShape::interpolateTick - Shouldn't be processing a destroyed shape!" );
  719. if ( !mPhysicsRep->isDynamic() )
  720. return;
  721. // Interpolate the position and rotation based on the delta.
  722. PhysicsState state;
  723. state.interpolate( mRenderState[1], mRenderState[0], delta );
  724. // Set the transform to the interpolated transform.
  725. setRenderTransform( state.getTransform() );
  726. }
  727. void PhysicsShape::processTick( const Move *move )
  728. {
  729. AssertFatal( mPhysicsRep && !mDestroyed, "PhysicsShape::processTick - Shouldn't be processing a destroyed shape!" );
  730. // Note that unlike TSStatic, the serverside PhysicsShape does not
  731. // need to play the ambient animation because even if the animation were
  732. // to move collision shapes it would not affect the physx representation.
  733. if ( !mPhysicsRep->isDynamic() )
  734. return;
  735. // SINGLE PLAYER HACK!!!!
  736. if ( PHYSICSMGR->isSinglePlayer() && isClientObject() && getServerObject() )
  737. {
  738. PhysicsShape *servObj = (PhysicsShape*)getServerObject();
  739. setTransform( servObj->mState.getTransform() );
  740. mRenderState[0] = servObj->mRenderState[0];
  741. mRenderState[1] = servObj->mRenderState[1];
  742. return;
  743. }
  744. // Store the last render state.
  745. mRenderState[0] = mRenderState[1];
  746. // If the last render state doesn't match the last simulation
  747. // state then we got a correction and need to
  748. Point3F errorDelta = mRenderState[1].position - mState.position;
  749. const bool doSmoothing = !errorDelta.isZero() && !smNoSmoothing;
  750. const bool wasSleeping = mState.sleeping;
  751. // Get the new physics state.
  752. if ( mPhysicsRep )
  753. {
  754. mPhysicsRep->getState( &mState );
  755. _updateContainerForces();
  756. }
  757. else
  758. {
  759. // This is where we could extrapolate.
  760. }
  761. // Smooth the correction back into the render state.
  762. mRenderState[1] = mState;
  763. if ( doSmoothing )
  764. {
  765. F32 correction = mClampF( errorDelta.len() / 20.0f, 0.1f, 0.9f );
  766. mRenderState[1].position.interpolate( mState.position, mRenderState[0].position, correction );
  767. mRenderState[1].orientation.interpolate( mState.orientation, mRenderState[0].orientation, correction );
  768. }
  769. // If we haven't been sleeping then update our transform
  770. // and set ourselves as dirty for the next client update.
  771. if ( !wasSleeping || !mState.sleeping )
  772. {
  773. // Set the transform on the parent so that
  774. // the physics object isn't moved.
  775. Parent::setTransform( mState.getTransform() );
  776. // If we're doing server simulation then we need
  777. // to send the client a state update.
  778. if ( isServerObject() && mPhysicsRep && !smNoCorrections &&
  779. !PHYSICSMGR->isSinglePlayer() // SINGLE PLAYER HACK!!!!
  780. )
  781. setMaskBits( StateMask );
  782. }
  783. }
  784. void PhysicsShape::advanceTime( F32 timeDelta )
  785. {
  786. if ( isClientObject() && mPlayAmbient && mAmbientThread != NULL )
  787. mShapeInst->advanceTime( timeDelta, mAmbientThread );
  788. }
  789. void PhysicsShape::_updateContainerForces()
  790. {
  791. PROFILE_SCOPE( PhysicsShape_updateContainerForces );
  792. // If we're not simulating don't update forces.
  793. if ( !mWorld->isEnabled() )
  794. return;
  795. ContainerQueryInfo info;
  796. info.box = getWorldBox();
  797. info.mass = getDataBlock()->mass;
  798. // Find and retreive physics info from intersecting WaterObject(s)
  799. getContainer()->findObjects( getWorldBox(), WaterObjectType|PhysicalZoneObjectType, findRouter, &info );
  800. // Calculate buoyancy and drag
  801. F32 angDrag = getDataBlock()->angularDamping;
  802. F32 linDrag = getDataBlock()->linearDamping;
  803. F32 buoyancy = 0.0f;
  804. Point3F cmass = mPhysicsRep->getCMassPosition();
  805. F32 density = getDataBlock()->buoyancyDensity;
  806. if ( density > 0.0f )
  807. {
  808. if ( info.waterCoverage > 0.0f )
  809. {
  810. F32 waterDragScale = info.waterViscosity * getDataBlock()->waterDampingScale;
  811. F32 powCoverage = mPow( info.waterCoverage, 0.25f );
  812. angDrag = mLerp( angDrag, angDrag * waterDragScale, powCoverage );
  813. linDrag = mLerp( linDrag, linDrag * waterDragScale, powCoverage );
  814. }
  815. buoyancy = ( info.waterDensity / density ) * mPow( info.waterCoverage, 2.0f );
  816. // A little hackery to prevent oscillation
  817. // Based on this blog post:
  818. // (http://reinot.blogspot.com/2005/11/oh-yes-they-float-georgie-they-all.html)
  819. // JCF: disabled!
  820. Point3F buoyancyForce = buoyancy * -mWorld->getGravity() * TickSec * getDataBlock()->mass;
  821. mPhysicsRep->applyImpulse( cmass, buoyancyForce );
  822. }
  823. // Update the dampening as the container might have changed.
  824. mPhysicsRep->setDamping( linDrag, angDrag );
  825. // Apply physical zone forces.
  826. if ( !info.appliedForce.isZero() )
  827. mPhysicsRep->applyImpulse( cmass, info.appliedForce );
  828. }
  829. void PhysicsShape::prepRenderImage( SceneRenderState *state )
  830. {
  831. AssertFatal( !mDestroyed, "PhysicsShape::prepRenderImage - Shouldn't be processing a destroyed shape!" );
  832. PROFILE_SCOPE( PhysicsShape_prepRenderImage );
  833. if( !mShapeInst )
  834. return;
  835. Point3F cameraOffset;
  836. getRenderTransform().getColumn(3,&cameraOffset);
  837. cameraOffset -= state->getDiffuseCameraPosition();
  838. F32 dist = cameraOffset.len();
  839. if (dist < 0.01f)
  840. dist = 0.01f;
  841. F32 invScale = (1.0f/getMax(getMax(mObjScale.x,mObjScale.y),mObjScale.z));
  842. if ( mShapeInst->setDetailFromDistance( state, dist * invScale ) < 0 )
  843. return;
  844. GFXTransformSaver saver;
  845. // Set up our TS render state.
  846. TSRenderState rdata;
  847. rdata.setSceneState( state );
  848. rdata.setFadeOverride( 1.0f );
  849. // We might have some forward lit materials
  850. // so pass down a query to gather lights.
  851. LightQuery query;
  852. query.init( getWorldSphere() );
  853. rdata.setLightQuery( &query );
  854. MatrixF mat = getRenderTransform();
  855. mat.scale( mObjScale );
  856. GFX->setWorldMatrix( mat );
  857. mShapeInst->animate();
  858. mShapeInst->render( rdata );
  859. }
  860. void PhysicsShape::destroy()
  861. {
  862. if ( mDestroyed )
  863. return;
  864. mDestroyed = true;
  865. setMaskBits( DamageMask );
  866. const Point3F lastLinVel = mPhysicsRep->isDynamic() ? mPhysicsRep->getLinVelocity() : Point3F::Zero;
  867. // Disable all simulation of the body... no collision or dynamics.
  868. mPhysicsRep->setSimulationEnabled( false );
  869. // On the client side we remove it from the scene graph
  870. // to disable rendering and volume queries.
  871. if ( isClientObject() )
  872. removeFromScene();
  873. // Stop doing tick processing for this SceneObject.
  874. setProcessTick( false );
  875. PhysicsShapeData *db = getDataBlock();
  876. if ( !db )
  877. return;
  878. const MatrixF &mat = getTransform();
  879. if ( isServerObject() )
  880. {
  881. // We only create the destroyed object on the server
  882. // and let ghosting deal with updating the client.
  883. if ( db->destroyedShape )
  884. {
  885. mDestroyedShape = new PhysicsShape();
  886. mDestroyedShape->setDataBlock( db->destroyedShape );
  887. mDestroyedShape->setTransform( mat );
  888. if ( !mDestroyedShape->registerObject() )
  889. delete mDestroyedShape.getObject();
  890. }
  891. return;
  892. }
  893. // Let the physics debris create itself.
  894. PhysicsDebris::create( db->debris, mat, lastLinVel );
  895. if ( db->explosion )
  896. {
  897. Explosion *splod = new Explosion();
  898. splod->setDataBlock( db->explosion );
  899. splod->setTransform( mat );
  900. splod->setInitialState( getPosition(), mat.getUpVector(), 1.0f );
  901. if ( !splod->registerObject() )
  902. delete splod;
  903. }
  904. }
  905. void PhysicsShape::restore()
  906. {
  907. if ( !mDestroyed )
  908. return;
  909. PhysicsShapeData *db = getDataBlock();
  910. const bool isDynamic = db && db->mass > 0.0f;
  911. if ( mDestroyedShape )
  912. mDestroyedShape->deleteObject();
  913. // Restore tick processing, add it back to
  914. // the scene, and enable collision and simulation.
  915. setProcessTick( isDynamic || mPlayAmbient );
  916. if ( isClientObject() )
  917. addToScene();
  918. mPhysicsRep->setSimulationEnabled( true );
  919. mDestroyed = false;
  920. setMaskBits( DamageMask );
  921. }
  922. DefineEngineMethod( PhysicsShape, isDestroyed, bool, (),,
  923. "@brief Returns if a PhysicsShape has been destroyed or not.\n\n" )
  924. {
  925. return object->isDestroyed();
  926. }
  927. DefineEngineMethod( PhysicsShape, destroy, void, (),,
  928. "@brief Disables rendering and physical simulation.\n\n"
  929. "Calling destroy() will also spawn any explosions, debris, and/or destroyedShape "
  930. "defined for it, as well as remove it from the scene graph.\n\n"
  931. "Destroyed objects are only created on the server. Ghosting will later update the client.\n\n"
  932. "@note This does not actually delete the PhysicsShape." )
  933. {
  934. object->destroy();
  935. }
  936. DefineEngineMethod( PhysicsShape, restore, void, (),,
  937. "@brief Restores the shape to its state before being destroyed.\n\n"
  938. "Re-enables rendering and physical simulation on the object and "
  939. "adds it to the client's scene graph. "
  940. "Has no effect if the shape is not destroyed.\n\n")
  941. {
  942. object->restore();
  943. }