2023-01-05 13:25:55 +01:00
|
|
|
/**************************************************************************/
|
|
|
|
/* vehicle_body_3d.cpp */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* 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
|
|
|
|
2020-03-27 08:44:44 +01:00
|
|
|
#include "vehicle_body_3d.h"
|
2014-08-14 15:31:38 +02:00
|
|
|
|
|
|
|
#define ROLLING_INFLUENCE_FIX
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
class btVehicleJacobianEntry {
|
2014-08-14 15:31:38 +02:00
|
|
|
public:
|
2017-03-05 16:44:50 +01:00
|
|
|
Vector3 m_linearJointAxis;
|
|
|
|
Vector3 m_aJ;
|
|
|
|
Vector3 m_bJ;
|
|
|
|
Vector3 m_0MinvJt;
|
|
|
|
Vector3 m_1MinvJt;
|
2014-08-14 15:31:38 +02:00
|
|
|
//Optimization: can be stored in the w/last component of one of the vectors
|
2022-05-02 16:28:25 +02:00
|
|
|
real_t m_Adiag = 0.0;
|
2014-08-14 15:31:38 +02:00
|
|
|
|
|
|
|
real_t getDiagonal() const { return m_Adiag; }
|
|
|
|
|
2020-05-12 17:01:17 +02:00
|
|
|
btVehicleJacobianEntry() {}
|
2017-03-05 16:44:50 +01:00
|
|
|
//constraint between two different rigidbodies
|
|
|
|
btVehicleJacobianEntry(
|
|
|
|
const Basis &world2A,
|
|
|
|
const Basis &world2B,
|
|
|
|
const Vector3 &rel_pos1,
|
|
|
|
const Vector3 &rel_pos2,
|
|
|
|
const Vector3 &jointAxis,
|
|
|
|
const Vector3 &inertiaInvA,
|
2014-08-14 15:31:38 +02:00
|
|
|
const real_t massInvA,
|
2017-03-05 16:44:50 +01:00
|
|
|
const Vector3 &inertiaInvB,
|
2017-12-06 21:36:34 +01:00
|
|
|
const real_t massInvB) :
|
|
|
|
m_linearJointAxis(jointAxis) {
|
2017-03-05 16:44:50 +01:00
|
|
|
m_aJ = world2A.xform(rel_pos1.cross(m_linearJointAxis));
|
|
|
|
m_bJ = world2B.xform(rel_pos2.cross(-m_linearJointAxis));
|
|
|
|
m_0MinvJt = inertiaInvA * m_aJ;
|
|
|
|
m_1MinvJt = inertiaInvB * m_bJ;
|
|
|
|
m_Adiag = massInvA + m_0MinvJt.dot(m_aJ) + massInvB + m_1MinvJt.dot(m_bJ);
|
|
|
|
|
|
|
|
//btAssert(m_Adiag > real_t(0.0));
|
|
|
|
}
|
2014-08-14 15:31:38 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
real_t getRelativeVelocity(const Vector3 &linvelA, const Vector3 &angvelA, const Vector3 &linvelB, const Vector3 &angvelB) {
|
|
|
|
Vector3 linrel = linvelA - linvelB;
|
|
|
|
Vector3 angvela = angvelA * m_aJ;
|
|
|
|
Vector3 angvelb = angvelB * m_bJ;
|
|
|
|
linrel *= m_linearJointAxis;
|
|
|
|
angvela += angvelb;
|
|
|
|
angvela += linrel;
|
|
|
|
real_t rel_vel2 = angvela[0] + angvela[1] + angvela[2];
|
|
|
|
return rel_vel2 + CMP_EPSILON;
|
|
|
|
}
|
2014-08-14 15:31:38 +02:00
|
|
|
};
|
|
|
|
|
2020-03-27 08:44:44 +01:00
|
|
|
void VehicleWheel3D::_notification(int p_what) {
|
2021-06-18 03:09:40 +02:00
|
|
|
switch (p_what) {
|
|
|
|
case NOTIFICATION_ENTER_TREE: {
|
|
|
|
VehicleBody3D *cb = Object::cast_to<VehicleBody3D>(get_parent());
|
|
|
|
if (!cb) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
body = cb;
|
|
|
|
local_xform = get_transform();
|
|
|
|
cb->wheels.push_back(this);
|
2014-08-14 15:31:38 +02:00
|
|
|
|
2021-06-18 03:09:40 +02:00
|
|
|
m_chassisConnectionPointCS = get_transform().origin;
|
2022-05-03 14:50:35 +02:00
|
|
|
m_wheelDirectionCS = -get_transform().basis.get_column(Vector3::AXIS_Y).normalized();
|
|
|
|
m_wheelAxleCS = get_transform().basis.get_column(Vector3::AXIS_X).normalized();
|
2021-06-18 03:09:40 +02:00
|
|
|
} break;
|
|
|
|
|
|
|
|
case NOTIFICATION_EXIT_TREE: {
|
|
|
|
VehicleBody3D *cb = Object::cast_to<VehicleBody3D>(get_parent());
|
|
|
|
if (!cb) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
cb->wheels.erase(this);
|
|
|
|
body = nullptr;
|
|
|
|
} break;
|
2014-08-14 15:31:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-19 17:43:15 +02:00
|
|
|
PackedStringArray VehicleWheel3D::get_configuration_warnings() const {
|
|
|
|
PackedStringArray warnings = Node::get_configuration_warnings();
|
2020-05-14 22:59:27 +02:00
|
|
|
|
2020-03-27 08:44:44 +01:00
|
|
|
if (!Object::cast_to<VehicleBody3D>(get_parent())) {
|
2022-03-28 15:24:14 +02:00
|
|
|
warnings.push_back(RTR("VehicleWheel3D serves to provide a wheel system to a VehicleBody3D. Please use it as a child of a VehicleBody3D."));
|
2017-09-05 19:32:48 +02:00
|
|
|
}
|
|
|
|
|
2020-10-29 11:01:28 +01:00
|
|
|
return warnings;
|
2017-09-05 19:32:48 +02:00
|
|
|
}
|
|
|
|
|
2020-03-27 19:21:27 +01:00
|
|
|
void VehicleWheel3D::_update(PhysicsDirectBodyState3D *s) {
|
2022-02-16 13:56:32 +01:00
|
|
|
if (m_raycastInfo.m_isInContact) {
|
2017-03-05 16:44:50 +01:00
|
|
|
real_t project = m_raycastInfo.m_contactNormalWS.dot(m_raycastInfo.m_wheelDirectionWS);
|
|
|
|
Vector3 chassis_velocity_at_contactPoint;
|
2014-08-14 15:31:38 +02:00
|
|
|
Vector3 relpos = m_raycastInfo.m_contactPointWS - s->get_transform().origin;
|
|
|
|
|
|
|
|
chassis_velocity_at_contactPoint = s->get_linear_velocity() +
|
2021-10-28 15:19:35 +02:00
|
|
|
(s->get_angular_velocity()).cross(relpos); // * mPos);
|
2014-08-14 15:31:38 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
real_t projVel = m_raycastInfo.m_contactNormalWS.dot(chassis_velocity_at_contactPoint);
|
|
|
|
if (project >= real_t(-0.1)) {
|
2014-08-14 15:31:38 +02:00
|
|
|
m_suspensionRelativeVelocity = real_t(0.0);
|
|
|
|
m_clippedInvContactDotSuspension = real_t(1.0) / real_t(0.1);
|
2017-03-05 16:44:50 +01:00
|
|
|
} else {
|
2014-08-14 15:31:38 +02:00
|
|
|
real_t inv = real_t(-1.) / project;
|
|
|
|
m_suspensionRelativeVelocity = projVel * inv;
|
|
|
|
m_clippedInvContactDotSuspension = inv;
|
|
|
|
}
|
2022-02-16 13:56:32 +01:00
|
|
|
} else { // Not in contact : position wheel in a nice (rest length) position
|
2014-08-14 15:31:38 +02:00
|
|
|
m_raycastInfo.m_suspensionLength = m_suspensionRestLength;
|
|
|
|
m_suspensionRelativeVelocity = real_t(0.0);
|
|
|
|
m_raycastInfo.m_contactNormalWS = -m_raycastInfo.m_wheelDirectionWS;
|
|
|
|
m_clippedInvContactDotSuspension = real_t(1.0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-30 00:22:12 +01:00
|
|
|
void VehicleWheel3D::set_radius(real_t p_radius) {
|
2017-03-05 16:44:50 +01:00
|
|
|
m_wheelRadius = p_radius;
|
2021-06-23 16:49:50 +02:00
|
|
|
update_gizmos();
|
2014-09-03 04:13:40 +02:00
|
|
|
}
|
|
|
|
|
2021-01-30 00:22:12 +01:00
|
|
|
real_t VehicleWheel3D::get_radius() const {
|
2014-09-03 04:13:40 +02:00
|
|
|
return m_wheelRadius;
|
|
|
|
}
|
|
|
|
|
2021-01-30 00:22:12 +01:00
|
|
|
void VehicleWheel3D::set_suspension_rest_length(real_t p_length) {
|
2017-03-05 16:44:50 +01:00
|
|
|
m_suspensionRestLength = p_length;
|
2021-06-23 16:49:50 +02:00
|
|
|
update_gizmos();
|
2014-09-03 04:13:40 +02:00
|
|
|
}
|
2020-05-14 14:29:06 +02:00
|
|
|
|
2021-01-30 00:22:12 +01:00
|
|
|
real_t VehicleWheel3D::get_suspension_rest_length() const {
|
2014-09-03 04:13:40 +02:00
|
|
|
return m_suspensionRestLength;
|
|
|
|
}
|
|
|
|
|
2021-01-30 00:22:12 +01:00
|
|
|
void VehicleWheel3D::set_suspension_travel(real_t p_length) {
|
2017-03-05 16:44:50 +01:00
|
|
|
m_maxSuspensionTravelCm = p_length / 0.01;
|
2014-09-03 04:13:40 +02:00
|
|
|
}
|
2020-05-14 14:29:06 +02:00
|
|
|
|
2021-01-30 00:22:12 +01:00
|
|
|
real_t VehicleWheel3D::get_suspension_travel() const {
|
2017-03-05 16:44:50 +01:00
|
|
|
return m_maxSuspensionTravelCm * 0.01;
|
2014-09-03 04:13:40 +02:00
|
|
|
}
|
|
|
|
|
2021-01-30 00:22:12 +01:00
|
|
|
void VehicleWheel3D::set_suspension_stiffness(real_t p_value) {
|
2017-03-05 16:44:50 +01:00
|
|
|
m_suspensionStiffness = p_value;
|
2014-09-03 04:13:40 +02:00
|
|
|
}
|
2020-05-14 14:29:06 +02:00
|
|
|
|
2021-01-30 00:22:12 +01:00
|
|
|
real_t VehicleWheel3D::get_suspension_stiffness() const {
|
2014-09-03 04:13:40 +02:00
|
|
|
return m_suspensionStiffness;
|
|
|
|
}
|
|
|
|
|
2021-01-30 00:22:12 +01:00
|
|
|
void VehicleWheel3D::set_suspension_max_force(real_t p_value) {
|
2017-03-05 16:44:50 +01:00
|
|
|
m_maxSuspensionForce = p_value;
|
2014-09-03 04:13:40 +02:00
|
|
|
}
|
2020-05-14 14:29:06 +02:00
|
|
|
|
2021-01-30 00:22:12 +01:00
|
|
|
real_t VehicleWheel3D::get_suspension_max_force() const {
|
2014-09-03 04:13:40 +02:00
|
|
|
return m_maxSuspensionForce;
|
|
|
|
}
|
|
|
|
|
2021-01-30 00:22:12 +01:00
|
|
|
void VehicleWheel3D::set_damping_compression(real_t p_value) {
|
2017-03-05 16:44:50 +01:00
|
|
|
m_wheelsDampingCompression = p_value;
|
2014-09-03 04:13:40 +02:00
|
|
|
}
|
2020-05-14 14:29:06 +02:00
|
|
|
|
2021-01-30 00:22:12 +01:00
|
|
|
real_t VehicleWheel3D::get_damping_compression() const {
|
2014-10-03 05:10:51 +02:00
|
|
|
return m_wheelsDampingCompression;
|
2014-09-03 04:13:40 +02:00
|
|
|
}
|
|
|
|
|
2021-01-30 00:22:12 +01:00
|
|
|
void VehicleWheel3D::set_damping_relaxation(real_t p_value) {
|
2017-03-05 16:44:50 +01:00
|
|
|
m_wheelsDampingRelaxation = p_value;
|
2014-09-03 04:13:40 +02:00
|
|
|
}
|
2020-05-14 14:29:06 +02:00
|
|
|
|
2021-01-30 00:22:12 +01:00
|
|
|
real_t VehicleWheel3D::get_damping_relaxation() const {
|
2014-09-03 04:13:40 +02:00
|
|
|
return m_wheelsDampingRelaxation;
|
|
|
|
}
|
|
|
|
|
2021-01-30 00:22:12 +01:00
|
|
|
void VehicleWheel3D::set_friction_slip(real_t p_value) {
|
2017-03-05 16:44:50 +01:00
|
|
|
m_frictionSlip = p_value;
|
2014-09-03 04:13:40 +02:00
|
|
|
}
|
2020-05-14 14:29:06 +02:00
|
|
|
|
2021-01-30 00:22:12 +01:00
|
|
|
real_t VehicleWheel3D::get_friction_slip() const {
|
2014-09-03 04:13:40 +02:00
|
|
|
return m_frictionSlip;
|
|
|
|
}
|
|
|
|
|
2021-01-30 00:22:12 +01:00
|
|
|
void VehicleWheel3D::set_roll_influence(real_t p_value) {
|
2017-05-19 14:10:10 +02:00
|
|
|
m_rollInfluence = p_value;
|
|
|
|
}
|
|
|
|
|
2021-01-30 00:22:12 +01:00
|
|
|
real_t VehicleWheel3D::get_roll_influence() const {
|
2017-05-19 14:10:10 +02:00
|
|
|
return m_rollInfluence;
|
|
|
|
}
|
|
|
|
|
2020-03-27 08:44:44 +01:00
|
|
|
bool VehicleWheel3D::is_in_contact() const {
|
2017-05-19 14:10:10 +02:00
|
|
|
return m_raycastInfo.m_isInContact;
|
|
|
|
}
|
|
|
|
|
2021-12-08 12:10:56 +01:00
|
|
|
Node3D *VehicleWheel3D::get_contact_body() const {
|
|
|
|
return m_raycastInfo.m_groundObject;
|
|
|
|
}
|
|
|
|
|
2020-03-27 08:44:44 +01:00
|
|
|
void VehicleWheel3D::_bind_methods() {
|
|
|
|
ClassDB::bind_method(D_METHOD("set_radius", "length"), &VehicleWheel3D::set_radius);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_radius"), &VehicleWheel3D::get_radius);
|
2014-08-14 15:31:38 +02:00
|
|
|
|
2020-03-27 08:44:44 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("set_suspension_rest_length", "length"), &VehicleWheel3D::set_suspension_rest_length);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_suspension_rest_length"), &VehicleWheel3D::get_suspension_rest_length);
|
2014-09-03 04:13:40 +02:00
|
|
|
|
2020-03-27 08:44:44 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("set_suspension_travel", "length"), &VehicleWheel3D::set_suspension_travel);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_suspension_travel"), &VehicleWheel3D::get_suspension_travel);
|
2014-08-14 15:31:38 +02:00
|
|
|
|
2020-03-27 08:44:44 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("set_suspension_stiffness", "length"), &VehicleWheel3D::set_suspension_stiffness);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_suspension_stiffness"), &VehicleWheel3D::get_suspension_stiffness);
|
2014-09-03 04:13:40 +02:00
|
|
|
|
2020-03-27 08:44:44 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("set_suspension_max_force", "length"), &VehicleWheel3D::set_suspension_max_force);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_suspension_max_force"), &VehicleWheel3D::get_suspension_max_force);
|
2014-09-03 04:13:40 +02:00
|
|
|
|
2020-03-27 08:44:44 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("set_damping_compression", "length"), &VehicleWheel3D::set_damping_compression);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_damping_compression"), &VehicleWheel3D::get_damping_compression);
|
2014-09-03 04:13:40 +02:00
|
|
|
|
2020-03-27 08:44:44 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("set_damping_relaxation", "length"), &VehicleWheel3D::set_damping_relaxation);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_damping_relaxation"), &VehicleWheel3D::get_damping_relaxation);
|
2014-09-03 04:13:40 +02:00
|
|
|
|
2020-03-27 08:44:44 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("set_use_as_traction", "enable"), &VehicleWheel3D::set_use_as_traction);
|
|
|
|
ClassDB::bind_method(D_METHOD("is_used_as_traction"), &VehicleWheel3D::is_used_as_traction);
|
2014-09-03 04:13:40 +02:00
|
|
|
|
2020-03-27 08:44:44 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("set_use_as_steering", "enable"), &VehicleWheel3D::set_use_as_steering);
|
|
|
|
ClassDB::bind_method(D_METHOD("is_used_as_steering"), &VehicleWheel3D::is_used_as_steering);
|
2014-09-03 04:13:40 +02:00
|
|
|
|
2020-03-27 08:44:44 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("set_friction_slip", "length"), &VehicleWheel3D::set_friction_slip);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_friction_slip"), &VehicleWheel3D::get_friction_slip);
|
2017-01-04 05:16:14 +01:00
|
|
|
|
2020-03-27 08:44:44 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("is_in_contact"), &VehicleWheel3D::is_in_contact);
|
2021-12-08 12:10:56 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("get_contact_body"), &VehicleWheel3D::get_contact_body);
|
2017-05-19 14:10:10 +02:00
|
|
|
|
2020-03-27 08:44:44 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("set_roll_influence", "roll_influence"), &VehicleWheel3D::set_roll_influence);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_roll_influence"), &VehicleWheel3D::get_roll_influence);
|
2017-05-19 14:10:10 +02:00
|
|
|
|
2020-03-27 08:44:44 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("get_skidinfo"), &VehicleWheel3D::get_skidinfo);
|
2017-09-30 03:02:16 +02:00
|
|
|
|
2020-03-27 08:44:44 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("get_rpm"), &VehicleWheel3D::get_rpm);
|
2019-04-10 00:49:37 +02:00
|
|
|
|
2020-03-27 08:44:44 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("set_engine_force", "engine_force"), &VehicleWheel3D::set_engine_force);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_engine_force"), &VehicleWheel3D::get_engine_force);
|
2019-07-18 20:16:28 +02:00
|
|
|
|
2020-03-27 08:44:44 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("set_brake", "brake"), &VehicleWheel3D::set_brake);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_brake"), &VehicleWheel3D::get_brake);
|
2019-07-18 20:16:28 +02:00
|
|
|
|
2020-03-27 08:44:44 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("set_steering", "steering"), &VehicleWheel3D::set_steering);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_steering"), &VehicleWheel3D::get_steering);
|
2019-07-18 20:16:28 +02:00
|
|
|
|
|
|
|
ADD_GROUP("Per-Wheel Motion", "");
|
2021-12-03 01:09:19 +01:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "engine_force", PROPERTY_HINT_RANGE, U"-1024,1024.0,0.01,or_greater,suffix:kg\u22C5m/s\u00B2 (N)"), "set_engine_force", "get_engine_force");
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "brake", PROPERTY_HINT_RANGE, U"0.0,1.0,0.01,suffix:kg\u22C5m/s\u00B2 (N)"), "set_brake", "get_brake");
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "steering", PROPERTY_HINT_RANGE, "-180,180.0,0.01,radians"), "set_steering", "get_steering");
|
2020-03-30 18:22:57 +02:00
|
|
|
ADD_GROUP("VehicleBody3D Motion", "");
|
2017-03-05 16:44:50 +01:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_as_traction"), "set_use_as_traction", "is_used_as_traction");
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_as_steering"), "set_use_as_steering", "is_used_as_steering");
|
|
|
|
ADD_GROUP("Wheel", "wheel_");
|
Variant: Added 64-bit packed arrays, renamed Variant::REAL to FLOAT.
- Renames PackedIntArray to PackedInt32Array.
- Renames PackedFloatArray to PackedFloat32Array.
- Adds PackedInt64Array and PackedFloat64Array.
- Renames Variant::REAL to Variant::FLOAT for consistency.
Packed arrays are for storing large amount of data and creating stuff like
meshes, buffers. textures, etc. Forcing them to be 64 is a huge waste of
memory. That said, many users requested the ability to have 64 bits packed
arrays for their games, so this is just an optional added type.
For Variant, the float datatype is always 64 bits, and exposed as `float`.
We still have `real_t` which is the datatype that can change from 32 to 64
bits depending on a compile flag (not entirely working right now, but that's
the idea). It affects math related datatypes and code only.
Neither Variant nor PackedArray make use of real_t, which is only intended
for math precision, so the term is removed from there to keep only float.
2020-02-24 19:20:53 +01:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "wheel_roll_influence"), "set_roll_influence", "get_roll_influence");
|
2021-12-03 01:09:19 +01:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "wheel_radius", PROPERTY_HINT_NONE, "suffix:m"), "set_radius", "get_radius");
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "wheel_rest_length", PROPERTY_HINT_NONE, "suffix:m"), "set_suspension_rest_length", "get_suspension_rest_length");
|
Variant: Added 64-bit packed arrays, renamed Variant::REAL to FLOAT.
- Renames PackedIntArray to PackedInt32Array.
- Renames PackedFloatArray to PackedFloat32Array.
- Adds PackedInt64Array and PackedFloat64Array.
- Renames Variant::REAL to Variant::FLOAT for consistency.
Packed arrays are for storing large amount of data and creating stuff like
meshes, buffers. textures, etc. Forcing them to be 64 is a huge waste of
memory. That said, many users requested the ability to have 64 bits packed
arrays for their games, so this is just an optional added type.
For Variant, the float datatype is always 64 bits, and exposed as `float`.
We still have `real_t` which is the datatype that can change from 32 to 64
bits depending on a compile flag (not entirely working right now, but that's
the idea). It affects math related datatypes and code only.
Neither Variant nor PackedArray make use of real_t, which is only intended
for math precision, so the term is removed from there to keep only float.
2020-02-24 19:20:53 +01:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "wheel_friction_slip"), "set_friction_slip", "get_friction_slip");
|
2017-03-05 16:44:50 +01:00
|
|
|
ADD_GROUP("Suspension", "suspension_");
|
2021-12-03 01:09:19 +01:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "suspension_travel", PROPERTY_HINT_NONE, "suffix:m"), "set_suspension_travel", "get_suspension_travel");
|
Variant: Added 64-bit packed arrays, renamed Variant::REAL to FLOAT.
- Renames PackedIntArray to PackedInt32Array.
- Renames PackedFloatArray to PackedFloat32Array.
- Adds PackedInt64Array and PackedFloat64Array.
- Renames Variant::REAL to Variant::FLOAT for consistency.
Packed arrays are for storing large amount of data and creating stuff like
meshes, buffers. textures, etc. Forcing them to be 64 is a huge waste of
memory. That said, many users requested the ability to have 64 bits packed
arrays for their games, so this is just an optional added type.
For Variant, the float datatype is always 64 bits, and exposed as `float`.
We still have `real_t` which is the datatype that can change from 32 to 64
bits depending on a compile flag (not entirely working right now, but that's
the idea). It affects math related datatypes and code only.
Neither Variant nor PackedArray make use of real_t, which is only intended
for math precision, so the term is removed from there to keep only float.
2020-02-24 19:20:53 +01:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "suspension_stiffness"), "set_suspension_stiffness", "get_suspension_stiffness");
|
2021-12-03 01:09:19 +01:00
|
|
|
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");
|
2017-03-05 16:44:50 +01:00
|
|
|
ADD_GROUP("Damping", "damping_");
|
Variant: Added 64-bit packed arrays, renamed Variant::REAL to FLOAT.
- Renames PackedIntArray to PackedInt32Array.
- Renames PackedFloatArray to PackedFloat32Array.
- Adds PackedInt64Array and PackedFloat64Array.
- Renames Variant::REAL to Variant::FLOAT for consistency.
Packed arrays are for storing large amount of data and creating stuff like
meshes, buffers. textures, etc. Forcing them to be 64 is a huge waste of
memory. That said, many users requested the ability to have 64 bits packed
arrays for their games, so this is just an optional added type.
For Variant, the float datatype is always 64 bits, and exposed as `float`.
We still have `real_t` which is the datatype that can change from 32 to 64
bits depending on a compile flag (not entirely working right now, but that's
the idea). It affects math related datatypes and code only.
Neither Variant nor PackedArray make use of real_t, which is only intended
for math precision, so the term is removed from there to keep only float.
2020-02-24 19:20:53 +01:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "damping_compression"), "set_damping_compression", "get_damping_compression");
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "damping_relaxation"), "set_damping_relaxation", "get_damping_relaxation");
|
2014-09-03 04:13:40 +02:00
|
|
|
}
|
|
|
|
|
2021-01-30 00:22:12 +01:00
|
|
|
void VehicleWheel3D::set_engine_force(real_t p_engine_force) {
|
2019-07-18 20:16:28 +02:00
|
|
|
m_engineForce = p_engine_force;
|
|
|
|
}
|
|
|
|
|
2021-01-30 00:22:12 +01:00
|
|
|
real_t VehicleWheel3D::get_engine_force() const {
|
2019-07-18 20:16:28 +02:00
|
|
|
return m_engineForce;
|
|
|
|
}
|
|
|
|
|
2021-01-30 00:22:12 +01:00
|
|
|
void VehicleWheel3D::set_brake(real_t p_brake) {
|
2019-07-18 20:16:28 +02:00
|
|
|
m_brake = p_brake;
|
|
|
|
}
|
2020-05-14 14:29:06 +02:00
|
|
|
|
2021-01-30 00:22:12 +01:00
|
|
|
real_t VehicleWheel3D::get_brake() const {
|
2019-07-18 20:16:28 +02:00
|
|
|
return m_brake;
|
|
|
|
}
|
|
|
|
|
2021-01-30 00:22:12 +01:00
|
|
|
void VehicleWheel3D::set_steering(real_t p_steering) {
|
2019-07-18 20:16:28 +02:00
|
|
|
m_steering = p_steering;
|
|
|
|
}
|
2020-05-14 14:29:06 +02:00
|
|
|
|
2021-01-30 00:22:12 +01:00
|
|
|
real_t VehicleWheel3D::get_steering() const {
|
2019-07-18 20:16:28 +02:00
|
|
|
return m_steering;
|
|
|
|
}
|
|
|
|
|
2020-03-27 08:44:44 +01:00
|
|
|
void VehicleWheel3D::set_use_as_traction(bool p_enable) {
|
2017-03-05 16:44:50 +01:00
|
|
|
engine_traction = p_enable;
|
2014-09-03 04:13:40 +02:00
|
|
|
}
|
|
|
|
|
2020-03-27 08:44:44 +01:00
|
|
|
bool VehicleWheel3D::is_used_as_traction() const {
|
2014-09-03 04:13:40 +02:00
|
|
|
return engine_traction;
|
|
|
|
}
|
|
|
|
|
2020-03-27 08:44:44 +01:00
|
|
|
void VehicleWheel3D::set_use_as_steering(bool p_enabled) {
|
2017-03-05 16:44:50 +01:00
|
|
|
steers = p_enabled;
|
2014-09-03 04:13:40 +02:00
|
|
|
}
|
|
|
|
|
2020-03-27 08:44:44 +01:00
|
|
|
bool VehicleWheel3D::is_used_as_steering() const {
|
2014-09-03 04:13:40 +02:00
|
|
|
return steers;
|
2014-08-14 15:31:38 +02:00
|
|
|
}
|
|
|
|
|
2021-01-30 00:22:12 +01:00
|
|
|
real_t VehicleWheel3D::get_skidinfo() const {
|
2017-09-30 03:02:16 +02:00
|
|
|
return m_skidInfo;
|
|
|
|
}
|
|
|
|
|
2021-01-30 00:22:12 +01:00
|
|
|
real_t VehicleWheel3D::get_rpm() const {
|
2019-04-10 00:49:37 +02:00
|
|
|
return m_rpm;
|
|
|
|
}
|
|
|
|
|
2020-03-27 08:44:44 +01:00
|
|
|
VehicleWheel3D::VehicleWheel3D() {
|
2014-08-14 15:31:38 +02:00
|
|
|
}
|
|
|
|
|
2020-03-27 19:21:27 +01:00
|
|
|
void VehicleBody3D::_update_wheel_transform(VehicleWheel3D &wheel, PhysicsDirectBodyState3D *s) {
|
2014-08-14 15:31:38 +02:00
|
|
|
wheel.m_raycastInfo.m_isInContact = false;
|
|
|
|
|
2020-10-17 07:08:21 +02:00
|
|
|
Transform3D chassisTrans = s->get_transform();
|
2017-01-14 12:26:56 +01:00
|
|
|
/*
|
|
|
|
if (interpolatedTransform && (getRigidBody()->getMotionState())) {
|
|
|
|
getRigidBody()->getMotionState()->getWorldTransform(chassisTrans);
|
|
|
|
}
|
|
|
|
*/
|
2014-08-14 15:31:38 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
wheel.m_raycastInfo.m_hardPointWS = chassisTrans.xform(wheel.m_chassisConnectionPointCS);
|
2014-09-03 04:13:40 +02:00
|
|
|
//wheel.m_raycastInfo.m_hardPointWS+=s->get_linear_velocity()*s->get_step();
|
2017-03-05 16:44:50 +01:00
|
|
|
wheel.m_raycastInfo.m_wheelDirectionWS = chassisTrans.get_basis().xform(wheel.m_wheelDirectionCS).normalized();
|
|
|
|
wheel.m_raycastInfo.m_wheelAxleWS = chassisTrans.get_basis().xform(wheel.m_wheelAxleCS).normalized();
|
2014-08-14 15:31:38 +02:00
|
|
|
}
|
|
|
|
|
2020-03-27 19:21:27 +01:00
|
|
|
void VehicleBody3D::_update_wheel(int p_idx, PhysicsDirectBodyState3D *s) {
|
2020-03-27 08:44:44 +01:00
|
|
|
VehicleWheel3D &wheel = *wheels[p_idx];
|
2017-03-05 16:44:50 +01:00
|
|
|
_update_wheel_transform(wheel, s);
|
2014-08-14 15:31:38 +02:00
|
|
|
|
|
|
|
Vector3 up = -wheel.m_raycastInfo.m_wheelDirectionWS;
|
2017-03-05 16:44:50 +01:00
|
|
|
const Vector3 &right = wheel.m_raycastInfo.m_wheelAxleWS;
|
2014-08-14 15:31:38 +02:00
|
|
|
Vector3 fwd = up.cross(right);
|
|
|
|
fwd = fwd.normalized();
|
|
|
|
|
2019-07-18 20:16:28 +02:00
|
|
|
Basis steeringMat(up, wheel.m_steering);
|
2014-08-14 15:31:38 +02:00
|
|
|
|
2017-12-21 19:12:37 +01:00
|
|
|
Basis rotatingMat(right, wheel.m_rotation);
|
2014-08-14 15:31:38 +02:00
|
|
|
|
2017-01-11 04:52:51 +01:00
|
|
|
Basis basis2(
|
2017-03-05 16:44:50 +01:00
|
|
|
right[0], up[0], fwd[0],
|
|
|
|
right[1], up[1], fwd[1],
|
|
|
|
right[2], up[2], fwd[2]);
|
2014-08-14 15:31:38 +02:00
|
|
|
|
|
|
|
wheel.m_worldTransform.set_basis(steeringMat * rotatingMat * basis2);
|
2014-09-03 04:13:40 +02:00
|
|
|
//wheel.m_worldTransform.set_basis(basis2 * (steeringMat * rotatingMat));
|
2014-08-14 15:31:38 +02:00
|
|
|
wheel.m_worldTransform.set_origin(
|
2017-03-05 16:44:50 +01:00
|
|
|
wheel.m_raycastInfo.m_hardPointWS + wheel.m_raycastInfo.m_wheelDirectionWS * wheel.m_raycastInfo.m_suspensionLength);
|
2014-08-14 15:31:38 +02:00
|
|
|
}
|
|
|
|
|
2020-03-27 19:21:27 +01:00
|
|
|
real_t VehicleBody3D::_ray_cast(int p_idx, PhysicsDirectBodyState3D *s) {
|
2020-03-27 08:44:44 +01:00
|
|
|
VehicleWheel3D &wheel = *wheels[p_idx];
|
2014-08-14 15:31:38 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
_update_wheel_transform(wheel, s);
|
2014-08-14 15:31:38 +02:00
|
|
|
|
|
|
|
real_t depth = -1;
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
real_t raylen = wheel.m_suspensionRestLength + wheel.m_wheelRadius;
|
2014-08-14 15:31:38 +02:00
|
|
|
|
|
|
|
Vector3 rayvector = wheel.m_raycastInfo.m_wheelDirectionWS * (raylen);
|
2014-09-03 04:13:40 +02:00
|
|
|
Vector3 source = wheel.m_raycastInfo.m_hardPointWS;
|
2014-08-14 15:31:38 +02:00
|
|
|
wheel.m_raycastInfo.m_contactPointWS = source + rayvector;
|
2017-03-05 16:44:50 +01:00
|
|
|
const Vector3 &target = wheel.m_raycastInfo.m_contactPointWS;
|
|
|
|
source -= wheel.m_wheelRadius * wheel.m_raycastInfo.m_wheelDirectionWS;
|
2014-08-14 15:31:38 +02:00
|
|
|
|
|
|
|
real_t param = real_t(0.);
|
|
|
|
|
2020-03-27 19:21:27 +01:00
|
|
|
PhysicsDirectSpaceState3D::RayResult rr;
|
2014-08-14 15:31:38 +02:00
|
|
|
|
2020-03-27 19:21:27 +01:00
|
|
|
PhysicsDirectSpaceState3D *ss = s->get_space_state();
|
2014-08-14 15:31:38 +02:00
|
|
|
|
2021-11-02 02:00:58 +01:00
|
|
|
PhysicsDirectSpaceState3D::RayParameters ray_params;
|
|
|
|
ray_params.from = source;
|
|
|
|
ray_params.to = target;
|
|
|
|
ray_params.exclude = exclude;
|
|
|
|
ray_params.collision_mask = get_collision_mask();
|
|
|
|
|
2020-04-02 01:20:12 +02:00
|
|
|
wheel.m_raycastInfo.m_groundObject = nullptr;
|
2021-12-08 12:10:56 +01:00
|
|
|
bool col = ss->intersect_ray(ray_params, rr);
|
2014-08-14 15:31:38 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (col) {
|
|
|
|
param = source.distance_to(rr.position) / source.distance_to(target);
|
2014-08-14 15:31:38 +02:00
|
|
|
depth = raylen * param;
|
2017-03-05 16:44:50 +01:00
|
|
|
wheel.m_raycastInfo.m_contactNormalWS = rr.normal;
|
2014-08-14 15:31:38 +02:00
|
|
|
|
|
|
|
wheel.m_raycastInfo.m_isInContact = true;
|
2020-05-14 16:41:43 +02:00
|
|
|
if (rr.collider) {
|
2020-03-26 22:49:16 +01:00
|
|
|
wheel.m_raycastInfo.m_groundObject = Object::cast_to<PhysicsBody3D>(rr.collider);
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-08-14 15:31:38 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
real_t hitDistance = param * raylen;
|
2014-08-14 15:31:38 +02:00
|
|
|
wheel.m_raycastInfo.m_suspensionLength = hitDistance - wheel.m_wheelRadius;
|
|
|
|
//clamp on max suspension travel
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
real_t minSuspensionLength = wheel.m_suspensionRestLength - wheel.m_maxSuspensionTravelCm * real_t(0.01);
|
|
|
|
real_t maxSuspensionLength = wheel.m_suspensionRestLength + wheel.m_maxSuspensionTravelCm * real_t(0.01);
|
|
|
|
if (wheel.m_raycastInfo.m_suspensionLength < minSuspensionLength) {
|
2014-08-14 15:31:38 +02:00
|
|
|
wheel.m_raycastInfo.m_suspensionLength = minSuspensionLength;
|
|
|
|
}
|
2017-03-05 16:44:50 +01:00
|
|
|
if (wheel.m_raycastInfo.m_suspensionLength > maxSuspensionLength) {
|
2014-08-14 15:31:38 +02:00
|
|
|
wheel.m_raycastInfo.m_suspensionLength = maxSuspensionLength;
|
|
|
|
}
|
|
|
|
|
|
|
|
wheel.m_raycastInfo.m_contactPointWS = rr.position;
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
real_t denominator = wheel.m_raycastInfo.m_contactNormalWS.dot(wheel.m_raycastInfo.m_wheelDirectionWS);
|
2014-08-14 15:31:38 +02:00
|
|
|
|
|
|
|
Vector3 chassis_velocity_at_contactPoint;
|
|
|
|
//Vector3 relpos = wheel.m_raycastInfo.m_contactPointWS-getRigidBody()->getCenterOfMassPosition();
|
|
|
|
|
|
|
|
//chassis_velocity_at_contactPoint = getRigidBody()->getVelocityInLocalPoint(relpos);
|
|
|
|
|
|
|
|
chassis_velocity_at_contactPoint = s->get_linear_velocity() +
|
2021-10-28 15:19:35 +02:00
|
|
|
(s->get_angular_velocity()).cross(wheel.m_raycastInfo.m_contactPointWS - s->get_transform().origin); // * mPos);
|
2014-08-14 15:31:38 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
real_t projVel = wheel.m_raycastInfo.m_contactNormalWS.dot(chassis_velocity_at_contactPoint);
|
2014-08-14 15:31:38 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (denominator >= real_t(-0.1)) {
|
2014-08-14 15:31:38 +02:00
|
|
|
wheel.m_suspensionRelativeVelocity = real_t(0.0);
|
|
|
|
wheel.m_clippedInvContactDotSuspension = real_t(1.0) / real_t(0.1);
|
2017-03-05 16:44:50 +01:00
|
|
|
} else {
|
2014-08-14 15:31:38 +02:00
|
|
|
real_t inv = real_t(-1.) / denominator;
|
|
|
|
wheel.m_suspensionRelativeVelocity = projVel * inv;
|
|
|
|
wheel.m_clippedInvContactDotSuspension = inv;
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
} else {
|
2014-08-14 15:31:38 +02:00
|
|
|
wheel.m_raycastInfo.m_isInContact = false;
|
|
|
|
//put wheel info as in rest position
|
|
|
|
wheel.m_raycastInfo.m_suspensionLength = wheel.m_suspensionRestLength;
|
|
|
|
wheel.m_suspensionRelativeVelocity = real_t(0.0);
|
2017-03-05 16:44:50 +01:00
|
|
|
wheel.m_raycastInfo.m_contactNormalWS = -wheel.m_raycastInfo.m_wheelDirectionWS;
|
2014-08-14 15:31:38 +02:00
|
|
|
wheel.m_clippedInvContactDotSuspension = real_t(1.0);
|
|
|
|
}
|
|
|
|
|
|
|
|
return depth;
|
|
|
|
}
|
|
|
|
|
2020-03-27 19:21:27 +01:00
|
|
|
void VehicleBody3D::_update_suspension(PhysicsDirectBodyState3D *s) {
|
2021-09-16 21:37:24 +02:00
|
|
|
real_t chassisMass = get_mass();
|
2014-08-14 15:31:38 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int w_it = 0; w_it < wheels.size(); w_it++) {
|
2020-03-27 08:44:44 +01:00
|
|
|
VehicleWheel3D &wheel_info = *wheels[w_it];
|
2014-08-14 15:31:38 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (wheel_info.m_raycastInfo.m_isInContact) {
|
2014-08-14 15:31:38 +02:00
|
|
|
real_t force;
|
2017-01-14 12:26:56 +01:00
|
|
|
//Spring
|
2014-08-14 15:31:38 +02:00
|
|
|
{
|
2017-03-05 16:44:50 +01:00
|
|
|
real_t susp_length = wheel_info.m_suspensionRestLength;
|
|
|
|
real_t current_length = wheel_info.m_raycastInfo.m_suspensionLength;
|
2014-08-14 15:31:38 +02:00
|
|
|
|
|
|
|
real_t length_diff = (susp_length - current_length);
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
force = wheel_info.m_suspensionStiffness * length_diff * wheel_info.m_clippedInvContactDotSuspension;
|
2014-08-14 15:31:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Damper
|
|
|
|
{
|
|
|
|
real_t projected_rel_vel = wheel_info.m_suspensionRelativeVelocity;
|
|
|
|
{
|
2017-03-05 16:44:50 +01:00
|
|
|
real_t susp_damping;
|
|
|
|
if (projected_rel_vel < real_t(0.0)) {
|
2014-08-14 15:31:38 +02:00
|
|
|
susp_damping = wheel_info.m_wheelsDampingCompression;
|
2017-03-05 16:44:50 +01:00
|
|
|
} else {
|
2014-08-14 15:31:38 +02:00
|
|
|
susp_damping = wheel_info.m_wheelsDampingRelaxation;
|
|
|
|
}
|
|
|
|
force -= susp_damping * projected_rel_vel;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// RESULT
|
|
|
|
wheel_info.m_wheelsSuspensionForce = force * chassisMass;
|
2017-03-05 16:44:50 +01:00
|
|
|
if (wheel_info.m_wheelsSuspensionForce < real_t(0.)) {
|
2014-08-14 15:31:38 +02:00
|
|
|
wheel_info.m_wheelsSuspensionForce = real_t(0.);
|
|
|
|
}
|
2017-03-05 16:44:50 +01:00
|
|
|
} else {
|
2014-08-14 15:31:38 +02:00
|
|
|
wheel_info.m_wheelsSuspensionForce = real_t(0.0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//bilateral constraint between two dynamic objects
|
2020-03-27 19:21:27 +01:00
|
|
|
void VehicleBody3D::_resolve_single_bilateral(PhysicsDirectBodyState3D *s, const Vector3 &pos1,
|
2020-03-26 22:49:16 +01:00
|
|
|
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 normalLenSqr = normal.length_squared();
|
|
|
|
//ERR_FAIL_COND( normalLenSqr < real_t(1.1));
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (normalLenSqr > real_t(1.1)) {
|
2014-08-14 15:31:38 +02:00
|
|
|
impulse = real_t(0.);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Vector3 rel_pos1 = pos1 - s->get_transform().origin;
|
|
|
|
Vector3 rel_pos2;
|
2020-05-14 16:41:43 +02:00
|
|
|
if (body2) {
|
2014-08-14 15:31:38 +02:00
|
|
|
rel_pos2 = pos2 - body2->get_global_transform().origin;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-08-14 15:31:38 +02:00
|
|
|
//this jacobian entry could be re-used for all iterations
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
Vector3 vel1 = s->get_linear_velocity() + (s->get_angular_velocity()).cross(rel_pos1); // * mPos);
|
2014-08-14 15:31:38 +02:00
|
|
|
Vector3 vel2;
|
|
|
|
|
2020-05-14 16:41:43 +02:00
|
|
|
if (body2) {
|
2017-03-05 16:44:50 +01:00
|
|
|
vel2 = body2->get_linear_velocity() + body2->get_angular_velocity().cross(rel_pos2);
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-08-14 15:31:38 +02:00
|
|
|
|
|
|
|
Vector3 vel = vel1 - vel2;
|
|
|
|
|
2017-01-11 04:52:51 +01:00
|
|
|
Basis b2trans;
|
2021-01-30 00:22:12 +01:00
|
|
|
real_t b2invmass = 0;
|
2014-08-14 15:31:38 +02:00
|
|
|
Vector3 b2lv;
|
|
|
|
Vector3 b2av;
|
|
|
|
Vector3 b2invinertia; //todo
|
|
|
|
|
|
|
|
if (body2) {
|
|
|
|
b2trans = body2->get_global_transform().basis.transposed();
|
|
|
|
b2invmass = body2->get_inverse_mass();
|
|
|
|
b2lv = body2->get_linear_velocity();
|
|
|
|
b2av = body2->get_angular_velocity();
|
|
|
|
}
|
|
|
|
|
|
|
|
btVehicleJacobianEntry jac(s->get_transform().basis.transposed(),
|
2017-03-05 16:44:50 +01:00
|
|
|
b2trans,
|
|
|
|
rel_pos1,
|
|
|
|
rel_pos2,
|
|
|
|
normal,
|
|
|
|
s->get_inverse_inertia_tensor().get_main_diagonal(),
|
2021-09-16 21:37:24 +02:00
|
|
|
1.0 / get_mass(),
|
2017-03-05 16:44:50 +01:00
|
|
|
b2invinertia,
|
|
|
|
b2invmass);
|
2014-08-14 15:31:38 +02:00
|
|
|
|
2017-08-21 21:15:36 +02:00
|
|
|
// FIXME: rel_vel assignment here is overwritten by the following assignment.
|
2021-03-12 14:35:16 +01:00
|
|
|
// What seems to be intended in the next assignment is: rel_vel = normal.dot(rel_vel);
|
2017-08-21 21:15:36 +02:00
|
|
|
// Investigate why.
|
2014-08-14 15:31:38 +02:00
|
|
|
real_t rel_vel = jac.getRelativeVelocity(
|
2017-03-05 16:44:50 +01:00
|
|
|
s->get_linear_velocity(),
|
|
|
|
s->get_transform().basis.transposed().xform(s->get_angular_velocity()),
|
|
|
|
b2lv,
|
|
|
|
b2trans.xform(b2av));
|
2014-08-14 15:31:38 +02:00
|
|
|
|
|
|
|
rel_vel = normal.dot(vel);
|
|
|
|
|
2018-03-30 00:30:38 +02:00
|
|
|
// !BAS! We had this set to 0.4, in bullet its 0.2
|
2018-05-26 00:26:39 +02:00
|
|
|
real_t contactDamping = real_t(0.2);
|
|
|
|
|
|
|
|
if (p_rollInfluence > 0.0) {
|
|
|
|
// !BAS! But seeing we apply this frame by frame, makes more sense to me to make this time based
|
|
|
|
// keeping in mind our anti roll factor if it is set
|
2019-06-18 23:40:49 +02:00
|
|
|
contactDamping = MIN(contactDamping, s->get_step() / p_rollInfluence);
|
2018-05-26 00:26:39 +02:00
|
|
|
}
|
2018-03-30 00:30:38 +02:00
|
|
|
|
2014-08-14 15:31:38 +02:00
|
|
|
#define ONLY_USE_LINEAR_MASS
|
|
|
|
#ifdef ONLY_USE_LINEAR_MASS
|
2021-09-16 21:37:24 +02:00
|
|
|
real_t massTerm = real_t(1.) / ((1.0 / get_mass()) + b2invmass);
|
2017-03-05 16:44:50 +01:00
|
|
|
impulse = -contactDamping * rel_vel * massTerm;
|
2014-08-14 15:31:38 +02:00
|
|
|
#else
|
|
|
|
real_t velocityImpulse = -contactDamping * rel_vel * jacDiagABInv;
|
|
|
|
impulse = velocityImpulse;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2020-03-27 19:21:27 +01:00
|
|
|
VehicleBody3D::btVehicleWheelContactPoint::btVehicleWheelContactPoint(PhysicsDirectBodyState3D *s, PhysicsBody3D *body1, const Vector3 &frictionPosWorld, const Vector3 &frictionDirectionWorld, real_t maxImpulse) :
|
2017-12-06 21:36:34 +01:00
|
|
|
m_s(s),
|
|
|
|
m_body1(body1),
|
|
|
|
m_frictionPositionWorld(frictionPosWorld),
|
|
|
|
m_frictionDirectionWorld(frictionDirectionWorld),
|
|
|
|
m_maxImpulse(maxImpulse) {
|
2021-01-30 00:22:12 +01:00
|
|
|
real_t denom0 = 0;
|
|
|
|
real_t denom1 = 0;
|
2014-08-14 15:31:38 +02:00
|
|
|
|
|
|
|
{
|
|
|
|
Vector3 r0 = frictionPosWorld - s->get_transform().origin;
|
|
|
|
Vector3 c0 = (r0).cross(frictionDirectionWorld);
|
|
|
|
Vector3 vec = s->get_inverse_inertia_tensor().xform_inv(c0).cross(r0);
|
2017-03-05 16:44:50 +01:00
|
|
|
denom0 = s->get_inverse_mass() + frictionDirectionWorld.dot(vec);
|
2014-08-14 15:31:38 +02:00
|
|
|
}
|
|
|
|
|
2016-07-08 00:32:28 +02:00
|
|
|
/* TODO: Why is this code unused?
|
2014-08-14 15:31:38 +02:00
|
|
|
if (body1) {
|
|
|
|
Vector3 r0 = frictionPosWorld - body1->get_global_transform().origin;
|
|
|
|
Vector3 c0 = (r0).cross(frictionDirectionWorld);
|
|
|
|
Vector3 vec = s->get_inverse_inertia_tensor().xform_inv(c0).cross(r0);
|
|
|
|
//denom1= body1->get_inverse_mass() + frictionDirectionWorld.dot(vec);
|
|
|
|
|
|
|
|
}
|
2016-07-08 00:32:28 +02:00
|
|
|
*/
|
2014-08-14 15:31:38 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
real_t relaxation = 1.f;
|
|
|
|
m_jacDiagABInv = relaxation / (denom0 + denom1);
|
2014-08-14 15:31:38 +02:00
|
|
|
}
|
|
|
|
|
2020-03-27 08:44:44 +01:00
|
|
|
real_t VehicleBody3D::_calc_rolling_friction(btVehicleWheelContactPoint &contactPoint) {
|
2017-03-05 16:44:50 +01:00
|
|
|
real_t j1 = 0.f;
|
2014-08-14 15:31:38 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
const Vector3 &contactPosWorld = contactPoint.m_frictionPositionWorld;
|
2014-08-14 15:31:38 +02:00
|
|
|
|
|
|
|
Vector3 rel_pos1 = contactPosWorld - contactPoint.m_s->get_transform().origin;
|
|
|
|
Vector3 rel_pos2;
|
2020-05-14 16:41:43 +02:00
|
|
|
if (contactPoint.m_body1) {
|
2014-08-14 15:31:38 +02:00
|
|
|
rel_pos2 = contactPosWorld - contactPoint.m_body1->get_global_transform().origin;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-08-14 15:31:38 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
real_t maxImpulse = contactPoint.m_maxImpulse;
|
2014-08-14 15:31:38 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
Vector3 vel1 = contactPoint.m_s->get_linear_velocity() + (contactPoint.m_s->get_angular_velocity()).cross(rel_pos1); // * mPos);
|
2014-08-14 15:31:38 +02:00
|
|
|
|
|
|
|
Vector3 vel2;
|
|
|
|
if (contactPoint.m_body1) {
|
2017-03-05 16:44:50 +01:00
|
|
|
vel2 = contactPoint.m_body1->get_linear_velocity() + contactPoint.m_body1->get_angular_velocity().cross(rel_pos2);
|
2014-08-14 15:31:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Vector3 vel = vel1 - vel2;
|
|
|
|
|
|
|
|
real_t vrel = contactPoint.m_frictionDirectionWorld.dot(vel);
|
|
|
|
|
|
|
|
// calculate j that moves us to zero relative velocity
|
|
|
|
j1 = -vrel * contactPoint.m_jacDiagABInv;
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
return CLAMP(j1, -maxImpulse, maxImpulse);
|
2014-08-14 15:31:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static const real_t sideFrictionStiffness2 = real_t(1.0);
|
2020-03-27 19:21:27 +01:00
|
|
|
void VehicleBody3D::_update_friction(PhysicsDirectBodyState3D *s) {
|
2014-08-14 15:31:38 +02:00
|
|
|
//calculate the impulse, so that the wheels don't move sidewards
|
|
|
|
int numWheel = wheels.size();
|
2020-05-14 16:41:43 +02:00
|
|
|
if (!numWheel) {
|
2014-08-14 15:31:38 +02:00
|
|
|
return;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2014-08-14 15:31:38 +02:00
|
|
|
|
|
|
|
m_forwardWS.resize(numWheel);
|
|
|
|
m_axle.resize(numWheel);
|
|
|
|
m_forwardImpulse.resize(numWheel);
|
|
|
|
m_sideImpulse.resize(numWheel);
|
|
|
|
|
|
|
|
//collapse all those loops into one!
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int i = 0; i < wheels.size(); i++) {
|
2018-07-25 03:11:03 +02:00
|
|
|
m_sideImpulse.write[i] = real_t(0.);
|
|
|
|
m_forwardImpulse.write[i] = real_t(0.);
|
2014-08-14 15:31:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int i = 0; i < wheels.size(); i++) {
|
2020-03-27 08:44:44 +01:00
|
|
|
VehicleWheel3D &wheelInfo = *wheels[i];
|
2014-08-14 15:31:38 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (wheelInfo.m_raycastInfo.m_isInContact) {
|
2014-08-14 15:31:38 +02:00
|
|
|
//const btTransform& wheelTrans = getWheelTransformWS( i );
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
Basis wheelBasis0 = wheelInfo.m_worldTransform.basis; //get_global_transform().basis;
|
2014-09-03 04:13:40 +02:00
|
|
|
|
2022-05-03 14:50:35 +02:00
|
|
|
m_axle.write[i] = wheelBasis0.get_column(Vector3::AXIS_X);
|
2014-09-03 04:13:40 +02:00
|
|
|
//m_axle[i] = wheelInfo.m_raycastInfo.m_wheelAxleWS;
|
2014-08-14 15:31:38 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
const Vector3 &surfNormalWS = wheelInfo.m_raycastInfo.m_contactNormalWS;
|
2014-08-14 15:31:38 +02:00
|
|
|
real_t proj = m_axle[i].dot(surfNormalWS);
|
2018-07-25 03:11:03 +02:00
|
|
|
m_axle.write[i] -= surfNormalWS * proj;
|
|
|
|
m_axle.write[i] = m_axle[i].normalized();
|
2014-08-14 15:31:38 +02:00
|
|
|
|
2018-07-25 03:11:03 +02:00
|
|
|
m_forwardWS.write[i] = surfNormalWS.cross(m_axle[i]);
|
|
|
|
m_forwardWS.write[i].normalize();
|
2014-08-14 15:31:38 +02:00
|
|
|
|
|
|
|
_resolve_single_bilateral(s, wheelInfo.m_raycastInfo.m_contactPointWS,
|
2017-03-05 16:44:50 +01:00
|
|
|
wheelInfo.m_raycastInfo.m_groundObject, wheelInfo.m_raycastInfo.m_contactPointWS,
|
2018-07-25 03:11:03 +02:00
|
|
|
m_axle[i], m_sideImpulse.write[i], wheelInfo.m_rollInfluence);
|
2014-08-14 15:31:38 +02:00
|
|
|
|
2018-07-25 03:11:03 +02:00
|
|
|
m_sideImpulse.write[i] *= sideFrictionStiffness2;
|
2014-08-14 15:31:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
real_t sideFactor = real_t(1.);
|
|
|
|
real_t fwdFactor = 0.5;
|
|
|
|
|
|
|
|
bool sliding = false;
|
|
|
|
{
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int wheel = 0; wheel < wheels.size(); wheel++) {
|
2020-03-27 08:44:44 +01:00
|
|
|
VehicleWheel3D &wheelInfo = *wheels[wheel];
|
2014-08-14 15:31:38 +02:00
|
|
|
|
|
|
|
//class btRigidBody* groundObject = (class btRigidBody*) wheelInfo.m_raycastInfo.m_groundObject;
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
real_t rollingFriction = 0.f;
|
2014-08-14 15:31:38 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (wheelInfo.m_raycastInfo.m_isInContact) {
|
2019-07-18 20:16:28 +02:00
|
|
|
if (wheelInfo.m_engineForce != 0.f) {
|
|
|
|
rollingFriction = -wheelInfo.m_engineForce * s->get_step();
|
2017-03-05 16:44:50 +01:00
|
|
|
} else {
|
2014-08-14 15:31:38 +02:00
|
|
|
real_t defaultRollingFrictionImpulse = 0.f;
|
2019-07-18 20:16:28 +02:00
|
|
|
real_t maxImpulse = wheelInfo.m_brake ? wheelInfo.m_brake : defaultRollingFrictionImpulse;
|
2017-03-05 16:44:50 +01:00
|
|
|
btVehicleWheelContactPoint contactPt(s, wheelInfo.m_raycastInfo.m_groundObject, wheelInfo.m_raycastInfo.m_contactPointWS, m_forwardWS[wheel], maxImpulse);
|
2014-08-14 15:31:38 +02:00
|
|
|
rollingFriction = _calc_rolling_friction(contactPt);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//switch between active rolling (throttle), braking and non-active rolling friction (no throttle/break)
|
|
|
|
|
2018-07-25 03:11:03 +02:00
|
|
|
m_forwardImpulse.write[wheel] = real_t(0.);
|
2017-03-05 16:44:50 +01:00
|
|
|
wheelInfo.m_skidInfo = real_t(1.);
|
2014-08-14 15:31:38 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (wheelInfo.m_raycastInfo.m_isInContact) {
|
|
|
|
wheelInfo.m_skidInfo = real_t(1.);
|
2014-08-14 15:31:38 +02:00
|
|
|
|
|
|
|
real_t maximp = wheelInfo.m_wheelsSuspensionForce * s->get_step() * wheelInfo.m_frictionSlip;
|
|
|
|
real_t maximpSide = maximp;
|
|
|
|
|
|
|
|
real_t maximpSquared = maximp * maximpSide;
|
|
|
|
|
2018-07-25 03:11:03 +02:00
|
|
|
m_forwardImpulse.write[wheel] = rollingFriction; //wheelInfo.m_engineForce* timeStep;
|
2014-08-14 15:31:38 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
real_t x = (m_forwardImpulse[wheel]) * fwdFactor;
|
|
|
|
real_t y = (m_sideImpulse[wheel]) * sideFactor;
|
2014-08-14 15:31:38 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
real_t impulseSquared = (x * x + y * y);
|
2014-08-14 15:31:38 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (impulseSquared > maximpSquared) {
|
2014-08-14 15:31:38 +02:00
|
|
|
sliding = true;
|
|
|
|
|
|
|
|
real_t factor = maximp / Math::sqrt(impulseSquared);
|
|
|
|
|
|
|
|
wheelInfo.m_skidInfo *= factor;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (sliding) {
|
|
|
|
for (int wheel = 0; wheel < wheels.size(); wheel++) {
|
|
|
|
if (m_sideImpulse[wheel] != real_t(0.)) {
|
|
|
|
if (wheels[wheel]->m_skidInfo < real_t(1.)) {
|
2018-07-25 03:11:03 +02:00
|
|
|
m_forwardImpulse.write[wheel] *= wheels[wheel]->m_skidInfo;
|
|
|
|
m_sideImpulse.write[wheel] *= wheels[wheel]->m_skidInfo;
|
2014-08-14 15:31:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// apply the impulses
|
|
|
|
{
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int wheel = 0; wheel < wheels.size(); wheel++) {
|
2020-03-27 08:44:44 +01:00
|
|
|
VehicleWheel3D &wheelInfo = *wheels[wheel];
|
2014-08-14 15:31:38 +02:00
|
|
|
|
|
|
|
Vector3 rel_pos = wheelInfo.m_raycastInfo.m_contactPointWS -
|
2021-10-28 15:19:35 +02:00
|
|
|
s->get_transform().origin;
|
2014-08-14 15:31:38 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (m_forwardImpulse[wheel] != real_t(0.)) {
|
2020-03-26 05:23:34 +01:00
|
|
|
s->apply_impulse(m_forwardWS[wheel] * (m_forwardImpulse[wheel]), rel_pos);
|
2014-08-14 15:31:38 +02:00
|
|
|
}
|
2017-03-05 16:44:50 +01:00
|
|
|
if (m_sideImpulse[wheel] != real_t(0.)) {
|
2020-03-26 22:49:16 +01:00
|
|
|
PhysicsBody3D *groundObject = wheelInfo.m_raycastInfo.m_groundObject;
|
2014-08-14 15:31:38 +02:00
|
|
|
|
|
|
|
Vector3 rel_pos2;
|
|
|
|
if (groundObject) {
|
2017-03-05 16:44:50 +01:00
|
|
|
rel_pos2 = wheelInfo.m_raycastInfo.m_contactPointWS - groundObject->get_global_transform().origin;
|
2014-08-14 15:31:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Vector3 sideImp = m_axle[wheel] * m_sideImpulse[wheel];
|
|
|
|
|
|
|
|
#if defined ROLLING_INFLUENCE_FIX // fix. It only worked if car's up was along Y - VT.
|
2020-10-17 07:08:21 +02:00
|
|
|
Vector3 vChassisWorldUp = s->get_transform().basis.transposed()[1]; //getRigidBody()->getCenterOfMassTransform3D().getBasis().getColumn(m_indexUpAxis);
|
2017-03-05 16:44:50 +01:00
|
|
|
rel_pos -= vChassisWorldUp * (vChassisWorldUp.dot(rel_pos) * (1.f - wheelInfo.m_rollInfluence));
|
2014-08-14 15:31:38 +02:00
|
|
|
#else
|
|
|
|
rel_pos[1] *= wheelInfo.m_rollInfluence; //?
|
|
|
|
#endif
|
2020-03-26 05:23:34 +01:00
|
|
|
s->apply_impulse(sideImp, rel_pos);
|
2014-08-14 15:31:38 +02:00
|
|
|
|
|
|
|
//apply friction impulse on the ground
|
|
|
|
//todo
|
|
|
|
//groundObject->applyImpulse(-sideImp,rel_pos2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-05 01:09:41 +02:00
|
|
|
void VehicleBody3D::_body_state_changed(PhysicsDirectBodyState3D *p_state) {
|
2022-08-25 19:35:52 +02:00
|
|
|
RigidBody3D::_body_state_changed(p_state);
|
2014-08-14 15:31:38 +02:00
|
|
|
|
2021-06-05 01:09:41 +02:00
|
|
|
real_t step = p_state->get_step();
|
2014-08-14 15:31:38 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int i = 0; i < wheels.size(); i++) {
|
2021-06-05 01:09:41 +02:00
|
|
|
_update_wheel(i, p_state);
|
2014-08-14 15:31:38 +02:00
|
|
|
}
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int i = 0; i < wheels.size(); i++) {
|
2021-06-05 01:09:41 +02:00
|
|
|
_ray_cast(i, p_state);
|
|
|
|
wheels[i]->set_transform(p_state->get_transform().inverse() * wheels[i]->m_worldTransform);
|
2014-08-14 15:31:38 +02:00
|
|
|
}
|
|
|
|
|
2021-06-05 01:09:41 +02:00
|
|
|
_update_suspension(p_state);
|
2014-08-14 15:31:38 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int i = 0; i < wheels.size(); i++) {
|
2014-08-14 15:31:38 +02:00
|
|
|
//apply suspension force
|
2020-03-27 08:44:44 +01:00
|
|
|
VehicleWheel3D &wheel = *wheels[i];
|
2014-08-14 15:31:38 +02:00
|
|
|
|
|
|
|
real_t suspensionForce = wheel.m_wheelsSuspensionForce;
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (suspensionForce > wheel.m_maxSuspensionForce) {
|
2014-08-14 15:31:38 +02:00
|
|
|
suspensionForce = wheel.m_maxSuspensionForce;
|
|
|
|
}
|
|
|
|
Vector3 impulse = wheel.m_raycastInfo.m_contactNormalWS * suspensionForce * step;
|
2021-06-05 01:09:41 +02:00
|
|
|
Vector3 relative_position = wheel.m_raycastInfo.m_contactPointWS - p_state->get_transform().origin;
|
2014-08-14 15:31:38 +02:00
|
|
|
|
2021-06-05 01:09:41 +02:00
|
|
|
p_state->apply_impulse(impulse, relative_position);
|
2014-08-14 15:31:38 +02:00
|
|
|
}
|
|
|
|
|
2021-06-05 01:09:41 +02:00
|
|
|
_update_friction(p_state);
|
2014-08-14 15:31:38 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
for (int i = 0; i < wheels.size(); i++) {
|
2020-03-27 08:44:44 +01:00
|
|
|
VehicleWheel3D &wheel = *wheels[i];
|
2021-06-05 01:09:41 +02:00
|
|
|
Vector3 relpos = wheel.m_raycastInfo.m_hardPointWS - p_state->get_transform().origin;
|
|
|
|
Vector3 vel = p_state->get_linear_velocity() + (p_state->get_angular_velocity()).cross(relpos); // * mPos);
|
2014-08-14 15:31:38 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
if (wheel.m_raycastInfo.m_isInContact) {
|
2021-06-05 01:09:41 +02:00
|
|
|
const Transform3D &chassisWorldTransform = p_state->get_transform();
|
2014-08-14 15:31:38 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
Vector3 fwd(
|
|
|
|
chassisWorldTransform.basis[0][Vector3::AXIS_Z],
|
|
|
|
chassisWorldTransform.basis[1][Vector3::AXIS_Z],
|
|
|
|
chassisWorldTransform.basis[2][Vector3::AXIS_Z]);
|
2014-08-14 15:31:38 +02:00
|
|
|
|
|
|
|
real_t proj = fwd.dot(wheel.m_raycastInfo.m_contactNormalWS);
|
|
|
|
fwd -= wheel.m_raycastInfo.m_contactNormalWS * proj;
|
|
|
|
|
|
|
|
real_t proj2 = fwd.dot(vel);
|
|
|
|
|
|
|
|
wheel.m_deltaRotation = (proj2 * step) / (wheel.m_wheelRadius);
|
|
|
|
}
|
|
|
|
|
2019-04-10 00:49:37 +02:00
|
|
|
wheel.m_rotation += wheel.m_deltaRotation;
|
|
|
|
wheel.m_rpm = ((wheel.m_deltaRotation / step) * 60) / Math_TAU;
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
wheel.m_deltaRotation *= real_t(0.99); //damping of rotation when not in contact
|
2014-08-14 15:31:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-30 00:22:12 +01:00
|
|
|
void VehicleBody3D::set_engine_force(real_t p_engine_force) {
|
2017-08-11 21:10:05 +02:00
|
|
|
engine_force = p_engine_force;
|
2019-07-18 20:16:28 +02:00
|
|
|
for (int i = 0; i < wheels.size(); i++) {
|
2020-03-27 08:44:44 +01:00
|
|
|
VehicleWheel3D &wheelInfo = *wheels[i];
|
2020-05-14 16:41:43 +02:00
|
|
|
if (wheelInfo.engine_traction) {
|
2019-07-18 20:16:28 +02:00
|
|
|
wheelInfo.m_engineForce = p_engine_force;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2019-07-18 20:16:28 +02:00
|
|
|
}
|
2014-09-03 04:13:40 +02:00
|
|
|
}
|
|
|
|
|
2021-01-30 00:22:12 +01:00
|
|
|
real_t VehicleBody3D::get_engine_force() const {
|
2014-09-03 04:13:40 +02:00
|
|
|
return engine_force;
|
|
|
|
}
|
|
|
|
|
2021-01-30 00:22:12 +01:00
|
|
|
void VehicleBody3D::set_brake(real_t p_brake) {
|
2017-03-05 16:44:50 +01:00
|
|
|
brake = p_brake;
|
2019-07-18 20:16:28 +02:00
|
|
|
for (int i = 0; i < wheels.size(); i++) {
|
2020-03-27 08:44:44 +01:00
|
|
|
VehicleWheel3D &wheelInfo = *wheels[i];
|
2019-07-18 20:16:28 +02:00
|
|
|
wheelInfo.m_brake = p_brake;
|
|
|
|
}
|
2014-09-03 04:13:40 +02:00
|
|
|
}
|
2020-05-14 14:29:06 +02:00
|
|
|
|
2021-01-30 00:22:12 +01:00
|
|
|
real_t VehicleBody3D::get_brake() const {
|
2014-09-03 04:13:40 +02:00
|
|
|
return brake;
|
|
|
|
}
|
|
|
|
|
2021-01-30 00:22:12 +01:00
|
|
|
void VehicleBody3D::set_steering(real_t p_steering) {
|
2017-03-05 16:44:50 +01:00
|
|
|
m_steeringValue = p_steering;
|
2019-07-18 20:16:28 +02:00
|
|
|
for (int i = 0; i < wheels.size(); i++) {
|
2020-03-27 08:44:44 +01:00
|
|
|
VehicleWheel3D &wheelInfo = *wheels[i];
|
2020-05-14 16:41:43 +02:00
|
|
|
if (wheelInfo.steers) {
|
2019-07-18 20:16:28 +02:00
|
|
|
wheelInfo.m_steering = p_steering;
|
2020-05-14 16:41:43 +02:00
|
|
|
}
|
2019-07-18 20:16:28 +02:00
|
|
|
}
|
2014-09-03 04:13:40 +02:00
|
|
|
}
|
2020-05-14 14:29:06 +02:00
|
|
|
|
2021-01-30 00:22:12 +01:00
|
|
|
real_t VehicleBody3D::get_steering() const {
|
2014-09-03 04:13:40 +02:00
|
|
|
return m_steeringValue;
|
|
|
|
}
|
|
|
|
|
2020-03-27 08:44:44 +01:00
|
|
|
void VehicleBody3D::_bind_methods() {
|
|
|
|
ClassDB::bind_method(D_METHOD("set_engine_force", "engine_force"), &VehicleBody3D::set_engine_force);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_engine_force"), &VehicleBody3D::get_engine_force);
|
2014-09-03 04:13:40 +02:00
|
|
|
|
2020-03-27 08:44:44 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("set_brake", "brake"), &VehicleBody3D::set_brake);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_brake"), &VehicleBody3D::get_brake);
|
2016-05-21 11:35:55 +02:00
|
|
|
|
2020-03-27 08:44:44 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("set_steering", "steering"), &VehicleBody3D::set_steering);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_steering"), &VehicleBody3D::get_steering);
|
2014-08-14 15:31:38 +02:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
ADD_GROUP("Motion", "");
|
2021-12-03 01:09:19 +01:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "engine_force", PROPERTY_HINT_RANGE, U"-1024,1024.0,0.01,or_greater,suffix:kg\u22C5m/s\u00B2 (N)"), "set_engine_force", "get_engine_force");
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "brake", PROPERTY_HINT_RANGE, U"0.0,1.0,0.01,suffix:kg\u22C5m/s\u00B2 (N)"), "set_brake", "get_brake");
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "steering", PROPERTY_HINT_RANGE, "-180,180.0,0.01,radians"), "set_steering", "get_steering");
|
2014-08-14 15:31:38 +02:00
|
|
|
}
|
|
|
|
|
2020-03-27 08:44:44 +01:00
|
|
|
VehicleBody3D::VehicleBody3D() {
|
2014-08-14 15:31:38 +02:00
|
|
|
exclude.insert(get_rid());
|
2014-09-03 04:13:40 +02:00
|
|
|
set_mass(40);
|
2014-08-14 15:31:38 +02:00
|
|
|
}
|