BsPhysXD6Joint.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "BsPhysXD6Joint.h"
  4. #include "BsFPhysxJoint.h"
  5. #include "BsPhysX.h"
  6. using namespace physx;
  7. namespace BansheeEngine
  8. {
  9. PxD6Axis::Enum toPxAxis(PhysXD6Joint::Axis axis)
  10. {
  11. switch(axis)
  12. {
  13. default:
  14. case PhysXD6Joint::Axis::X:
  15. return PxD6Axis::eX;
  16. case PhysXD6Joint::Axis::Y:
  17. return PxD6Axis::eY;
  18. case PhysXD6Joint::Axis::Z:
  19. return PxD6Axis::eZ;
  20. case PhysXD6Joint::Axis::Twist:
  21. return PxD6Axis::eTWIST;
  22. case PhysXD6Joint::Axis::SwingY:
  23. return PxD6Axis::eSWING1;
  24. case PhysXD6Joint::Axis::SwingZ:
  25. return PxD6Axis::eSWING2;
  26. }
  27. }
  28. PxD6Motion::Enum toPxMotion(PhysXD6Joint::Motion motion)
  29. {
  30. switch(motion)
  31. {
  32. default:
  33. case PhysXD6Joint::Motion::Free:
  34. return PxD6Motion::eFREE;
  35. case PhysXD6Joint::Motion::Limited:
  36. return PxD6Motion::eLIMITED;
  37. case PhysXD6Joint::Motion::Locked:
  38. return PxD6Motion::eLOCKED;
  39. }
  40. }
  41. PxD6Drive::Enum toPxDrive(PhysXD6Joint::DriveType drive)
  42. {
  43. switch(drive)
  44. {
  45. default:
  46. case PhysXD6Joint::DriveType::X:
  47. return PxD6Drive::eX;
  48. case PhysXD6Joint::DriveType::Y:
  49. return PxD6Drive::eY;
  50. case PhysXD6Joint::DriveType::Z:
  51. return PxD6Drive::eZ;
  52. case PhysXD6Joint::DriveType::Swing:
  53. return PxD6Drive::eSWING;
  54. case PhysXD6Joint::DriveType::Twist:
  55. return PxD6Drive::eTWIST;
  56. case PhysXD6Joint::DriveType::SLERP:
  57. return PxD6Drive::eSLERP;
  58. }
  59. }
  60. PhysXD6Joint::Motion fromPxMotion(PxD6Motion::Enum motion)
  61. {
  62. switch (motion)
  63. {
  64. default:
  65. case PxD6Motion::eFREE:
  66. return PhysXD6Joint::Motion::Free;
  67. case PxD6Motion::eLIMITED:
  68. return PhysXD6Joint::Motion::Limited;
  69. case PxD6Motion::eLOCKED:
  70. return PhysXD6Joint::Motion::Locked;
  71. }
  72. }
  73. PhysXD6Joint::DriveType fromPxDrive(PxD6Drive::Enum drive)
  74. {
  75. switch (drive)
  76. {
  77. default:
  78. case PxD6Drive::eX:
  79. return PhysXD6Joint::DriveType::X;
  80. case PxD6Drive::eY:
  81. return PhysXD6Joint::DriveType::Y;
  82. case PxD6Drive::eZ:
  83. return PhysXD6Joint::DriveType::Z;
  84. case PxD6Drive::eSWING:
  85. return PhysXD6Joint::DriveType::Swing;
  86. case PxD6Drive::eTWIST:
  87. return PhysXD6Joint::DriveType::Twist;
  88. case PxD6Drive::eSLERP:
  89. return PhysXD6Joint::DriveType::SLERP;
  90. }
  91. }
  92. PhysXD6Joint::PhysXD6Joint(PxPhysics* physx)
  93. {
  94. PxD6Joint* joint = PxD6JointCreate(*physx, nullptr, PxTransform(), nullptr, PxTransform());
  95. joint->userData = this;
  96. mInternal = bs_new<FPhysXJoint>(joint);
  97. }
  98. PhysXD6Joint::~PhysXD6Joint()
  99. {
  100. bs_delete(mInternal);
  101. }
  102. PhysXD6Joint::Motion PhysXD6Joint::getMotion(Axis axis) const
  103. {
  104. return fromPxMotion(getInternal()->getMotion(toPxAxis(axis)));
  105. }
  106. void PhysXD6Joint::setMotion(Axis axis, Motion motion)
  107. {
  108. getInternal()->setMotion(toPxAxis(axis), toPxMotion(motion));
  109. }
  110. Radian PhysXD6Joint::getTwist() const
  111. {
  112. return Radian(getInternal()->getTwist());
  113. }
  114. Radian PhysXD6Joint::getSwingY() const
  115. {
  116. return Radian(getInternal()->getSwingYAngle());
  117. }
  118. Radian PhysXD6Joint::getSwingZ() const
  119. {
  120. return Radian(getInternal()->getSwingZAngle());
  121. }
  122. LimitLinear PhysXD6Joint::getLimitLinear() const
  123. {
  124. PxJointLinearLimit pxLimit = getInternal()->getLinearLimit();
  125. LimitLinear limit;
  126. limit.extent = pxLimit.value;
  127. limit.contactDist = pxLimit.contactDistance;
  128. limit.restitution = pxLimit.restitution;
  129. limit.spring.stiffness = pxLimit.stiffness;
  130. limit.spring.damping = pxLimit.damping;
  131. return limit;
  132. }
  133. void PhysXD6Joint::setLimitLinear(const LimitLinear& limit)
  134. {
  135. PxJointLinearLimit pxLimit(gPhysX().getScale(), limit.extent, limit.contactDist);
  136. pxLimit.stiffness = limit.spring.stiffness;
  137. pxLimit.damping = limit.spring.damping;
  138. pxLimit.restitution = limit.restitution;
  139. getInternal()->setLinearLimit(pxLimit);
  140. }
  141. LimitAngularRange PhysXD6Joint::getLimitTwist() const
  142. {
  143. PxJointAngularLimitPair pxLimit = getInternal()->getTwistLimit();
  144. LimitAngularRange limit;
  145. limit.lower = pxLimit.lower;
  146. limit.upper = pxLimit.upper;
  147. limit.contactDist = pxLimit.contactDistance;
  148. limit.restitution = pxLimit.restitution;
  149. limit.spring.stiffness = pxLimit.stiffness;
  150. limit.spring.damping = pxLimit.damping;
  151. return limit;
  152. }
  153. void PhysXD6Joint::setLimitTwist(const LimitAngularRange& limit)
  154. {
  155. PxJointAngularLimitPair pxLimit(limit.lower.valueRadians(), limit.upper.valueRadians(), limit.contactDist);
  156. pxLimit.stiffness = limit.spring.stiffness;
  157. pxLimit.damping = limit.spring.damping;
  158. pxLimit.restitution = limit.restitution;
  159. getInternal()->setTwistLimit(pxLimit);
  160. }
  161. LimitConeRange PhysXD6Joint::getLimitSwing() const
  162. {
  163. PxJointLimitCone pxLimit = getInternal()->getSwingLimit();
  164. LimitConeRange limit;
  165. limit.yLimitAngle = pxLimit.yAngle;
  166. limit.zLimitAngle = pxLimit.zAngle;
  167. limit.contactDist = pxLimit.contactDistance;
  168. limit.restitution = pxLimit.restitution;
  169. limit.spring.stiffness = pxLimit.stiffness;
  170. limit.spring.damping = pxLimit.damping;
  171. return limit;
  172. }
  173. void PhysXD6Joint::setLimitSwing(const LimitConeRange& limit)
  174. {
  175. PxJointLimitCone pxLimit(limit.yLimitAngle.valueRadians(), limit.zLimitAngle.valueRadians(), limit.contactDist);
  176. pxLimit.stiffness = limit.spring.stiffness;
  177. pxLimit.damping = limit.spring.damping;
  178. pxLimit.restitution = limit.restitution;
  179. getInternal()->setSwingLimit(pxLimit);
  180. }
  181. PhysXD6Joint::Drive PhysXD6Joint::getDrive(DriveType type) const
  182. {
  183. PxD6JointDrive pxDrive = getInternal()->getDrive(toPxDrive(type));
  184. Drive drive;
  185. drive.acceleration = pxDrive.flags & PxD6JointDriveFlag::eACCELERATION;
  186. drive.stiffness = pxDrive.stiffness;
  187. drive.damping = pxDrive.damping;
  188. drive.forceLimit = pxDrive.forceLimit;
  189. return drive;
  190. }
  191. void PhysXD6Joint::setDrive(DriveType type, const Drive& drive)
  192. {
  193. PxD6JointDrive pxDrive;
  194. if(drive.acceleration)
  195. pxDrive.flags = PxD6JointDriveFlag::eACCELERATION;
  196. pxDrive.stiffness = drive.stiffness;
  197. pxDrive.damping = drive.damping;
  198. pxDrive.forceLimit = drive.forceLimit;
  199. getInternal()->setDrive(toPxDrive(type), pxDrive);
  200. }
  201. Vector3 PhysXD6Joint::getDrivePosition() const
  202. {
  203. return fromPxVector(getInternal()->getDrivePosition().p);
  204. }
  205. Quaternion PhysXD6Joint::getDriveRotation() const
  206. {
  207. return fromPxQuaternion(getInternal()->getDrivePosition().q);
  208. }
  209. void PhysXD6Joint::setDriveTransform(const Vector3& position, const Quaternion& rotation)
  210. {
  211. getInternal()->setDrivePosition(toPxTransform(position, rotation));
  212. }
  213. Vector3 PhysXD6Joint::getDriveLinearVelocity() const
  214. {
  215. PxVec3 linear;
  216. PxVec3 angular;
  217. getInternal()->getDriveVelocity(linear, angular);
  218. return fromPxVector(linear);
  219. }
  220. Vector3 PhysXD6Joint::getDriveAngularVelocity() const
  221. {
  222. PxVec3 linear;
  223. PxVec3 angular;
  224. getInternal()->getDriveVelocity(linear, angular);
  225. return fromPxVector(angular);
  226. }
  227. void PhysXD6Joint::setDriveVelocity(const Vector3& linear, const Vector3& angular)
  228. {
  229. getInternal()->setDriveVelocity(toPxVector(linear), toPxVector(angular));
  230. }
  231. PxD6Joint* PhysXD6Joint::getInternal() const
  232. {
  233. FPhysXJoint* internal = static_cast<FPhysXJoint*>(mInternal);
  234. return static_cast<PxD6Joint*>(internal->_getInternal());
  235. }
  236. }