Ragdoll.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. //
  2. // Copyright (c) 2008-2015 the Urho3D project.
  3. // Copyright (c) 2015 Xamarin Inc
  4. // Copyright (c) 2016 THUNDERBEAST GAMES LLC
  5. //
  6. // Permission is hereby granted, free of charge, to any person obtaining a copy
  7. // of this software and associated documentation files (the "Software"), to deal
  8. // in the Software without restriction, including without limitation the rights
  9. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. // copies of the Software, and to permit persons to whom the Software is
  11. // furnished to do so, subject to the following conditions:
  12. //
  13. // The above copyright notice and this permission notice shall be included in
  14. // all copies or substantial portions of the Software.
  15. //
  16. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. // THE SOFTWARE.
  23. //
  24. using System;
  25. using AtomicEngine;
  26. namespace FeatureExamples
  27. {
  28. class Ragdoll : CSComponent
  29. {
  30. public void Start()
  31. {
  32. SubscribeToEvent<NodeCollisionEvent>(Node, HandleNodeCollision);
  33. }
  34. void HandleNodeCollision(NodeCollisionEvent e)
  35. {
  36. // Get the other colliding body, make sure it is moving (has nonzero mass)
  37. RigidBody otherBody = e.OtherBody;
  38. if (otherBody.Mass > 0.0f)
  39. {
  40. HitEffect (Node); // bam!
  41. // We do not need the physics components in the AnimatedModel's root scene node anymore
  42. Node.RemoveComponent<RigidBody>();
  43. Node.RemoveComponent<CollisionShape>();
  44. // Create RigidBody & CollisionShape components to bones
  45. CreateRagdollBone("Bip01_Pelvis", ShapeType.SHAPE_BOX, new Vector3(0.3f, 0.2f, 0.25f), new Vector3(0.0f, 0.0f, 0.0f),
  46. new Quaternion(0.0f, 0.0f, 0.0f));
  47. CreateRagdollBone("Bip01_Spine1", ShapeType.SHAPE_BOX, new Vector3(0.35f, 0.2f, 0.3f), new Vector3(0.15f, 0.0f, 0.0f),
  48. new Quaternion(0.0f, 0.0f, 0.0f));
  49. CreateRagdollBone("Bip01_L_Thigh", ShapeType.SHAPE_CAPSULE, new Vector3(0.175f, 0.45f, 0.175f), new Vector3(0.25f, 0.0f, 0.0f),
  50. new Quaternion(0.0f, 0.0f, 90.0f));
  51. CreateRagdollBone("Bip01_R_Thigh", ShapeType.SHAPE_CAPSULE, new Vector3(0.175f, 0.45f, 0.175f), new Vector3(0.25f, 0.0f, 0.0f),
  52. new Quaternion(0.0f, 0.0f, 90.0f));
  53. CreateRagdollBone("Bip01_L_Calf", ShapeType.SHAPE_CAPSULE, new Vector3(0.15f, 0.55f, 0.15f), new Vector3(0.25f, 0.0f, 0.0f),
  54. new Quaternion(0.0f, 0.0f, 90.0f));
  55. CreateRagdollBone("Bip01_R_Calf", ShapeType.SHAPE_CAPSULE, new Vector3(0.15f, 0.55f, 0.15f), new Vector3(0.25f, 0.0f, 0.0f),
  56. new Quaternion(0.0f, 0.0f, 90.0f));
  57. CreateRagdollBone("Bip01_Head", ShapeType.SHAPE_BOX, new Vector3(0.2f, 0.2f, 0.2f), new Vector3(0.1f, 0.0f, 0.0f),
  58. new Quaternion(0.0f, 0.0f, 0.0f));
  59. CreateRagdollBone("Bip01_L_UpperArm", ShapeType.SHAPE_CAPSULE, new Vector3(0.15f, 0.35f, 0.15f), new Vector3(0.1f, 0.0f, 0.0f),
  60. new Quaternion(0.0f, 0.0f, 90.0f));
  61. CreateRagdollBone("Bip01_R_UpperArm", ShapeType.SHAPE_CAPSULE, new Vector3(0.15f, 0.35f, 0.15f), new Vector3(0.1f, 0.0f, 0.0f),
  62. new Quaternion(0.0f, 0.0f, 90.0f));
  63. CreateRagdollBone("Bip01_L_Forearm", ShapeType.SHAPE_CAPSULE, new Vector3(0.125f, 0.4f, 0.125f), new Vector3(0.2f, 0.0f, 0.0f),
  64. new Quaternion(0.0f, 0.0f, 90.0f));
  65. CreateRagdollBone("Bip01_R_Forearm", ShapeType.SHAPE_CAPSULE, new Vector3(0.125f, 0.4f, 0.125f), new Vector3(0.2f, 0.0f, 0.0f),
  66. new Quaternion(0.0f, 0.0f, 90.0f));
  67. Vector3 back = new Vector3(0f, 0f, -1f);
  68. Vector3 forward = new Vector3(0f, 0f, 1f);
  69. Vector3 left = new Vector3(-1f, 0f, 0f);
  70. Vector3 down = new Vector3(0f, -1f, 0f);
  71. Vector3 up = new Vector3(0f, 1f, 0f);
  72. // Create Constraints between bones
  73. CreateRagdollConstraint("Bip01_L_Thigh", "Bip01_Pelvis", ConstraintType.CONSTRAINT_CONETWIST, back, forward,
  74. new Vector2(45.0f, 45.0f), Vector2.Zero);
  75. CreateRagdollConstraint("Bip01_R_Thigh", "Bip01_Pelvis", ConstraintType.CONSTRAINT_CONETWIST, back, forward,
  76. new Vector2(45.0f, 45.0f), Vector2.Zero);
  77. CreateRagdollConstraint("Bip01_L_Calf", "Bip01_L_Thigh", ConstraintType.CONSTRAINT_HINGE, back, back,
  78. new Vector2(90.0f, 0.0f), Vector2.Zero);
  79. CreateRagdollConstraint("Bip01_R_Calf", "Bip01_R_Thigh", ConstraintType.CONSTRAINT_HINGE, back, back,
  80. new Vector2(90.0f, 0.0f), Vector2.Zero);
  81. CreateRagdollConstraint("Bip01_Spine1", "Bip01_Pelvis", ConstraintType.CONSTRAINT_HINGE, forward, forward,
  82. new Vector2(45.0f, 0.0f), new Vector2(-10.0f, 0.0f));
  83. CreateRagdollConstraint("Bip01_Head", "Bip01_Spine1", ConstraintType.CONSTRAINT_CONETWIST, left, left,
  84. new Vector2(0.0f, 30.0f), Vector2.Zero);
  85. CreateRagdollConstraint("Bip01_L_UpperArm", "Bip01_Spine1", ConstraintType.CONSTRAINT_CONETWIST, down, up,
  86. new Vector2(45.0f, 45.0f), Vector2.Zero, false);
  87. CreateRagdollConstraint("Bip01_R_UpperArm", "Bip01_Spine1", ConstraintType.CONSTRAINT_CONETWIST, down, up,
  88. new Vector2(45.0f, 45.0f), Vector2.Zero, false);
  89. CreateRagdollConstraint("Bip01_L_Forearm", "Bip01_L_UpperArm", ConstraintType.CONSTRAINT_HINGE, back, back,
  90. new Vector2(90.0f, 0.0f), Vector2.Zero);
  91. CreateRagdollConstraint("Bip01_R_Forearm", "Bip01_R_UpperArm", ConstraintType.CONSTRAINT_HINGE, back, back,
  92. new Vector2(90.0f, 0.0f), Vector2.Zero);
  93. // Disable keyframe animation from all bones so that they will not interfere with the ragdoll
  94. AnimatedModel model = GetComponent<AnimatedModel>();
  95. Skeleton skeleton = model.Skeleton;
  96. for (uint i = 0; i < skeleton.GetNumBones(); ++i)
  97. {
  98. skeleton.GetBone(i).Animated = false;
  99. }
  100. // Finally remove self from the scene node. Note that this must be the last operation performed in the function
  101. Remove();
  102. }
  103. }
  104. void CreateRagdollBone(string boneName, ShapeType type, Vector3 size, Vector3 position, Quaternion rotation)
  105. {
  106. // Find the correct child scene node recursively
  107. Node boneNode = Node.GetChild(boneName, true);
  108. if (boneNode == null)
  109. {
  110. Log.Warn($"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.Mass = 1.0f;
  116. // Set damping parameters to smooth out the motion
  117. body.LinearDamping = 0.05f;
  118. body.AngularDamping = 0.85f;
  119. // Set rest thresholds to ensure the ragdoll rigid bodies come to rest to not consume CPU endlessly
  120. body.LinearRestThreshold = 1.5f;
  121. body.AngularRestThreshold = 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 == ShapeType.SHAPE_BOX)
  125. shape.SetBox(size, position, rotation);
  126. else
  127. shape.SetCapsule(size.X, size.Y, position, rotation);
  128. }
  129. void CreateRagdollConstraint(string boneName, string parentName, ConstraintType type,
  130. Vector3 axis, Vector3 parentAxis, Vector2 highLimit, Vector2 lowLimit, bool disableCollision = true)
  131. {
  132. Node boneNode = Node.GetChild(boneName, true);
  133. Node parentNode = Node.GetChild(parentName, true);
  134. if (boneNode == null)
  135. {
  136. Log.Warn($"Could not find bone {boneName} for creating ragdoll constraint");
  137. return;
  138. }
  139. if (parentNode == null)
  140. {
  141. Log.Warn($"Could not find bone {parentName} for creating ragdoll constraint");
  142. return;
  143. }
  144. Constraint constraint = boneNode.CreateComponent<Constraint>();
  145. constraint.ConstraintType = type;
  146. // Most of the constraints in the ragdoll will work better when the connected bodies don't collide against each other
  147. constraint.DisableCollision = disableCollision;
  148. // The connected body must be specified before setting the world position
  149. constraint.OtherBody = parentNode.GetComponent<RigidBody>();
  150. // Position the constraint at the child bone we are connecting
  151. constraint.SetWorldPosition(boneNode.WorldPosition);
  152. // Configure axes and limits
  153. constraint.SetAxis(axis);
  154. constraint.SetOtherAxis(parentAxis);
  155. constraint.HighLimit = highLimit;
  156. constraint.LowLimit = lowLimit;
  157. }
  158. void HitEffect ( Node parent ) // ragdoll gets the donuts blown out of him
  159. {
  160. Random rand = new Random();
  161. var cache = GetSubsystem<ResourceCache>();
  162. Scene myscene = parent.GetScene ();
  163. Vector3 where = parent.GetWorldPosition ();
  164. for (int mm=0; mm<5; mm++)
  165. {
  166. Node xNode = myscene.CreateChild("Stuffing");
  167. xNode.SetPosition( new Vector3( (float)rand.Next(0,1), (float)rand.Next(0,4), (float)rand.Next(0,15)) + where );
  168. xNode.SetRotation( new Quaternion((float)rand.Next(0,360), 0, (float)rand.Next(0,360)));
  169. xNode.SetScale(0.3f);
  170. ParticleEmitter pEmitter = xNode.CreateComponent<ParticleEmitter>();
  171. ................pEmitter.SetEffect(cache.GetResource<ParticleEffect>("Particle/Fire.xml"));
  172. ................pEmitter.SetEnabled(true); // sparkle donuts.
  173. //create obj
  174. StaticModel boxObject = xNode.CreateComponent<StaticModel>();
  175. boxObject.Model = cache.Get<Model>("Models/Torus.mdl");
  176. boxObject.SetMaterial(cache.Get<Material>("Materials/Mushroom.xml"));
  177. boxObject.CastShadows = true;
  178. RigidBody body = xNode.CreateComponent<RigidBody>();
  179. body.Mass = 0.45f;
  180. body.RollingFriction = 0.15f;
  181. body.Friction = 0.75f;
  182. body.UseGravity = true;
  183. // Disable collision event signaling to reduce CPU load of the physics simulation a111
  184. body.SetCollisionEventMode( CollisionEventMode.COLLISION_NEVER );
  185. CollisionShape shape = xNode.CreateComponent<CollisionShape>();
  186. shape.SetSphere(1.0f, Vector3.Zero, Quaternion.Identity );
  187. float objectVelocity = 22.0f;
  188. //Node.Rotation
  189. body.SetLinearVelocity( new Quaternion((float)rand.Next(0,360), 0, (float)rand.Next(0,360))* new Vector3(0.0f, 0.02f, 1.0f) * objectVelocity);
  190. }
  191. }
  192. }
  193. }