2023-01-05 13:25:55 +01:00
|
|
|
/**************************************************************************/
|
|
|
|
/* vehicle_body_3d.h */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* https://godotengine.org */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
|
|
|
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/**************************************************************************/
|
2018-01-05 00:50:27 +01:00
|
|
|
|
2022-07-23 23:41:51 +02:00
|
|
|
#ifndef VEHICLE_BODY_3D_H
|
|
|
|
#define VEHICLE_BODY_3D_H
|
2014-08-14 15:31:38 +02:00
|
|
|
|
2020-03-26 22:49:16 +01:00
|
|
|
#include "scene/3d/physics_body_3d.h"
|
2014-08-14 15:31:38 +02:00
|
|
|
|
2020-03-27 08:44:44 +01:00
|
|
|
class VehicleBody3D;
|
2014-08-14 15:31:38 +02:00
|
|
|
|
2020-03-27 08:44:44 +01:00
|
|
|
class VehicleWheel3D : public Node3D {
|
|
|
|
GDCLASS(VehicleWheel3D, Node3D);
|
2014-08-14 15:31:38 +02:00
|
|
|
|
2020-03-27 08:44:44 +01:00
|
|
|
friend class VehicleBody3D;
|
2014-08-14 15:31:38 +02:00
|
|
|
|
2020-10-17 07:08:21 +02:00
|
|
|
Transform3D m_worldTransform;
|
|
|
|
Transform3D local_xform;
|
2021-02-07 22:29:31 +01:00
|
|
|
bool engine_traction = false;
|
|
|
|
bool steers = false;
|
2014-08-14 15:31:38 +02:00
|
|
|
|
|
|
|
Vector3 m_chassisConnectionPointCS; //const
|
|
|
|
Vector3 m_wheelDirectionCS; //const
|
|
|
|
Vector3 m_wheelAxleCS; // const or modified by steering
|
|
|
|
|
2021-02-07 22:29:31 +01:00
|
|
|
real_t m_suspensionRestLength = 0.15;
|
2023-04-03 06:44:30 +02:00
|
|
|
real_t m_maxSuspensionTravel = 0.2;
|
2021-02-07 22:29:31 +01:00
|
|
|
real_t m_wheelRadius = 0.5;
|
2014-08-14 15:31:38 +02:00
|
|
|
|
2021-02-07 22:29:31 +01:00
|
|
|
real_t m_suspensionStiffness = 5.88;
|
|
|
|
real_t m_wheelsDampingCompression = 0.83;
|
|
|
|
real_t m_wheelsDampingRelaxation = 0.88;
|
|
|
|
real_t m_frictionSlip = 10.5;
|
|
|
|
real_t m_maxSuspensionForce = 6000.0;
|
|
|
|
bool m_bIsFrontWheel = false;
|
2014-08-14 15:31:38 +02:00
|
|
|
|
2021-02-07 22:29:31 +01:00
|
|
|
VehicleBody3D *body = nullptr;
|
2014-08-14 15:31:38 +02:00
|
|
|
|
2017-01-14 12:26:56 +01:00
|
|
|
//btVector3 m_wheelAxleCS; // const or modified by steering ?
|
2014-08-14 15:31:38 +02:00
|
|
|
|
2021-02-07 22:29:31 +01:00
|
|
|
real_t m_steering = 0.0;
|
|
|
|
real_t m_rotation = 0.0;
|
|
|
|
real_t m_deltaRotation = 0.0;
|
|
|
|
real_t m_rpm = 0.0;
|
|
|
|
real_t m_rollInfluence = 0.1;
|
|
|
|
real_t m_engineForce = 0.0;
|
|
|
|
real_t m_brake = 0.0;
|
2014-08-14 15:31:38 +02:00
|
|
|
|
2021-02-07 22:29:31 +01:00
|
|
|
real_t m_clippedInvContactDotSuspension = 1.0;
|
|
|
|
real_t m_suspensionRelativeVelocity = 0.0;
|
2014-08-14 15:31:38 +02:00
|
|
|
//calculated by suspension
|
2021-02-07 22:29:31 +01:00
|
|
|
real_t m_wheelsSuspensionForce = 0.0;
|
|
|
|
real_t m_skidInfo = 0.0;
|
2014-08-14 15:31:38 +02:00
|
|
|
|
|
|
|
struct RaycastInfo {
|
|
|
|
//set by raycaster
|
|
|
|
Vector3 m_contactNormalWS; //contactnormal
|
|
|
|
Vector3 m_contactPointWS; //raycast hitpoint
|
2021-02-07 22:29:31 +01:00
|
|
|
real_t m_suspensionLength = 0.0;
|
2014-08-14 15:31:38 +02:00
|
|
|
Vector3 m_hardPointWS; //raycast starting point
|
|
|
|
Vector3 m_wheelDirectionWS; //direction in worldspace
|
|
|
|
Vector3 m_wheelAxleWS; // axle in worldspace
|
2021-02-07 22:29:31 +01:00
|
|
|
bool m_isInContact = false;
|
2021-02-09 18:24:36 +01:00
|
|
|
PhysicsBody3D *m_groundObject = nullptr; //could be general void* ptr
|
2014-08-14 15:31:38 +02:00
|
|
|
} m_raycastInfo;
|
|
|
|
|
2020-03-27 19:21:27 +01:00
|
|
|
void _update(PhysicsDirectBodyState3D *s);
|
2014-08-14 15:31:38 +02:00
|
|
|
|
|
|
|
protected:
|
|
|
|
void _notification(int p_what);
|
|
|
|
static void _bind_methods();
|
|
|
|
|
|
|
|
public:
|
2021-01-30 00:22:12 +01:00
|
|
|
void set_radius(real_t p_radius);
|
|
|
|
real_t get_radius() const;
|
2014-09-03 04:13:40 +02:00
|
|
|
|
2021-01-30 00:22:12 +01:00
|
|
|
void set_suspension_rest_length(real_t p_length);
|
|
|
|
real_t get_suspension_rest_length() const;
|
2014-09-03 04:13:40 +02:00
|
|
|
|
2021-01-30 00:22:12 +01:00
|
|
|
void set_suspension_travel(real_t p_length);
|
|
|
|
real_t get_suspension_travel() const;
|
2014-09-03 04:13:40 +02:00
|
|
|
|
2021-01-30 00:22:12 +01:00
|
|
|
void set_suspension_stiffness(real_t p_value);
|
|
|
|
real_t get_suspension_stiffness() const;
|
2014-09-03 04:13:40 +02:00
|
|
|
|
2021-01-30 00:22:12 +01:00
|
|
|
void set_suspension_max_force(real_t p_value);
|
|
|
|
real_t get_suspension_max_force() const;
|
2014-09-03 04:13:40 +02:00
|
|
|
|
2021-01-30 00:22:12 +01:00
|
|
|
void set_damping_compression(real_t p_value);
|
|
|
|
real_t get_damping_compression() const;
|
2014-09-03 04:13:40 +02:00
|
|
|
|
2021-01-30 00:22:12 +01:00
|
|
|
void set_damping_relaxation(real_t p_value);
|
|
|
|
real_t get_damping_relaxation() const;
|
2014-09-03 04:13:40 +02:00
|
|
|
|
2021-01-30 00:22:12 +01:00
|
|
|
void set_friction_slip(real_t p_value);
|
|
|
|
real_t get_friction_slip() const;
|
2014-09-03 04:13:40 +02:00
|
|
|
|
|
|
|
void set_use_as_traction(bool p_enable);
|
|
|
|
bool is_used_as_traction() const;
|
|
|
|
|
|
|
|
void set_use_as_steering(bool p_enabled);
|
|
|
|
bool is_used_as_steering() const;
|
2014-08-14 15:31:38 +02:00
|
|
|
|
2017-05-19 14:10:10 +02:00
|
|
|
bool is_in_contact() const;
|
|
|
|
|
2021-12-08 12:10:56 +01:00
|
|
|
Node3D *get_contact_body() const;
|
|
|
|
|
2021-01-30 00:22:12 +01:00
|
|
|
void set_roll_influence(real_t p_value);
|
|
|
|
real_t get_roll_influence() const;
|
2017-05-19 14:10:10 +02:00
|
|
|
|
2021-01-30 00:22:12 +01:00
|
|
|
real_t get_skidinfo() const;
|
2017-09-30 03:02:16 +02:00
|
|
|
|
2021-01-30 00:22:12 +01:00
|
|
|
real_t get_rpm() const;
|
2019-04-10 00:49:37 +02:00
|
|
|
|
2021-01-30 00:22:12 +01:00
|
|
|
void set_engine_force(real_t p_engine_force);
|
|
|
|
real_t get_engine_force() const;
|
2019-07-18 20:16:28 +02:00
|
|
|
|
2021-01-30 00:22:12 +01:00
|
|
|
void set_brake(real_t p_brake);
|
|
|
|
real_t get_brake() const;
|
2019-07-18 20:16:28 +02:00
|
|
|
|
2021-01-30 00:22:12 +01:00
|
|
|
void set_steering(real_t p_steering);
|
|
|
|
real_t get_steering() const;
|
2019-07-18 20:16:28 +02:00
|
|
|
|
2022-09-19 17:43:15 +02:00
|
|
|
PackedStringArray get_configuration_warnings() const override;
|
2017-09-05 19:32:48 +02:00
|
|
|
|
2020-03-27 08:44:44 +01:00
|
|
|
VehicleWheel3D();
|
2014-08-14 15:31:38 +02:00
|
|
|
};
|
|
|
|
|
2022-08-25 19:35:52 +02:00
|
|
|
class VehicleBody3D : public RigidBody3D {
|
|
|
|
GDCLASS(VehicleBody3D, RigidBody3D);
|
2014-08-14 15:31:38 +02:00
|
|
|
|
2021-02-07 22:29:31 +01:00
|
|
|
real_t engine_force = 0.0;
|
|
|
|
real_t brake = 0.0;
|
2014-09-03 04:13:40 +02:00
|
|
|
|
2021-02-07 22:29:31 +01:00
|
|
|
real_t m_pitchControl = 0.0;
|
|
|
|
real_t m_steeringValue = 0.0;
|
|
|
|
real_t m_currentVehicleSpeedKmHour = 0.0;
|
2014-08-14 15:31:38 +02:00
|
|
|
|
2022-05-19 17:00:06 +02:00
|
|
|
HashSet<RID> exclude;
|
2014-08-14 15:31:38 +02:00
|
|
|
|
|
|
|
Vector<Vector3> m_forwardWS;
|
|
|
|
Vector<Vector3> m_axle;
|
|
|
|
Vector<real_t> m_forwardImpulse;
|
|
|
|
Vector<real_t> m_sideImpulse;
|
|
|
|
|
|
|
|
struct btVehicleWheelContactPoint {
|
2021-02-07 22:29:31 +01:00
|
|
|
PhysicsDirectBodyState3D *m_s = nullptr;
|
|
|
|
PhysicsBody3D *m_body1 = nullptr;
|
2014-08-14 15:31:38 +02:00
|
|
|
Vector3 m_frictionPositionWorld;
|
|
|
|
Vector3 m_frictionDirectionWorld;
|
2021-02-07 22:29:31 +01:00
|
|
|
real_t m_jacDiagABInv = 0.0;
|
|
|
|
real_t m_maxImpulse = 0.0;
|
2014-08-14 15:31:38 +02:00
|
|
|
|
2020-03-27 19:21:27 +01:00
|
|
|
btVehicleWheelContactPoint(PhysicsDirectBodyState3D *s, PhysicsBody3D *body1, const Vector3 &frictionPosWorld, const Vector3 &frictionDirectionWorld, real_t maxImpulse);
|
2014-08-14 15:31:38 +02:00
|
|
|
};
|
|
|
|
|
2020-03-27 19:21:27 +01:00
|
|
|
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);
|
2014-08-14 15:31:38 +02:00
|
|
|
real_t _calc_rolling_friction(btVehicleWheelContactPoint &contactPoint);
|
|
|
|
|
2020-03-27 19:21:27 +01:00
|
|
|
void _update_friction(PhysicsDirectBodyState3D *s);
|
|
|
|
void _update_suspension(PhysicsDirectBodyState3D *s);
|
|
|
|
real_t _ray_cast(int p_idx, PhysicsDirectBodyState3D *s);
|
|
|
|
void _update_wheel_transform(VehicleWheel3D &wheel, PhysicsDirectBodyState3D *s);
|
|
|
|
void _update_wheel(int p_idx, PhysicsDirectBodyState3D *s);
|
2014-08-14 15:31:38 +02:00
|
|
|
|
2020-03-27 08:44:44 +01:00
|
|
|
friend class VehicleWheel3D;
|
|
|
|
Vector<VehicleWheel3D *> wheels;
|
2014-08-14 15:31:38 +02:00
|
|
|
|
|
|
|
static void _bind_methods();
|
|
|
|
|
2021-06-05 01:09:41 +02:00
|
|
|
static void _body_state_changed_callback(void *p_instance, PhysicsDirectBodyState3D *p_state);
|
|
|
|
virtual void _body_state_changed(PhysicsDirectBodyState3D *p_state) override;
|
2014-08-14 15:31:38 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
public:
|
2021-01-30 00:22:12 +01:00
|
|
|
void set_engine_force(real_t p_engine_force);
|
|
|
|
real_t get_engine_force() const;
|
2014-09-03 04:13:40 +02:00
|
|
|
|
2021-01-30 00:22:12 +01:00
|
|
|
void set_brake(real_t p_brake);
|
|
|
|
real_t get_brake() const;
|
2014-09-03 04:13:40 +02:00
|
|
|
|
2021-01-30 00:22:12 +01:00
|
|
|
void set_steering(real_t p_steering);
|
|
|
|
real_t get_steering() const;
|
2016-05-21 11:52:21 +02:00
|
|
|
|
2020-03-27 08:44:44 +01:00
|
|
|
VehicleBody3D();
|
2014-08-14 15:31:38 +02:00
|
|
|
};
|
|
|
|
|
2022-07-23 23:41:51 +02:00
|
|
|
#endif // VEHICLE_BODY_3D_H
|