BsPhysXSliderJoint.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "BsPhysXSliderJoint.h"
  4. #include "BsFPhysXJoint.h"
  5. #include "BsPhysX.h"
  6. #include "BsPhysXRigidbody.h"
  7. #include "PxRigidDynamic.h"
  8. using namespace physx;
  9. namespace bs
  10. {
  11. PxPrismaticJointFlag::Enum toPxFlag(SliderJointFlag flag)
  12. {
  13. switch (flag)
  14. {
  15. default:
  16. case SliderJointFlag::Limit:
  17. return PxPrismaticJointFlag::eLIMIT_ENABLED;
  18. }
  19. }
  20. PhysXSliderJoint::PhysXSliderJoint(PxPhysics* physx, const SLIDER_JOINT_DESC& desc)
  21. :SliderJoint(desc)
  22. {
  23. PxRigidActor* actor0 = nullptr;
  24. if (desc.bodies[0].body != nullptr)
  25. actor0 = static_cast<PhysXRigidbody*>(desc.bodies[0].body)->_getInternal();
  26. PxRigidActor* actor1 = nullptr;
  27. if (desc.bodies[1].body != nullptr)
  28. actor1 = static_cast<PhysXRigidbody*>(desc.bodies[1].body)->_getInternal();
  29. PxTransform tfrm0 = toPxTransform(desc.bodies[0].position, desc.bodies[0].rotation);
  30. PxTransform tfrm1 = toPxTransform(desc.bodies[1].position, desc.bodies[1].rotation);
  31. PxPrismaticJoint* joint = PxPrismaticJointCreate(*physx, actor0, tfrm0, actor1, tfrm1);
  32. joint->userData = this;
  33. mInternal = bs_new<FPhysXJoint>(joint, desc);
  34. PxPrismaticJointFlags flags;
  35. if (((UINT32)desc.flag & (UINT32)SliderJointFlag::Limit) != 0)
  36. flags |= PxPrismaticJointFlag::eLIMIT_ENABLED;
  37. joint->setPrismaticJointFlags(flags);
  38. // Calls to virtual methods are okay here
  39. setLimit(desc.limit);
  40. }
  41. PhysXSliderJoint::~PhysXSliderJoint()
  42. {
  43. bs_delete(mInternal);
  44. }
  45. float PhysXSliderJoint::getPosition() const
  46. {
  47. return getInternal()->getPosition();
  48. }
  49. float PhysXSliderJoint::getSpeed() const
  50. {
  51. return getInternal()->getVelocity();
  52. }
  53. LimitLinearRange PhysXSliderJoint::getLimit() const
  54. {
  55. PxJointLinearLimitPair pxLimit = getInternal()->getLimit();
  56. LimitLinearRange limit;
  57. limit.lower = pxLimit.lower;
  58. limit.upper = pxLimit.upper;
  59. limit.contactDist = pxLimit.contactDistance;
  60. limit.restitution = pxLimit.restitution;
  61. limit.spring.stiffness = pxLimit.stiffness;
  62. limit.spring.damping = pxLimit.damping;
  63. return limit;
  64. }
  65. void PhysXSliderJoint::setLimit(const LimitLinearRange& limit)
  66. {
  67. PxJointLinearLimitPair pxLimit(gPhysX().getScale(), limit.lower, limit.upper, limit.contactDist);
  68. pxLimit.stiffness = limit.spring.stiffness;
  69. pxLimit.damping = limit.spring.damping;
  70. pxLimit.restitution = limit.restitution;
  71. getInternal()->setLimit(pxLimit);
  72. }
  73. void PhysXSliderJoint::setFlag(SliderJointFlag flag, bool enabled)
  74. {
  75. getInternal()->setPrismaticJointFlag(toPxFlag(flag), enabled);
  76. }
  77. bool PhysXSliderJoint::hasFlag(SliderJointFlag flag) const
  78. {
  79. return getInternal()->getPrismaticJointFlags() & toPxFlag(flag);
  80. }
  81. PxPrismaticJoint* PhysXSliderJoint::getInternal() const
  82. {
  83. FPhysXJoint* internal = static_cast<FPhysXJoint*>(mInternal);
  84. return static_cast<PxPrismaticJoint*>(internal->_getInternal());
  85. }
  86. }