BsPhysXRigidbody.cpp 10 KB

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