0.2.0-dev Download ZIP
1066 lines ·36.6 KB Raw
| 1 | /**************************************************************************/ |
| 2 | /* vehicle_body_3d.cpp */ |
| 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 | #include "vehicle_body_3d.h" |
| 32 | |
| 33 | #include "core/config/engine.h" |
| 34 | #include "core/object/class_db.h" |
| 35 | |
| 36 | #define ROLLING_INFLUENCE_FIX |
| 37 | |
| 38 | class btVehicleJacobianEntry { |
| 39 | public: |
| 40 | Vector3 m_linearJointAxis; |
| 41 | Vector3 m_aJ; |
| 42 | Vector3 m_bJ; |
| 43 | Vector3 m_0MinvJt; |
| 44 | Vector3 m_1MinvJt; |
| 45 | //Optimization: can be stored in the w/last component of one of the vectors |
| 46 | real_t m_Adiag = 0.0; |
| 47 | |
| 48 | real_t getDiagonal() const { return m_Adiag; } |
| 49 | |
| 50 | btVehicleJacobianEntry() {} |
| 51 | //constraint between two different rigidbodies |
| 52 | btVehicleJacobianEntry( |
| 53 | const Basis &world2A, |
| 54 | const Basis &world2B, |
| 55 | const Vector3 &rel_pos1, |
| 56 | const Vector3 &rel_pos2, |
| 57 | const Vector3 &jointAxis, |
| 58 | const Vector3 &inertiaInvA, |
| 59 | const real_t massInvA, |
| 60 | const Vector3 &inertiaInvB, |
| 61 | const real_t massInvB) : |
| 62 | m_linearJointAxis(jointAxis) { |
| 63 | m_aJ = world2A.xform(rel_pos1.cross(m_linearJointAxis)); |
| 64 | m_bJ = world2B.xform(rel_pos2.cross(-m_linearJointAxis)); |
| 65 | m_0MinvJt = inertiaInvA * m_aJ; |
| 66 | m_1MinvJt = inertiaInvB * m_bJ; |
| 67 | m_Adiag = massInvA + m_0MinvJt.dot(m_aJ) + massInvB + m_1MinvJt.dot(m_bJ); |
| 68 | |
| 69 | //btAssert(m_Adiag > real_t(0.0)); |
| 70 | } |
| 71 | |
| 72 | real_t getRelativeVelocity(const Vector3 &linvelA, const Vector3 &angvelA, const Vector3 &linvelB, const Vector3 &angvelB) { |
| 73 | Vector3 linrel = linvelA - linvelB; |
| 74 | Vector3 angvela = angvelA * m_aJ; |
| 75 | Vector3 angvelb = angvelB * m_bJ; |
| 76 | linrel *= m_linearJointAxis; |
| 77 | angvela += angvelb; |
| 78 | angvela += linrel; |
| 79 | real_t rel_vel2 = angvela[0] + angvela[1] + angvela[2]; |
| 80 | return rel_vel2 + CMP_EPSILON; |
| 81 | } |
| 82 | }; |
| 83 | |
| 84 | void VehicleWheel3D::FTIData::update_world_xform(Transform3D &r_xform, real_t p_interpolation_fraction) { |
| 85 | // Note that when unset (during the first few frames before a physics tick) |
| 86 | // the xform will be whatever it was loaded as. |
| 87 | if (!unset) { |
| 88 | real_t f = p_interpolation_fraction; |
| 89 | |
| 90 | WheelXform i; |
| 91 | i.up = prev.up.lerp(curr.up, f); |
| 92 | i.up.normalize(); |
| 93 | i.right = prev.right.lerp(curr.right, f); |
| 94 | i.right.normalize(); |
| 95 | |
| 96 | Vector3 fwd = i.up.cross(i.right); |
| 97 | fwd.normalize(); |
| 98 | |
| 99 | i.origin = prev.origin.lerp(curr.origin, f); |
| 100 | i.steering = Math::lerp(prev.steering, curr.steering, f); |
| 101 | |
| 102 | real_t rotation = Math::lerp(curr_rotation - curr_rotation_delta, curr_rotation, f); |
| 103 | |
| 104 | Basis steeringMat(i.up, i.steering); |
| 105 | |
| 106 | Basis rotatingMat(i.right, rotation); |
| 107 | |
| 108 | Basis basis2( |
| 109 | i.right[0], i.up[0], fwd[0], |
| 110 | i.right[1], i.up[1], fwd[1], |
| 111 | i.right[2], i.up[2], fwd[2]); |
| 112 | |
| 113 | r_xform.set_basis(steeringMat * rotatingMat * basis2); |
| 114 | r_xform.set_origin(i.origin); |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | void VehicleWheel3D::_notification(int p_what) { |
| 119 | switch (p_what) { |
| 120 | case NOTIFICATION_ENTER_TREE: { |
| 121 | VehicleBody3D *cb = Object::cast_to<VehicleBody3D>(get_parent()); |
| 122 | if (!cb) { |
| 123 | return; |
| 124 | } |
| 125 | body = cb; |
| 126 | local_xform = get_transform(); |
| 127 | cb->wheels.push_back(this); |
| 128 | |
| 129 | m_chassisConnectionPointCS = get_transform().origin; |
| 130 | m_wheelDirectionCS = -get_transform().basis.get_column(Vector3::AXIS_Y).normalized(); |
| 131 | m_wheelAxleCS = get_transform().basis.get_column(Vector3::AXIS_X).normalized(); |
| 132 | } break; |
| 133 | |
| 134 | case NOTIFICATION_EXIT_TREE: { |
| 135 | VehicleBody3D *cb = Object::cast_to<VehicleBody3D>(get_parent()); |
| 136 | if (!cb) { |
| 137 | return; |
| 138 | } |
| 139 | cb->wheels.erase(this); |
| 140 | body = nullptr; |
| 141 | } break; |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | PackedStringArray VehicleWheel3D::get_configuration_warnings() const { |
| 146 | PackedStringArray warnings = Node3D::get_configuration_warnings(); |
| 147 | |
| 148 | if (!Object::cast_to<VehicleBody3D>(get_parent())) { |
| 149 | warnings.push_back(RTR("VehicleWheel3D serves to provide a wheel system to a VehicleBody3D. Please use it as a child of a VehicleBody3D.")); |
| 150 | } |
| 151 | |
| 152 | return warnings; |
| 153 | } |
| 154 | |
| 155 | void VehicleWheel3D::_update(PhysicsDirectBodyState3D *s) { |
| 156 | if (m_raycastInfo.m_isInContact) { |
| 157 | real_t project = m_raycastInfo.m_contactNormalWS.dot(m_raycastInfo.m_wheelDirectionWS); |
| 158 | Vector3 chassis_velocity_at_contactPoint; |
| 159 | Vector3 relpos = m_raycastInfo.m_contactPointWS - s->get_transform().origin; |
| 160 | |
| 161 | chassis_velocity_at_contactPoint = s->get_linear_velocity() + |
| 162 | (s->get_angular_velocity()).cross(relpos); // * mPos); |
| 163 | |
| 164 | real_t projVel = m_raycastInfo.m_contactNormalWS.dot(chassis_velocity_at_contactPoint); |
| 165 | if (project >= real_t(-0.1)) { |
| 166 | m_suspensionRelativeVelocity = real_t(0.0); |
| 167 | m_clippedInvContactDotSuspension = real_t(1.0) / real_t(0.1); |
| 168 | } else { |
| 169 | real_t inv = real_t(-1.) / project; |
| 170 | m_suspensionRelativeVelocity = projVel * inv; |
| 171 | m_clippedInvContactDotSuspension = inv; |
| 172 | } |
| 173 | } else { // Not in contact : position wheel in a nice (rest length) position |
| 174 | m_raycastInfo.m_suspensionLength = m_suspensionRestLength; |
| 175 | m_suspensionRelativeVelocity = real_t(0.0); |
| 176 | m_raycastInfo.m_contactNormalWS = -m_raycastInfo.m_wheelDirectionWS; |
| 177 | m_clippedInvContactDotSuspension = real_t(1.0); |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | void VehicleWheel3D::set_radius(real_t p_radius) { |
| 182 | m_wheelRadius = p_radius; |
| 183 | update_gizmos(); |
| 184 | } |
| 185 | |
| 186 | real_t VehicleWheel3D::get_radius() const { |
| 187 | return m_wheelRadius; |
| 188 | } |
| 189 | |
| 190 | void VehicleWheel3D::set_suspension_rest_length(real_t p_length) { |
| 191 | m_suspensionRestLength = p_length; |
| 192 | update_gizmos(); |
| 193 | } |
| 194 | |
| 195 | real_t VehicleWheel3D::get_suspension_rest_length() const { |
| 196 | return m_suspensionRestLength; |
| 197 | } |
| 198 | |
| 199 | void VehicleWheel3D::set_suspension_travel(real_t p_length) { |
| 200 | m_maxSuspensionTravel = p_length; |
| 201 | } |
| 202 | |
| 203 | real_t VehicleWheel3D::get_suspension_travel() const { |
| 204 | return m_maxSuspensionTravel; |
| 205 | } |
| 206 | |
| 207 | void VehicleWheel3D::set_suspension_stiffness(real_t p_value) { |
| 208 | m_suspensionStiffness = p_value; |
| 209 | } |
| 210 | |
| 211 | real_t VehicleWheel3D::get_suspension_stiffness() const { |
| 212 | return m_suspensionStiffness; |
| 213 | } |
| 214 | |
| 215 | void VehicleWheel3D::set_suspension_max_force(real_t p_value) { |
| 216 | m_maxSuspensionForce = p_value; |
| 217 | } |
| 218 | |
| 219 | real_t VehicleWheel3D::get_suspension_max_force() const { |
| 220 | return m_maxSuspensionForce; |
| 221 | } |
| 222 | |
| 223 | void VehicleWheel3D::set_damping_compression(real_t p_value) { |
| 224 | m_wheelsDampingCompression = p_value; |
| 225 | } |
| 226 | |
| 227 | real_t VehicleWheel3D::get_damping_compression() const { |
| 228 | return m_wheelsDampingCompression; |
| 229 | } |
| 230 | |
| 231 | void VehicleWheel3D::set_damping_relaxation(real_t p_value) { |
| 232 | m_wheelsDampingRelaxation = p_value; |
| 233 | } |
| 234 | |
| 235 | real_t VehicleWheel3D::get_damping_relaxation() const { |
| 236 | return m_wheelsDampingRelaxation; |
| 237 | } |
| 238 | |
| 239 | void VehicleWheel3D::set_friction_slip(real_t p_value) { |
| 240 | m_frictionSlip = p_value; |
| 241 | } |
| 242 | |
| 243 | real_t VehicleWheel3D::get_friction_slip() const { |
| 244 | return m_frictionSlip; |
| 245 | } |
| 246 | |
| 247 | void VehicleWheel3D::set_roll_influence(real_t p_value) { |
| 248 | m_rollInfluence = p_value; |
| 249 | } |
| 250 | |
| 251 | real_t VehicleWheel3D::get_roll_influence() const { |
| 252 | return m_rollInfluence; |
| 253 | } |
| 254 | |
| 255 | bool VehicleWheel3D::is_in_contact() const { |
| 256 | return m_raycastInfo.m_isInContact; |
| 257 | } |
| 258 | |
| 259 | Vector3 VehicleWheel3D::get_contact_point() const { |
| 260 | return m_raycastInfo.m_contactPointWS; |
| 261 | } |
| 262 | |
| 263 | Vector3 VehicleWheel3D::get_contact_normal() const { |
| 264 | return m_raycastInfo.m_contactNormalWS; |
| 265 | } |
| 266 | |
| 267 | Node3D *VehicleWheel3D::get_contact_body() const { |
| 268 | return m_raycastInfo.m_groundObject; |
| 269 | } |
| 270 | |
| 271 | void VehicleWheel3D::_bind_methods() { |
| 272 | ClassDB::bind_method(D_METHOD("set_radius", "length"), &VehicleWheel3D::set_radius); |
| 273 | ClassDB::bind_method(D_METHOD("get_radius"), &VehicleWheel3D::get_radius); |
| 274 | |
| 275 | ClassDB::bind_method(D_METHOD("set_suspension_rest_length", "length"), &VehicleWheel3D::set_suspension_rest_length); |
| 276 | ClassDB::bind_method(D_METHOD("get_suspension_rest_length"), &VehicleWheel3D::get_suspension_rest_length); |
| 277 | |
| 278 | ClassDB::bind_method(D_METHOD("set_suspension_travel", "length"), &VehicleWheel3D::set_suspension_travel); |
| 279 | ClassDB::bind_method(D_METHOD("get_suspension_travel"), &VehicleWheel3D::get_suspension_travel); |
| 280 | |
| 281 | ClassDB::bind_method(D_METHOD("set_suspension_stiffness", "length"), &VehicleWheel3D::set_suspension_stiffness); |
| 282 | ClassDB::bind_method(D_METHOD("get_suspension_stiffness"), &VehicleWheel3D::get_suspension_stiffness); |
| 283 | |
| 284 | ClassDB::bind_method(D_METHOD("set_suspension_max_force", "length"), &VehicleWheel3D::set_suspension_max_force); |
| 285 | ClassDB::bind_method(D_METHOD("get_suspension_max_force"), &VehicleWheel3D::get_suspension_max_force); |
| 286 | |
| 287 | ClassDB::bind_method(D_METHOD("set_damping_compression", "length"), &VehicleWheel3D::set_damping_compression); |
| 288 | ClassDB::bind_method(D_METHOD("get_damping_compression"), &VehicleWheel3D::get_damping_compression); |
| 289 | |
| 290 | ClassDB::bind_method(D_METHOD("set_damping_relaxation", "length"), &VehicleWheel3D::set_damping_relaxation); |
| 291 | ClassDB::bind_method(D_METHOD("get_damping_relaxation"), &VehicleWheel3D::get_damping_relaxation); |
| 292 | |
| 293 | ClassDB::bind_method(D_METHOD("set_use_as_traction", "enable"), &VehicleWheel3D::set_use_as_traction); |
| 294 | ClassDB::bind_method(D_METHOD("is_used_as_traction"), &VehicleWheel3D::is_used_as_traction); |
| 295 | |
| 296 | ClassDB::bind_method(D_METHOD("set_use_as_steering", "enable"), &VehicleWheel3D::set_use_as_steering); |
| 297 | ClassDB::bind_method(D_METHOD("is_used_as_steering"), &VehicleWheel3D::is_used_as_steering); |
| 298 | |
| 299 | ClassDB::bind_method(D_METHOD("set_friction_slip", "length"), &VehicleWheel3D::set_friction_slip); |
| 300 | ClassDB::bind_method(D_METHOD("get_friction_slip"), &VehicleWheel3D::get_friction_slip); |
| 301 | |
| 302 | ClassDB::bind_method(D_METHOD("is_in_contact"), &VehicleWheel3D::is_in_contact); |
| 303 | ClassDB::bind_method(D_METHOD("get_contact_body"), &VehicleWheel3D::get_contact_body); |
| 304 | ClassDB::bind_method(D_METHOD("get_contact_point"), &VehicleWheel3D::get_contact_point); |
| 305 | ClassDB::bind_method(D_METHOD("get_contact_normal"), &VehicleWheel3D::get_contact_normal); |
| 306 | |
| 307 | ClassDB::bind_method(D_METHOD("set_roll_influence", "roll_influence"), &VehicleWheel3D::set_roll_influence); |
| 308 | ClassDB::bind_method(D_METHOD("get_roll_influence"), &VehicleWheel3D::get_roll_influence); |
| 309 | |
| 310 | ClassDB::bind_method(D_METHOD("get_skidinfo"), &VehicleWheel3D::get_skidinfo); |
| 311 | |
| 312 | ClassDB::bind_method(D_METHOD("get_rpm"), &VehicleWheel3D::get_rpm); |
| 313 | |
| 314 | ClassDB::bind_method(D_METHOD("set_engine_force", "engine_force"), &VehicleWheel3D::set_engine_force); |
| 315 | ClassDB::bind_method(D_METHOD("get_engine_force"), &VehicleWheel3D::get_engine_force); |
| 316 | |
| 317 | ClassDB::bind_method(D_METHOD("set_brake", "brake"), &VehicleWheel3D::set_brake); |
| 318 | ClassDB::bind_method(D_METHOD("get_brake"), &VehicleWheel3D::get_brake); |
| 319 | |
| 320 | ClassDB::bind_method(D_METHOD("set_steering", "steering"), &VehicleWheel3D::set_steering); |
| 321 | ClassDB::bind_method(D_METHOD("get_steering"), &VehicleWheel3D::get_steering); |
| 322 | |
| 323 | ADD_GROUP("Per-Wheel Motion", ""); |
| 324 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "engine_force", PROPERTY_HINT_RANGE, U"-1024,1024,0.01,or_less,or_greater,suffix:kg\u22C5m/s\u00B2 (N)"), "set_engine_force", "get_engine_force"); |
| 325 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "brake", PROPERTY_HINT_RANGE, U"-128,128,0.01,or_less,or_greater,suffix:kg\u22C5m/s\u00B2 (N)"), "set_brake", "get_brake"); |
| 326 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "steering", PROPERTY_HINT_RANGE, "-180,180,0.01,radians_as_degrees"), "set_steering", "get_steering"); |
| 327 | ADD_GROUP("VehicleBody3D Motion", ""); |
| 328 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_as_traction"), "set_use_as_traction", "is_used_as_traction"); |
| 329 | ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_as_steering"), "set_use_as_steering", "is_used_as_steering"); |
| 330 | ADD_GROUP("Wheel", "wheel_"); |
| 331 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "wheel_roll_influence"), "set_roll_influence", "get_roll_influence"); |
| 332 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "wheel_radius", PROPERTY_HINT_NONE, "suffix:m"), "set_radius", "get_radius"); |
| 333 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "wheel_rest_length", PROPERTY_HINT_NONE, "suffix:m"), "set_suspension_rest_length", "get_suspension_rest_length"); |
| 334 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "wheel_friction_slip"), "set_friction_slip", "get_friction_slip"); |
| 335 | ADD_GROUP("Suspension", "suspension_"); |
| 336 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "suspension_travel", PROPERTY_HINT_NONE, "suffix:m"), "set_suspension_travel", "get_suspension_travel"); |
| 337 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "suspension_stiffness", PROPERTY_HINT_NONE, U"suffix:N/mm"), "set_suspension_stiffness", "get_suspension_stiffness"); |
| 338 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "suspension_max_force", PROPERTY_HINT_NONE, U"suffix:kg\u22C5m/s\u00B2 (N)"), "set_suspension_max_force", "get_suspension_max_force"); |
| 339 | ADD_GROUP("Damping", "damping_"); |
| 340 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "damping_compression", PROPERTY_HINT_NONE, U"suffix:N\u22C5s/mm"), "set_damping_compression", "get_damping_compression"); |
| 341 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "damping_relaxation", PROPERTY_HINT_NONE, U"suffix:N\u22C5s/mm"), "set_damping_relaxation", "get_damping_relaxation"); |
| 342 | } |
| 343 | |
| 344 | void VehicleWheel3D::set_engine_force(real_t p_engine_force) { |
| 345 | m_engineForce = p_engine_force; |
| 346 | } |
| 347 | |
| 348 | real_t VehicleWheel3D::get_engine_force() const { |
| 349 | return m_engineForce; |
| 350 | } |
| 351 | |
| 352 | void VehicleWheel3D::set_brake(real_t p_brake) { |
| 353 | m_brake = p_brake; |
| 354 | } |
| 355 | |
| 356 | real_t VehicleWheel3D::get_brake() const { |
| 357 | return m_brake; |
| 358 | } |
| 359 | |
| 360 | void VehicleWheel3D::set_steering(real_t p_steering) { |
| 361 | m_steering = p_steering; |
| 362 | } |
| 363 | |
| 364 | real_t VehicleWheel3D::get_steering() const { |
| 365 | return m_steering; |
| 366 | } |
| 367 | |
| 368 | void VehicleWheel3D::set_use_as_traction(bool p_enable) { |
| 369 | engine_traction = p_enable; |
| 370 | } |
| 371 | |
| 372 | bool VehicleWheel3D::is_used_as_traction() const { |
| 373 | return engine_traction; |
| 374 | } |
| 375 | |
| 376 | void VehicleWheel3D::set_use_as_steering(bool p_enabled) { |
| 377 | steers = p_enabled; |
| 378 | } |
| 379 | |
| 380 | bool VehicleWheel3D::is_used_as_steering() const { |
| 381 | return steers; |
| 382 | } |
| 383 | |
| 384 | real_t VehicleWheel3D::get_skidinfo() const { |
| 385 | return m_skidInfo; |
| 386 | } |
| 387 | |
| 388 | real_t VehicleWheel3D::get_rpm() const { |
| 389 | return m_rpm; |
| 390 | } |
| 391 | |
| 392 | VehicleWheel3D::VehicleWheel3D() { |
| 393 | set_physics_interpolation_mode(PHYSICS_INTERPOLATION_MODE_OFF); |
| 394 | } |
| 395 | |
| 396 | void VehicleBody3D::_update_wheel_transform(VehicleWheel3D &wheel, PhysicsDirectBodyState3D *s) { |
| 397 | wheel.m_raycastInfo.m_isInContact = false; |
| 398 | |
| 399 | Transform3D chassisTrans = s->get_transform(); |
| 400 | /* |
| 401 | if (interpolatedTransform && (getRigidBody()->getMotionState())) { |
| 402 | getRigidBody()->getMotionState()->getWorldTransform(chassisTrans); |
| 403 | } |
| 404 | */ |
| 405 | |
| 406 | wheel.m_raycastInfo.m_hardPointWS = chassisTrans.xform(wheel.m_chassisConnectionPointCS); |
| 407 | //wheel.m_raycastInfo.m_hardPointWS+=s->get_linear_velocity()*s->get_step(); |
| 408 | wheel.m_raycastInfo.m_wheelDirectionWS = chassisTrans.get_basis().xform(wheel.m_wheelDirectionCS).normalized(); |
| 409 | wheel.m_raycastInfo.m_wheelAxleWS = chassisTrans.get_basis().xform(wheel.m_wheelAxleCS).normalized(); |
| 410 | } |
| 411 | |
| 412 | void VehicleBody3D::_update_wheel(int p_idx, PhysicsDirectBodyState3D *s) { |
| 413 | VehicleWheel3D &wheel = *wheels[p_idx]; |
| 414 | _update_wheel_transform(wheel, s); |
| 415 | |
| 416 | Vector3 up = -wheel.m_raycastInfo.m_wheelDirectionWS; |
| 417 | const Vector3 &right = wheel.m_raycastInfo.m_wheelAxleWS; |
| 418 | Vector3 fwd = up.cross(right); |
| 419 | fwd = fwd.normalized(); |
| 420 | |
| 421 | VehicleWheel3D::FTIData &id = wheel.fti_data; |
| 422 | |
| 423 | Vector3 origin = wheel.m_raycastInfo.m_hardPointWS + wheel.m_raycastInfo.m_wheelDirectionWS * wheel.m_raycastInfo.m_suspensionLength; |
| 424 | |
| 425 | if (is_physics_interpolated_and_enabled()) { |
| 426 | id.curr.up = up; |
| 427 | id.curr.right = right; |
| 428 | id.curr.steering = wheel.m_steering; |
| 429 | id.curr.origin = origin; |
| 430 | |
| 431 | // Pump the xform the first update to the wheel, |
| 432 | // otherwise the world xform prev will be NULL. |
| 433 | if (id.unset || id.reset_queued) { |
| 434 | id.unset = false; |
| 435 | id.reset_queued = false; |
| 436 | id.pump(); |
| 437 | } |
| 438 | |
| 439 | // The physics etc relies on the rest of this being correct, even with FTI, |
| 440 | // so we can't pre-maturely return here. |
| 441 | } |
| 442 | |
| 443 | Basis steeringMat(up, wheel.m_steering); |
| 444 | |
| 445 | Basis rotatingMat(right, wheel.m_rotation); |
| 446 | |
| 447 | Basis basis2( |
| 448 | right[0], up[0], fwd[0], |
| 449 | right[1], up[1], fwd[1], |
| 450 | right[2], up[2], fwd[2]); |
| 451 | |
| 452 | wheel.m_worldTransform.set_basis(steeringMat * rotatingMat * basis2); |
| 453 | //wheel.m_worldTransform.set_basis(basis2 * (steeringMat * rotatingMat)); |
| 454 | wheel.m_worldTransform.set_origin(origin); |
| 455 | } |
| 456 | |
| 457 | real_t VehicleBody3D::_ray_cast(int p_idx, PhysicsDirectBodyState3D *s) { |
| 458 | VehicleWheel3D &wheel = *wheels[p_idx]; |
| 459 | |
| 460 | _update_wheel_transform(wheel, s); |
| 461 | |
| 462 | real_t depth = -1; |
| 463 | |
| 464 | real_t raylen = wheel.m_suspensionRestLength + wheel.m_wheelRadius; |
| 465 | |
| 466 | Vector3 rayvector = wheel.m_raycastInfo.m_wheelDirectionWS * (raylen); |
| 467 | Vector3 source = wheel.m_raycastInfo.m_hardPointWS; |
| 468 | wheel.m_raycastInfo.m_contactPointWS = source + rayvector; |
| 469 | const Vector3 &target = wheel.m_raycastInfo.m_contactPointWS; |
| 470 | source -= wheel.m_wheelRadius * wheel.m_raycastInfo.m_wheelDirectionWS; |
| 471 | |
| 472 | real_t param = real_t(0.); |
| 473 | |
| 474 | PS3DT::RayResult rr; |
| 475 | |
| 476 | PhysicsDirectSpaceState3D *ss = s->get_space_state(); |
| 477 | |
| 478 | PS3DT::RayParameters ray_params; |
| 479 | ray_params.from = source; |
| 480 | ray_params.to = target; |
| 481 | ray_params.exclude = exclude; |
| 482 | ray_params.collision_mask = get_collision_mask(); |
| 483 | |
| 484 | wheel.m_raycastInfo.m_groundObject = nullptr; |
| 485 | bool col = ss->intersect_ray(ray_params, rr); |
| 486 | |
| 487 | if (col) { |
| 488 | param = source.distance_to(rr.position) / source.distance_to(target); |
| 489 | depth = raylen * param; |
| 490 | wheel.m_raycastInfo.m_contactNormalWS = rr.normal; |
| 491 | |
| 492 | wheel.m_raycastInfo.m_isInContact = true; |
| 493 | if (rr.collider_id.is_valid()) { |
| 494 | wheel.m_raycastInfo.m_groundObject = ObjectDB::get_instance<PhysicsBody3D>(rr.collider_id); |
| 495 | } |
| 496 | |
| 497 | real_t hitDistance = param * raylen; |
| 498 | wheel.m_raycastInfo.m_suspensionLength = hitDistance - wheel.m_wheelRadius; |
| 499 | //clamp on max suspension travel |
| 500 | |
| 501 | real_t minSuspensionLength = wheel.m_suspensionRestLength - wheel.m_maxSuspensionTravel; |
| 502 | real_t maxSuspensionLength = wheel.m_suspensionRestLength + wheel.m_maxSuspensionTravel; |
| 503 | if (wheel.m_raycastInfo.m_suspensionLength < minSuspensionLength) { |
| 504 | wheel.m_raycastInfo.m_suspensionLength = minSuspensionLength; |
| 505 | } |
| 506 | if (wheel.m_raycastInfo.m_suspensionLength > maxSuspensionLength) { |
| 507 | wheel.m_raycastInfo.m_suspensionLength = maxSuspensionLength; |
| 508 | } |
| 509 | |
| 510 | wheel.m_raycastInfo.m_contactPointWS = rr.position; |
| 511 | |
| 512 | real_t denominator = wheel.m_raycastInfo.m_contactNormalWS.dot(wheel.m_raycastInfo.m_wheelDirectionWS); |
| 513 | |
| 514 | Vector3 chassis_velocity_at_contactPoint; |
| 515 | //Vector3 relpos = wheel.m_raycastInfo.m_contactPointWS-getRigidBody()->getCenterOfMassPosition(); |
| 516 | |
| 517 | //chassis_velocity_at_contactPoint = getRigidBody()->getVelocityInLocalPoint(relpos); |
| 518 | |
| 519 | chassis_velocity_at_contactPoint = s->get_linear_velocity() + |
| 520 | (s->get_angular_velocity()).cross(wheel.m_raycastInfo.m_contactPointWS - s->get_transform().origin); // * mPos); |
| 521 | |
| 522 | real_t projVel = wheel.m_raycastInfo.m_contactNormalWS.dot(chassis_velocity_at_contactPoint); |
| 523 | |
| 524 | if (denominator >= real_t(-0.1)) { |
| 525 | wheel.m_suspensionRelativeVelocity = real_t(0.0); |
| 526 | wheel.m_clippedInvContactDotSuspension = real_t(1.0) / real_t(0.1); |
| 527 | } else { |
| 528 | real_t inv = real_t(-1.) / denominator; |
| 529 | wheel.m_suspensionRelativeVelocity = projVel * inv; |
| 530 | wheel.m_clippedInvContactDotSuspension = inv; |
| 531 | } |
| 532 | |
| 533 | } else { |
| 534 | wheel.m_raycastInfo.m_isInContact = false; |
| 535 | //put wheel info as in rest position |
| 536 | wheel.m_raycastInfo.m_suspensionLength = wheel.m_suspensionRestLength; |
| 537 | wheel.m_suspensionRelativeVelocity = real_t(0.0); |
| 538 | wheel.m_raycastInfo.m_contactNormalWS = -wheel.m_raycastInfo.m_wheelDirectionWS; |
| 539 | wheel.m_clippedInvContactDotSuspension = real_t(1.0); |
| 540 | } |
| 541 | |
| 542 | return depth; |
| 543 | } |
| 544 | |
| 545 | void VehicleBody3D::_update_suspension(PhysicsDirectBodyState3D *s) { |
| 546 | real_t chassisMass = get_mass(); |
| 547 | |
| 548 | for (int w_it = 0; w_it < wheels.size(); w_it++) { |
| 549 | VehicleWheel3D &wheel_info = *wheels[w_it]; |
| 550 | |
| 551 | if (wheel_info.m_raycastInfo.m_isInContact) { |
| 552 | real_t force; |
| 553 | //Spring |
| 554 | { |
| 555 | real_t susp_length = wheel_info.m_suspensionRestLength; |
| 556 | real_t current_length = wheel_info.m_raycastInfo.m_suspensionLength; |
| 557 | |
| 558 | real_t length_diff = (susp_length - current_length); |
| 559 | |
| 560 | force = wheel_info.m_suspensionStiffness * length_diff * wheel_info.m_clippedInvContactDotSuspension; |
| 561 | } |
| 562 | |
| 563 | // Damper |
| 564 | { |
| 565 | real_t projected_rel_vel = wheel_info.m_suspensionRelativeVelocity; |
| 566 | { |
| 567 | real_t susp_damping; |
| 568 | if (projected_rel_vel < real_t(0.0)) { |
| 569 | susp_damping = wheel_info.m_wheelsDampingCompression; |
| 570 | } else { |
| 571 | susp_damping = wheel_info.m_wheelsDampingRelaxation; |
| 572 | } |
| 573 | force -= susp_damping * projected_rel_vel; |
| 574 | } |
| 575 | } |
| 576 | |
| 577 | // RESULT |
| 578 | wheel_info.m_wheelsSuspensionForce = force * chassisMass; |
| 579 | if (wheel_info.m_wheelsSuspensionForce < real_t(0.)) { |
| 580 | wheel_info.m_wheelsSuspensionForce = real_t(0.); |
| 581 | } |
| 582 | } else { |
| 583 | wheel_info.m_wheelsSuspensionForce = real_t(0.0); |
| 584 | } |
| 585 | } |
| 586 | } |
| 587 | |
| 588 | //bilateral constraint between two dynamic objects |
| 589 | void VehicleBody3D::_resolve_single_bilateral(PhysicsDirectBodyState3D *s, const Vector3 &pos1, |
| 590 | PhysicsBody3D *body2, const Vector3 &pos2, const Vector3 &normal, real_t &impulse, const real_t p_rollInfluence) { |
| 591 | real_t normalLenSqr = normal.length_squared(); |
| 592 | //ERR_FAIL_COND( normalLenSqr < real_t(1.1)); |
| 593 | |
| 594 | if (normalLenSqr > real_t(1.1)) { |
| 595 | impulse = real_t(0.); |
| 596 | return; |
| 597 | } |
| 598 | |
| 599 | Vector3 rel_pos1 = pos1 - s->get_transform().origin; |
| 600 | Vector3 rel_pos2; |
| 601 | if (body2) { |
| 602 | rel_pos2 = pos2 - body2->get_global_transform().origin; |
| 603 | } |
| 604 | // This Jacobian entry could be reused for all iterations. |
| 605 | |
| 606 | Vector3 vel1 = s->get_linear_velocity() + (s->get_angular_velocity()).cross(rel_pos1); // * mPos); |
| 607 | Vector3 vel2; |
| 608 | |
| 609 | if (body2) { |
| 610 | vel2 = body2->get_linear_velocity() + body2->get_angular_velocity().cross(rel_pos2); |
| 611 | } |
| 612 | |
| 613 | Vector3 vel = vel1 - vel2; |
| 614 | |
| 615 | Basis b2trans; |
| 616 | real_t b2invmass = 0; |
| 617 | Vector3 b2lv; |
| 618 | Vector3 b2av; |
| 619 | Vector3 b2invinertia; //todo |
| 620 | |
| 621 | if (body2) { |
| 622 | b2trans = body2->get_global_transform().basis.transposed(); |
| 623 | b2invmass = body2->get_inverse_mass(); |
| 624 | b2lv = body2->get_linear_velocity(); |
| 625 | b2av = body2->get_angular_velocity(); |
| 626 | } |
| 627 | |
| 628 | btVehicleJacobianEntry jac(s->get_transform().basis.transposed(), |
| 629 | b2trans, |
| 630 | rel_pos1, |
| 631 | rel_pos2, |
| 632 | normal, |
| 633 | s->get_inverse_inertia_tensor().get_main_diagonal(), |
| 634 | 1.0 / get_mass(), |
| 635 | b2invinertia, |
| 636 | b2invmass); |
| 637 | |
| 638 | // FIXME: rel_vel assignment here is overwritten by the following assignment. |
| 639 | // What seems to be intended in the next assignment is: rel_vel = normal.dot(rel_vel); |
| 640 | // Investigate why. |
| 641 | real_t rel_vel = jac.getRelativeVelocity( |
| 642 | s->get_linear_velocity(), |
| 643 | s->get_transform().basis.transposed().xform(s->get_angular_velocity()), |
| 644 | b2lv, |
| 645 | b2trans.xform(b2av)); |
| 646 | |
| 647 | rel_vel = normal.dot(vel); |
| 648 | |
| 649 | // !BAS! We had this set to 0.4, in bullet its 0.2 |
| 650 | real_t contactDamping = real_t(0.2); |
| 651 | |
| 652 | if (p_rollInfluence > 0.0) { |
| 653 | // !BAS! But seeing we apply this frame by frame, makes more sense to me to make this time based |
| 654 | // keeping in mind our anti roll factor if it is set |
| 655 | contactDamping = MIN(contactDamping, s->get_step() / p_rollInfluence); |
| 656 | } |
| 657 | |
| 658 | #define ONLY_USE_LINEAR_MASS |
| 659 | #ifdef ONLY_USE_LINEAR_MASS |
| 660 | real_t massTerm = real_t(1.) / ((1.0 / get_mass()) + b2invmass); |
| 661 | impulse = -contactDamping * rel_vel * massTerm; |
| 662 | #else |
| 663 | real_t velocityImpulse = -contactDamping * rel_vel * jacDiagABInv; |
| 664 | impulse = velocityImpulse; |
| 665 | #endif |
| 666 | } |
| 667 | |
| 668 | VehicleBody3D::btVehicleWheelContactPoint::btVehicleWheelContactPoint(PhysicsDirectBodyState3D *s, PhysicsBody3D *body1, const Vector3 &frictionPosWorld, const Vector3 &frictionDirectionWorld, real_t maxImpulse) : |
| 669 | m_s(s), |
| 670 | m_body1(body1), |
| 671 | m_frictionPositionWorld(frictionPosWorld), |
| 672 | m_frictionDirectionWorld(frictionDirectionWorld), |
| 673 | m_maxImpulse(maxImpulse) { |
| 674 | real_t denom0 = 0; |
| 675 | real_t denom1 = 0; |
| 676 | |
| 677 | { |
| 678 | Vector3 r0 = frictionPosWorld - s->get_transform().origin; |
| 679 | Vector3 c0 = (r0).cross(frictionDirectionWorld); |
| 680 | Vector3 vec = s->get_inverse_inertia_tensor().xform_inv(c0).cross(r0); |
| 681 | denom0 = s->get_inverse_mass() + frictionDirectionWorld.dot(vec); |
| 682 | } |
| 683 | |
| 684 | /* TODO: Why is this code unused? |
| 685 | if (body1) { |
| 686 | Vector3 r0 = frictionPosWorld - body1->get_global_transform().origin; |
| 687 | Vector3 c0 = (r0).cross(frictionDirectionWorld); |
| 688 | Vector3 vec = s->get_inverse_inertia_tensor().xform_inv(c0).cross(r0); |
| 689 | //denom1= body1->get_inverse_mass() + frictionDirectionWorld.dot(vec); |
| 690 | |
| 691 | } |
| 692 | */ |
| 693 | |
| 694 | real_t relaxation = 1.f; |
| 695 | m_jacDiagABInv = relaxation / (denom0 + denom1); |
| 696 | } |
| 697 | |
| 698 | real_t VehicleBody3D::_calc_rolling_friction(btVehicleWheelContactPoint &contactPoint) { |
| 699 | real_t j1 = 0.f; |
| 700 | |
| 701 | const Vector3 &contactPosWorld = contactPoint.m_frictionPositionWorld; |
| 702 | |
| 703 | Vector3 rel_pos1 = contactPosWorld - contactPoint.m_s->get_transform().origin; |
| 704 | Vector3 rel_pos2; |
| 705 | if (contactPoint.m_body1) { |
| 706 | rel_pos2 = contactPosWorld - contactPoint.m_body1->get_global_transform().origin; |
| 707 | } |
| 708 | |
| 709 | real_t maxImpulse = contactPoint.m_maxImpulse; |
| 710 | |
| 711 | Vector3 vel1 = contactPoint.m_s->get_linear_velocity() + (contactPoint.m_s->get_angular_velocity()).cross(rel_pos1); // * mPos); |
| 712 | |
| 713 | Vector3 vel2; |
| 714 | if (contactPoint.m_body1) { |
| 715 | vel2 = contactPoint.m_body1->get_linear_velocity() + contactPoint.m_body1->get_angular_velocity().cross(rel_pos2); |
| 716 | } |
| 717 | |
| 718 | Vector3 vel = vel1 - vel2; |
| 719 | |
| 720 | real_t vrel = contactPoint.m_frictionDirectionWorld.dot(vel); |
| 721 | |
| 722 | // calculate j that moves us to zero relative velocity |
| 723 | j1 = -vrel * contactPoint.m_jacDiagABInv; |
| 724 | |
| 725 | return CLAMP(j1, -maxImpulse, maxImpulse); |
| 726 | } |
| 727 | |
| 728 | static const real_t sideFrictionStiffness2 = real_t(1.0); |
| 729 | void VehicleBody3D::_update_friction(PhysicsDirectBodyState3D *s) { |
| 730 | //calculate the impulse, so that the wheels don't move sidewards |
| 731 | int numWheel = wheels.size(); |
| 732 | if (!numWheel) { |
| 733 | return; |
| 734 | } |
| 735 | |
| 736 | m_forwardWS.resize(numWheel); |
| 737 | m_axle.resize(numWheel); |
| 738 | m_forwardImpulse.resize(numWheel); |
| 739 | m_sideImpulse.resize(numWheel); |
| 740 | |
| 741 | //collapse all those loops into one! |
| 742 | for (int i = 0; i < wheels.size(); i++) { |
| 743 | m_sideImpulse.write[i] = real_t(0.); |
| 744 | m_forwardImpulse.write[i] = real_t(0.); |
| 745 | } |
| 746 | |
| 747 | { |
| 748 | for (int i = 0; i < wheels.size(); i++) { |
| 749 | VehicleWheel3D &wheelInfo = *wheels[i]; |
| 750 | |
| 751 | if (wheelInfo.m_raycastInfo.m_isInContact) { |
| 752 | //const btTransform& wheelTrans = getWheelTransformWS( i ); |
| 753 | |
| 754 | Basis wheelBasis0 = wheelInfo.m_worldTransform.basis; //get_global_transform().basis; |
| 755 | |
| 756 | m_axle.write[i] = wheelBasis0.get_column(Vector3::AXIS_X); |
| 757 | //m_axle[i] = wheelInfo.m_raycastInfo.m_wheelAxleWS; |
| 758 | |
| 759 | const Vector3 &surfNormalWS = wheelInfo.m_raycastInfo.m_contactNormalWS; |
| 760 | real_t proj = m_axle[i].dot(surfNormalWS); |
| 761 | m_axle.write[i] -= surfNormalWS * proj; |
| 762 | m_axle.write[i] = m_axle[i].normalized(); |
| 763 | |
| 764 | m_forwardWS.write[i] = surfNormalWS.cross(m_axle[i]); |
| 765 | m_forwardWS.write[i].normalize(); |
| 766 | |
| 767 | _resolve_single_bilateral(s, wheelInfo.m_raycastInfo.m_contactPointWS, |
| 768 | wheelInfo.m_raycastInfo.m_groundObject, wheelInfo.m_raycastInfo.m_contactPointWS, |
| 769 | m_axle[i], m_sideImpulse.write[i], wheelInfo.m_rollInfluence); |
| 770 | |
| 771 | m_sideImpulse.write[i] *= sideFrictionStiffness2; |
| 772 | } |
| 773 | } |
| 774 | } |
| 775 | |
| 776 | real_t sideFactor = real_t(1.); |
| 777 | real_t fwdFactor = 0.5; |
| 778 | |
| 779 | bool sliding = false; |
| 780 | { |
| 781 | for (int wheel = 0; wheel < wheels.size(); wheel++) { |
| 782 | VehicleWheel3D &wheelInfo = *wheels[wheel]; |
| 783 | |
| 784 | //class btRigidBody* groundObject = (class btRigidBody*) wheelInfo.m_raycastInfo.m_groundObject; |
| 785 | |
| 786 | real_t rollingFriction = 0.f; |
| 787 | |
| 788 | if (wheelInfo.m_raycastInfo.m_isInContact) { |
| 789 | if (wheelInfo.m_engineForce != 0.f) { |
| 790 | rollingFriction = -wheelInfo.m_engineForce * s->get_step(); |
| 791 | } else { |
| 792 | real_t defaultRollingFrictionImpulse = 0.f; |
| 793 | real_t maxImpulse = wheelInfo.m_brake ? wheelInfo.m_brake : defaultRollingFrictionImpulse; |
| 794 | btVehicleWheelContactPoint contactPt(s, wheelInfo.m_raycastInfo.m_groundObject, wheelInfo.m_raycastInfo.m_contactPointWS, m_forwardWS[wheel], maxImpulse); |
| 795 | rollingFriction = _calc_rolling_friction(contactPt); |
| 796 | } |
| 797 | } |
| 798 | |
| 799 | //switch between active rolling (throttle), braking and non-active rolling friction (no throttle/break) |
| 800 | |
| 801 | m_forwardImpulse.write[wheel] = real_t(0.); |
| 802 | wheelInfo.m_skidInfo = real_t(1.); |
| 803 | |
| 804 | if (wheelInfo.m_raycastInfo.m_isInContact) { |
| 805 | wheelInfo.m_skidInfo = real_t(1.); |
| 806 | |
| 807 | real_t maximp = wheelInfo.m_wheelsSuspensionForce * s->get_step() * wheelInfo.m_frictionSlip; |
| 808 | real_t maximpSide = maximp; |
| 809 | |
| 810 | real_t maximpSquared = maximp * maximpSide; |
| 811 | |
| 812 | m_forwardImpulse.write[wheel] = rollingFriction; //wheelInfo.m_engineForce* timeStep; |
| 813 | |
| 814 | real_t x = (m_forwardImpulse[wheel]) * fwdFactor; |
| 815 | real_t y = (m_sideImpulse[wheel]) * sideFactor; |
| 816 | |
| 817 | real_t impulseSquared = (x * x + y * y); |
| 818 | |
| 819 | if (impulseSquared > maximpSquared) { |
| 820 | sliding = true; |
| 821 | |
| 822 | real_t factor = maximp / Math::sqrt(impulseSquared); |
| 823 | |
| 824 | wheelInfo.m_skidInfo *= factor; |
| 825 | } |
| 826 | } |
| 827 | } |
| 828 | } |
| 829 | |
| 830 | if (sliding) { |
| 831 | for (int wheel = 0; wheel < wheels.size(); wheel++) { |
| 832 | if (m_sideImpulse[wheel] != real_t(0.)) { |
| 833 | if (wheels[wheel]->m_skidInfo < real_t(1.)) { |
| 834 | m_forwardImpulse.write[wheel] *= wheels[wheel]->m_skidInfo; |
| 835 | m_sideImpulse.write[wheel] *= wheels[wheel]->m_skidInfo; |
| 836 | } |
| 837 | } |
| 838 | } |
| 839 | } |
| 840 | |
| 841 | // apply the impulses |
| 842 | { |
| 843 | for (int wheel = 0; wheel < wheels.size(); wheel++) { |
| 844 | VehicleWheel3D &wheelInfo = *wheels[wheel]; |
| 845 | |
| 846 | Vector3 rel_pos = wheelInfo.m_raycastInfo.m_contactPointWS - |
| 847 | s->get_transform().origin; |
| 848 | |
| 849 | if (m_forwardImpulse[wheel] != real_t(0.)) { |
| 850 | s->apply_impulse(m_forwardWS[wheel] * (m_forwardImpulse[wheel]), rel_pos); |
| 851 | } |
| 852 | if (m_sideImpulse[wheel] != real_t(0.)) { |
| 853 | PhysicsBody3D *groundObject = wheelInfo.m_raycastInfo.m_groundObject; |
| 854 | |
| 855 | Vector3 rel_pos2; |
| 856 | if (groundObject) { |
| 857 | rel_pos2 = wheelInfo.m_raycastInfo.m_contactPointWS - groundObject->get_global_transform().origin; |
| 858 | } |
| 859 | |
| 860 | Vector3 sideImp = m_axle[wheel] * m_sideImpulse[wheel]; |
| 861 | |
| 862 | #if defined ROLLING_INFLUENCE_FIX // fix. It only worked if car's up was along Y - VT. |
| 863 | Vector3 vChassisWorldUp = s->get_transform().basis.transposed()[1]; //getRigidBody()->getCenterOfMassTransform3D().getBasis().getColumn(m_indexUpAxis); |
| 864 | rel_pos -= vChassisWorldUp * (vChassisWorldUp.dot(rel_pos) * (1.f - wheelInfo.m_rollInfluence)); |
| 865 | #else |
| 866 | rel_pos[1] *= wheelInfo.m_rollInfluence; //? |
| 867 | #endif |
| 868 | s->apply_impulse(sideImp, rel_pos); |
| 869 | |
| 870 | //apply friction impulse on the ground |
| 871 | //todo |
| 872 | //groundObject->applyImpulse(-sideImp,rel_pos2); |
| 873 | } |
| 874 | } |
| 875 | } |
| 876 | } |
| 877 | |
| 878 | void VehicleBody3D::_physics_interpolated_changed() { |
| 879 | _update_process_mode(); |
| 880 | RigidBody3D::_physics_interpolated_changed(); |
| 881 | } |
| 882 | |
| 883 | void VehicleBody3D::fti_pump_xform() { |
| 884 | for (int i = 0; i < wheels.size(); i++) { |
| 885 | VehicleWheel3D &w = *wheels[i]; |
| 886 | w.fti_data.pump(); |
| 887 | } |
| 888 | |
| 889 | RigidBody3D::fti_pump_xform(); |
| 890 | } |
| 891 | |
| 892 | void VehicleBody3D::_update_process_mode() { |
| 893 | set_process_internal(is_physics_interpolated_and_enabled()); |
| 894 | } |
| 895 | |
| 896 | void VehicleBody3D::_notification(int p_what) { |
| 897 | switch (p_what) { |
| 898 | case NOTIFICATION_ENTER_TREE: { |
| 899 | _update_process_mode(); |
| 900 | } break; |
| 901 | case NOTIFICATION_INTERNAL_PROCESS: { |
| 902 | #ifdef DEV_ENABLED |
| 903 | if (!is_physics_interpolated_and_enabled()) { |
| 904 | WARN_PRINT_ONCE("VehicleBody NOTIFICATION_INTERNAL_PROCESS with physics interpolation OFF. (benign)"); |
| 905 | } |
| 906 | #endif |
| 907 | real_t f = Engine::get_singleton()->get_physics_interpolation_fraction(); |
| 908 | |
| 909 | Transform3D xform; |
| 910 | Transform3D inv_vehicle_xform = get_global_transform_interpolated().affine_inverse(); |
| 911 | |
| 912 | for (int i = 0; i < wheels.size(); i++) { |
| 913 | VehicleWheel3D &w = *wheels[i]; |
| 914 | w.fti_data.update_world_xform(xform, f); |
| 915 | w.set_transform(inv_vehicle_xform * xform); |
| 916 | } |
| 917 | } break; |
| 918 | case NOTIFICATION_RESET_PHYSICS_INTERPOLATION: { |
| 919 | _update_process_mode(); |
| 920 | for (int i = 0; i < wheels.size(); i++) { |
| 921 | VehicleWheel3D &w = *wheels[i]; |
| 922 | w.fti_data.reset_queued = true; |
| 923 | } |
| 924 | } break; |
| 925 | default: |
| 926 | break; |
| 927 | } |
| 928 | } |
| 929 | |
| 930 | void VehicleBody3D::_body_state_changed(PhysicsDirectBodyState3D *p_state) { |
| 931 | RigidBody3D::_body_state_changed(p_state); |
| 932 | |
| 933 | real_t step = p_state->get_step(); |
| 934 | |
| 935 | for (int i = 0; i < wheels.size(); i++) { |
| 936 | _update_wheel(i, p_state); |
| 937 | } |
| 938 | |
| 939 | bool use_fti = is_physics_interpolated_and_enabled(); |
| 940 | |
| 941 | for (int i = 0; i < wheels.size(); i++) { |
| 942 | _ray_cast(i, p_state); |
| 943 | if (!use_fti) { |
| 944 | // TODO: can this path also just use world space directly instead of inverse parent space? |
| 945 | wheels[i]->set_transform(p_state->get_transform().inverse() * wheels[i]->m_worldTransform); |
| 946 | } |
| 947 | } |
| 948 | |
| 949 | _update_suspension(p_state); |
| 950 | |
| 951 | for (int i = 0; i < wheels.size(); i++) { |
| 952 | //apply suspension force |
| 953 | VehicleWheel3D &wheel = *wheels[i]; |
| 954 | |
| 955 | real_t suspensionForce = wheel.m_wheelsSuspensionForce; |
| 956 | |
| 957 | if (suspensionForce > wheel.m_maxSuspensionForce) { |
| 958 | suspensionForce = wheel.m_maxSuspensionForce; |
| 959 | } |
| 960 | Vector3 impulse = wheel.m_raycastInfo.m_contactNormalWS * suspensionForce * step; |
| 961 | Vector3 relative_position = wheel.m_raycastInfo.m_contactPointWS - p_state->get_transform().origin; |
| 962 | |
| 963 | p_state->apply_impulse(impulse, relative_position); |
| 964 | } |
| 965 | |
| 966 | _update_friction(p_state); |
| 967 | |
| 968 | for (int i = 0; i < wheels.size(); i++) { |
| 969 | VehicleWheel3D &wheel = *wheels[i]; |
| 970 | Vector3 relpos = wheel.m_raycastInfo.m_hardPointWS - p_state->get_transform().origin; |
| 971 | Vector3 vel = p_state->get_linear_velocity() + (p_state->get_angular_velocity()).cross(relpos); |
| 972 | |
| 973 | if (wheel.m_raycastInfo.m_isInContact) { |
| 974 | const Transform3D &chassisWorldTransform = p_state->get_transform(); |
| 975 | |
| 976 | // Get forward vector. |
| 977 | Vector3 fwd( |
| 978 | chassisWorldTransform.basis[0][Vector3::AXIS_Z], |
| 979 | chassisWorldTransform.basis[1][Vector3::AXIS_Z], |
| 980 | chassisWorldTransform.basis[2][Vector3::AXIS_Z]); |
| 981 | |
| 982 | // Apply steering rotation to forward vector for steerable wheels. |
| 983 | if (wheel.steers) { |
| 984 | Basis steering_mat(Vector3(0, 1, 0), wheel.m_steering); |
| 985 | fwd = steering_mat.xform(fwd); |
| 986 | } |
| 987 | |
| 988 | real_t proj = fwd.dot(wheel.m_raycastInfo.m_contactNormalWS); |
| 989 | fwd -= wheel.m_raycastInfo.m_contactNormalWS * proj; |
| 990 | fwd.normalize(); |
| 991 | |
| 992 | real_t proj2 = fwd.dot(vel); |
| 993 | |
| 994 | wheel.m_deltaRotation = (proj2 * step) / (wheel.m_wheelRadius); |
| 995 | } |
| 996 | |
| 997 | wheel.m_rotation += wheel.m_deltaRotation; |
| 998 | if (use_fti) { |
| 999 | wheel.fti_data.rotate(wheel.m_deltaRotation); |
| 1000 | } |
| 1001 | wheel.m_rpm = ((wheel.m_deltaRotation / step) * 60) / Math::TAU; |
| 1002 | |
| 1003 | wheel.m_deltaRotation *= real_t(0.99); //damping of rotation when not in contact |
| 1004 | } |
| 1005 | } |
| 1006 | |
| 1007 | void VehicleBody3D::set_engine_force(real_t p_engine_force) { |
| 1008 | engine_force = p_engine_force; |
| 1009 | for (int i = 0; i < wheels.size(); i++) { |
| 1010 | VehicleWheel3D &wheelInfo = *wheels[i]; |
| 1011 | if (wheelInfo.engine_traction) { |
| 1012 | wheelInfo.m_engineForce = p_engine_force; |
| 1013 | } |
| 1014 | } |
| 1015 | } |
| 1016 | |
| 1017 | real_t VehicleBody3D::get_engine_force() const { |
| 1018 | return engine_force; |
| 1019 | } |
| 1020 | |
| 1021 | void VehicleBody3D::set_brake(real_t p_brake) { |
| 1022 | brake = p_brake; |
| 1023 | for (int i = 0; i < wheels.size(); i++) { |
| 1024 | VehicleWheel3D &wheelInfo = *wheels[i]; |
| 1025 | wheelInfo.m_brake = p_brake; |
| 1026 | } |
| 1027 | } |
| 1028 | |
| 1029 | real_t VehicleBody3D::get_brake() const { |
| 1030 | return brake; |
| 1031 | } |
| 1032 | |
| 1033 | void VehicleBody3D::set_steering(real_t p_steering) { |
| 1034 | m_steeringValue = p_steering; |
| 1035 | for (int i = 0; i < wheels.size(); i++) { |
| 1036 | VehicleWheel3D &wheelInfo = *wheels[i]; |
| 1037 | if (wheelInfo.steers) { |
| 1038 | wheelInfo.m_steering = p_steering; |
| 1039 | } |
| 1040 | } |
| 1041 | } |
| 1042 | |
| 1043 | real_t VehicleBody3D::get_steering() const { |
| 1044 | return m_steeringValue; |
| 1045 | } |
| 1046 | |
| 1047 | void VehicleBody3D::_bind_methods() { |
| 1048 | ClassDB::bind_method(D_METHOD("set_engine_force", "engine_force"), &VehicleBody3D::set_engine_force); |
| 1049 | ClassDB::bind_method(D_METHOD("get_engine_force"), &VehicleBody3D::get_engine_force); |
| 1050 | |
| 1051 | ClassDB::bind_method(D_METHOD("set_brake", "brake"), &VehicleBody3D::set_brake); |
| 1052 | ClassDB::bind_method(D_METHOD("get_brake"), &VehicleBody3D::get_brake); |
| 1053 | |
| 1054 | ClassDB::bind_method(D_METHOD("set_steering", "steering"), &VehicleBody3D::set_steering); |
| 1055 | ClassDB::bind_method(D_METHOD("get_steering"), &VehicleBody3D::get_steering); |
| 1056 | |
| 1057 | ADD_GROUP("Motion", ""); |
| 1058 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "engine_force", PROPERTY_HINT_RANGE, U"-1024,1024,0.01,or_less,or_greater,suffix:kg\u22C5m/s\u00B2 (N)"), "set_engine_force", "get_engine_force"); |
| 1059 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "brake", PROPERTY_HINT_RANGE, U"-128,128,0.01,or_less,or_greater,suffix:kg\u22C5m/s\u00B2 (N)"), "set_brake", "get_brake"); |
| 1060 | ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "steering", PROPERTY_HINT_RANGE, "-180,180,0.01,radians_as_degrees"), "set_steering", "get_steering"); |
| 1061 | } |
| 1062 | |
| 1063 | VehicleBody3D::VehicleBody3D() { |
| 1064 | exclude.insert(get_rid()); |
| 1065 | set_mass(40); |
| 1066 | } |