Vehicle.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /******************************************************************************
  2. Use 'Vehicle' to create vehicle type physical actor (like car).
  3. /******************************************************************************/
  4. enum WHEEL_TYPE
  5. {
  6. WHEEL_LEFT_FRONT , // left front wheel
  7. WHEEL_RIGHT_FRONT, // right front wheel
  8. WHEEL_LEFT_REAR , // left rear wheel
  9. WHEEL_RIGHT_REAR , // right rear wheel
  10. WHEEL_NUM , // number of wheels
  11. };
  12. inline Bool IsFront(WHEEL_TYPE wheel) {return wheel<=WHEEL_RIGHT_FRONT;} // if wheel type is front wheel
  13. inline Bool IsRear (WHEEL_TYPE wheel) {return wheel>=WHEEL_LEFT_REAR ;} // if wheel type is rear wheel
  14. inline Bool IsLeft (WHEEL_TYPE wheel) {return !(wheel&1) ;} // if wheel type is left wheel
  15. inline Bool IsRight(WHEEL_TYPE wheel) {return (wheel&1) ;} // if wheel type is right wheel
  16. /******************************************************************************/
  17. const_mem_addr struct Vehicle // Physical actor of vehicle type !! must be stored in constant memory address !!
  18. {
  19. enum WHEEL_DRIVE : Byte
  20. {
  21. AWD, // all wheel drive (also known as 4WD four wheel drive)
  22. FWD, // front wheel drive
  23. RWD, // rear wheel drive
  24. };
  25. struct Wheel
  26. {
  27. Flt radius, // wheel radius, default=0.35 m
  28. width ; // wheel width , default=0.25 m
  29. Vec pos ; // wheel position in vehicle
  30. void set(Flt radius, Flt width, C Vec &pos) {T.radius=radius; T.width=width; T.pos=pos;}
  31. Wheel() {set(0.35f, 0.25f, VecZero);}
  32. };
  33. struct Params // all of these parameters (except masses) will be scaled according to 'scale' during vehicle creation
  34. {
  35. Wheel wheel[WHEEL_NUM]; // parameters for each wheel
  36. };
  37. // manage
  38. Vehicle& del (); // manually delete
  39. Bool createTry(C PhysBody &body, C Params &params, Flt density=1, Flt scale=1); // create from 'body' physical body, 'params' parameters, 'density' density and 'scale' scaling, false on fail
  40. Vehicle& create (C PhysBody &body, C Params &params, Flt density=1, Flt scale=1); // create from 'body' physical body, 'params' parameters, 'density' density and 'scale' scaling, Exit on fail
  41. // get / set
  42. Bool is()C; // if created
  43. Int onGroundNum()C; // get number of weels on ground
  44. Bool onGroundAny()C; // if vehicle is on ground (at least one wheel on ground)
  45. Bool onGroundAll()C; // if vehicle is on ground (all wheels on ground)
  46. Bool onGround(WHEEL_TYPE wheel)C; // if wheel is on ground
  47. #if EE_PRIVATE
  48. Vec wheelAxis (WHEEL_TYPE wheel)C; // get wheel rotation axis (X)
  49. Vec wheelDir (WHEEL_TYPE wheel)C; // get wheel direction axis (Z)
  50. #endif
  51. Matrix wheelMatrix (WHEEL_TYPE wheel, Int flip_side=-1)C; // get wheel matrix in world space, 'flip_side'=if flip/mirror the wheel matrix (-1=flip left wheels only, 0=don't flip, 1=flip right wheels only)
  52. Vec wheelVel (WHEEL_TYPE wheel)C; // get wheel velocity in world space
  53. Vec wheelAngVel (WHEEL_TYPE wheel)C; // get wheel angular velocity in world space
  54. Vec wheelContact (WHEEL_TYPE wheel)C; // get wheel last contact point with the ground, Vec(0,0,0) if never was on ground
  55. Vec wheelContactN(WHEEL_TYPE wheel)C; // get wheel last contact point normal with the ground, Vec(0,1,0) if never was on ground
  56. Flt wheelRadius (WHEEL_TYPE wheel)C; // get wheel radius
  57. Flt wheelCompress(WHEEL_TYPE wheel)C; // get wheel compression, value <0 means wheel is loose in air, value 0 means wheel is at rest, value 1 means wheel is fully compressed
  58. #if EE_PRIVATE
  59. Flt wheelLongSlip(WHEEL_TYPE wheel)C; // get wheel longitudinal slip (how much does the wheel slip on ground in "forward" direction)
  60. Flt wheelLatSlip (WHEEL_TYPE wheel)C; // get wheel lateral slip (how much does the wheel slip on ground in "side " direction)
  61. #endif
  62. Flt speed()C; // get vehicle forward speed (this value is positive when moving forward and negative when moving backwards)
  63. Flt sideSpeed()C; // get vehicle side speed (this value is positive when moving right and negative when moving left )
  64. Flt accel()C; Vehicle& accel(Flt accel ); // get/set acceleration , -1..1
  65. Flt brake()C; Vehicle& brake(Flt brake ); // get/set current brake , 0..1
  66. Flt angle()C; Vehicle& angle(Flt angle ); // get/set current steer angle , -1..1
  67. Flt maxAccel()C; Vehicle& maxAccel(Flt accel ); // get/set max acceleration force, 0..Inf , default=10
  68. Flt maxBackAccel()C; Vehicle& maxBackAccel(Flt accel ); // get/set max back acceleration force, 0..Inf , default=4.5
  69. Flt maxBrake()C; Vehicle& maxBrake(Flt brake ); // get/set max brake force, 0..Inf , default=12
  70. Flt brakeRatio()C; Vehicle& brakeRatio(Flt ratio ); // get/set front/rear brake ratio, 0..1 , default=0.43, this parameter specifies how much of the braking goes into front wheels, rear wheels will have a value "1-ratio" (for example if front has 0.6, then rear will have 0.4)
  71. Flt maxAngle()C; Vehicle& maxAngle(Flt angle ); // get/set max steer angle , 0..PI_2, default=PI_3
  72. Flt frictionLinear()C; Vehicle& frictionLinear(Flt friction); // get/set wheel linear fricion, 0..Inf , default=13 , this parameter specifies the linear portion of the friction when moving sideways, side velocity will be decreased by this value by subtraction
  73. Flt frictionScalar()C; Vehicle& frictionScalar(Flt friction); // get/set wheel scalar fricion, 0..1 , default=0.3 , this parameter specifies the scalar portion of the friction when moving sideways, side velocity will be decreased by this value by multiplication
  74. Flt suspSpring ()C; Vehicle& suspSpring (Flt spring ); // get/set suspension spring strength, 0..Inf , default=20 , this parameter specifies bounciness strength of the suspension
  75. Flt suspDamping ()C; Vehicle& suspDamping (Flt damping ); // get/set suspension spring damping , 0..Inf , default=0.7 , this parameter specifies how quickly does the suspension stabilize
  76. Flt suspCompress()C; Vehicle& suspCompress(Flt compress); // get/set suspension max compression, 0..1 , default=0.75 (1.0 means full wheel radius)
  77. WHEEL_DRIVE wheelDrive()C; Vehicle& wheelDrive(WHEEL_DRIVE wd); // get/set wheel drive , default=WHEEL_AWD
  78. Flt energy ( )C; // get kinetic energy , 0..Inf
  79. Flt damping ( )C; Vehicle& damping ( Flt damping); // get/set linear damping, 0..Inf, default=0.05
  80. Flt adamping ( )C; Vehicle& adamping ( Flt damping); // get/set angular damping, 0..Inf, default=0.05
  81. Flt mass ( )C; Vehicle& mass ( Flt mass ); // get/set mass , 0..Inf
  82. Vec massCenterL( )C; Vehicle& massCenterL(C Vec &center ); // get/set mass center in actor local space
  83. Vec massCenterW( )C; Vehicle& massCenterW(C Vec &center ); // get/set mass center in world space
  84. Vec inertia ( )C; Vehicle& inertia (C Vec &inertia); // get/set inertia tensor
  85. Vec pos ( )C; Vehicle& pos (C Vec &pos ); // get/set position
  86. Matrix3 orn ( )C; Vehicle& orn (C Matrix3 &orn ); // get/set orientation, 'orn' must be normalized
  87. Matrix matrix ( )C; Vehicle& matrix (C Matrix &matrix ); // get/set matrix , 'matrix' must be normalized
  88. Vec vel ( )C; Vehicle& vel (C Vec &vel ); // get/set velocity
  89. Vec angVel ( )C; Vehicle& angVel (C Vec &vel ); // get/set angular velocity
  90. Vec pointVelL (C Vec &pos)C; // get point velocity ('pos' is in actor local space)
  91. Vec pointVelW (C Vec &pos)C; // get point velocity ('pos' is in world space)
  92. Box box ( )C; // get bounding box in world space
  93. Flt scale ( )C {return _scale;} // get scale that was used during vehicle creation
  94. Vehicle& addTorque (C Vec &torque ); // add torque , unit = mass * rotation / time**2
  95. Vehicle& addAngVel (C Vec &ang_vel ); // add angular velocity , unit = rotation / time
  96. Vehicle& addForce (C Vec &force ); // add force , unit = mass * distance / time**2
  97. Vehicle& addForce (C Vec &force , C Vec &pos); // add force at world 'pos' position, unit = mass * distance / time**2
  98. Vehicle& addImpulse(C Vec &impulse ); // add impulse , unit = mass * distance / time
  99. Vehicle& addImpulse(C Vec &impulse, C Vec &pos); // add impulse at world 'pos' position, unit = mass * distance / time
  100. Vehicle& addVel (C Vec &vel ); // add velocity , unit = distance / time
  101. Vehicle& addAccel (C Vec &accel ); // add acceleration , unit = distance / time**2
  102. Bool gravity ()C; Vehicle& gravity (Bool on ); // get/set if gravity is enabled for this vehicle
  103. Bool ray ()C; Vehicle& ray (Bool on ); // get/set if this actor should be included when performing ray tests
  104. Bool collision ()C; Vehicle& collision (Bool on ); // get/set if this actor should collide with other actors in the world
  105. Bool sleep ()C; Vehicle& sleep (Bool sleep ); // get/set sleeping
  106. Flt sleepEnergy()C; Vehicle& sleepEnergy(Flt energy ); // get/set the amount of energy below the actor is put to sleep, default=0.005
  107. Bool ccd ()C; Vehicle& ccd (Bool on ); // get/set continuous collision detection
  108. Ptr user ()C; Vehicle& user (Ptr user ); // get/set custom user data
  109. Ptr obj ()C; Vehicle& obj (Ptr obj ); // get/set pointer to object containing the actor
  110. Byte group ()C; Vehicle& group (Byte group ); // get/set collision group (0..31, default value is taken according to ACTOR_GROUP)
  111. Byte dominance ()C; Vehicle& dominance (Byte dominance); // get/set dominance index (0..31, default=0), for more information about dominance please check comments on 'Physics.dominance' method
  112. PhysMtrl* material ()C; Vehicle& material (PhysMtrl *material ); // get/set physics material for vehicle (use 'null' for default material)
  113. // io
  114. Bool saveState(File &f)C; // save vehicle state (following data is not saved: physical body, mass, density, scale, damping, max ang vel, mass center, inertia, material), false on fail
  115. Bool loadState(File &f) ; // load vehicle 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 vehicle from a physical body and then call this method to set its state according to data from the file
  116. #if EE_PRIVATE
  117. void zero ();
  118. void update();
  119. #endif
  120. ~Vehicle() {del();}
  121. Vehicle();
  122. #if !EE_PRIVATE
  123. private:
  124. #endif
  125. struct _Wheel
  126. {
  127. Flt radius, compress, angle, angle_vel;
  128. Vec pos;
  129. Plane contact;
  130. };
  131. WHEEL_DRIVE _wd;
  132. Flt _accel, _brake, _angle, _max_accel, _max_back_accel, _max_brake, _brake_ratio, _max_angle, _fric_linear, _fric_scalar, _susp_spring, _susp_damping, _susp_compress, _scale, _avg_wheel_dist;
  133. _Wheel _wheel[WHEEL_NUM];
  134. Actor _actor;
  135. };
  136. /******************************************************************************/