Debian Codegraph @

modules/jolt_physics/objects/jolt_body_3d.h

raw symbols SCIP
1 /**************************************************************************/
2 /* jolt_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 #pragma once
32
33 #include "jolt_physics_direct_body_state_3d.h"
34 #include "jolt_shaped_object_3d.h"
35
36 class JoltArea3D;
37 class JoltJoint3D;
38 class JoltSoftBody3D;
39
40 class JoltBody3D final : public JoltShapedObject3D {
41 public:
42 typedef PhysicsServer3D::BodyDampMode DampMode;
43
44 struct Contact {
45 Vector3 normal;
46 Vector3 position;
47 Vector3 collider_position;
48 Vector3 velocity;
49 Vector3 collider_velocity;
50 Vector3 impulse;
51 ObjectID collider_id;
52 RID collider_rid;
53 float depth = 0.0f;
54 int shape_index = 0;
55 int collider_shape_index = 0;
56 };
57
58 private:
59 friend class JoltBodyActivationListener3D;
60
61 SelfList<JoltBody3D> call_queries_element;
62
63 LocalVector<RID> exceptions;
64 LocalVector<Contact> contacts;
65 LocalVector<JoltArea3D *> areas;
66 LocalVector<JoltJoint3D *> joints;
67
68 Variant custom_integration_userdata;
69
70 Transform3D kinematic_transform;
71
72 Vector3 inertia;
73 Vector3 center_of_mass_custom;
74 Vector3 constant_force;
75 Vector3 constant_torque;
76 Vector3 linear_surface_velocity;
77 Vector3 angular_surface_velocity;
78 Vector3 gravity;
79
80 Callable state_sync_callback;
81 Callable custom_integration_callback;
82
83 JoltPhysicsDirectBodyState3D *direct_state = nullptr;
84
85 PhysicsServer3D::BodyMode mode = PhysicsServer3D::BODY_MODE_RIGID;
86
87 DampMode linear_damp_mode = PhysicsServer3D::BODY_DAMP_MODE_COMBINE;
88 DampMode angular_damp_mode = PhysicsServer3D::BODY_DAMP_MODE_COMBINE;
89
90 float mass = 1.0f;
91 float linear_damp = 0.0f;
92 float angular_damp = 0.0f;
93 float total_linear_damp = 0.0f;
94 float total_angular_damp = 0.0f;
95 float gravity_scale = 1.0f;
96 float collision_priority = 1.0f;
97
98 int contact_count = 0;
99
100 uint32_t locked_axes = 0;
101
102 bool sleep_allowed = true;
103 bool sleep_initially = false;
104 bool custom_center_of_mass = false;
105 bool custom_integrator = false;
106
107 virtual JPH::BroadPhaseLayer _get_broad_phase_layer() const override;
108 virtual JPH::ObjectLayer _get_object_layer() const override;
109
110 virtual JPH::EMotionType _get_motion_type() const override;
111
112 virtual void _add_to_space() override;
113
114 bool _should_call_queries() const { return state_sync_callback.is_valid() || custom_integration_callback.is_valid(); }
115 void _enqueue_call_queries();
116 void _dequeue_call_queries();
117
118 void _integrate_forces(float p_step, JPH::Body &p_jolt_body);
119 void _move_kinematic(float p_step, JPH::Body &p_jolt_body);
120
121 JPH::EAllowedDOFs _calculate_allowed_dofs() const;
122
123 JPH::MassProperties _calculate_mass_properties(const JPH::Shape &p_shape) const;
124 JPH::MassProperties _calculate_mass_properties() const;
125
126 void _on_wake_up();
127
128 void _update_mass_properties();
129 void _update_gravity(JPH::Body &p_jolt_body);
130 void _update_damp();
131 void _update_kinematic_transform();
132 void _update_group_filter();
133 void _update_joint_constraints();
134 void _update_possible_kinematic_contacts();
135 void _update_sleep_allowed();
136
137 void _destroy_joint_constraints();
138
139 void _exit_all_areas();
140
141 void _mode_changed();
142 virtual void _shapes_committed() override;
143 virtual void _space_changing() override;
144 virtual void _space_changed() override;
145 void _areas_changed();
146 void _joints_changed();
147 void _transform_changed();
148 void _motion_changed();
149 void _exceptions_changed();
150 void _axis_lock_changed();
151 void _contact_reporting_changed();
152 void _sleep_allowed_changed();
153
154 public:
155 JoltBody3D();
156 virtual ~JoltBody3D() override;
157
158 void set_transform(Transform3D p_transform);
159
160 Variant get_state(PhysicsServer3D::BodyState p_state) const;
161 void set_state(PhysicsServer3D::BodyState p_state, const Variant &p_value);
162
163 Variant get_param(PhysicsServer3D::BodyParameter p_param) const;
164 void set_param(PhysicsServer3D::BodyParameter p_param, const Variant &p_value);
165
166 bool has_state_sync_callback() const { return state_sync_callback.is_valid(); }
167 void set_state_sync_callback(const Callable &p_callback) { state_sync_callback = p_callback; }
168
169 bool has_custom_integration_callback() const { return custom_integration_callback.is_valid(); }
170 void set_custom_integration_callback(const Callable &p_callback, const Variant &p_userdata) {
171 custom_integration_callback = p_callback;
172 custom_integration_userdata = p_userdata;
173 }
174
175 bool has_custom_integrator() const { return custom_integrator; }
176 void set_custom_integrator(bool p_enabled);
177
178 bool is_sleeping() const;
179 void set_is_sleeping(bool p_enabled);
180
181 void put_to_sleep() { set_is_sleeping(true); }
182 void wake_up() { set_is_sleeping(false); }
183
184 bool is_sleep_allowed() const { return sleep_allowed; }
185 bool is_sleep_actually_allowed() const;
186 void set_is_sleep_allowed(bool p_enabled);
187
188 Basis get_principal_inertia_axes() const;
189 Vector3 get_inverse_inertia() const;
190 Basis get_inverse_inertia_tensor() const;
191
192 void set_linear_velocity(const Vector3 &p_velocity);
193 void set_angular_velocity(const Vector3 &p_velocity);
194 void set_axis_velocity(const Vector3 &p_axis_velocity);
195
196 virtual Vector3 get_velocity_at_position(const Vector3 &p_position) const override;
197
198 virtual bool has_custom_center_of_mass() const override { return custom_center_of_mass; }
199 virtual Vector3 get_center_of_mass_custom() const override { return center_of_mass_custom; }
200 void set_center_of_mass_custom(const Vector3 &p_center_of_mass);
201
202 int get_max_contacts_reported() const { return contacts.size(); }
203 void set_max_contacts_reported(int p_count);
204
205 int get_contact_count() const { return contact_count; }
206 const Contact &get_contact(int p_index) { return contacts[p_index]; }
207 virtual bool reports_contacts() const override { return !contacts.is_empty(); }
208
209 bool reports_all_kinematic_contacts() const;
210
211 void add_contact(const JoltBody3D *p_collider, float p_depth, int p_shape_index, int p_collider_shape_index, const Vector3 &p_normal, const Vector3 &p_position, const Vector3 &p_collider_position, const Vector3 &p_velocity, const Vector3 &p_collider_velocity, const Vector3 &p_impulse);
212
213 void reset_mass_properties();
214
215 void apply_force(const Vector3 &p_force, const Vector3 &p_position);
216 void apply_central_force(const Vector3 &p_force);
217 void apply_impulse(const Vector3 &p_impulse, const Vector3 &p_position);
218
219 void apply_central_impulse(const Vector3 &p_impulse);
220 void apply_torque(const Vector3 &p_torque);
221 void apply_torque_impulse(const Vector3 &p_impulse);
222
223 void add_constant_central_force(const Vector3 &p_force);
224 void add_constant_force(const Vector3 &p_force, const Vector3 &p_position);
225 void add_constant_torque(const Vector3 &p_torque);
226
227 Vector3 get_constant_force() const;
228 void set_constant_force(const Vector3 &p_force);
229
230 Vector3 get_constant_torque() const;
231 void set_constant_torque(const Vector3 &p_torque);
232
233 Vector3 get_linear_surface_velocity() const { return linear_surface_velocity; }
234 Vector3 get_angular_surface_velocity() const { return angular_surface_velocity; }
235
236 void add_collision_exception(const RID &p_excepted_body);
237 void remove_collision_exception(const RID &p_excepted_body);
238 bool has_collision_exception(const RID &p_excepted_body) const;
239
240 const LocalVector<RID> &get_collision_exceptions() const { return exceptions; }
241
242 void add_area(JoltArea3D *p_area);
243 void remove_area(JoltArea3D *p_area);
244
245 void add_joint(JoltJoint3D *p_joint);
246 void remove_joint(JoltJoint3D *p_joint);
247
248 void call_queries();
249
250 virtual void pre_step(float p_step, JPH::Body &p_jolt_body) override;
251
252 JoltPhysicsDirectBodyState3D *get_direct_state();
253
254 PhysicsServer3D::BodyMode get_mode() const { return mode; }
255
256 void set_mode(PhysicsServer3D::BodyMode p_mode);
257
258 bool is_static() const { return mode == PhysicsServer3D::BODY_MODE_STATIC; }
259 bool is_kinematic() const { return mode == PhysicsServer3D::BODY_MODE_KINEMATIC; }
260 bool is_rigid_free() const { return mode == PhysicsServer3D::BODY_MODE_RIGID; }
261 bool is_rigid_linear() const { return mode == PhysicsServer3D::BODY_MODE_RIGID_LINEAR; }
262 bool is_rigid() const { return is_rigid_free() || is_rigid_linear(); }
263
264 bool is_ccd_enabled() const;
265 void set_ccd_enabled(bool p_enabled);
266
267 float get_mass() const { return mass; }
268 void set_mass(float p_mass);
269
270 Vector3 get_inertia() const { return inertia; }
271 void set_inertia(const Vector3 &p_inertia);
272
273 float get_bounce() const;
274 void set_bounce(float p_bounce);
275
276 float get_friction() const;
277 void set_friction(float p_friction);
278
279 float get_gravity_scale() const { return gravity_scale; }
280 void set_gravity_scale(float p_scale);
281
282 Vector3 get_gravity() const { return gravity; }
283
284 float get_linear_damp() const { return linear_damp; }
285 void set_linear_damp(float p_damp);
286
287 float get_angular_damp() const { return angular_damp; }
288 void set_angular_damp(float p_damp);
289
290 float get_total_linear_damp() const { return total_linear_damp; }
291 float get_total_angular_damp() const { return total_angular_damp; }
292
293 float get_collision_priority() const { return collision_priority; }
294 void set_collision_priority(float p_priority) { collision_priority = p_priority; }
295
296 DampMode get_linear_damp_mode() const { return linear_damp_mode; }
297 void set_linear_damp_mode(DampMode p_mode);
298
299 DampMode get_angular_damp_mode() const { return angular_damp_mode; }
300 void set_angular_damp_mode(DampMode p_mode);
301
302 bool is_axis_locked(PhysicsServer3D::BodyAxis p_axis) const;
303 void set_axis_lock(PhysicsServer3D::BodyAxis p_axis, bool p_enabled);
304 bool are_axes_locked() const { return locked_axes != 0; }
305
306 virtual bool can_interact_with(const JoltBody3D &p_other) const override;
307 virtual bool can_interact_with(const JoltSoftBody3D &p_other) const override;
308 virtual bool can_interact_with(const JoltArea3D &p_other) const override;
309 };