BsPhysXRigidbody.cpp 10 KB

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