0.2.0-dev Download ZIP
261 lines ·8.2 KB Raw
| 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 | |
| 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 | // If a wheel is added on a frame, the xform will not be set until it has been physics updated at least once. |
| 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 | // Wrap rotation to prevent float error. |
| 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; //const |
| 87 | Vector3 m_wheelDirectionCS; //const |
| 88 | Vector3 m_wheelAxleCS; // const or modified by steering |
| 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 | //btVector3 m_wheelAxleCS; // const or modified by steering ? |
| 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 | //calculated by suspension |
| 116 | real_t m_wheelsSuspensionForce = 0.0; |
| 117 | real_t m_skidInfo = 0.0; |
| 118 | |
| 119 | struct RaycastInfo { |
| 120 | //set by raycaster |
| 121 | Vector3 m_contactNormalWS; //contactnormal |
| 122 | Vector3 m_contactPointWS; //raycast hitpoint |
| 123 | real_t m_suspensionLength = 0.0; |
| 124 | Vector3 m_hardPointWS; //raycast starting point |
| 125 | Vector3 m_wheelDirectionWS; //direction in worldspace |
| 126 | Vector3 m_wheelAxleWS; // axle in worldspace |
| 127 | bool m_isInContact = false; |
| 128 | PhysicsBody3D *m_groundObject = nullptr; //could be general void* ptr |
| 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 | }; |