BsFPhysXJoint.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "BsFPhysXJoint.h"
  4. #include "BsPhysXRigidbody.h"
  5. #include "PxRigidDynamic.h"
  6. using namespace physx;
  7. namespace BansheeEngine
  8. {
  9. PxJointActorIndex::Enum toJointActor(JointBody body)
  10. {
  11. if (body == JointBody::A)
  12. return PxJointActorIndex::eACTOR0;
  13. return PxJointActorIndex::eACTOR1;
  14. }
  15. FPhysXJoint::FPhysXJoint(physx::PxJoint* joint)
  16. :mJoint(joint)
  17. {
  18. }
  19. FPhysXJoint::~FPhysXJoint()
  20. {
  21. mJoint->release();
  22. }
  23. Rigidbody* FPhysXJoint::getBody(JointBody body) const
  24. {
  25. PxRigidActor* actorA = nullptr;
  26. PxRigidActor* actorB = nullptr;
  27. mJoint->getActors(actorA, actorB);
  28. PxRigidActor* wantedActor = body == JointBody::A ? actorA : actorB;
  29. if (wantedActor == nullptr)
  30. return nullptr;
  31. return (Rigidbody*)wantedActor->userData;
  32. }
  33. void FPhysXJoint::setBody(JointBody body, Rigidbody* value)
  34. {
  35. PxRigidActor* actorA = nullptr;
  36. PxRigidActor* actorB = nullptr;
  37. mJoint->getActors(actorA, actorB);
  38. PxRigidActor* actor = nullptr;
  39. if (value != nullptr)
  40. actor = static_cast<PhysXRigidbody*>(value)->_getInternal();
  41. if (body == JointBody::A)
  42. actorA = actor;
  43. else
  44. actorB = actor;
  45. mJoint->setActors(actorA, actorB);
  46. }
  47. Vector3 FPhysXJoint::getPosition(JointBody body) const
  48. {
  49. PxVec3 position = mJoint->getLocalPose(toJointActor(body)).p;
  50. return fromPxVector(position);
  51. }
  52. Quaternion FPhysXJoint::getRotation(JointBody body) const
  53. {
  54. PxQuat rotation = mJoint->getLocalPose(toJointActor(body)).q;
  55. return fromPxQuaternion(rotation);
  56. }
  57. void FPhysXJoint::setTransform(JointBody body, const Vector3& position, const Quaternion& rotation)
  58. {
  59. PxTransform transform = toPxTransform(position, rotation);
  60. mJoint->setLocalPose(toJointActor(body), transform);
  61. }
  62. float FPhysXJoint::getBreakForce() const
  63. {
  64. float force = 0.0f;
  65. float torque = 0.0f;
  66. mJoint->getBreakForce(force, torque);
  67. return force;
  68. }
  69. void FPhysXJoint::setBreakForce(float force)
  70. {
  71. float dummy = 0.0f;
  72. float torque = 0.0f;
  73. mJoint->getBreakForce(dummy, torque);
  74. mJoint->setBreakForce(force, torque);
  75. }
  76. float FPhysXJoint::getBreakTorque() const
  77. {
  78. float force = 0.0f;
  79. float torque = 0.0f;
  80. mJoint->getBreakForce(force, torque);
  81. return torque;
  82. }
  83. void FPhysXJoint::setBreakTorque(float torque)
  84. {
  85. float force = 0.0f;
  86. float dummy = 0.0f;
  87. mJoint->getBreakForce(force, dummy);
  88. mJoint->setBreakForce(force, torque);
  89. }
  90. bool FPhysXJoint::getEnableCollision() const
  91. {
  92. return mJoint->getConstraintFlags() & PxConstraintFlag::eCOLLISION_ENABLED;
  93. }
  94. void FPhysXJoint::setEnableCollision(bool value)
  95. {
  96. mJoint->setConstraintFlag(PxConstraintFlag::eCOLLISION_ENABLED, value);
  97. }
  98. }