rigidBodyComponent.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  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 "T3D/components/physics/rigidBodycomponent.h"
  23. #include "core/util/safeDelete.h"
  24. #include "console/consoleTypes.h"
  25. #include "console/consoleObject.h"
  26. #include "core/stream/bitStream.h"
  27. #include "console/engineAPI.h"
  28. #include "sim/netConnection.h"
  29. #include "T3D/physics/physicsBody.h"
  30. #include "T3D/physics/physicsPlugin.h"
  31. #include "T3D/physics/physicsWorld.h"
  32. #include "T3D/physics/physicsCollision.h"
  33. #include "T3D/components/collision/collisioncomponent.h"
  34. bool RigidBodyComponent::smNoCorrections = false;
  35. bool RigidBodyComponent::smNoSmoothing = false;
  36. //////////////////////////////////////////////////////////////////////////
  37. // Constructor/Destructor
  38. //////////////////////////////////////////////////////////////////////////
  39. RigidBodyComponent::RigidBodyComponent() : Component()
  40. {
  41. mMass = 20;
  42. mDynamicFriction = 1;
  43. mStaticFriction = 0.1f;
  44. mRestitution = 10;
  45. mLinearDamping = 0;
  46. mAngularDamping = 0;
  47. mLinearSleepThreshold = 1;
  48. mAngularSleepThreshold = 1;
  49. mWaterDampingScale = 0.1f;
  50. mBuoyancyDensity = 1;
  51. mSimType = SimType_ServerOnly;
  52. mPhysicsRep = NULL;
  53. mResetPos = MatrixF::Identity;
  54. mOwnerColComponent = NULL;
  55. mFriendlyName = "RigidBody(Component)";
  56. }
  57. RigidBodyComponent::~RigidBodyComponent()
  58. {
  59. }
  60. IMPLEMENT_CO_NETOBJECT_V1(RigidBodyComponent);
  61. bool RigidBodyComponent::onAdd()
  62. {
  63. if(! Parent::onAdd())
  64. return false;
  65. return true;
  66. }
  67. void RigidBodyComponent::onRemove()
  68. {
  69. Parent::onRemove();
  70. }
  71. void RigidBodyComponent::initPersistFields()
  72. {
  73. Parent::initPersistFields();
  74. }
  75. //This is mostly a catch for situations where the behavior is re-added to the object and the like and we may need to force an update to the behavior
  76. void RigidBodyComponent::onComponentAdd()
  77. {
  78. Parent::onComponentAdd();
  79. if (isServerObject())
  80. {
  81. storeRestorePos();
  82. PhysicsPlugin::getPhysicsResetSignal().notify(this, &RigidBodyComponent::_onPhysicsReset);
  83. }
  84. CollisionComponent *colComp = mOwner->getComponent<CollisionComponent>();
  85. if (colComp)
  86. {
  87. colComp->onCollisionChanged.notify(this, &RigidBodyComponent::updatePhysics);
  88. updatePhysics(colComp->getCollisionData());
  89. }
  90. else
  91. updatePhysics();
  92. }
  93. void RigidBodyComponent::onComponentRemove()
  94. {
  95. Parent::onComponentRemove();
  96. if (isServerObject())
  97. {
  98. PhysicsPlugin::getPhysicsResetSignal().remove(this, &RigidBodyComponent::_onPhysicsReset);
  99. }
  100. CollisionComponent *colComp = mOwner->getComponent<CollisionComponent>();
  101. if (colComp)
  102. {
  103. colComp->onCollisionChanged.remove(this, &RigidBodyComponent::updatePhysics);
  104. }
  105. SAFE_DELETE(mPhysicsRep);
  106. }
  107. void RigidBodyComponent::componentAddedToOwner(Component *comp)
  108. {
  109. CollisionComponent *colComp = dynamic_cast<CollisionComponent*>(comp);
  110. if (colComp)
  111. {
  112. colComp->onCollisionChanged.notify(this, &RigidBodyComponent::updatePhysics);
  113. updatePhysics(colComp->getCollisionData());
  114. }
  115. }
  116. void RigidBodyComponent::componentRemovedFromOwner(Component *comp)
  117. {
  118. //test if this is a shape component!
  119. CollisionComponent *colComp = dynamic_cast<CollisionComponent*>(comp);
  120. if (colComp)
  121. {
  122. colComp->onCollisionChanged.remove(this, &RigidBodyComponent::updatePhysics);
  123. updatePhysics();
  124. }
  125. }
  126. void RigidBodyComponent::ownerTransformSet(MatrixF *mat)
  127. {
  128. if (mPhysicsRep)
  129. mPhysicsRep->setTransform(mOwner->getTransform());
  130. }
  131. void RigidBodyComponent::updatePhysics(PhysicsCollision* collision)
  132. {
  133. SAFE_DELETE(mPhysicsRep);
  134. if (!PHYSICSMGR)
  135. return;
  136. mWorld = PHYSICSMGR->getWorld(isServerObject() ? "server" : "client");
  137. if (!collision)
  138. return;
  139. mPhysicsRep = PHYSICSMGR->createBody();
  140. mPhysicsRep->init(collision, mMass, 0, mOwner, mWorld);
  141. mPhysicsRep->setMaterial(mRestitution, mDynamicFriction, mStaticFriction);
  142. mPhysicsRep->setDamping(mLinearDamping, mAngularDamping);
  143. mPhysicsRep->setSleepThreshold(mLinearSleepThreshold, mAngularSleepThreshold);
  144. mPhysicsRep->setTransform(mOwner->getTransform());
  145. // The reset position is the transform on the server
  146. // at creation time... its not used on the client.
  147. if (isServerObject())
  148. {
  149. storeRestorePos();
  150. PhysicsPlugin::getPhysicsResetSignal().notify(this, &RigidBodyComponent::_onPhysicsReset);
  151. }
  152. }
  153. U32 RigidBodyComponent::packUpdate(NetConnection *con, U32 mask, BitStream *stream)
  154. {
  155. U32 retMask = Parent::packUpdate(con, mask, stream);
  156. if (stream->writeFlag(mask & StateMask))
  157. {
  158. // This will encode the position relative to the control
  159. // object position.
  160. //
  161. // This will compress the position to as little as 6.25
  162. // bytes if the position is within about 30 meters of the
  163. // control object.
  164. //
  165. // Worst case its a full 12 bytes + 2 bits if the position
  166. // is more than 500 meters from the control object.
  167. //
  168. stream->writeCompressedPoint(mState.position);
  169. // Use only 3.5 bytes to send the orientation.
  170. stream->writeQuat(mState.orientation, 9);
  171. // If the server object has been set to sleep then
  172. // we don't need to send any velocity.
  173. if (!stream->writeFlag(mState.sleeping))
  174. {
  175. // This gives me ~0.015f resolution in velocity magnitude
  176. // while only costing me 1 bit of the velocity is zero length,
  177. // <5 bytes in normal cases, and <8 bytes if the velocity is
  178. // greater than 1000.
  179. AssertWarn(mState.linVelocity.len() < 1000.0f,
  180. "PhysicsShape::packUpdate - The linVelocity is out of range!");
  181. stream->writeVector(mState.linVelocity, 1000.0f, 16, 9);
  182. // For angular velocity we get < 0.01f resolution in magnitude
  183. // with the most common case being under 4 bytes.
  184. AssertWarn(mState.angVelocity.len() < 10.0f,
  185. "PhysicsShape::packUpdate - The angVelocity is out of range!");
  186. stream->writeVector(mState.angVelocity, 10.0f, 10, 9);
  187. }
  188. }
  189. return retMask;
  190. }
  191. void RigidBodyComponent::unpackUpdate(NetConnection *con, BitStream *stream)
  192. {
  193. Parent::unpackUpdate(con, stream);
  194. if (stream->readFlag()) // StateMask
  195. {
  196. PhysicsState state;
  197. // Read the encoded and compressed position... commonly only 6.25 bytes.
  198. stream->readCompressedPoint(&state.position);
  199. // Read the compressed quaternion... 3.5 bytes.
  200. stream->readQuat(&state.orientation, 9);
  201. state.sleeping = stream->readFlag();
  202. if (!state.sleeping)
  203. {
  204. stream->readVector(&state.linVelocity, 1000.0f, 16, 9);
  205. stream->readVector(&state.angVelocity, 10.0f, 10, 9);
  206. }
  207. if (!smNoCorrections && mPhysicsRep && mPhysicsRep->isDynamic())
  208. {
  209. // Set the new state on the physics object immediately.
  210. mPhysicsRep->applyCorrection(state.getTransform());
  211. mPhysicsRep->setSleeping(state.sleeping);
  212. if (!state.sleeping)
  213. {
  214. mPhysicsRep->setLinVelocity(state.linVelocity);
  215. mPhysicsRep->setAngVelocity(state.angVelocity);
  216. }
  217. mPhysicsRep->getState(&mState);
  218. }
  219. // If there is no physics object then just set the
  220. // new state... the tick will take care of the
  221. // interpolation and extrapolation.
  222. if (!mPhysicsRep || !mPhysicsRep->isDynamic())
  223. mState = state;
  224. }
  225. }
  226. void RigidBodyComponent::processTick()
  227. {
  228. Parent::processTick();
  229. if (!mPhysicsRep || !PHYSICSMGR)
  230. return;
  231. // Note that unlike TSStatic, the serverside PhysicsShape does not
  232. // need to play the ambient animation because even if the animation were
  233. // to move collision shapes it would not affect the physx representation.
  234. PROFILE_START(RigidBodyComponent_ProcessTick);
  235. if (!mPhysicsRep->isDynamic())
  236. return;
  237. // SINGLE PLAYER HACK!!!!
  238. if (PHYSICSMGR->isSinglePlayer() && isClientObject() && getServerObject())
  239. {
  240. RigidBodyComponent *servObj = (RigidBodyComponent*)getServerObject();
  241. mOwner->setTransform(servObj->mState.getTransform());
  242. mRenderState[0] = servObj->mRenderState[0];
  243. mRenderState[1] = servObj->mRenderState[1];
  244. return;
  245. }
  246. // Store the last render state.
  247. mRenderState[0] = mRenderState[1];
  248. // If the last render state doesn't match the last simulation
  249. // state then we got a correction and need to
  250. Point3F errorDelta = mRenderState[1].position - mState.position;
  251. const bool doSmoothing = !errorDelta.isZero() && !smNoSmoothing;
  252. const bool wasSleeping = mState.sleeping;
  253. // Get the new physics state.
  254. mPhysicsRep->getState(&mState);
  255. updateContainerForces();
  256. // Smooth the correction back into the render state.
  257. mRenderState[1] = mState;
  258. if (doSmoothing)
  259. {
  260. F32 correction = mClampF(errorDelta.len() / 20.0f, 0.1f, 0.9f);
  261. mRenderState[1].position.interpolate(mState.position, mRenderState[0].position, correction);
  262. mRenderState[1].orientation.interpolate(mState.orientation, mRenderState[0].orientation, correction);
  263. }
  264. //Check if any collisions occured
  265. findContact();
  266. // If we haven't been sleeping then update our transform
  267. // and set ourselves as dirty for the next client update.
  268. if (!wasSleeping || !mState.sleeping)
  269. {
  270. // Set the transform on the parent so that
  271. // the physics object isn't moved.
  272. mOwner->setTransform(mState.getTransform());
  273. // If we're doing server simulation then we need
  274. // to send the client a state update.
  275. if (isServerObject() && mPhysicsRep && !smNoCorrections &&
  276. !PHYSICSMGR->isSinglePlayer() // SINGLE PLAYER HACK!!!!
  277. )
  278. setMaskBits(StateMask);
  279. }
  280. PROFILE_END();
  281. }
  282. void RigidBodyComponent::findContact()
  283. {
  284. SceneObject *contactObject = NULL;
  285. VectorF *contactNormal = new VectorF(0, 0, 0);
  286. Vector<SceneObject*> overlapObjects;
  287. mPhysicsRep->findContact(&contactObject, contactNormal, &overlapObjects);
  288. if (!overlapObjects.empty())
  289. {
  290. //fire our signal that the physics sim said collisions happened
  291. onPhysicsCollision.trigger(*contactNormal, overlapObjects);
  292. }
  293. }
  294. void RigidBodyComponent::_onPhysicsReset(PhysicsResetEvent reset)
  295. {
  296. if (reset == PhysicsResetEvent_Store)
  297. mResetPos = mOwner->getTransform();
  298. else if (reset == PhysicsResetEvent_Restore)
  299. {
  300. mOwner->setTransform(mResetPos);
  301. }
  302. }
  303. void RigidBodyComponent::storeRestorePos()
  304. {
  305. mResetPos = mOwner->getTransform();
  306. }
  307. void RigidBodyComponent::applyImpulse(const Point3F &pos, const VectorF &vec)
  308. {
  309. if (mPhysicsRep && mPhysicsRep->isDynamic())
  310. mPhysicsRep->applyImpulse(pos, vec);
  311. }
  312. void RigidBodyComponent::applyRadialImpulse(const Point3F &origin, F32 radius, F32 magnitude)
  313. {
  314. if (!mPhysicsRep || !mPhysicsRep->isDynamic())
  315. return;
  316. // TODO: Find a better approximation of the
  317. // force vector using the object box.
  318. VectorF force = mOwner->getWorldBox().getCenter() - origin;
  319. F32 dist = force.magnitudeSafe();
  320. force.normalize();
  321. if (dist == 0.0f)
  322. force *= magnitude;
  323. else
  324. force *= mClampF(radius / dist, 0.0f, 1.0f) * magnitude;
  325. mPhysicsRep->applyImpulse(origin, force);
  326. // TODO: There is no simple way to really sync this sort of an
  327. // event with the client.
  328. //
  329. // The best is to send the current physics snapshot, calculate the
  330. // time difference from when this event occured and the time when the
  331. // client recieves it, and then extrapolate where it should be.
  332. //
  333. // Even then its impossible to be absolutely sure its synced.
  334. //
  335. // Bottom line... you shouldn't use physics over the network like this.
  336. //
  337. }
  338. void RigidBodyComponent::updateContainerForces()
  339. {
  340. PROFILE_SCOPE(RigidBodyComponent_updateContainerForces);
  341. // If we're not simulating don't update forces.
  342. PhysicsWorld *world = PHYSICSMGR->getWorld(isServerObject() ? "server" : "client");
  343. if (!world || !world->isEnabled())
  344. return;
  345. ContainerQueryInfo info;
  346. info.box = mOwner->getWorldBox();
  347. info.mass = mMass;
  348. // Find and retreive physics info from intersecting WaterObject(s)
  349. mOwner->getContainer()->findObjects(mOwner->getWorldBox(), WaterObjectType | PhysicalZoneObjectType, findRouter, &info);
  350. // Calculate buoyancy and drag
  351. F32 angDrag = mAngularDamping;
  352. F32 linDrag = mLinearDamping;
  353. F32 buoyancy = 0.0f;
  354. Point3F cmass = mPhysicsRep->getCMassPosition();
  355. F32 density = mBuoyancyDensity;
  356. if (density > 0.0f)
  357. {
  358. if (info.waterCoverage > 0.0f)
  359. {
  360. F32 waterDragScale = info.waterViscosity * mWaterDampingScale;
  361. F32 powCoverage = mPow(info.waterCoverage, 0.25f);
  362. angDrag = mLerp(angDrag, angDrag * waterDragScale, powCoverage);
  363. linDrag = mLerp(linDrag, linDrag * waterDragScale, powCoverage);
  364. }
  365. buoyancy = (info.waterDensity / density) * mPow(info.waterCoverage, 2.0f);
  366. // A little hackery to prevent oscillation
  367. // Based on this blog post:
  368. // (http://reinot.blogspot.com/2005/11/oh-yes-they-float-georgie-they-all.html)
  369. // JCF: disabled!
  370. Point3F buoyancyForce = buoyancy * -world->getGravity() * TickSec * mMass;
  371. mPhysicsRep->applyImpulse(cmass, buoyancyForce);
  372. }
  373. // Update the dampening as the container might have changed.
  374. mPhysicsRep->setDamping(linDrag, angDrag);
  375. // Apply physical zone forces.
  376. if (!info.appliedForce.isZero())
  377. mPhysicsRep->applyImpulse(cmass, info.appliedForce);
  378. }