BsPhysXSphericalJoint.cpp 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "BsPhysXSphericalJoint.h"
  4. #include "BsFPhysXJoint.h"
  5. #include "BsPhysXRigidbody.h"
  6. #include "PxRigidDynamic.h"
  7. using namespace physx;
  8. namespace bs
  9. {
  10. PxSphericalJointFlag::Enum toPxFlag(SphericalJointFlag flag)
  11. {
  12. switch (flag)
  13. {
  14. default:
  15. case SphericalJointFlag::Limit:
  16. return PxSphericalJointFlag::eLIMIT_ENABLED;
  17. }
  18. }
  19. PhysXSphericalJoint::PhysXSphericalJoint(PxPhysics* physx, const SPHERICAL_JOINT_DESC& desc)
  20. :SphericalJoint(desc)
  21. {
  22. PxRigidActor* actor0 = nullptr;
  23. if (desc.bodies[0].body != nullptr)
  24. actor0 = static_cast<PhysXRigidbody*>(desc.bodies[0].body)->_getInternal();
  25. PxRigidActor* actor1 = nullptr;
  26. if (desc.bodies[1].body != nullptr)
  27. actor1 = static_cast<PhysXRigidbody*>(desc.bodies[1].body)->_getInternal();
  28. PxTransform tfrm0 = toPxTransform(desc.bodies[0].position, desc.bodies[0].rotation);
  29. PxTransform tfrm1 = toPxTransform(desc.bodies[1].position, desc.bodies[1].rotation);
  30. PxSphericalJoint* joint = PxSphericalJointCreate(*physx, actor0, tfrm0, actor1, tfrm1);
  31. joint->userData = this;
  32. mInternal = bs_new<FPhysXJoint>(joint, desc);
  33. PxSphericalJointFlags flags;
  34. if (((UINT32)desc.flag & (UINT32)SphericalJointFlag::Limit) != 0)
  35. flags |= PxSphericalJointFlag::eLIMIT_ENABLED;
  36. joint->setSphericalJointFlags(flags);
  37. // Calls to virtual methods are okay here
  38. setLimit(desc.limit);
  39. }
  40. PhysXSphericalJoint::~PhysXSphericalJoint()
  41. {
  42. bs_delete(mInternal);
  43. }
  44. LimitConeRange PhysXSphericalJoint::getLimit() const
  45. {
  46. PxJointLimitCone pxLimit = getInternal()->getLimitCone();
  47. LimitConeRange limit;
  48. limit.yLimitAngle = pxLimit.yAngle;
  49. limit.zLimitAngle = pxLimit.zAngle;
  50. limit.contactDist = pxLimit.contactDistance;
  51. limit.restitution = pxLimit.restitution;
  52. limit.spring.stiffness = pxLimit.stiffness;
  53. limit.spring.damping = pxLimit.damping;
  54. return limit;
  55. }
  56. void PhysXSphericalJoint::setLimit(const LimitConeRange& limit)
  57. {
  58. PxJointLimitCone pxLimit(limit.yLimitAngle.valueRadians(), limit.zLimitAngle.valueRadians(), limit.contactDist);
  59. pxLimit.stiffness = limit.spring.stiffness;
  60. pxLimit.damping = limit.spring.damping;
  61. pxLimit.restitution = limit.restitution;
  62. getInternal()->setLimitCone(pxLimit);
  63. }
  64. void PhysXSphericalJoint::setFlag(SphericalJointFlag flag, bool enabled)
  65. {
  66. getInternal()->setSphericalJointFlag(toPxFlag(flag), enabled);
  67. }
  68. bool PhysXSphericalJoint::hasFlag(SphericalJointFlag flag) const
  69. {
  70. return getInternal()->getSphericalJointFlags() & toPxFlag(flag);
  71. }
  72. PxSphericalJoint* PhysXSphericalJoint::getInternal() const
  73. {
  74. FPhysXJoint* internal = static_cast<FPhysXJoint*>(mInternal);
  75. return static_cast<PxSphericalJoint*>(internal->_getInternal());
  76. }
  77. }