Ragdoll.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /******************************************************************************
  2. Use 'Ragdoll' to simplify management of character physical bones representation.
  3. It can automatically create bone 'Actors' linked with 'Joints' from a skeleton,
  4. and handle blending with the 'AnimatedSkeleton' animations.
  5. /******************************************************************************/
  6. struct Ragdoll // Physical Ragdoll, set of Bone Actors linked with Joints
  7. {
  8. struct Bone // ragdoll bone
  9. {
  10. Char8 name[32]; // name
  11. Actor actor ; // actor
  12. #if !EE_PRIVATE
  13. private:
  14. #endif
  15. Byte skel_bone, rbon_parent;
  16. };
  17. // manage
  18. Ragdoll& del ( ); // delete
  19. Ragdoll& create (C AnimatedSkeleton &anim_skel, Flt density=1, Bool kinematic=false); // create from animated skeleton, 'kinematic'=if create the bone actors as kinematic (additionally this will not create joints), Exit on fail
  20. Bool createTry(C AnimatedSkeleton &anim_skel, Flt density=1, Bool kinematic=false); // create from animated skeleton, 'kinematic'=if create the bone actors as kinematic (additionally this will not create joints), false on fail
  21. // operations
  22. Ragdoll& fromSkel (C AnimatedSkeleton &anim_skel, C Vec &vel=VecZero, Bool immediate_even_for_kinematic_ragdoll=false); // set ragdoll from skeleton , 'anim_skel' must have its matrixes updated, 'anim_skel' must be set to the same skeleton which ragdoll was created from, setting ragdoll bone matrixes is done using 'Actor::kinematicMoveTo' for kinematic ragdolls and 'Actor::matrix' for non kinematic ragdolls, however since 'Actor::kinematicMoveTo' doesn't set the matrixes immediately, you can set 'immediate_even_for_kinematic_ragdoll' to true, which will force setting the bone matrixes immediately using 'Actor::matrix' method.
  23. Ragdoll& toSkel ( AnimatedSkeleton &anim_skel ); // set skeleton from ragdoll , 'anim_skel' must be set to the same skeleton which ragdoll was created from
  24. Ragdoll& toSkelBlend( AnimatedSkeleton &anim_skel, Flt blend ); // blend ragdoll animations into skeleton, 'anim_skel' must have its matrixes updated, 'anim_skel' must be set to the same skeleton which ragdoll was created from
  25. // get / set
  26. Bool is()C {return _skel!=null;} // if created
  27. Int bones( )C {return _bones.elms();} // get number of bones
  28. Bone& bone (Int i) {return _bones[i] ;} // get i-th bone
  29. C Bone& bone (Int i)C {return _bones[i] ;} // get i-th bone
  30. Vec pos ()C; Ragdoll& pos (C Vec &pos ); // get/set ragdoll position, position is taken from the main bone
  31. Vec vel ()C; Ragdoll& vel (C Vec &vel ); // get/set ragdoll velocity, velocity is taken from the main bone, however setting velocity applies to all ragdoll bones equally
  32. Flt damping ()C; Ragdoll& damping (Flt damping ); // get/set linear damping, 0..Inf, default=0.05
  33. Flt adamping ()C; Ragdoll& adamping (Flt damping ); // get/set angular damping, 0..Inf, default=0.05
  34. Bool kinematic ()C; Ragdoll& kinematic (Bool on ); // get/set if kinematic, only dynamic actors (with mass!=0) can be changed into kinematic actors
  35. Bool gravity ()C; Ragdoll& gravity (Bool on ); // get/set if gravity is enabled for this ragdoll
  36. Bool ray ()C; Ragdoll& ray (Bool on ); // get/set if this ragdoll should be included when performing ray tests
  37. Bool collision ()C; Ragdoll& collision (Bool on ); // get/set if this ragdoll should collide with other actors in the world
  38. Bool sleep ()C; Ragdoll& sleep (Bool sleep ); // get/set sleeping
  39. Flt sleepEnergy()C; Ragdoll& sleepEnergy(Flt energy ); // get/set the amount of energy below the ragdoll is put to sleep, default=0.1
  40. Bool ccd ()C; Ragdoll& ccd (Bool on ); // get/set continuous collision detection
  41. Ptr user ()C; Ragdoll& user (Ptr user ); // get/set user data
  42. Ptr obj ()C; Ragdoll& obj (Ptr obj ); // get/set pointer to object containing the ragdoll
  43. Byte group ()C; Ragdoll& group (Byte group ); // get/set collision group (0..31, ACTOR_GROUP)
  44. Byte dominance ()C; Ragdoll& dominance (Byte dominance); // get/set dominance index (0..31, default=0), for more information about dominance please check comments on 'Physics.dominance' method
  45. PhysMtrl* material ()C; Ragdoll& material (PhysMtrl *material ); // get/set physics material (use 'null' for default material)
  46. Ragdoll& active(Bool on ); // set if active by calling 'Actor::active' on all ragdoll bone actors
  47. Ragdoll& ignore(Actor &actor, Bool ignore=true); // ignore collisions with 'actor'
  48. Int findBoneI(CChar8 *name); // find ragdoll bone index, -1 on fail
  49. Bone* findBone (CChar8 *name); // find ragdoll bone , null on fail
  50. Int getBoneI(CChar8 *name); // get ragdoll bone index, Exit on fail
  51. Bone& getBone (CChar8 *name); // get ragdoll bone , Exit on fail
  52. Int findBoneIndexFromSkelBone (Byte skel_bone_index)C; // find ragdoll bone index, from skeleton bone index, -1 on fail
  53. Int findBoneIndexFromVtxMatrix(Byte matrix_index)C; // find ragdoll bone index, from vertex matrix index, -1 on fail
  54. // draw
  55. void draw(C Color &color=WHITE)C; // this can be optionally called outside of Render function
  56. // io
  57. Bool saveState(File &f, Bool include_matrix_vel=true)C; // save ragdoll state (following data is not saved: physical body, mass, density, scale, damping, max ang vel, mass center, inertia, material), false on fail, 'include_matrix_vel'=include current bone matrixes and velocities
  58. Bool loadState(File &f ) ; // load ragdoll state (following data is not loaded: physical body, mass, density, scale, damping, max ang vel, mass center, inertia, material), false on fail, typically you should first create a Ragdoll and then call this method to set its state according to data from the file
  59. #if EE_PRIVATE
  60. void zero();
  61. #endif
  62. ~Ragdoll() {del();}
  63. Ragdoll();
  64. #if !EE_PRIVATE
  65. private:
  66. #endif
  67. Flt _scale ;
  68. C Skeleton *_skel ;
  69. Mems<Bone > _bones ;
  70. Memc<Int > _resets;
  71. Memc<Joint> _joints;
  72. Aggregate _aggr ;
  73. };
  74. /******************************************************************************/
  75. inline Int Elms(C Ragdoll &ragdoll) {return ragdoll.bones();}
  76. /******************************************************************************/