Controller.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. /******************************************************************************
  2. Use 'Controller' to simplify management of character 'Actor' (a physical object).
  3. It can handle basic movement, crouching, jumping and flying.
  4. /******************************************************************************/
  5. struct Controller // Character Controller
  6. {
  7. Bool fall_control ; // if have full control over movement when falling , default=false
  8. Flt height_crouched, // desired height when crouched , default="height*0.67" (set in create methods)
  9. step_height ; // max step height on which the Controller can climb, default="radius" (set in create methods)
  10. Actor actor ; // actor, has default group set according to ACTOR_GROUP
  11. // get
  12. Bool onGround ()C {return _on_ground ;} // if character is on ground
  13. Bool crouched ()C {return _crouched ;} // if character is crouched
  14. Bool flying ()C {return _flying ;} // if character is in flying mode
  15. Flt radius ()C {return _radius ;} // character radius
  16. Flt height ()C {return _height ;} // character height
  17. Flt heightCur ()C {return _height_cur ;} // current character height (this can be affected by crouching/standing)
  18. Flt timeInAir ()C {return _time_in_air ;} // get current amount of time while in air, as soon as player stands on ground this value is reset to zero
  19. C Plane& groundPlane()C {return _ground_plane ;} // ground contact plane, this is valid when character is on ground
  20. C Vec & shapeOffset()C {return _shape_offset ;} // get capsule shape offset in the actor local space , crouching does not affect the result of this method
  21. Vec center ()C {return actor.pos()+_shape_offset;} // get controller center (this is the center of actor's capsule in world space), crouching does not affect the result of this method
  22. // manage
  23. Controller& del ( ); // delete manually
  24. Controller& createCapsule(Flt radius, Flt height, C Vec &pos, C Vec *anchor=null); // create from capsule
  25. Controller& create (C Capsule &capsule , C Vec *anchor=null) {return createCapsule(capsule.r, capsule.h, capsule.pos, anchor);} // create from capsule shape, here 'capsule.up' vector is ignored, because the Controller requires that the capsule up vector is always set to Vec(0,1,0)
  26. // operations
  27. void flying(Bool on ); // set if flying mode enabled
  28. void radius(Flt radius ); // set actor capsule radius
  29. void update(C Vec &velocity, Bool crouch, Flt jump); // update, 'velocity'=wanted velocity, 'crouch'=if want to crouch, 'jump'=jumping velocity !! don't call between Physics.startSimulation and Physics.stopSimulation !!
  30. // io
  31. Bool save(File &f)C; // save, false on fail
  32. Bool load(File &f) ; // load, false on fail
  33. Controller();
  34. #if !EE_PRIVATE
  35. private:
  36. #endif
  37. Bool _on_ground, _crouched, _jumping, _flying;
  38. Flt _radius, _height, _height_cur, _time_in_air, _time_jump;
  39. Vec _vel_prev, _step_normal, _shape_offset;
  40. Plane _ground_plane;
  41. #if EE_PRIVATE
  42. void capsuleHeight(Flt height);
  43. void zero();
  44. #endif
  45. };
  46. /******************************************************************************/