1 | /**************************************************************************/ |
2 | /* vehicle_body_3d.h */ |
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 | #ifndef VEHICLE_BODY_3D_H |
32 | #define VEHICLE_BODY_3D_H |
33 | |
34 | #include "scene/3d/physics_body_3d.h" |
35 | |
36 | class VehicleBody3D; |
37 | |
38 | class VehicleWheel3D : public Node3D { |
39 | GDCLASS(VehicleWheel3D, Node3D); |
40 | |
41 | friend class VehicleBody3D; |
42 | |
43 | Transform3D m_worldTransform; |
44 | Transform3D local_xform; |
45 | bool engine_traction = false; |
46 | bool steers = false; |
47 | |
48 | Vector3 m_chassisConnectionPointCS; //const |
49 | Vector3 m_wheelDirectionCS; //const |
50 | Vector3 m_wheelAxleCS; // const or modified by steering |
51 | |
52 | real_t m_suspensionRestLength = 0.15; |
53 | real_t m_maxSuspensionTravel = 0.2; |
54 | real_t m_wheelRadius = 0.5; |
55 | |
56 | real_t m_suspensionStiffness = 5.88; |
57 | real_t m_wheelsDampingCompression = 0.83; |
58 | real_t m_wheelsDampingRelaxation = 0.88; |
59 | real_t m_frictionSlip = 10.5; |
60 | real_t m_maxSuspensionForce = 6000.0; |
61 | bool m_bIsFrontWheel = false; |
62 | |
63 | VehicleBody3D *body = nullptr; |
64 | |
65 | //btVector3 m_wheelAxleCS; // const or modified by steering ? |
66 | |
67 | real_t m_steering = 0.0; |
68 | real_t m_rotation = 0.0; |
69 | real_t m_deltaRotation = 0.0; |
70 | real_t m_rpm = 0.0; |
71 | real_t m_rollInfluence = 0.1; |
72 | real_t m_engineForce = 0.0; |
73 | real_t m_brake = 0.0; |
74 | |
75 | real_t m_clippedInvContactDotSuspension = 1.0; |
76 | real_t m_suspensionRelativeVelocity = 0.0; |
77 | //calculated by suspension |
78 | real_t m_wheelsSuspensionForce = 0.0; |
79 | real_t m_skidInfo = 0.0; |
80 | |
81 | struct RaycastInfo { |
82 | //set by raycaster |
83 | Vector3 m_contactNormalWS; //contactnormal |
84 | Vector3 m_contactPointWS; //raycast hitpoint |
85 | real_t m_suspensionLength = 0.0; |
86 | Vector3 m_hardPointWS; //raycast starting point |
87 | Vector3 m_wheelDirectionWS; //direction in worldspace |
88 | Vector3 m_wheelAxleWS; // axle in worldspace |
89 | bool m_isInContact = false; |
90 | PhysicsBody3D *m_groundObject = nullptr; //could be general void* ptr |
91 | } m_raycastInfo; |
92 | |
93 | void _update(PhysicsDirectBodyState3D *s); |
94 | |
95 | protected: |
96 | void _notification(int p_what); |
97 | static void _bind_methods(); |
98 | |
99 | public: |
100 | void set_radius(real_t p_radius); |
101 | real_t get_radius() const; |
102 | |
103 | void set_suspension_rest_length(real_t p_length); |
104 | real_t get_suspension_rest_length() const; |
105 | |
106 | void set_suspension_travel(real_t p_length); |
107 | real_t get_suspension_travel() const; |
108 | |
109 | void set_suspension_stiffness(real_t p_value); |
110 | real_t get_suspension_stiffness() const; |
111 | |
112 | void set_suspension_max_force(real_t p_value); |
113 | real_t get_suspension_max_force() const; |
114 | |
115 | void set_damping_compression(real_t p_value); |
116 | real_t get_damping_compression() const; |
117 | |
118 | void set_damping_relaxation(real_t p_value); |
119 | real_t get_damping_relaxation() const; |
120 | |
121 | void set_friction_slip(real_t p_value); |
122 | real_t get_friction_slip() const; |
123 | |
124 | void set_use_as_traction(bool p_enable); |
125 | bool is_used_as_traction() const; |
126 | |
127 | void set_use_as_steering(bool p_enabled); |
128 | bool is_used_as_steering() const; |
129 | |
130 | bool is_in_contact() const; |
131 | |
132 | Node3D *get_contact_body() const; |
133 | |
134 | void set_roll_influence(real_t p_value); |
135 | real_t get_roll_influence() const; |
136 | |
137 | real_t get_skidinfo() const; |
138 | |
139 | real_t get_rpm() const; |
140 | |
141 | void set_engine_force(real_t p_engine_force); |
142 | real_t get_engine_force() const; |
143 | |
144 | void set_brake(real_t p_brake); |
145 | real_t get_brake() const; |
146 | |
147 | void set_steering(real_t p_steering); |
148 | real_t get_steering() const; |
149 | |
150 | PackedStringArray get_configuration_warnings() const override; |
151 | |
152 | VehicleWheel3D(); |
153 | }; |
154 | |
155 | class VehicleBody3D : public RigidBody3D { |
156 | GDCLASS(VehicleBody3D, RigidBody3D); |
157 | |
158 | real_t engine_force = 0.0; |
159 | real_t brake = 0.0; |
160 | |
161 | real_t m_pitchControl = 0.0; |
162 | real_t m_steeringValue = 0.0; |
163 | real_t m_currentVehicleSpeedKmHour = 0.0; |
164 | |
165 | HashSet<RID> exclude; |
166 | |
167 | Vector<Vector3> m_forwardWS; |
168 | Vector<Vector3> m_axle; |
169 | Vector<real_t> m_forwardImpulse; |
170 | Vector<real_t> m_sideImpulse; |
171 | |
172 | struct btVehicleWheelContactPoint { |
173 | PhysicsDirectBodyState3D *m_s = nullptr; |
174 | PhysicsBody3D *m_body1 = nullptr; |
175 | Vector3 m_frictionPositionWorld; |
176 | Vector3 m_frictionDirectionWorld; |
177 | real_t m_jacDiagABInv = 0.0; |
178 | real_t m_maxImpulse = 0.0; |
179 | |
180 | btVehicleWheelContactPoint(PhysicsDirectBodyState3D *s, PhysicsBody3D *body1, const Vector3 &frictionPosWorld, const Vector3 &frictionDirectionWorld, real_t maxImpulse); |
181 | }; |
182 | |
183 | 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); |
184 | real_t _calc_rolling_friction(btVehicleWheelContactPoint &contactPoint); |
185 | |
186 | void _update_friction(PhysicsDirectBodyState3D *s); |
187 | void _update_suspension(PhysicsDirectBodyState3D *s); |
188 | real_t _ray_cast(int p_idx, PhysicsDirectBodyState3D *s); |
189 | void _update_wheel_transform(VehicleWheel3D &wheel, PhysicsDirectBodyState3D *s); |
190 | void _update_wheel(int p_idx, PhysicsDirectBodyState3D *s); |
191 | |
192 | friend class VehicleWheel3D; |
193 | Vector<VehicleWheel3D *> wheels; |
194 | |
195 | static void _bind_methods(); |
196 | |
197 | static void _body_state_changed_callback(void *p_instance, PhysicsDirectBodyState3D *p_state); |
198 | virtual void _body_state_changed(PhysicsDirectBodyState3D *p_state) override; |
199 | |
200 | public: |
201 | void set_engine_force(real_t p_engine_force); |
202 | real_t get_engine_force() const; |
203 | |
204 | void set_brake(real_t p_brake); |
205 | real_t get_brake() const; |
206 | |
207 | void set_steering(real_t p_steering); |
208 | real_t get_steering() const; |
209 | |
210 | VehicleBody3D(); |
211 | }; |
212 | |
213 | #endif // VEHICLE_BODY_3D_H |
214 | |