| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
#pragma once |
| 32 |
|
| 33 |
#include "scene/3d/physics/physics_body_3d.h" |
| 34 |
#include "scene/3d/physics/rigid_body_3d.h" |
| 35 |
|
| 36 |
class VehicleBody3D; |
| 37 |
|
| 38 |
class VehicleWheel3D : public Node3D { |
| 39 |
GDCLASS(VehicleWheel3D, Node3D); |
| 40 |
|
| 41 |
friend class VehicleBody3D; |
| 42 |
|
| 43 |
struct WheelXform { |
| 44 |
Vector3 up; |
| 45 |
Vector3 right; |
| 46 |
Vector3 origin; |
| 47 |
real_t steering = 0; |
| 48 |
}; |
| 49 |
|
| 50 |
class FTIData { |
| 51 |
real_t curr_rotation = 0; |
| 52 |
real_t curr_rotation_delta = 0; |
| 53 |
|
| 54 |
public: |
| 55 |
WheelXform curr; |
| 56 |
WheelXform prev; |
| 57 |
|
| 58 |
|
| 59 |
bool unset = true; |
| 60 |
bool reset_queued = false; |
| 61 |
|
| 62 |
void rotate(real_t p_delta) { |
| 63 |
curr_rotation += p_delta; |
| 64 |
curr_rotation_delta = p_delta; |
| 65 |
|
| 66 |
|
| 67 |
double wrapped = Math::fmod(curr_rotation + Math::PI, Math::TAU); |
| 68 |
if (wrapped < 0) { |
| 69 |
wrapped += Math::TAU; |
| 70 |
} |
| 71 |
curr_rotation = wrapped - Math::PI; |
| 72 |
} |
| 73 |
|
| 74 |
void pump() { |
| 75 |
prev = curr; |
| 76 |
curr_rotation_delta = 0; |
| 77 |
} |
| 78 |
void update_world_xform(Transform3D &r_xform, real_t p_interpolation_fraction); |
| 79 |
} fti_data; |
| 80 |
|
| 81 |
Transform3D m_worldTransform; |
| 82 |
Transform3D local_xform; |
| 83 |
bool engine_traction = false; |
| 84 |
bool steers = false; |
| 85 |
|
| 86 |
Vector3 m_chassisConnectionPointCS; |
| 87 |
Vector3 m_wheelDirectionCS; |
| 88 |
Vector3 m_wheelAxleCS; |
| 89 |
|
| 90 |
real_t m_suspensionRestLength = 0.15; |
| 91 |
real_t m_maxSuspensionTravel = 0.2; |
| 92 |
real_t m_wheelRadius = 0.5; |
| 93 |
|
| 94 |
real_t m_suspensionStiffness = 5.88; |
| 95 |
real_t m_wheelsDampingCompression = 0.83; |
| 96 |
real_t m_wheelsDampingRelaxation = 0.88; |
| 97 |
real_t m_frictionSlip = 10.5; |
| 98 |
real_t m_maxSuspensionForce = 6000.0; |
| 99 |
bool m_bIsFrontWheel = false; |
| 100 |
|
| 101 |
VehicleBody3D *body = nullptr; |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
real_t m_steering = 0.0; |
| 106 |
real_t m_rotation = 0.0; |
| 107 |
real_t m_deltaRotation = 0.0; |
| 108 |
real_t m_rpm = 0.0; |
| 109 |
real_t m_rollInfluence = 0.1; |
| 110 |
real_t m_engineForce = 0.0; |
| 111 |
real_t m_brake = 0.0; |
| 112 |
|
| 113 |
real_t m_clippedInvContactDotSuspension = 1.0; |
| 114 |
real_t m_suspensionRelativeVelocity = 0.0; |
| 115 |
|
| 116 |
real_t m_wheelsSuspensionForce = 0.0; |
| 117 |
real_t m_skidInfo = 0.0; |
| 118 |
|
| 119 |
struct RaycastInfo { |
| 120 |
|
| 121 |
Vector3 m_contactNormalWS; |
| 122 |
Vector3 m_contactPointWS; |
| 123 |
real_t m_suspensionLength = 0.0; |
| 124 |
Vector3 m_hardPointWS; |
| 125 |
Vector3 m_wheelDirectionWS; |
| 126 |
Vector3 m_wheelAxleWS; |
| 127 |
bool m_isInContact = false; |
| 128 |
PhysicsBody3D *m_groundObject = nullptr; |
| 129 |
} m_raycastInfo; |
| 130 |
|
| 131 |
void _update(PhysicsDirectBodyState3D *s); |
| 132 |
|
| 133 |
protected: |
| 134 |
void _notification(int p_what); |
| 135 |
static void _bind_methods(); |
| 136 |
|
| 137 |
public: |
| 138 |
void set_radius(real_t p_radius); |
| 139 |
real_t get_radius() const; |
| 140 |
|
| 141 |
void set_suspension_rest_length(real_t p_length); |
| 142 |
real_t get_suspension_rest_length() const; |
| 143 |
|
| 144 |
void set_suspension_travel(real_t p_length); |
| 145 |
real_t get_suspension_travel() const; |
| 146 |
|
| 147 |
void set_suspension_stiffness(real_t p_value); |
| 148 |
real_t get_suspension_stiffness() const; |
| 149 |
|
| 150 |
void set_suspension_max_force(real_t p_value); |
| 151 |
real_t get_suspension_max_force() const; |
| 152 |
|
| 153 |
void set_damping_compression(real_t p_value); |
| 154 |
real_t get_damping_compression() const; |
| 155 |
|
| 156 |
void set_damping_relaxation(real_t p_value); |
| 157 |
real_t get_damping_relaxation() const; |
| 158 |
|
| 159 |
void set_friction_slip(real_t p_value); |
| 160 |
real_t get_friction_slip() const; |
| 161 |
|
| 162 |
void set_use_as_traction(bool p_enable); |
| 163 |
bool is_used_as_traction() const; |
| 164 |
|
| 165 |
void set_use_as_steering(bool p_enabled); |
| 166 |
bool is_used_as_steering() const; |
| 167 |
|
| 168 |
bool is_in_contact() const; |
| 169 |
|
| 170 |
Vector3 get_contact_point() const; |
| 171 |
|
| 172 |
Vector3 get_contact_normal() const; |
| 173 |
|
| 174 |
Node3D *get_contact_body() const; |
| 175 |
|
| 176 |
void set_roll_influence(real_t p_value); |
| 177 |
real_t get_roll_influence() const; |
| 178 |
|
| 179 |
real_t get_skidinfo() const; |
| 180 |
|
| 181 |
real_t get_rpm() const; |
| 182 |
|
| 183 |
void set_engine_force(real_t p_engine_force); |
| 184 |
real_t get_engine_force() const; |
| 185 |
|
| 186 |
void set_brake(real_t p_brake); |
| 187 |
real_t get_brake() const; |
| 188 |
|
| 189 |
void set_steering(real_t p_steering); |
| 190 |
real_t get_steering() const; |
| 191 |
|
| 192 |
PackedStringArray get_configuration_warnings() const override; |
| 193 |
|
| 194 |
VehicleWheel3D(); |
| 195 |
}; |
| 196 |
|
| 197 |
class VehicleBody3D : public RigidBody3D { |
| 198 |
GDCLASS(VehicleBody3D, RigidBody3D); |
| 199 |
|
| 200 |
real_t engine_force = 0.0; |
| 201 |
real_t brake = 0.0; |
| 202 |
|
| 203 |
real_t m_pitchControl = 0.0; |
| 204 |
real_t m_steeringValue = 0.0; |
| 205 |
real_t m_currentVehicleSpeedKmHour = 0.0; |
| 206 |
|
| 207 |
HashSet<RID> exclude; |
| 208 |
|
| 209 |
Vector<Vector3> m_forwardWS; |
| 210 |
Vector<Vector3> m_axle; |
| 211 |
Vector<real_t> m_forwardImpulse; |
| 212 |
Vector<real_t> m_sideImpulse; |
| 213 |
|
| 214 |
struct btVehicleWheelContactPoint { |
| 215 |
PhysicsDirectBodyState3D *m_s = nullptr; |
| 216 |
PhysicsBody3D *m_body1 = nullptr; |
| 217 |
Vector3 m_frictionPositionWorld; |
| 218 |
Vector3 m_frictionDirectionWorld; |
| 219 |
real_t m_jacDiagABInv = 0.0; |
| 220 |
real_t m_maxImpulse = 0.0; |
| 221 |
|
| 222 |
btVehicleWheelContactPoint(PhysicsDirectBodyState3D *s, PhysicsBody3D *body1, const Vector3 &frictionPosWorld, const Vector3 &frictionDirectionWorld, real_t maxImpulse); |
| 223 |
}; |
| 224 |
|
| 225 |
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); |
| 226 |
real_t _calc_rolling_friction(btVehicleWheelContactPoint &contactPoint); |
| 227 |
|
| 228 |
void _update_friction(PhysicsDirectBodyState3D *s); |
| 229 |
void _update_suspension(PhysicsDirectBodyState3D *s); |
| 230 |
real_t _ray_cast(int p_idx, PhysicsDirectBodyState3D *s); |
| 231 |
void _update_wheel_transform(VehicleWheel3D &wheel, PhysicsDirectBodyState3D *s); |
| 232 |
void _update_wheel(int p_idx, PhysicsDirectBodyState3D *s); |
| 233 |
|
| 234 |
void _update_process_mode(); |
| 235 |
|
| 236 |
friend class VehicleWheel3D; |
| 237 |
Vector<VehicleWheel3D *> wheels; |
| 238 |
|
| 239 |
static void _bind_methods(); |
| 240 |
|
| 241 |
static void _body_state_changed_callback(void *p_instance, PhysicsDirectBodyState3D *p_state); |
| 242 |
virtual void _body_state_changed(PhysicsDirectBodyState3D *p_state) override; |
| 243 |
|
| 244 |
protected: |
| 245 |
void _notification(int p_what); |
| 246 |
|
| 247 |
virtual void _physics_interpolated_changed() override; |
| 248 |
virtual void fti_pump_xform() override; |
| 249 |
|
| 250 |
public: |
| 251 |
void set_engine_force(real_t p_engine_force); |
| 252 |
real_t get_engine_force() const; |
| 253 |
|
| 254 |
void set_brake(real_t p_brake); |
| 255 |
real_t get_brake() const; |
| 256 |
|
| 257 |
void set_steering(real_t p_steering); |
| 258 |
real_t get_steering() const; |
| 259 |
|
| 260 |
VehicleBody3D(); |
| 261 |
}; |