CreateRagdoll.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. //
  2. // Copyright (c) 2008-2016 the Urho3D project.
  3. // Copyright (c) 2014-2016, THUNDERBEAST GAMES LLC All rights reserved
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #include <Atomic/Graphics/AnimatedModel.h>
  24. #include <Atomic/IO/Log.h>
  25. #include <Atomic/Physics/PhysicsEvents.h>
  26. #include <Atomic/Physics/RigidBody.h>
  27. #include "CreateRagdoll.h"
  28. #include <Atomic/DebugNew.h>
  29. CreateRagdoll::CreateRagdoll(Context* context) :
  30. Component(context)
  31. {
  32. }
  33. void CreateRagdoll::OnNodeSet(Node* node)
  34. {
  35. // If the node pointer is non-null, this component has been created into a scene node. Subscribe to physics collisions that
  36. // concern this scene node
  37. if (node)
  38. SubscribeToEvent(node, E_NODECOLLISION, ATOMIC_HANDLER(CreateRagdoll, HandleNodeCollision));
  39. }
  40. void CreateRagdoll::HandleNodeCollision(StringHash eventType, VariantMap& eventData)
  41. {
  42. using namespace NodeCollision;
  43. // Get the other colliding body, make sure it is moving (has nonzero mass)
  44. RigidBody* otherBody = static_cast<RigidBody*>(eventData[P_OTHERBODY].GetPtr());
  45. if (otherBody->GetMass() > 0.0f)
  46. {
  47. // We do not need the physics components in the AnimatedModel's root scene node anymore
  48. node_->RemoveComponent<RigidBody>();
  49. node_->RemoveComponent<CollisionShape>();
  50. // Create RigidBody & CollisionShape components to bones
  51. CreateRagdollBone("Bip01_Pelvis", SHAPE_BOX, Vector3(0.3f, 0.2f, 0.25f), Vector3(0.0f, 0.0f, 0.0f),
  52. Quaternion(0.0f, 0.0f, 0.0f));
  53. CreateRagdollBone("Bip01_Spine1", SHAPE_BOX, Vector3(0.35f, 0.2f, 0.3f), Vector3(0.15f, 0.0f, 0.0f),
  54. Quaternion(0.0f, 0.0f, 0.0f));
  55. CreateRagdollBone("Bip01_L_Thigh", SHAPE_CAPSULE, Vector3(0.175f, 0.45f, 0.175f), Vector3(0.25f, 0.0f, 0.0f),
  56. Quaternion(0.0f, 0.0f, 90.0f));
  57. CreateRagdollBone("Bip01_R_Thigh", SHAPE_CAPSULE, Vector3(0.175f, 0.45f, 0.175f), Vector3(0.25f, 0.0f, 0.0f),
  58. Quaternion(0.0f, 0.0f, 90.0f));
  59. CreateRagdollBone("Bip01_L_Calf", SHAPE_CAPSULE, Vector3(0.15f, 0.55f, 0.15f), Vector3(0.25f, 0.0f, 0.0f),
  60. Quaternion(0.0f, 0.0f, 90.0f));
  61. CreateRagdollBone("Bip01_R_Calf", SHAPE_CAPSULE, Vector3(0.15f, 0.55f, 0.15f), Vector3(0.25f, 0.0f, 0.0f),
  62. Quaternion(0.0f, 0.0f, 90.0f));
  63. CreateRagdollBone("Bip01_Head", SHAPE_BOX, Vector3(0.2f, 0.2f, 0.2f), Vector3(0.1f, 0.0f, 0.0f),
  64. Quaternion(0.0f, 0.0f, 0.0f));
  65. CreateRagdollBone("Bip01_L_UpperArm", SHAPE_CAPSULE, Vector3(0.15f, 0.35f, 0.15f), Vector3(0.1f, 0.0f, 0.0f),
  66. Quaternion(0.0f, 0.0f, 90.0f));
  67. CreateRagdollBone("Bip01_R_UpperArm", SHAPE_CAPSULE, Vector3(0.15f, 0.35f, 0.15f), Vector3(0.1f, 0.0f, 0.0f),
  68. Quaternion(0.0f, 0.0f, 90.0f));
  69. CreateRagdollBone("Bip01_L_Forearm", SHAPE_CAPSULE, Vector3(0.125f, 0.4f, 0.125f), Vector3(0.2f, 0.0f, 0.0f),
  70. Quaternion(0.0f, 0.0f, 90.0f));
  71. CreateRagdollBone("Bip01_R_Forearm", SHAPE_CAPSULE, Vector3(0.125f, 0.4f, 0.125f), Vector3(0.2f, 0.0f, 0.0f),
  72. Quaternion(0.0f, 0.0f, 90.0f));
  73. // Create Constraints between bones
  74. CreateRagdollConstraint("Bip01_L_Thigh", "Bip01_Pelvis", CONSTRAINT_CONETWIST, Vector3::BACK, Vector3::FORWARD,
  75. Vector2(45.0f, 45.0f), Vector2::ZERO);
  76. CreateRagdollConstraint("Bip01_R_Thigh", "Bip01_Pelvis", CONSTRAINT_CONETWIST, Vector3::BACK, Vector3::FORWARD,
  77. Vector2(45.0f, 45.0f), Vector2::ZERO);
  78. CreateRagdollConstraint("Bip01_L_Calf", "Bip01_L_Thigh", CONSTRAINT_HINGE, Vector3::BACK, Vector3::BACK,
  79. Vector2(90.0f, 0.0f), Vector2::ZERO);
  80. CreateRagdollConstraint("Bip01_R_Calf", "Bip01_R_Thigh", CONSTRAINT_HINGE, Vector3::BACK, Vector3::BACK,
  81. Vector2(90.0f, 0.0f), Vector2::ZERO);
  82. CreateRagdollConstraint("Bip01_Spine1", "Bip01_Pelvis", CONSTRAINT_HINGE, Vector3::FORWARD, Vector3::FORWARD,
  83. Vector2(45.0f, 0.0f), Vector2(-10.0f, 0.0f));
  84. CreateRagdollConstraint("Bip01_Head", "Bip01_Spine1", CONSTRAINT_CONETWIST, Vector3::LEFT, Vector3::LEFT,
  85. Vector2(0.0f, 30.0f), Vector2::ZERO);
  86. CreateRagdollConstraint("Bip01_L_UpperArm", "Bip01_Spine1", CONSTRAINT_CONETWIST, Vector3::DOWN, Vector3::UP,
  87. Vector2(45.0f, 45.0f), Vector2::ZERO, false);
  88. CreateRagdollConstraint("Bip01_R_UpperArm", "Bip01_Spine1", CONSTRAINT_CONETWIST, Vector3::DOWN, Vector3::UP,
  89. Vector2(45.0f, 45.0f), Vector2::ZERO, false);
  90. CreateRagdollConstraint("Bip01_L_Forearm", "Bip01_L_UpperArm", CONSTRAINT_HINGE, Vector3::BACK, Vector3::BACK,
  91. Vector2(90.0f, 0.0f), Vector2::ZERO);
  92. CreateRagdollConstraint("Bip01_R_Forearm", "Bip01_R_UpperArm", CONSTRAINT_HINGE, Vector3::BACK, Vector3::BACK,
  93. Vector2(90.0f, 0.0f), Vector2::ZERO);
  94. // Disable keyframe animation from all bones so that they will not interfere with the ragdoll
  95. AnimatedModel* model = GetComponent<AnimatedModel>();
  96. Skeleton& skeleton = model->GetSkeleton();
  97. for (unsigned i = 0; i < skeleton.GetNumBones(); ++i)
  98. skeleton.GetBone(i)->animated_ = false;
  99. // Finally remove self from the scene node. Note that this must be the last operation performed in the function
  100. Remove();
  101. }
  102. }
  103. void CreateRagdoll::CreateRagdollBone(const String& boneName, ShapeType type, const Vector3& size, const Vector3& position,
  104. const Quaternion& rotation)
  105. {
  106. // Find the correct child scene node recursively
  107. Node* boneNode = node_->GetChild(boneName, true);
  108. if (!boneNode)
  109. {
  110. ATOMIC_LOGWARNING("Could not find bone " + boneName + " for creating ragdoll physics components");
  111. return;
  112. }
  113. RigidBody* body = boneNode->CreateComponent<RigidBody>();
  114. // Set mass to make movable
  115. body->SetMass(1.0f);
  116. // Set damping parameters to smooth out the motion
  117. body->SetLinearDamping(0.05f);
  118. body->SetAngularDamping(0.85f);
  119. // Set rest thresholds to ensure the ragdoll rigid bodies come to rest to not consume CPU endlessly
  120. body->SetLinearRestThreshold(1.5f);
  121. body->SetAngularRestThreshold(2.5f);
  122. CollisionShape* shape = boneNode->CreateComponent<CollisionShape>();
  123. // We use either a box or a capsule shape for all of the bones
  124. if (type == SHAPE_BOX)
  125. shape->SetBox(size, position, rotation);
  126. else
  127. shape->SetCapsule(size.x_, size.y_, position, rotation);
  128. }
  129. void CreateRagdoll::CreateRagdollConstraint(const String& boneName, const String& parentName, ConstraintType type,
  130. const Vector3& axis, const Vector3& parentAxis, const Vector2& highLimit, const Vector2& lowLimit,
  131. bool disableCollision)
  132. {
  133. Node* boneNode = node_->GetChild(boneName, true);
  134. Node* parentNode = node_->GetChild(parentName, true);
  135. if (!boneNode)
  136. {
  137. ATOMIC_LOGWARNING("Could not find bone " + boneName + " for creating ragdoll constraint");
  138. return;
  139. }
  140. if (!parentNode)
  141. {
  142. ATOMIC_LOGWARNING("Could not find bone " + parentName + " for creating ragdoll constraint");
  143. return;
  144. }
  145. Constraint* constraint = boneNode->CreateComponent<Constraint>();
  146. constraint->SetConstraintType(type);
  147. // Most of the constraints in the ragdoll will work better when the connected bodies don't collide against each other
  148. constraint->SetDisableCollision(disableCollision);
  149. // The connected body must be specified before setting the world position
  150. constraint->SetOtherBody(parentNode->GetComponent<RigidBody>());
  151. // Position the constraint at the child bone we are connecting
  152. constraint->SetWorldPosition(boneNode->GetWorldPosition());
  153. // Configure axes and limits
  154. constraint->SetAxis(axis);
  155. constraint->SetOtherAxis(parentAxis);
  156. constraint->SetHighLimit(highLimit);
  157. constraint->SetLowLimit(lowLimit);
  158. }