BsPhysXRigidbody.cpp 8.3 KB

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