vehicle_body_3d.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /**************************************************************************/
  2. /* vehicle_body_3d.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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_3D_H
  31. #define VEHICLE_BODY_3D_H
  32. #include "scene/3d/physics/physics_body_3d.h"
  33. #include "scene/3d/physics/rigid_body_3d.h"
  34. class VehicleBody3D;
  35. class VehicleWheel3D : public Node3D {
  36. GDCLASS(VehicleWheel3D, Node3D);
  37. friend class VehicleBody3D;
  38. Transform3D m_worldTransform;
  39. Transform3D local_xform;
  40. bool engine_traction = false;
  41. bool steers = false;
  42. Vector3 m_chassisConnectionPointCS; //const
  43. Vector3 m_wheelDirectionCS; //const
  44. Vector3 m_wheelAxleCS; // const or modified by steering
  45. real_t m_suspensionRestLength = 0.15;
  46. real_t m_maxSuspensionTravel = 0.2;
  47. real_t m_wheelRadius = 0.5;
  48. real_t m_suspensionStiffness = 5.88;
  49. real_t m_wheelsDampingCompression = 0.83;
  50. real_t m_wheelsDampingRelaxation = 0.88;
  51. real_t m_frictionSlip = 10.5;
  52. real_t m_maxSuspensionForce = 6000.0;
  53. bool m_bIsFrontWheel = false;
  54. VehicleBody3D *body = nullptr;
  55. //btVector3 m_wheelAxleCS; // const or modified by steering ?
  56. real_t m_steering = 0.0;
  57. real_t m_rotation = 0.0;
  58. real_t m_deltaRotation = 0.0;
  59. real_t m_rpm = 0.0;
  60. real_t m_rollInfluence = 0.1;
  61. real_t m_engineForce = 0.0;
  62. real_t m_brake = 0.0;
  63. real_t m_clippedInvContactDotSuspension = 1.0;
  64. real_t m_suspensionRelativeVelocity = 0.0;
  65. //calculated by suspension
  66. real_t m_wheelsSuspensionForce = 0.0;
  67. real_t m_skidInfo = 0.0;
  68. struct RaycastInfo {
  69. //set by raycaster
  70. Vector3 m_contactNormalWS; //contactnormal
  71. Vector3 m_contactPointWS; //raycast hitpoint
  72. real_t m_suspensionLength = 0.0;
  73. Vector3 m_hardPointWS; //raycast starting point
  74. Vector3 m_wheelDirectionWS; //direction in worldspace
  75. Vector3 m_wheelAxleWS; // axle in worldspace
  76. bool m_isInContact = false;
  77. PhysicsBody3D *m_groundObject = nullptr; //could be general void* ptr
  78. } m_raycastInfo;
  79. void _update(PhysicsDirectBodyState3D *s);
  80. protected:
  81. void _notification(int p_what);
  82. static void _bind_methods();
  83. public:
  84. void set_radius(real_t p_radius);
  85. real_t get_radius() const;
  86. void set_suspension_rest_length(real_t p_length);
  87. real_t get_suspension_rest_length() const;
  88. void set_suspension_travel(real_t p_length);
  89. real_t get_suspension_travel() const;
  90. void set_suspension_stiffness(real_t p_value);
  91. real_t get_suspension_stiffness() const;
  92. void set_suspension_max_force(real_t p_value);
  93. real_t get_suspension_max_force() const;
  94. void set_damping_compression(real_t p_value);
  95. real_t get_damping_compression() const;
  96. void set_damping_relaxation(real_t p_value);
  97. real_t get_damping_relaxation() const;
  98. void set_friction_slip(real_t p_value);
  99. real_t get_friction_slip() const;
  100. void set_use_as_traction(bool p_enable);
  101. bool is_used_as_traction() const;
  102. void set_use_as_steering(bool p_enabled);
  103. bool is_used_as_steering() const;
  104. bool is_in_contact() const;
  105. Vector3 get_contact_point() const;
  106. Vector3 get_contact_normal() const;
  107. Node3D *get_contact_body() const;
  108. void set_roll_influence(real_t p_value);
  109. real_t get_roll_influence() const;
  110. real_t get_skidinfo() const;
  111. real_t get_rpm() const;
  112. void set_engine_force(real_t p_engine_force);
  113. real_t get_engine_force() const;
  114. void set_brake(real_t p_brake);
  115. real_t get_brake() const;
  116. void set_steering(real_t p_steering);
  117. real_t get_steering() const;
  118. PackedStringArray get_configuration_warnings() const override;
  119. VehicleWheel3D();
  120. };
  121. class VehicleBody3D : public RigidBody3D {
  122. GDCLASS(VehicleBody3D, RigidBody3D);
  123. real_t engine_force = 0.0;
  124. real_t brake = 0.0;
  125. real_t m_pitchControl = 0.0;
  126. real_t m_steeringValue = 0.0;
  127. real_t m_currentVehicleSpeedKmHour = 0.0;
  128. HashSet<RID> exclude;
  129. Vector<Vector3> m_forwardWS;
  130. Vector<Vector3> m_axle;
  131. Vector<real_t> m_forwardImpulse;
  132. Vector<real_t> m_sideImpulse;
  133. struct btVehicleWheelContactPoint {
  134. PhysicsDirectBodyState3D *m_s = nullptr;
  135. PhysicsBody3D *m_body1 = nullptr;
  136. Vector3 m_frictionPositionWorld;
  137. Vector3 m_frictionDirectionWorld;
  138. real_t m_jacDiagABInv = 0.0;
  139. real_t m_maxImpulse = 0.0;
  140. btVehicleWheelContactPoint(PhysicsDirectBodyState3D *s, PhysicsBody3D *body1, const Vector3 &frictionPosWorld, const Vector3 &frictionDirectionWorld, real_t maxImpulse);
  141. };
  142. void _resolve_single_bilateral(PhysicsDirectBodyState3D *s, const Vector3 &pos1, PhysicsBody3D *body2, const Vector3 &pos2, const Vector3 &normal, real_t &impulse, const real_t p_rollInfluence);
  143. real_t _calc_rolling_friction(btVehicleWheelContactPoint &contactPoint);
  144. void _update_friction(PhysicsDirectBodyState3D *s);
  145. void _update_suspension(PhysicsDirectBodyState3D *s);
  146. real_t _ray_cast(int p_idx, PhysicsDirectBodyState3D *s);
  147. void _update_wheel_transform(VehicleWheel3D &wheel, PhysicsDirectBodyState3D *s);
  148. void _update_wheel(int p_idx, PhysicsDirectBodyState3D *s);
  149. friend class VehicleWheel3D;
  150. Vector<VehicleWheel3D *> wheels;
  151. static void _bind_methods();
  152. static void _body_state_changed_callback(void *p_instance, PhysicsDirectBodyState3D *p_state);
  153. virtual void _body_state_changed(PhysicsDirectBodyState3D *p_state) override;
  154. public:
  155. void set_engine_force(real_t p_engine_force);
  156. real_t get_engine_force() const;
  157. void set_brake(real_t p_brake);
  158. real_t get_brake() const;
  159. void set_steering(real_t p_steering);
  160. real_t get_steering() const;
  161. VehicleBody3D();
  162. };
  163. #endif // VEHICLE_BODY_3D_H