joint.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 PhysicsResource* pr, const uint32_t index, const Actor& actor_0, const Actor& actor_1)
  57. : m_resource(pr)
  58. , m_index(index)
  59. {
  60. const PhysicsJoint* joint = physics_resource::joint(m_resource, m_index);
  61. PxVec3 anchor_0(joint->anchor_0.x, joint->anchor_0.y, joint->anchor_0.z);
  62. PxVec3 anchor_1(joint->anchor_1.x, joint->anchor_1.y, joint->anchor_1.z);
  63. switch(joint->type)
  64. {
  65. case JointType::FIXED:
  66. {
  67. m_joint = PxFixedJointCreate(*physics, actor_0.m_actor, PxTransform(anchor_0), actor_1.m_actor, PxTransform(anchor_1));
  68. break;
  69. }
  70. case JointType::SPHERICAL:
  71. {
  72. PxJointLimitCone limit_cone(joint->y_limit_angle, joint->z_limit_angle, joint->contact_dist);
  73. limit_cone.restitution = joint->restitution;
  74. limit_cone.damping = joint->damping;
  75. limit_cone.contactDistance = joint->distance;
  76. m_joint = PxSphericalJointCreate(*physics, actor_0.m_actor, PxTransform(anchor_0), actor_1.m_actor, PxTransform(anchor_1));
  77. static_cast<PxSphericalJoint*>(m_joint)->setLimitCone(limit_cone);
  78. static_cast<PxSphericalJoint*>(m_joint)->setSphericalJointFlag(PxSphericalJointFlag::eLIMIT_ENABLED, true);
  79. break;
  80. }
  81. case JointType::REVOLUTE:
  82. {
  83. PxJointAngularLimitPair limit_pair(joint->lower_limit, joint->upper_limit, joint->contact_dist);
  84. limit_pair.damping = joint->damping;
  85. m_joint = PxRevoluteJointCreate(*physics, actor_0.m_actor, PxTransform(anchor_0), actor_1.m_actor, PxTransform(anchor_1));
  86. static_cast<PxRevoluteJoint*>(m_joint)->setLimit(limit_pair);
  87. static_cast<PxRevoluteJoint*>(m_joint)->setRevoluteJointFlag(PxRevoluteJointFlag::eLIMIT_ENABLED, true);
  88. static_cast<PxRevoluteJoint*>(m_joint)->setDriveVelocity(10.0f);
  89. static_cast<PxRevoluteJoint*>(m_joint)->setRevoluteJointFlag(PxRevoluteJointFlag::eDRIVE_ENABLED, true);
  90. break;
  91. }
  92. case JointType::PRISMATIC:
  93. {
  94. PxJointLinearLimitPair limit_pair(PxTolerancesScale(), joint->lower_limit, joint->upper_limit, joint->contact_dist);
  95. limit_pair.damping = joint->damping;
  96. m_joint = PxPrismaticJointCreate(*physics, actor_0.m_actor, PxTransform(anchor_0), actor_1.m_actor, PxTransform(anchor_1));
  97. static_cast<PxPrismaticJoint*>(m_joint)->setLimit(limit_pair);
  98. static_cast<PxPrismaticJoint*>(m_joint)->setPrismaticJointFlag(PxPrismaticJointFlag::eLIMIT_ENABLED, true);
  99. break;
  100. }
  101. case JointType::DISTANCE:
  102. {
  103. m_joint = PxDistanceJointCreate(*physics, actor_0.m_actor, PxTransform(anchor_0), actor_1.m_actor, PxTransform(anchor_1));
  104. static_cast<PxDistanceJoint*>(m_joint)->setMaxDistance(joint->max_distance);
  105. static_cast<PxDistanceJoint*>(m_joint)->setDistanceJointFlag(PxDistanceJointFlag::eMAX_DISTANCE_ENABLED, true);
  106. break;
  107. }
  108. case JointType::D6:
  109. {
  110. // Must be implemented
  111. break;
  112. }
  113. }
  114. if (joint->breakable) m_joint->setBreakForce(joint->break_force, joint->break_torque);
  115. }
  116. Joint::~Joint()
  117. {
  118. }
  119. } // namespace crown