2016-06-18 20:46:12 +08:00
|
|
|
/*************************************************************************/
|
|
|
|
/* vehicle_body.h */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2017-08-27 20:16:55 +08:00
|
|
|
/* https://godotengine.org */
|
2016-06-18 20:46:12 +08:00
|
|
|
/*************************************************************************/
|
2019-01-01 19:53:14 +08:00
|
|
|
/* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
|
2016-06-18 20:46:12 +08:00
|
|
|
/* */
|
|
|
|
/* 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 07:50:27 +08:00
|
|
|
|
2014-08-14 21:31:38 +08:00
|
|
|
#ifndef VEHICLE_BODY_H
|
|
|
|
#define VEHICLE_BODY_H
|
|
|
|
|
|
|
|
#include "scene/3d/physics_body.h"
|
|
|
|
|
|
|
|
class VehicleBody;
|
|
|
|
|
|
|
|
class VehicleWheel : public Spatial {
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
GDCLASS(VehicleWheel, Spatial);
|
2014-08-14 21:31:38 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
friend class VehicleBody;
|
2014-08-14 21:31:38 +08:00
|
|
|
|
|
|
|
Transform m_worldTransform;
|
|
|
|
Transform local_xform;
|
2014-09-03 10:13:40 +08:00
|
|
|
bool engine_traction;
|
|
|
|
bool steers;
|
2014-08-14 21:31:38 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
Vector3 m_chassisConnectionPointCS; //const
|
|
|
|
Vector3 m_wheelDirectionCS; //const
|
|
|
|
Vector3 m_wheelAxleCS; // const or modified by steering
|
2014-08-14 21:31:38 +08:00
|
|
|
|
|
|
|
real_t m_suspensionRestLength;
|
|
|
|
real_t m_maxSuspensionTravelCm;
|
|
|
|
real_t m_wheelRadius;
|
|
|
|
|
|
|
|
real_t m_suspensionStiffness;
|
|
|
|
real_t m_wheelsDampingCompression;
|
|
|
|
real_t m_wheelsDampingRelaxation;
|
|
|
|
real_t m_frictionSlip;
|
|
|
|
real_t m_maxSuspensionForce;
|
|
|
|
bool m_bIsFrontWheel;
|
|
|
|
|
|
|
|
VehicleBody *body;
|
|
|
|
|
2017-01-14 19:26:56 +08:00
|
|
|
//btVector3 m_wheelAxleCS; // const or modified by steering ?
|
2014-08-14 21:31:38 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
real_t m_steering;
|
|
|
|
real_t m_rotation;
|
|
|
|
real_t m_deltaRotation;
|
2019-04-10 06:49:37 +08:00
|
|
|
real_t m_rpm;
|
2017-03-05 23:44:50 +08:00
|
|
|
real_t m_rollInfluence;
|
2019-07-19 02:16:28 +08:00
|
|
|
real_t m_engineForce;
|
2017-03-05 23:44:50 +08:00
|
|
|
real_t m_brake;
|
2014-08-14 21:31:38 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
real_t m_clippedInvContactDotSuspension;
|
|
|
|
real_t m_suspensionRelativeVelocity;
|
2014-08-14 21:31:38 +08:00
|
|
|
//calculated by suspension
|
2017-03-05 23:44:50 +08:00
|
|
|
real_t m_wheelsSuspensionForce;
|
|
|
|
real_t m_skidInfo;
|
2014-08-14 21:31:38 +08:00
|
|
|
|
|
|
|
struct RaycastInfo {
|
|
|
|
//set by raycaster
|
2017-03-05 23:44:50 +08:00
|
|
|
Vector3 m_contactNormalWS; //contactnormal
|
|
|
|
Vector3 m_contactPointWS; //raycast hitpoint
|
|
|
|
real_t m_suspensionLength;
|
|
|
|
Vector3 m_hardPointWS; //raycast starting point
|
|
|
|
Vector3 m_wheelDirectionWS; //direction in worldspace
|
|
|
|
Vector3 m_wheelAxleWS; // axle in worldspace
|
2014-08-14 21:31:38 +08:00
|
|
|
bool m_isInContact;
|
2017-03-05 23:44:50 +08:00
|
|
|
PhysicsBody *m_groundObject; //could be general void* ptr
|
2014-08-14 21:31:38 +08:00
|
|
|
} m_raycastInfo;
|
|
|
|
|
|
|
|
void _update(PhysicsDirectBodyState *s);
|
|
|
|
|
|
|
|
protected:
|
|
|
|
void _notification(int p_what);
|
|
|
|
static void _bind_methods();
|
|
|
|
|
|
|
|
public:
|
2014-09-03 10:13:40 +08:00
|
|
|
void set_radius(float p_radius);
|
|
|
|
float get_radius() const;
|
|
|
|
|
|
|
|
void set_suspension_rest_length(float p_length);
|
|
|
|
float get_suspension_rest_length() const;
|
|
|
|
|
|
|
|
void set_suspension_travel(float p_length);
|
|
|
|
float get_suspension_travel() const;
|
|
|
|
|
|
|
|
void set_suspension_stiffness(float p_value);
|
|
|
|
float get_suspension_stiffness() const;
|
|
|
|
|
|
|
|
void set_suspension_max_force(float p_value);
|
|
|
|
float get_suspension_max_force() const;
|
|
|
|
|
|
|
|
void set_damping_compression(float p_value);
|
|
|
|
float get_damping_compression() const;
|
|
|
|
|
|
|
|
void set_damping_relaxation(float p_value);
|
|
|
|
float get_damping_relaxation() const;
|
|
|
|
|
|
|
|
void set_friction_slip(float p_value);
|
|
|
|
float get_friction_slip() const;
|
|
|
|
|
|
|
|
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 21:31:38 +08:00
|
|
|
|
2017-05-19 20:10:10 +08:00
|
|
|
bool is_in_contact() const;
|
|
|
|
|
|
|
|
void set_roll_influence(float p_value);
|
|
|
|
float get_roll_influence() const;
|
|
|
|
|
2017-09-30 09:02:16 +08:00
|
|
|
float get_skidinfo() const;
|
|
|
|
|
2019-04-10 06:49:37 +08:00
|
|
|
float get_rpm() const;
|
|
|
|
|
2019-07-19 02:16:28 +08:00
|
|
|
void set_engine_force(float p_engine_force);
|
|
|
|
float get_engine_force() const;
|
|
|
|
|
|
|
|
void set_brake(float p_brake);
|
|
|
|
float get_brake() const;
|
|
|
|
|
|
|
|
void set_steering(float p_steering);
|
|
|
|
float get_steering() const;
|
|
|
|
|
2017-09-06 01:32:48 +08:00
|
|
|
String get_configuration_warning() const;
|
|
|
|
|
2014-08-14 21:31:38 +08:00
|
|
|
VehicleWheel();
|
|
|
|
};
|
|
|
|
|
2017-12-22 02:12:37 +08:00
|
|
|
class VehicleBody : public RigidBody {
|
2014-08-14 21:31:38 +08:00
|
|
|
|
2017-12-22 02:12:37 +08:00
|
|
|
GDCLASS(VehicleBody, RigidBody);
|
2014-08-14 21:31:38 +08:00
|
|
|
|
2014-09-03 10:13:40 +08:00
|
|
|
float engine_force;
|
|
|
|
float brake;
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
real_t m_pitchControl;
|
|
|
|
real_t m_steeringValue;
|
|
|
|
real_t m_currentVehicleSpeedKmHour;
|
2014-08-14 21:31:38 +08:00
|
|
|
|
|
|
|
Set<RID> exclude;
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
Vector<Vector3> m_forwardWS;
|
|
|
|
Vector<Vector3> m_axle;
|
|
|
|
Vector<real_t> m_forwardImpulse;
|
|
|
|
Vector<real_t> m_sideImpulse;
|
2014-08-14 21:31:38 +08:00
|
|
|
|
|
|
|
struct btVehicleWheelContactPoint {
|
|
|
|
PhysicsDirectBodyState *m_s;
|
2017-03-05 23:44:50 +08:00
|
|
|
PhysicsBody *m_body1;
|
|
|
|
Vector3 m_frictionPositionWorld;
|
|
|
|
Vector3 m_frictionDirectionWorld;
|
|
|
|
real_t m_jacDiagABInv;
|
|
|
|
real_t m_maxImpulse;
|
2014-08-14 21:31:38 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
btVehicleWheelContactPoint(PhysicsDirectBodyState *s, PhysicsBody *body1, const Vector3 &frictionPosWorld, const Vector3 &frictionDirectionWorld, real_t maxImpulse);
|
2014-08-14 21:31:38 +08:00
|
|
|
};
|
|
|
|
|
2018-07-25 09:11:03 +08:00
|
|
|
void _resolve_single_bilateral(PhysicsDirectBodyState *s, const Vector3 &pos1, PhysicsBody *body2, const Vector3 &pos2, const Vector3 &normal, real_t &impulse, const real_t p_rollInfluence);
|
2017-03-05 23:44:50 +08:00
|
|
|
real_t _calc_rolling_friction(btVehicleWheelContactPoint &contactPoint);
|
2014-08-14 21:31:38 +08:00
|
|
|
|
|
|
|
void _update_friction(PhysicsDirectBodyState *s);
|
|
|
|
void _update_suspension(PhysicsDirectBodyState *s);
|
2017-03-05 23:44:50 +08:00
|
|
|
real_t _ray_cast(int p_idx, PhysicsDirectBodyState *s);
|
|
|
|
void _update_wheel_transform(VehicleWheel &wheel, PhysicsDirectBodyState *s);
|
|
|
|
void _update_wheel(int p_idx, PhysicsDirectBodyState *s);
|
2014-08-14 21:31:38 +08:00
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
friend class VehicleWheel;
|
|
|
|
Vector<VehicleWheel *> wheels;
|
2014-08-14 21:31:38 +08:00
|
|
|
|
|
|
|
static void _bind_methods();
|
|
|
|
|
|
|
|
void _direct_state_changed(Object *p_state);
|
|
|
|
|
2017-03-05 23:44:50 +08:00
|
|
|
public:
|
2014-09-03 10:13:40 +08:00
|
|
|
void set_engine_force(float p_engine_force);
|
|
|
|
float get_engine_force() const;
|
|
|
|
|
2017-08-12 03:10:05 +08:00
|
|
|
void set_brake(float p_brake);
|
2014-09-03 10:13:40 +08:00
|
|
|
float get_brake() const;
|
|
|
|
|
|
|
|
void set_steering(float p_steering);
|
|
|
|
float get_steering() const;
|
2016-05-21 17:52:21 +08:00
|
|
|
|
2014-08-14 21:31:38 +08:00
|
|
|
VehicleBody();
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif // VEHICLE_BODY_H
|