BsPhysXRigidbody.cpp 9.1 KB

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