BsPhysXRigidbody.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. #include "BsPhysXRigidbody.h"
  2. #include "Physics/BsCollider.h"
  3. #include "BsFPhysXCollider.h"
  4. #include "Scene/BsSceneObject.h"
  5. #include "Physics/BsPhysics.h"
  6. #include "PxRigidDynamic.h"
  7. #include "PxScene.h"
  8. #include "extensions/PxRigidBodyExt.h"
  9. #include "BsPhysX.h"
  10. using namespace physx;
  11. namespace bs
  12. {
  13. PxForceMode::Enum toPxForceMode(ForceMode mode)
  14. {
  15. switch(mode)
  16. {
  17. case ForceMode::Force:
  18. return PxForceMode::eFORCE;
  19. case ForceMode::Impulse:
  20. return PxForceMode::eIMPULSE;
  21. case ForceMode::Velocity:
  22. return PxForceMode::eVELOCITY_CHANGE;
  23. case ForceMode::Acceleration:
  24. return PxForceMode::eACCELERATION;
  25. }
  26. return PxForceMode::eFORCE;
  27. }
  28. PxForceMode::Enum toPxForceMode(PointForceMode mode)
  29. {
  30. switch (mode)
  31. {
  32. case PointForceMode::Force:
  33. return PxForceMode::eFORCE;
  34. case PointForceMode::Impulse:
  35. return PxForceMode::eIMPULSE;
  36. }
  37. return PxForceMode::eFORCE;
  38. }
  39. PhysXRigidbody::PhysXRigidbody(PxPhysics* physx, PxScene* scene, const HSceneObject& linkedSO)
  40. :Rigidbody(linkedSO)
  41. {
  42. const Transform& tfrm = linkedSO->getTransform();
  43. PxTransform pxTfrm = toPxTransform(tfrm.getPosition(), tfrm.getRotation());
  44. mInternal = physx->createRigidDynamic(pxTfrm);
  45. mInternal->userData = this;
  46. scene->addActor(*mInternal);
  47. }
  48. PhysXRigidbody::~PhysXRigidbody()
  49. {
  50. mInternal->userData = nullptr;
  51. mInternal->release();
  52. }
  53. void PhysXRigidbody::move(const Vector3& position)
  54. {
  55. if (getIsKinematic())
  56. {
  57. PxTransform target;
  58. if (!mInternal->getKinematicTarget(target))
  59. target = PxTransform(PxIdentity);
  60. target.p = toPxVector(position);
  61. mInternal->setKinematicTarget(target);
  62. }
  63. else
  64. {
  65. setTransform(position, getRotation());
  66. }
  67. }
  68. void PhysXRigidbody::rotate(const Quaternion& rotation)
  69. {
  70. if (getIsKinematic())
  71. {
  72. PxTransform target;
  73. if (!mInternal->getKinematicTarget(target))
  74. target = PxTransform(PxIdentity);
  75. target.q = toPxQuaternion(rotation);
  76. mInternal->setKinematicTarget(target);
  77. }
  78. else
  79. {
  80. setTransform(getPosition(), rotation);
  81. }
  82. }
  83. Vector3 PhysXRigidbody::getPosition() const
  84. {
  85. return fromPxVector(mInternal->getGlobalPose().p);
  86. }
  87. Quaternion PhysXRigidbody::getRotation() const
  88. {
  89. return fromPxQuaternion(mInternal->getGlobalPose().q);
  90. }
  91. void PhysXRigidbody::setTransform(const Vector3& pos, const Quaternion& rot)
  92. {
  93. mInternal->setGlobalPose(toPxTransform(pos, rot));
  94. }
  95. void PhysXRigidbody::setMass(float mass)
  96. {
  97. if(((UINT32)mFlags & (UINT32)RigidbodyFlag::AutoMass) != 0)
  98. {
  99. LOGWRN("Attempting to set Rigidbody mass, but it has automatic mass calculation turned on.");
  100. return;
  101. }
  102. mInternal->setMass(mass);
  103. }
  104. float PhysXRigidbody::getMass() const
  105. {
  106. return mInternal->getMass();
  107. }
  108. void PhysXRigidbody::setIsKinematic(bool kinematic)
  109. {
  110. mInternal->setRigidBodyFlag(PxRigidBodyFlag::eKINEMATIC, kinematic);
  111. }
  112. bool PhysXRigidbody::getIsKinematic() const
  113. {
  114. return ((UINT32)mInternal->getRigidBodyFlags() & PxRigidBodyFlag::eKINEMATIC) != 0;
  115. }
  116. bool PhysXRigidbody::isSleeping() const
  117. {
  118. return mInternal->isSleeping();
  119. }
  120. void PhysXRigidbody::sleep()
  121. {
  122. mInternal->putToSleep();
  123. }
  124. void PhysXRigidbody::wakeUp()
  125. {
  126. mInternal->wakeUp();
  127. }
  128. void PhysXRigidbody::setSleepThreshold(float threshold)
  129. {
  130. mInternal->setSleepThreshold(threshold);
  131. }
  132. float PhysXRigidbody::getSleepThreshold() const
  133. {
  134. return mInternal->getSleepThreshold();
  135. }
  136. void PhysXRigidbody::setUseGravity(bool gravity)
  137. {
  138. mInternal->setActorFlag(PxActorFlag::eDISABLE_GRAVITY, !gravity);
  139. }
  140. bool PhysXRigidbody::getUseGravity() const
  141. {
  142. return ((UINT32)mInternal->getActorFlags() & PxActorFlag::eDISABLE_GRAVITY) == 0;
  143. }
  144. void PhysXRigidbody::setVelocity(const Vector3& velocity)
  145. {
  146. mInternal->setLinearVelocity(toPxVector(velocity));
  147. }
  148. Vector3 PhysXRigidbody::getVelocity() const
  149. {
  150. return fromPxVector(mInternal->getLinearVelocity());
  151. }
  152. void PhysXRigidbody::setAngularVelocity(const Vector3& velocity)
  153. {
  154. mInternal->setAngularVelocity(toPxVector(velocity));
  155. }
  156. Vector3 PhysXRigidbody::getAngularVelocity() const
  157. {
  158. return fromPxVector(mInternal->getAngularVelocity());
  159. }
  160. void PhysXRigidbody::setDrag(float drag)
  161. {
  162. mInternal->setLinearDamping(drag);
  163. }
  164. float PhysXRigidbody::getDrag() const
  165. {
  166. return mInternal->getLinearDamping();
  167. }
  168. void PhysXRigidbody::setAngularDrag(float drag)
  169. {
  170. mInternal->setAngularDamping(drag);
  171. }
  172. float PhysXRigidbody::getAngularDrag() const
  173. {
  174. return mInternal->getAngularDamping();
  175. }
  176. void PhysXRigidbody::setInertiaTensor(const Vector3& tensor)
  177. {
  178. if (((UINT32)mFlags & (UINT32)RigidbodyFlag::AutoTensors) != 0)
  179. {
  180. LOGWRN("Attempting to set Rigidbody inertia tensor, but it has automatic tensor calculation turned on.");
  181. return;
  182. }
  183. mInternal->setMassSpaceInertiaTensor(toPxVector(tensor));
  184. }
  185. Vector3 PhysXRigidbody::getInertiaTensor() const
  186. {
  187. return fromPxVector(mInternal->getMassSpaceInertiaTensor());
  188. }
  189. void PhysXRigidbody::setMaxAngularVelocity(float maxVelocity)
  190. {
  191. mInternal->setMaxAngularVelocity(maxVelocity);
  192. }
  193. float PhysXRigidbody::getMaxAngularVelocity() const
  194. {
  195. return mInternal->getMaxAngularVelocity();
  196. }
  197. void PhysXRigidbody::setCenterOfMass(const Vector3& position, const Quaternion& rotation)
  198. {
  199. if (((UINT32)mFlags & (UINT32)RigidbodyFlag::AutoTensors) != 0)
  200. {
  201. LOGWRN("Attempting to set Rigidbody center of mass, but it has automatic tensor calculation turned on.");
  202. return;
  203. }
  204. mInternal->setCMassLocalPose(toPxTransform(position, rotation));
  205. }
  206. Vector3 PhysXRigidbody::getCenterOfMassPosition() const
  207. {
  208. PxTransform cMassTfrm = mInternal->getCMassLocalPose();
  209. return fromPxVector(cMassTfrm.p);
  210. }
  211. Quaternion PhysXRigidbody::getCenterOfMassRotation() const
  212. {
  213. PxTransform cMassTfrm = mInternal->getCMassLocalPose();
  214. return fromPxQuaternion(cMassTfrm.q);
  215. }
  216. void PhysXRigidbody::setPositionSolverCount(UINT32 count)
  217. {
  218. mInternal->setSolverIterationCounts(std::max(1U, count), getVelocitySolverCount());
  219. }
  220. UINT32 PhysXRigidbody::getPositionSolverCount() const
  221. {
  222. UINT32 posCount = 1;
  223. UINT32 velCount = 1;
  224. mInternal->getSolverIterationCounts(posCount, velCount);
  225. return posCount;
  226. }
  227. void PhysXRigidbody::setVelocitySolverCount(UINT32 count)
  228. {
  229. mInternal->setSolverIterationCounts(getPositionSolverCount(), std::max(1U, count));
  230. }
  231. UINT32 PhysXRigidbody::getVelocitySolverCount() const
  232. {
  233. UINT32 posCount = 1;
  234. UINT32 velCount = 1;
  235. mInternal->getSolverIterationCounts(posCount, velCount);
  236. return velCount;
  237. }
  238. void PhysXRigidbody::setFlags(RigidbodyFlag flags)
  239. {
  240. bool ccdEnabledOld = mInternal->getRigidBodyFlags() & PxRigidBodyFlag::eENABLE_CCD;
  241. bool ccdEnabledNew = ((UINT32)flags & (UINT32)RigidbodyFlag::CCD) != 0;
  242. if(ccdEnabledOld != ccdEnabledNew)
  243. {
  244. if(ccdEnabledNew)
  245. {
  246. if (!gPhysics().hasFlag(PhysicsFlag::CCD_Enable))
  247. LOGWRN("Enabling CCD on a Rigidbody but CCD is not enabled globally.");
  248. }
  249. mInternal->setRigidBodyFlag(PxRigidBodyFlag::eENABLE_CCD, ccdEnabledNew);
  250. // Enable/disable CCD on shapes so the filter can handle them properly
  251. UINT32 numShapes = mInternal->getNbShapes();
  252. PxShape** shapes = (PxShape**)bs_stack_alloc(sizeof(PxShape*) * numShapes);
  253. mInternal->getShapes(shapes, sizeof(PxShape*) * numShapes);
  254. for (UINT32 i = 0; i < numShapes; i++)
  255. {
  256. Collider* collider = (Collider*)shapes[i]->userData;
  257. collider->_getInternal()->_setCCD(ccdEnabledNew);
  258. }
  259. }
  260. Rigidbody::setFlags(flags);
  261. }
  262. void PhysXRigidbody::addForce(const Vector3& force, ForceMode mode)
  263. {
  264. mInternal->addForce(toPxVector(force), toPxForceMode(mode));
  265. }
  266. void PhysXRigidbody::addTorque(const Vector3& force, ForceMode mode)
  267. {
  268. mInternal->addTorque(toPxVector(force), toPxForceMode(mode));
  269. }
  270. void PhysXRigidbody::addForceAtPoint(const Vector3& force, const Vector3& position, PointForceMode mode)
  271. {
  272. const PxVec3& pxForce = toPxVector(force);
  273. const PxVec3& pxPos = toPxVector(position);
  274. const PxTransform globalPose = mInternal->getGlobalPose();
  275. PxVec3 centerOfMass = globalPose.transform(mInternal->getCMassLocalPose().p);
  276. PxForceMode::Enum pxMode = toPxForceMode(mode);
  277. PxVec3 torque = (pxPos - centerOfMass).cross(pxForce);
  278. mInternal->addForce(pxForce, pxMode);
  279. mInternal->addTorque(torque, pxMode);
  280. }
  281. Vector3 PhysXRigidbody::getVelocityAtPoint(const Vector3& point) const
  282. {
  283. const PxVec3& pxPoint = toPxVector(point);
  284. const PxTransform globalPose = mInternal->getGlobalPose();
  285. const PxVec3 centerOfMass = globalPose.transform(mInternal->getCMassLocalPose().p);
  286. const PxVec3 rpoint = pxPoint - centerOfMass;
  287. PxVec3 velocity = mInternal->getLinearVelocity();
  288. velocity += mInternal->getAngularVelocity().cross(rpoint);
  289. return fromPxVector(velocity);
  290. }
  291. void PhysXRigidbody::updateMassDistribution()
  292. {
  293. if (((UINT32)mFlags & (UINT32)RigidbodyFlag::AutoTensors) == 0)
  294. return;
  295. if (((UINT32)mFlags & (UINT32)RigidbodyFlag::AutoMass) == 0)
  296. {
  297. PxRigidBodyExt::setMassAndUpdateInertia(*mInternal, mInternal->getMass());
  298. }
  299. else
  300. {
  301. UINT32 numShapes = mInternal->getNbShapes();
  302. if (numShapes == 0)
  303. {
  304. PxRigidBodyExt::setMassAndUpdateInertia(*mInternal, mInternal->getMass());
  305. return;
  306. }
  307. PxShape** shapes = (PxShape**)bs_stack_alloc(sizeof(PxShape*) * numShapes);
  308. mInternal->getShapes(shapes, numShapes);
  309. float* masses = (float*)bs_stack_alloc(sizeof(float) * numShapes);
  310. for (UINT32 i = 0; i < numShapes; i++)
  311. masses[i] = ((Collider*)shapes[i]->userData)->getMass();
  312. PxRigidBodyExt::setMassAndUpdateInertia(*mInternal, masses, numShapes);
  313. bs_stack_free(masses);
  314. bs_stack_free(shapes);
  315. }
  316. }
  317. void PhysXRigidbody::addCollider(FCollider* collider)
  318. {
  319. if (collider == nullptr)
  320. return;
  321. FPhysXCollider* physxCollider = static_cast<FPhysXCollider*>(collider);
  322. physxCollider->_setCCD(((UINT32)mFlags & (UINT32)RigidbodyFlag::CCD) != 0);
  323. mInternal->attachShape(*physxCollider->_getShape());
  324. }
  325. void PhysXRigidbody::removeCollider(FCollider* collider)
  326. {
  327. if (collider == nullptr)
  328. return;
  329. FPhysXCollider* physxCollider = static_cast<FPhysXCollider*>(collider);
  330. physxCollider->_setCCD(false);
  331. mInternal->detachShape(*physxCollider->_getShape());
  332. }
  333. void PhysXRigidbody::removeColliders()
  334. {
  335. UINT32 numShapes = mInternal->getNbShapes();
  336. PxShape** shapes = (PxShape**)bs_stack_alloc(sizeof(PxShape*) * numShapes);
  337. mInternal->getShapes(shapes, sizeof(PxShape*) * numShapes);
  338. for (UINT32 i = 0; i < numShapes; i++)
  339. {
  340. Collider* collider = (Collider*)shapes[i]->userData;
  341. collider->_getInternal()->_setCCD(false);
  342. mInternal->detachShape(*shapes[i]);
  343. }
  344. bs_stack_free(shapes);
  345. }
  346. }