summaryrefslogtreecommitdiff
path: root/scene/3d/vehicle_body_3d.h
blob: 646071a36380898fbef88f89f98dbdda5392f457 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/*************************************************************************/
/*  vehicle_body_3d.h                                                    */
/*************************************************************************/
/*                       This file is part of:                           */
/*                           GODOT ENGINE                                */
/*                      https://godotengine.org                          */
/*************************************************************************/
/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur.                 */
/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md).   */
/*                                                                       */
/* 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.                */
/*************************************************************************/

#ifndef VEHICLE_BODY_H
#define VEHICLE_BODY_H

#include "scene/3d/physics_body_3d.h"

class VehicleBody3D;

class VehicleWheel3D : public Node3D {
	GDCLASS(VehicleWheel3D, Node3D);

	friend class VehicleBody3D;

	Transform m_worldTransform;
	Transform local_xform;
	bool engine_traction = false;
	bool steers = false;

	Vector3 m_chassisConnectionPointCS; //const
	Vector3 m_wheelDirectionCS; //const
	Vector3 m_wheelAxleCS; // const or modified by steering

	real_t m_suspensionRestLength = 0.15;
	real_t m_maxSuspensionTravelCm = 500.0;
	real_t m_wheelRadius = 0.5;

	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;

	VehicleBody3D *body = nullptr;

	//btVector3	m_wheelAxleCS; // const or modified by steering ?

	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;

	real_t m_clippedInvContactDotSuspension = 1.0;
	real_t m_suspensionRelativeVelocity = 0.0;
	//calculated by suspension
	real_t m_wheelsSuspensionForce = 0.0;
	real_t m_skidInfo = 0.0;

	struct RaycastInfo {
		//set by raycaster
		Vector3 m_contactNormalWS; //contactnormal
		Vector3 m_contactPointWS; //raycast hitpoint
		real_t m_suspensionLength = 0.0;
		Vector3 m_hardPointWS; //raycast starting point
		Vector3 m_wheelDirectionWS; //direction in worldspace
		Vector3 m_wheelAxleWS; // axle in worldspace
		bool m_isInContact = false;
		PhysicsBody3D *m_groundObject = nullptr; //could be general void* ptr
	} m_raycastInfo;

	void _update(PhysicsDirectBodyState3D *s);

protected:
	void _notification(int p_what);
	static void _bind_methods();

public:
	void set_radius(real_t p_radius);
	real_t get_radius() const;

	void set_suspension_rest_length(real_t p_length);
	real_t get_suspension_rest_length() const;

	void set_suspension_travel(real_t p_length);
	real_t get_suspension_travel() const;

	void set_suspension_stiffness(real_t p_value);
	real_t get_suspension_stiffness() const;

	void set_suspension_max_force(real_t p_value);
	real_t get_suspension_max_force() const;

	void set_damping_compression(real_t p_value);
	real_t get_damping_compression() const;

	void set_damping_relaxation(real_t p_value);
	real_t get_damping_relaxation() const;

	void set_friction_slip(real_t p_value);
	real_t 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;

	bool is_in_contact() const;

	void set_roll_influence(real_t p_value);
	real_t get_roll_influence() const;

	real_t get_skidinfo() const;

	real_t get_rpm() const;

	void set_engine_force(real_t p_engine_force);
	real_t get_engine_force() const;

	void set_brake(real_t p_brake);
	real_t get_brake() const;

	void set_steering(real_t p_steering);
	real_t get_steering() const;

	TypedArray<String> get_configuration_warnings() const override;

	VehicleWheel3D();
};

class VehicleBody3D : public RigidBody3D {
	GDCLASS(VehicleBody3D, RigidBody3D);

	real_t engine_force = 0.0;
	real_t brake = 0.0;

	real_t m_pitchControl = 0.0;
	real_t m_steeringValue = 0.0;
	real_t m_currentVehicleSpeedKmHour = 0.0;

	Set<RID> exclude;

	Vector<Vector3> m_forwardWS;
	Vector<Vector3> m_axle;
	Vector<real_t> m_forwardImpulse;
	Vector<real_t> m_sideImpulse;

	struct btVehicleWheelContactPoint {
		PhysicsDirectBodyState3D *m_s = nullptr;
		PhysicsBody3D *m_body1 = nullptr;
		Vector3 m_frictionPositionWorld;
		Vector3 m_frictionDirectionWorld;
		real_t m_jacDiagABInv = 0.0;
		real_t m_maxImpulse = 0.0;

		btVehicleWheelContactPoint(PhysicsDirectBodyState3D *s, PhysicsBody3D *body1, const Vector3 &frictionPosWorld, const Vector3 &frictionDirectionWorld, real_t maxImpulse);
	};

	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);
	real_t _calc_rolling_friction(btVehicleWheelContactPoint &contactPoint);

	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);

	friend class VehicleWheel3D;
	Vector<VehicleWheel3D *> wheels;

	static void _bind_methods();

	void _direct_state_changed(Object *p_state) override;

public:
	void set_engine_force(real_t p_engine_force);
	real_t get_engine_force() const;

	void set_brake(real_t p_brake);
	real_t get_brake() const;

	void set_steering(real_t p_steering);
	real_t get_steering() const;

	VehicleBody3D();
};

#endif // VEHICLE_BODY_H