Joint.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /******************************************************************************
  2. Use 'Joint' to link 2 actors together, or one actor to fixed position in world.
  3. /******************************************************************************/
  4. struct Spring
  5. {
  6. Flt spring, damping;
  7. Spring(Flt spring=0, Flt damping=0) {T.spring=spring; T.damping=damping;}
  8. };
  9. /******************************************************************************/
  10. struct Joint // a Joint between 2 actors, or one actor and fixed position in world
  11. {
  12. // manage
  13. Joint& del ( ); // delete manually
  14. Joint& create (Actor &a0, Actor *a1 ); // create default joint
  15. Joint& createHinge (Actor &a0, Actor *a1, C Vec &anchor , C Vec &axis , Bool collision=false); // create hinge joint, 'anchor'=world space anchor position, 'axis'=world space joint axis, 'collision'=if allow for collisions between 'a0' and 'a1'
  16. Joint& createHinge (Actor &a0, Actor *a1, C Vec &anchor , C Vec &axis , Flt min_angle , Flt max_angle , Bool collision=false); // create hinge joint, 'anchor'=world space anchor position, 'axis'=world space joint axis, 'collision'=if allow for collisions between 'a0' and 'a1'
  17. Joint& createSpherical(Actor &a0, Actor *a1, C Vec &anchor , C Vec &axis , Flt *swing=null , Flt *twist=null , Bool collision=false); // create spherical joint, 'anchor'=world space anchor position, 'axis'=world space joint axis, 'swing'=maximum allowed swing angle rotation (0..PI) null for no limits, 'twist'=maximum allowed twist angle rotation (0..PI) null for no limits
  18. Joint& createSliding (Actor &a0, Actor *a1, C Vec &anchor , C Vec &dir , Flt min, Flt max , Bool collision=false); // create sliding joint, 'anchor'=world space anchor position, 'dir' =world space sliding direction, 'min'=minumum allowed distance along 'dir' (-Inf..'max'), 'max'=maximum allowed distance along 'dir' ('min'..Inf)
  19. Joint& createDist (Actor &a0, Actor *a1, C Vec &anchor0, C Vec &anchor1, Flt min, Flt max, Spring *spring=null, Bool collision=false); // create distance based joint, here 'anchor0 anchor1' are in local spaces of actors
  20. Joint& createBodyHinge (Actor &bone, Actor &parent, C Vec &anchor, C Vec &axis, Flt min_angle, Flt max_angle); // create body hinge joint, 'anchor'=world space anchor position, 'axis'=world space joint axis, 'collision'=if allow for collisions between 'a0' and 'a1'
  21. Joint& createBodySpherical(Actor &bone, Actor &parent, C Vec &anchor, C Vec &axis, Flt swing , Flt twist ); // create body spherical joint, 'anchor'=world space anchor position, 'axis'=world space joint axis, 'swing'=maximum allowed swing angle rotation (0..PI), 'twist'=maximum allowed twist angle rotation (0..PI)
  22. // get / set
  23. Bool is ( )C {return _joint!=null;} // if created
  24. Bool broken ( )C; // if joint is broken
  25. Joint& breakable(Flt max_force, Flt max_torque) ; // set breakable parameters, if forces applied to the joint will exceed given parameters the joint will break (use <0 values for non-breakable joint)
  26. #if EE_PRIVATE
  27. Matrix localAnchor(Bool index)C; // get anchor in actor local space (index=which actor), MatrixIdentity on fail
  28. void changed();
  29. #endif
  30. // hinge joint specific (following methods are supported only on PhysX)
  31. Flt hingeAngle ()C; // get joint angle in range (-PI, PI]
  32. Flt hingeVel ()C; // get joint velocity
  33. Bool hingeDriveEnabled ()C; Joint& hingeDriveEnabled (Bool on ); // get/set if drive is enabled
  34. Bool hingeDriveFreeSpin ()C; Joint& hingeDriveFreeSpin (Bool on ); // get/set if the existing velocity is beyond the drive velocity, do not add force
  35. Flt hingeDriveVel ()C; Joint& hingeDriveVel (Flt vel ); // get/set drive target velocity, 0..Inf, default=0
  36. Flt hingeDriveForceLimit()C; Joint& hingeDriveForceLimit(Flt limit); // get/set the maximum torque the drive can exert, setting this to a very large value if 'hingeDriveVel' is also very large may cause unexpected results, 0..Inf, default=Inf
  37. Flt hingeDriveGearRatio ()C; Joint& hingeDriveGearRatio (Flt ratio); // get/set the gear ratio for the drive, when setting up the drive constraint, the velocity of the first actor is scaled by this value, and its response to drive torque is scaled down. So if the drive target velocity is zero, the second actor will be driven to the velocity of the first scaled by the gear ratio. 0..Inf, default=1
  38. // io
  39. Bool save(File &f )C; // save, does not save information about which actors are joined, false on fail
  40. Bool load(File &f, Actor &a0, Actor *a1) ; // load, 'a0' and 'a1' are actors which the joint should link, false on fail
  41. ~Joint() {del();}
  42. Joint() {_joint=null;}
  43. #if !EE_PRIVATE
  44. private:
  45. #endif
  46. #if EE_PRIVATE
  47. PHYS_API(PxJoint, btTypedConstraint) *_joint;
  48. #else
  49. Ptr _joint;
  50. #endif
  51. NO_COPY_CONSTRUCTOR(Joint);
  52. };
  53. /******************************************************************************/