joint.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. * Copyright (c) 2012-2014 Daniele Bartolini and individual contributors.
  3. * License: https://github.com/taylor001/crown/blob/master/LICENSE
  4. */
  5. #include "joint.h"
  6. #include "actor.h"
  7. #include "vector3.h"
  8. #include "physics_resource.h"
  9. #include "scene_graph.h"
  10. #include "math_utils.h"
  11. #include "PxVec3.h"
  12. #include "PxMat44.h"
  13. #include "PxRigidActor.h"
  14. #include "PxRigidDynamic.h"
  15. #include "PxRigidStatic.h"
  16. #include "PxBoxGeometry.h"
  17. #include "PxFixedJoint.h"
  18. #include "PxSphericalJoint.h"
  19. #include "PxRevoluteJoint.h"
  20. #include "PxPrismaticJoint.h"
  21. #include "PxDistanceJoint.h"
  22. #include "PxJointLimit.h"
  23. #include "PxTolerancesScale.h"
  24. #include "actor.h"
  25. using physx::PxPhysics;
  26. using physx::PxScene;
  27. using physx::PxTransform;
  28. using physx::PxVec3;
  29. using physx::PxMat44;
  30. using physx::PxRigidActor;
  31. using physx::PxBoxGeometry;
  32. using physx::PxReal;
  33. using physx::PxPi;
  34. using physx::PxConstraintFlag;
  35. using physx::PxJoint;
  36. using physx::PxFixedJoint;
  37. using physx::PxFixedJointCreate;
  38. using physx::PxSphericalJoint;
  39. using physx::PxSphericalJointCreate;
  40. using physx::PxSphericalJointFlag;
  41. using physx::PxJointLimitCone;
  42. using physx::PxRevoluteJoint;
  43. using physx::PxRevoluteJointCreate;
  44. using physx::PxRevoluteJointFlag;
  45. using physx::PxPrismaticJoint;
  46. using physx::PxPrismaticJointCreate;
  47. using physx::PxPrismaticJointFlag;
  48. using physx::PxDistanceJoint;
  49. using physx::PxDistanceJointCreate;
  50. using physx::PxDistanceJointFlag;
  51. using physx::PxJointLinearLimitPair;
  52. using physx::PxTolerancesScale;
  53. using physx::PxJointAngularLimitPair;
  54. namespace crown
  55. {
  56. Joint::Joint(PxPhysics* physics, const JointResource* jr, const Actor& actor_0, const Actor& actor_1)
  57. : m_resource(jr)
  58. {
  59. PxVec3 anchor_0(jr->anchor_0.x, jr->anchor_0.y, jr->anchor_0.z);
  60. PxVec3 anchor_1(jr->anchor_1.x, jr->anchor_1.y, jr->anchor_1.z);
  61. switch(jr->type)
  62. {
  63. case JointType::FIXED:
  64. {
  65. m_joint = PxFixedJointCreate(*physics, actor_0.m_actor, PxTransform(anchor_0), actor_1.m_actor, PxTransform(anchor_1));
  66. break;
  67. }
  68. case JointType::SPHERICAL:
  69. {
  70. PxJointLimitCone limit_cone(jr->y_limit_angle, jr->z_limit_angle, jr->contact_dist);
  71. limit_cone.restitution = jr->restitution;
  72. limit_cone.damping = jr->damping;
  73. limit_cone.contactDistance = jr->distance;
  74. m_joint = PxSphericalJointCreate(*physics, actor_0.m_actor, PxTransform(anchor_0), actor_1.m_actor, PxTransform(anchor_1));
  75. static_cast<PxSphericalJoint*>(m_joint)->setLimitCone(limit_cone);
  76. static_cast<PxSphericalJoint*>(m_joint)->setSphericalJointFlag(PxSphericalJointFlag::eLIMIT_ENABLED, true);
  77. break;
  78. }
  79. case JointType::REVOLUTE:
  80. {
  81. PxJointAngularLimitPair limit_pair(jr->lower_limit, jr->upper_limit, jr->contact_dist);
  82. limit_pair.damping = jr->damping;
  83. m_joint = PxRevoluteJointCreate(*physics, actor_0.m_actor, PxTransform(anchor_0), actor_1.m_actor, PxTransform(anchor_1));
  84. static_cast<PxRevoluteJoint*>(m_joint)->setLimit(limit_pair);
  85. static_cast<PxRevoluteJoint*>(m_joint)->setRevoluteJointFlag(PxRevoluteJointFlag::eLIMIT_ENABLED, true);
  86. static_cast<PxRevoluteJoint*>(m_joint)->setDriveVelocity(10.0f);
  87. static_cast<PxRevoluteJoint*>(m_joint)->setRevoluteJointFlag(PxRevoluteJointFlag::eDRIVE_ENABLED, true);
  88. break;
  89. }
  90. case JointType::PRISMATIC:
  91. {
  92. PxJointLinearLimitPair limit_pair(PxTolerancesScale(), jr->lower_limit, jr->upper_limit, jr->contact_dist);
  93. limit_pair.damping = jr->damping;
  94. m_joint = PxPrismaticJointCreate(*physics, actor_0.m_actor, PxTransform(anchor_0), actor_1.m_actor, PxTransform(anchor_1));
  95. static_cast<PxPrismaticJoint*>(m_joint)->setLimit(limit_pair);
  96. static_cast<PxPrismaticJoint*>(m_joint)->setPrismaticJointFlag(PxPrismaticJointFlag::eLIMIT_ENABLED, true);
  97. break;
  98. }
  99. case JointType::DISTANCE:
  100. {
  101. m_joint = PxDistanceJointCreate(*physics, actor_0.m_actor, PxTransform(anchor_0), actor_1.m_actor, PxTransform(anchor_1));
  102. static_cast<PxDistanceJoint*>(m_joint)->setMaxDistance(jr->max_distance);
  103. static_cast<PxDistanceJoint*>(m_joint)->setDistanceJointFlag(PxDistanceJointFlag::eMAX_DISTANCE_ENABLED, true);
  104. break;
  105. }
  106. case JointType::D6:
  107. {
  108. // Must be implemented
  109. break;
  110. }
  111. }
  112. if (jr->breakable) m_joint->setBreakForce(jr->break_force, jr->break_torque);
  113. }
  114. } // namespace crown