1 | /**************************************************************************/ |
2 | /* godot_physics_server_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 GODOT_PHYSICS_SERVER_3D_H |
32 | #define GODOT_PHYSICS_SERVER_3D_H |
33 | |
34 | #include "godot_joint_3d.h" |
35 | #include "godot_shape_3d.h" |
36 | #include "godot_space_3d.h" |
37 | #include "godot_step_3d.h" |
38 | |
39 | #include "core/templates/rid_owner.h" |
40 | #include "servers/physics_server_3d.h" |
41 | |
42 | class GodotPhysicsServer3D : public PhysicsServer3D { |
43 | GDCLASS(GodotPhysicsServer3D, PhysicsServer3D); |
44 | |
45 | friend class GodotPhysicsDirectSpaceState3D; |
46 | bool active = true; |
47 | |
48 | int island_count = 0; |
49 | int active_objects = 0; |
50 | int collision_pairs = 0; |
51 | |
52 | bool using_threads = false; |
53 | bool doing_sync = false; |
54 | bool flushing_queries = false; |
55 | |
56 | GodotStep3D *stepper = nullptr; |
57 | HashSet<const GodotSpace3D *> active_spaces; |
58 | |
59 | mutable RID_PtrOwner<GodotShape3D, true> shape_owner; |
60 | mutable RID_PtrOwner<GodotSpace3D, true> space_owner; |
61 | mutable RID_PtrOwner<GodotArea3D, true> area_owner; |
62 | mutable RID_PtrOwner<GodotBody3D, true> body_owner; |
63 | mutable RID_PtrOwner<GodotSoftBody3D, true> soft_body_owner; |
64 | mutable RID_PtrOwner<GodotJoint3D, true> joint_owner; |
65 | |
66 | //void _clear_query(QuerySW *p_query); |
67 | friend class GodotCollisionObject3D; |
68 | SelfList<GodotCollisionObject3D>::List pending_shape_update_list; |
69 | void _update_shapes(); |
70 | |
71 | static GodotPhysicsServer3D *godot_singleton; |
72 | |
73 | public: |
74 | struct CollCbkData { |
75 | int max; |
76 | int amount; |
77 | Vector3 *ptr = nullptr; |
78 | }; |
79 | |
80 | static void _shape_col_cbk(const Vector3 &p_point_A, int p_index_A, const Vector3 &p_point_B, int p_index_B, const Vector3 &normal, void *p_userdata); |
81 | |
82 | virtual RID world_boundary_shape_create() override; |
83 | virtual RID separation_ray_shape_create() override; |
84 | virtual RID sphere_shape_create() override; |
85 | virtual RID box_shape_create() override; |
86 | virtual RID capsule_shape_create() override; |
87 | virtual RID cylinder_shape_create() override; |
88 | virtual RID convex_polygon_shape_create() override; |
89 | virtual RID concave_polygon_shape_create() override; |
90 | virtual RID heightmap_shape_create() override; |
91 | virtual RID custom_shape_create() override; |
92 | |
93 | virtual void shape_set_data(RID p_shape, const Variant &p_data) override; |
94 | virtual void shape_set_custom_solver_bias(RID p_shape, real_t p_bias) override; |
95 | |
96 | virtual ShapeType shape_get_type(RID p_shape) const override; |
97 | virtual Variant shape_get_data(RID p_shape) const override; |
98 | |
99 | virtual void shape_set_margin(RID p_shape, real_t p_margin) override; |
100 | virtual real_t shape_get_margin(RID p_shape) const override; |
101 | |
102 | virtual real_t shape_get_custom_solver_bias(RID p_shape) const override; |
103 | |
104 | /* SPACE API */ |
105 | |
106 | virtual RID space_create() override; |
107 | virtual void space_set_active(RID p_space, bool p_active) override; |
108 | virtual bool space_is_active(RID p_space) const override; |
109 | |
110 | virtual void space_set_param(RID p_space, SpaceParameter p_param, real_t p_value) override; |
111 | virtual real_t space_get_param(RID p_space, SpaceParameter p_param) const override; |
112 | |
113 | // this function only works on physics process, errors and returns null otherwise |
114 | virtual PhysicsDirectSpaceState3D *space_get_direct_state(RID p_space) override; |
115 | |
116 | virtual void space_set_debug_contacts(RID p_space, int p_max_contacts) override; |
117 | virtual Vector<Vector3> space_get_contacts(RID p_space) const override; |
118 | virtual int space_get_contact_count(RID p_space) const override; |
119 | |
120 | /* AREA API */ |
121 | |
122 | virtual RID area_create() override; |
123 | |
124 | virtual void area_set_space(RID p_area, RID p_space) override; |
125 | virtual RID area_get_space(RID p_area) const override; |
126 | |
127 | virtual void area_add_shape(RID p_area, RID p_shape, const Transform3D &p_transform = Transform3D(), bool p_disabled = false) override; |
128 | virtual void area_set_shape(RID p_area, int p_shape_idx, RID p_shape) override; |
129 | virtual void area_set_shape_transform(RID p_area, int p_shape_idx, const Transform3D &p_transform) override; |
130 | |
131 | virtual int area_get_shape_count(RID p_area) const override; |
132 | virtual RID area_get_shape(RID p_area, int p_shape_idx) const override; |
133 | virtual Transform3D area_get_shape_transform(RID p_area, int p_shape_idx) const override; |
134 | |
135 | virtual void area_remove_shape(RID p_area, int p_shape_idx) override; |
136 | virtual void area_clear_shapes(RID p_area) override; |
137 | |
138 | virtual void area_set_shape_disabled(RID p_area, int p_shape_idx, bool p_disabled) override; |
139 | |
140 | virtual void area_attach_object_instance_id(RID p_area, ObjectID p_id) override; |
141 | virtual ObjectID area_get_object_instance_id(RID p_area) const override; |
142 | |
143 | virtual void area_set_param(RID p_area, AreaParameter p_param, const Variant &p_value) override; |
144 | virtual void area_set_transform(RID p_area, const Transform3D &p_transform) override; |
145 | |
146 | virtual Variant area_get_param(RID p_area, AreaParameter p_param) const override; |
147 | virtual Transform3D area_get_transform(RID p_area) const override; |
148 | |
149 | virtual void area_set_ray_pickable(RID p_area, bool p_enable) override; |
150 | |
151 | virtual void area_set_collision_layer(RID p_area, uint32_t p_layer) override; |
152 | virtual uint32_t area_get_collision_layer(RID p_area) const override; |
153 | |
154 | virtual void area_set_collision_mask(RID p_area, uint32_t p_mask) override; |
155 | virtual uint32_t area_get_collision_mask(RID p_area) const override; |
156 | |
157 | virtual void area_set_monitorable(RID p_area, bool p_monitorable) override; |
158 | |
159 | virtual void area_set_monitor_callback(RID p_area, const Callable &p_callback) override; |
160 | virtual void area_set_area_monitor_callback(RID p_area, const Callable &p_callback) override; |
161 | |
162 | /* BODY API */ |
163 | |
164 | // create a body of a given type |
165 | virtual RID body_create() override; |
166 | |
167 | virtual void body_set_space(RID p_body, RID p_space) override; |
168 | virtual RID body_get_space(RID p_body) const override; |
169 | |
170 | virtual void body_set_mode(RID p_body, BodyMode p_mode) override; |
171 | virtual BodyMode body_get_mode(RID p_body) const override; |
172 | |
173 | virtual void body_add_shape(RID p_body, RID p_shape, const Transform3D &p_transform = Transform3D(), bool p_disabled = false) override; |
174 | virtual void body_set_shape(RID p_body, int p_shape_idx, RID p_shape) override; |
175 | virtual void body_set_shape_transform(RID p_body, int p_shape_idx, const Transform3D &p_transform) override; |
176 | |
177 | virtual int body_get_shape_count(RID p_body) const override; |
178 | virtual RID body_get_shape(RID p_body, int p_shape_idx) const override; |
179 | virtual Transform3D body_get_shape_transform(RID p_body, int p_shape_idx) const override; |
180 | |
181 | virtual void body_set_shape_disabled(RID p_body, int p_shape_idx, bool p_disabled) override; |
182 | |
183 | virtual void body_remove_shape(RID p_body, int p_shape_idx) override; |
184 | virtual void body_clear_shapes(RID p_body) override; |
185 | |
186 | virtual void body_attach_object_instance_id(RID p_body, ObjectID p_id) override; |
187 | virtual ObjectID body_get_object_instance_id(RID p_body) const override; |
188 | |
189 | virtual void body_set_enable_continuous_collision_detection(RID p_body, bool p_enable) override; |
190 | virtual bool body_is_continuous_collision_detection_enabled(RID p_body) const override; |
191 | |
192 | virtual void body_set_collision_layer(RID p_body, uint32_t p_layer) override; |
193 | virtual uint32_t body_get_collision_layer(RID p_body) const override; |
194 | |
195 | virtual void body_set_collision_mask(RID p_body, uint32_t p_mask) override; |
196 | virtual uint32_t body_get_collision_mask(RID p_body) const override; |
197 | |
198 | virtual void body_set_collision_priority(RID p_body, real_t p_priority) override; |
199 | virtual real_t body_get_collision_priority(RID p_body) const override; |
200 | |
201 | virtual void body_set_user_flags(RID p_body, uint32_t p_flags) override; |
202 | virtual uint32_t body_get_user_flags(RID p_body) const override; |
203 | |
204 | virtual void body_set_param(RID p_body, BodyParameter p_param, const Variant &p_value) override; |
205 | virtual Variant body_get_param(RID p_body, BodyParameter p_param) const override; |
206 | |
207 | virtual void body_reset_mass_properties(RID p_body) override; |
208 | |
209 | virtual void body_set_state(RID p_body, BodyState p_state, const Variant &p_variant) override; |
210 | virtual Variant body_get_state(RID p_body, BodyState p_state) const override; |
211 | |
212 | virtual void body_apply_central_impulse(RID p_body, const Vector3 &p_impulse) override; |
213 | virtual void body_apply_impulse(RID p_body, const Vector3 &p_impulse, const Vector3 &p_position = Vector3()) override; |
214 | virtual void body_apply_torque_impulse(RID p_body, const Vector3 &p_impulse) override; |
215 | |
216 | virtual void body_apply_central_force(RID p_body, const Vector3 &p_force) override; |
217 | virtual void body_apply_force(RID p_body, const Vector3 &p_force, const Vector3 &p_position = Vector3()) override; |
218 | virtual void body_apply_torque(RID p_body, const Vector3 &p_torque) override; |
219 | |
220 | virtual void body_add_constant_central_force(RID p_body, const Vector3 &p_force) override; |
221 | virtual void body_add_constant_force(RID p_body, const Vector3 &p_force, const Vector3 &p_position = Vector3()) override; |
222 | virtual void body_add_constant_torque(RID p_body, const Vector3 &p_torque) override; |
223 | |
224 | virtual void body_set_constant_force(RID p_body, const Vector3 &p_force) override; |
225 | virtual Vector3 body_get_constant_force(RID p_body) const override; |
226 | |
227 | virtual void body_set_constant_torque(RID p_body, const Vector3 &p_torque) override; |
228 | virtual Vector3 body_get_constant_torque(RID p_body) const override; |
229 | |
230 | virtual void body_set_axis_velocity(RID p_body, const Vector3 &p_axis_velocity) override; |
231 | |
232 | virtual void body_set_axis_lock(RID p_body, BodyAxis p_axis, bool p_lock) override; |
233 | virtual bool body_is_axis_locked(RID p_body, BodyAxis p_axis) const override; |
234 | |
235 | virtual void body_add_collision_exception(RID p_body, RID p_body_b) override; |
236 | virtual void body_remove_collision_exception(RID p_body, RID p_body_b) override; |
237 | virtual void body_get_collision_exceptions(RID p_body, List<RID> *p_exceptions) override; |
238 | |
239 | virtual void body_set_contacts_reported_depth_threshold(RID p_body, real_t p_threshold) override; |
240 | virtual real_t body_get_contacts_reported_depth_threshold(RID p_body) const override; |
241 | |
242 | virtual void body_set_omit_force_integration(RID p_body, bool p_omit) override; |
243 | virtual bool body_is_omitting_force_integration(RID p_body) const override; |
244 | |
245 | virtual void body_set_max_contacts_reported(RID p_body, int p_contacts) override; |
246 | virtual int body_get_max_contacts_reported(RID p_body) const override; |
247 | |
248 | virtual void body_set_state_sync_callback(RID p_body, const Callable &p_callable) override; |
249 | virtual void body_set_force_integration_callback(RID p_body, const Callable &p_callable, const Variant &p_udata = Variant()) override; |
250 | |
251 | virtual void body_set_ray_pickable(RID p_body, bool p_enable) override; |
252 | |
253 | virtual bool body_test_motion(RID p_body, const MotionParameters &p_parameters, MotionResult *r_result = nullptr) override; |
254 | |
255 | // this function only works on physics process, errors and returns null otherwise |
256 | virtual PhysicsDirectBodyState3D *body_get_direct_state(RID p_body) override; |
257 | |
258 | /* SOFT BODY */ |
259 | |
260 | virtual RID soft_body_create() override; |
261 | |
262 | virtual void soft_body_update_rendering_server(RID p_body, PhysicsServer3DRenderingServerHandler *p_rendering_server_handler) override; |
263 | |
264 | virtual void soft_body_set_space(RID p_body, RID p_space) override; |
265 | virtual RID soft_body_get_space(RID p_body) const override; |
266 | |
267 | virtual void soft_body_set_collision_layer(RID p_body, uint32_t p_layer) override; |
268 | virtual uint32_t soft_body_get_collision_layer(RID p_body) const override; |
269 | |
270 | virtual void soft_body_set_collision_mask(RID p_body, uint32_t p_mask) override; |
271 | virtual uint32_t soft_body_get_collision_mask(RID p_body) const override; |
272 | |
273 | virtual void soft_body_add_collision_exception(RID p_body, RID p_body_b) override; |
274 | virtual void soft_body_remove_collision_exception(RID p_body, RID p_body_b) override; |
275 | virtual void soft_body_get_collision_exceptions(RID p_body, List<RID> *p_exceptions) override; |
276 | |
277 | virtual void soft_body_set_state(RID p_body, BodyState p_state, const Variant &p_variant) override; |
278 | virtual Variant soft_body_get_state(RID p_body, BodyState p_state) const override; |
279 | |
280 | virtual void soft_body_set_transform(RID p_body, const Transform3D &p_transform) override; |
281 | |
282 | virtual void soft_body_set_ray_pickable(RID p_body, bool p_enable) override; |
283 | |
284 | virtual void soft_body_set_simulation_precision(RID p_body, int p_simulation_precision) override; |
285 | virtual int soft_body_get_simulation_precision(RID p_body) const override; |
286 | |
287 | virtual void soft_body_set_total_mass(RID p_body, real_t p_total_mass) override; |
288 | virtual real_t soft_body_get_total_mass(RID p_body) const override; |
289 | |
290 | virtual void soft_body_set_linear_stiffness(RID p_body, real_t p_stiffness) override; |
291 | virtual real_t soft_body_get_linear_stiffness(RID p_body) const override; |
292 | |
293 | virtual void soft_body_set_pressure_coefficient(RID p_body, real_t p_pressure_coefficient) override; |
294 | virtual real_t soft_body_get_pressure_coefficient(RID p_body) const override; |
295 | |
296 | virtual void soft_body_set_damping_coefficient(RID p_body, real_t p_damping_coefficient) override; |
297 | virtual real_t soft_body_get_damping_coefficient(RID p_body) const override; |
298 | |
299 | virtual void soft_body_set_drag_coefficient(RID p_body, real_t p_drag_coefficient) override; |
300 | virtual real_t soft_body_get_drag_coefficient(RID p_body) const override; |
301 | |
302 | virtual void soft_body_set_mesh(RID p_body, RID p_mesh) override; |
303 | |
304 | virtual AABB soft_body_get_bounds(RID p_body) const override; |
305 | |
306 | virtual void soft_body_move_point(RID p_body, int p_point_index, const Vector3 &p_global_position) override; |
307 | virtual Vector3 soft_body_get_point_global_position(RID p_body, int p_point_index) const override; |
308 | |
309 | virtual void soft_body_remove_all_pinned_points(RID p_body) override; |
310 | virtual void soft_body_pin_point(RID p_body, int p_point_index, bool p_pin) override; |
311 | virtual bool soft_body_is_point_pinned(RID p_body, int p_point_index) const override; |
312 | |
313 | /* JOINT API */ |
314 | |
315 | virtual RID joint_create() override; |
316 | |
317 | virtual void joint_clear(RID p_joint) override; //resets type |
318 | |
319 | virtual void joint_make_pin(RID p_joint, RID p_body_A, const Vector3 &p_local_A, RID p_body_B, const Vector3 &p_local_B) override; |
320 | |
321 | virtual void pin_joint_set_param(RID p_joint, PinJointParam p_param, real_t p_value) override; |
322 | virtual real_t pin_joint_get_param(RID p_joint, PinJointParam p_param) const override; |
323 | |
324 | virtual void pin_joint_set_local_a(RID p_joint, const Vector3 &p_A) override; |
325 | virtual Vector3 pin_joint_get_local_a(RID p_joint) const override; |
326 | |
327 | virtual void pin_joint_set_local_b(RID p_joint, const Vector3 &p_B) override; |
328 | virtual Vector3 pin_joint_get_local_b(RID p_joint) const override; |
329 | |
330 | virtual void joint_make_hinge(RID p_joint, RID p_body_A, const Transform3D &p_frame_A, RID p_body_B, const Transform3D &p_frame_B) override; |
331 | virtual void joint_make_hinge_simple(RID p_joint, RID p_body_A, const Vector3 &p_pivot_A, const Vector3 &p_axis_A, RID p_body_B, const Vector3 &p_pivot_B, const Vector3 &p_axis_B) override; |
332 | |
333 | virtual void hinge_joint_set_param(RID p_joint, HingeJointParam p_param, real_t p_value) override; |
334 | virtual real_t hinge_joint_get_param(RID p_joint, HingeJointParam p_param) const override; |
335 | |
336 | virtual void hinge_joint_set_flag(RID p_joint, HingeJointFlag p_flag, bool p_value) override; |
337 | virtual bool hinge_joint_get_flag(RID p_joint, HingeJointFlag p_flag) const override; |
338 | |
339 | virtual void joint_make_slider(RID p_joint, RID p_body_A, const Transform3D &p_local_frame_A, RID p_body_B, const Transform3D &p_local_frame_B) override; //reference frame is A |
340 | |
341 | virtual void slider_joint_set_param(RID p_joint, SliderJointParam p_param, real_t p_value) override; |
342 | virtual real_t slider_joint_get_param(RID p_joint, SliderJointParam p_param) const override; |
343 | |
344 | virtual void joint_make_cone_twist(RID p_joint, RID p_body_A, const Transform3D &p_local_frame_A, RID p_body_B, const Transform3D &p_local_frame_B) override; //reference frame is A |
345 | |
346 | virtual void cone_twist_joint_set_param(RID p_joint, ConeTwistJointParam p_param, real_t p_value) override; |
347 | virtual real_t cone_twist_joint_get_param(RID p_joint, ConeTwistJointParam p_param) const override; |
348 | |
349 | virtual void joint_make_generic_6dof(RID p_joint, RID p_body_A, const Transform3D &p_local_frame_A, RID p_body_B, const Transform3D &p_local_frame_B) override; //reference frame is A |
350 | |
351 | virtual void generic_6dof_joint_set_param(RID p_joint, Vector3::Axis, G6DOFJointAxisParam p_param, real_t p_value) override; |
352 | virtual real_t generic_6dof_joint_get_param(RID p_joint, Vector3::Axis, G6DOFJointAxisParam p_param) const override; |
353 | |
354 | virtual void generic_6dof_joint_set_flag(RID p_joint, Vector3::Axis, G6DOFJointAxisFlag p_flag, bool p_enable) override; |
355 | virtual bool generic_6dof_joint_get_flag(RID p_joint, Vector3::Axis, G6DOFJointAxisFlag p_flag) const override; |
356 | |
357 | virtual JointType joint_get_type(RID p_joint) const override; |
358 | |
359 | virtual void joint_set_solver_priority(RID p_joint, int p_priority) override; |
360 | virtual int joint_get_solver_priority(RID p_joint) const override; |
361 | |
362 | virtual void joint_disable_collisions_between_bodies(RID p_joint, bool p_disable) override; |
363 | virtual bool joint_is_disabled_collisions_between_bodies(RID p_joint) const override; |
364 | |
365 | /* MISC */ |
366 | |
367 | virtual void free(RID p_rid) override; |
368 | |
369 | virtual void set_active(bool p_active) override; |
370 | virtual void init() override; |
371 | virtual void step(real_t p_step) override; |
372 | virtual void sync() override; |
373 | virtual void flush_queries() override; |
374 | virtual void end_sync() override; |
375 | virtual void finish() override; |
376 | |
377 | virtual bool is_flushing_queries() const override { return flushing_queries; } |
378 | |
379 | int get_process_info(ProcessInfo p_info) override; |
380 | |
381 | GodotPhysicsServer3D(bool p_using_threads = false); |
382 | ~GodotPhysicsServer3D() {} |
383 | }; |
384 | |
385 | #endif // GODOT_PHYSICS_SERVER_3D_H |
386 | |