vehicle_body.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /*************************************************************************/
  2. /* vehicle_body.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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 RigidBody {
  110. GDCLASS(VehicleBody, RigidBody);
  111. float engine_force;
  112. float brake;
  113. real_t m_pitchControl;
  114. real_t m_steeringValue;
  115. real_t m_currentVehicleSpeedKmHour;
  116. Set<RID> exclude;
  117. Vector<Vector3> m_forwardWS;
  118. Vector<Vector3> m_axle;
  119. Vector<real_t> m_forwardImpulse;
  120. Vector<real_t> m_sideImpulse;
  121. struct btVehicleWheelContactPoint {
  122. PhysicsDirectBodyState *m_s;
  123. PhysicsBody *m_body1;
  124. Vector3 m_frictionPositionWorld;
  125. Vector3 m_frictionDirectionWorld;
  126. real_t m_jacDiagABInv;
  127. real_t m_maxImpulse;
  128. btVehicleWheelContactPoint(PhysicsDirectBodyState *s, PhysicsBody *body1, const Vector3 &frictionPosWorld, const Vector3 &frictionDirectionWorld, real_t maxImpulse);
  129. };
  130. void _resolve_single_bilateral(PhysicsDirectBodyState *s, const Vector3 &pos1, PhysicsBody *body2, const Vector3 &pos2, const Vector3 &normal, real_t &impulse, const real_t p_rollInfluence);
  131. real_t _calc_rolling_friction(btVehicleWheelContactPoint &contactPoint);
  132. void _update_friction(PhysicsDirectBodyState *s);
  133. void _update_suspension(PhysicsDirectBodyState *s);
  134. real_t _ray_cast(int p_idx, PhysicsDirectBodyState *s);
  135. void _update_wheel_transform(VehicleWheel &wheel, PhysicsDirectBodyState *s);
  136. void _update_wheel(int p_idx, PhysicsDirectBodyState *s);
  137. friend class VehicleWheel;
  138. Vector<VehicleWheel *> wheels;
  139. static void _bind_methods();
  140. void _direct_state_changed(Object *p_state);
  141. public:
  142. void set_engine_force(float p_engine_force);
  143. float get_engine_force() const;
  144. void set_brake(float p_brake);
  145. float get_brake() const;
  146. void set_steering(float p_steering);
  147. float get_steering() const;
  148. VehicleBody();
  149. };
  150. #endif // VEHICLE_BODY_H