vehicle_body.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /*************************************************************************/
  2. /* vehicle_body.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #ifndef VEHICLE_BODY_H
  31. #define VEHICLE_BODY_H
  32. #include "scene/3d/physics_body.h"
  33. class VehicleBody;
  34. class VehicleWheel : public Spatial {
  35. GDCLASS(VehicleWheel, Spatial);
  36. friend class VehicleBody;
  37. Transform m_worldTransform;
  38. Transform local_xform;
  39. bool engine_traction;
  40. bool steers;
  41. Vector3 m_chassisConnectionPointCS; //const
  42. Vector3 m_wheelDirectionCS; //const
  43. Vector3 m_wheelAxleCS; // const or modified by steering
  44. real_t m_suspensionRestLength;
  45. real_t m_maxSuspensionTravelCm;
  46. real_t m_wheelRadius;
  47. real_t m_suspensionStiffness;
  48. real_t m_wheelsDampingCompression;
  49. real_t m_wheelsDampingRelaxation;
  50. real_t m_frictionSlip;
  51. real_t m_maxSuspensionForce;
  52. bool m_bIsFrontWheel;
  53. VehicleBody *body;
  54. //btVector3 m_wheelAxleCS; // const or modified by steering ?
  55. real_t m_steering;
  56. real_t m_rotation;
  57. real_t m_deltaRotation;
  58. real_t m_rollInfluence;
  59. //real_t m_engineForce;
  60. real_t m_brake;
  61. real_t m_clippedInvContactDotSuspension;
  62. real_t m_suspensionRelativeVelocity;
  63. //calculated by suspension
  64. real_t m_wheelsSuspensionForce;
  65. real_t m_skidInfo;
  66. struct RaycastInfo {
  67. //set by raycaster
  68. Vector3 m_contactNormalWS; //contactnormal
  69. Vector3 m_contactPointWS; //raycast hitpoint
  70. real_t m_suspensionLength;
  71. Vector3 m_hardPointWS; //raycast starting point
  72. Vector3 m_wheelDirectionWS; //direction in worldspace
  73. Vector3 m_wheelAxleWS; // axle in worldspace
  74. bool m_isInContact;
  75. PhysicsBody *m_groundObject; //could be general void* ptr
  76. } m_raycastInfo;
  77. void _update(PhysicsDirectBodyState *s);
  78. protected:
  79. void _notification(int p_what);
  80. static void _bind_methods();
  81. public:
  82. void set_radius(float p_radius);
  83. float get_radius() const;
  84. void set_suspension_rest_length(float p_length);
  85. float get_suspension_rest_length() const;
  86. void set_suspension_travel(float p_length);
  87. float get_suspension_travel() const;
  88. void set_suspension_stiffness(float p_value);
  89. float get_suspension_stiffness() const;
  90. void set_suspension_max_force(float p_value);
  91. float get_suspension_max_force() const;
  92. void set_damping_compression(float p_value);
  93. float get_damping_compression() const;
  94. void set_damping_relaxation(float p_value);
  95. float get_damping_relaxation() const;
  96. void set_friction_slip(float p_value);
  97. float get_friction_slip() const;
  98. void set_use_as_traction(bool p_enable);
  99. bool is_used_as_traction() const;
  100. void set_use_as_steering(bool p_enabled);
  101. bool is_used_as_steering() const;
  102. bool is_in_contact() const;
  103. void set_roll_influence(float p_value);
  104. float get_roll_influence() const;
  105. float get_skidinfo() const;
  106. String get_configuration_warning() const;
  107. VehicleWheel();
  108. };
  109. class VehicleBody : public PhysicsBody {
  110. GDCLASS(VehicleBody, PhysicsBody);
  111. real_t mass;
  112. real_t friction;
  113. float engine_force;
  114. float brake;
  115. Vector3 linear_velocity;
  116. Vector3 angular_velocity;
  117. bool ccd;
  118. real_t m_pitchControl;
  119. real_t m_steeringValue;
  120. real_t m_currentVehicleSpeedKmHour;
  121. Set<RID> exclude;
  122. Vector<Vector3> m_forwardWS;
  123. Vector<Vector3> m_axle;
  124. Vector<real_t> m_forwardImpulse;
  125. Vector<real_t> m_sideImpulse;
  126. struct btVehicleWheelContactPoint {
  127. PhysicsDirectBodyState *m_s;
  128. PhysicsBody *m_body1;
  129. Vector3 m_frictionPositionWorld;
  130. Vector3 m_frictionDirectionWorld;
  131. real_t m_jacDiagABInv;
  132. real_t m_maxImpulse;
  133. btVehicleWheelContactPoint(PhysicsDirectBodyState *s, PhysicsBody *body1, const Vector3 &frictionPosWorld, const Vector3 &frictionDirectionWorld, real_t maxImpulse);
  134. };
  135. void _resolve_single_bilateral(PhysicsDirectBodyState *s, const Vector3 &pos1, PhysicsBody *body2, const Vector3 &pos2, const Vector3 &normal, real_t &impulse);
  136. real_t _calc_rolling_friction(btVehicleWheelContactPoint &contactPoint);
  137. void _update_friction(PhysicsDirectBodyState *s);
  138. void _update_suspension(PhysicsDirectBodyState *s);
  139. real_t _ray_cast(int p_idx, PhysicsDirectBodyState *s);
  140. void _update_wheel_transform(VehicleWheel &wheel, PhysicsDirectBodyState *s);
  141. void _update_wheel(int p_idx, PhysicsDirectBodyState *s);
  142. friend class VehicleWheel;
  143. Vector<VehicleWheel *> wheels;
  144. static void _bind_methods();
  145. void _direct_state_changed(Object *p_state);
  146. public:
  147. void set_mass(real_t p_mass);
  148. real_t get_mass() const;
  149. void set_friction(real_t p_friction);
  150. real_t get_friction() const;
  151. void set_engine_force(float p_engine_force);
  152. float get_engine_force() const;
  153. void set_brake(float p_brake);
  154. float get_brake() const;
  155. void set_steering(float p_steering);
  156. float get_steering() const;
  157. Vector3 get_linear_velocity() const;
  158. VehicleBody();
  159. };
  160. #endif // VEHICLE_BODY_H