1/**************************************************************************/
2/* physics_server_2d.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 PHYSICS_SERVER_2D_H
32#define PHYSICS_SERVER_2D_H
33
34#include "core/io/resource.h"
35#include "core/object/class_db.h"
36#include "core/object/ref_counted.h"
37
38class PhysicsDirectSpaceState2D;
39template <typename T>
40class TypedArray;
41
42class PhysicsDirectBodyState2D : public Object {
43 GDCLASS(PhysicsDirectBodyState2D, Object);
44
45protected:
46 static void _bind_methods();
47
48public:
49 virtual Vector2 get_total_gravity() const = 0; // get gravity vector working on this body space/area
50 virtual real_t get_total_linear_damp() const = 0; // get density of this body space/area
51 virtual real_t get_total_angular_damp() const = 0; // get density of this body space/area
52
53 virtual Vector2 get_center_of_mass() const = 0;
54 virtual Vector2 get_center_of_mass_local() const = 0;
55 virtual real_t get_inverse_mass() const = 0; // get the mass
56 virtual real_t get_inverse_inertia() const = 0; // get density of this body space
57
58 virtual void set_linear_velocity(const Vector2 &p_velocity) = 0;
59 virtual Vector2 get_linear_velocity() const = 0;
60
61 virtual void set_angular_velocity(real_t p_velocity) = 0;
62 virtual real_t get_angular_velocity() const = 0;
63
64 virtual void set_transform(const Transform2D &p_transform) = 0;
65 virtual Transform2D get_transform() const = 0;
66
67 virtual Vector2 get_velocity_at_local_position(const Vector2 &p_position) const = 0;
68
69 virtual void apply_central_impulse(const Vector2 &p_impulse) = 0;
70 virtual void apply_torque_impulse(real_t p_torque) = 0;
71 virtual void apply_impulse(const Vector2 &p_impulse, const Vector2 &p_position = Vector2()) = 0;
72
73 virtual void apply_central_force(const Vector2 &p_force) = 0;
74 virtual void apply_force(const Vector2 &p_force, const Vector2 &p_position = Vector2()) = 0;
75 virtual void apply_torque(real_t p_torque) = 0;
76
77 virtual void add_constant_central_force(const Vector2 &p_force) = 0;
78 virtual void add_constant_force(const Vector2 &p_force, const Vector2 &p_position = Vector2()) = 0;
79 virtual void add_constant_torque(real_t p_torque) = 0;
80
81 virtual void set_constant_force(const Vector2 &p_force) = 0;
82 virtual Vector2 get_constant_force() const = 0;
83
84 virtual void set_constant_torque(real_t p_torque) = 0;
85 virtual real_t get_constant_torque() const = 0;
86
87 virtual void set_sleep_state(bool p_enable) = 0;
88 virtual bool is_sleeping() const = 0;
89
90 virtual int get_contact_count() const = 0;
91
92 virtual Vector2 get_contact_local_position(int p_contact_idx) const = 0;
93 virtual Vector2 get_contact_local_normal(int p_contact_idx) const = 0;
94 virtual int get_contact_local_shape(int p_contact_idx) const = 0;
95 virtual Vector2 get_contact_local_velocity_at_position(int p_contact_idx) const = 0;
96
97 virtual RID get_contact_collider(int p_contact_idx) const = 0;
98 virtual Vector2 get_contact_collider_position(int p_contact_idx) const = 0;
99 virtual ObjectID get_contact_collider_id(int p_contact_idx) const = 0;
100 virtual Object *get_contact_collider_object(int p_contact_idx) const;
101 virtual int get_contact_collider_shape(int p_contact_idx) const = 0;
102 virtual Vector2 get_contact_collider_velocity_at_position(int p_contact_idx) const = 0;
103 virtual Vector2 get_contact_impulse(int p_contact_idx) const = 0;
104
105 virtual real_t get_step() const = 0;
106 virtual void integrate_forces();
107
108 virtual PhysicsDirectSpaceState2D *get_space_state() = 0;
109
110 PhysicsDirectBodyState2D();
111};
112
113class PhysicsRayQueryParameters2D;
114class PhysicsPointQueryParameters2D;
115class PhysicsShapeQueryParameters2D;
116
117class PhysicsDirectSpaceState2D : public Object {
118 GDCLASS(PhysicsDirectSpaceState2D, Object);
119
120 Dictionary _intersect_ray(const Ref<PhysicsRayQueryParameters2D> &p_ray_query);
121 TypedArray<Dictionary> _intersect_point(const Ref<PhysicsPointQueryParameters2D> &p_point_query, int p_max_results = 32);
122 TypedArray<Dictionary> _intersect_shape(const Ref<PhysicsShapeQueryParameters2D> &p_shape_query, int p_max_results = 32);
123 Vector<real_t> _cast_motion(const Ref<PhysicsShapeQueryParameters2D> &p_shape_query);
124 TypedArray<Vector2> _collide_shape(const Ref<PhysicsShapeQueryParameters2D> &p_shape_query, int p_max_results = 32);
125 Dictionary _get_rest_info(const Ref<PhysicsShapeQueryParameters2D> &p_shape_query);
126
127protected:
128 static void _bind_methods();
129
130public:
131 struct RayParameters {
132 Vector2 from;
133 Vector2 to;
134 HashSet<RID> exclude;
135 uint32_t collision_mask = UINT32_MAX;
136
137 bool collide_with_bodies = true;
138 bool collide_with_areas = false;
139
140 bool hit_from_inside = false;
141 };
142
143 struct RayResult {
144 Vector2 position;
145 Vector2 normal;
146 RID rid;
147 ObjectID collider_id;
148 Object *collider = nullptr;
149 int shape = 0;
150 };
151
152 virtual bool intersect_ray(const RayParameters &p_parameters, RayResult &r_result) = 0;
153
154 struct ShapeResult {
155 RID rid;
156 ObjectID collider_id;
157 Object *collider = nullptr;
158 int shape = 0;
159 };
160
161 struct PointParameters {
162 Vector2 position;
163 ObjectID canvas_instance_id;
164 HashSet<RID> exclude;
165 uint32_t collision_mask = UINT32_MAX;
166
167 bool collide_with_bodies = true;
168 bool collide_with_areas = false;
169
170 bool pick_point = false;
171 };
172
173 virtual int intersect_point(const PointParameters &p_parameters, ShapeResult *r_results, int p_result_max) = 0;
174
175 struct ShapeParameters {
176 RID shape_rid;
177 Transform2D transform;
178 Vector2 motion;
179 real_t margin = 0.0;
180 HashSet<RID> exclude;
181 uint32_t collision_mask = UINT32_MAX;
182
183 bool collide_with_bodies = true;
184 bool collide_with_areas = false;
185 };
186
187 struct ShapeRestInfo {
188 Vector2 point;
189 Vector2 normal;
190 RID rid;
191 ObjectID collider_id;
192 int shape = 0;
193 Vector2 linear_velocity; // Velocity at contact point.
194 };
195
196 virtual int intersect_shape(const ShapeParameters &p_parameters, ShapeResult *r_results, int p_result_max) = 0;
197 virtual bool cast_motion(const ShapeParameters &p_parameters, real_t &p_closest_safe, real_t &p_closest_unsafe) = 0;
198 virtual bool collide_shape(const ShapeParameters &p_parameters, Vector2 *r_results, int p_result_max, int &r_result_count) = 0;
199 virtual bool rest_info(const ShapeParameters &p_parameters, ShapeRestInfo *r_info) = 0;
200
201 PhysicsDirectSpaceState2D();
202};
203
204class PhysicsTestMotionParameters2D;
205class PhysicsTestMotionResult2D;
206
207class PhysicsServer2D : public Object {
208 GDCLASS(PhysicsServer2D, Object);
209
210 static PhysicsServer2D *singleton;
211
212 virtual bool _body_test_motion(RID p_body, const Ref<PhysicsTestMotionParameters2D> &p_parameters, const Ref<PhysicsTestMotionResult2D> &p_result = Ref<PhysicsTestMotionResult2D>());
213
214protected:
215 static void _bind_methods();
216
217public:
218 static PhysicsServer2D *get_singleton();
219
220 enum ShapeType {
221 SHAPE_WORLD_BOUNDARY, ///< plane:"plane"
222 SHAPE_SEPARATION_RAY, ///< float:"length"
223 SHAPE_SEGMENT, ///< float:"length"
224 SHAPE_CIRCLE, ///< float:"radius"
225 SHAPE_RECTANGLE, ///< vec3:"extents"
226 SHAPE_CAPSULE,
227 SHAPE_CONVEX_POLYGON, ///< array of planes:"planes"
228 SHAPE_CONCAVE_POLYGON, ///< Vector2 array:"triangles" , or Dictionary with "indices" (int array) and "triangles" (Vector2 array)
229 SHAPE_CUSTOM, ///< Server-Implementation based custom shape, calling shape_create() with this value will result in an error
230 };
231
232 virtual RID world_boundary_shape_create() = 0;
233 virtual RID separation_ray_shape_create() = 0;
234 virtual RID segment_shape_create() = 0;
235 virtual RID circle_shape_create() = 0;
236 virtual RID rectangle_shape_create() = 0;
237 virtual RID capsule_shape_create() = 0;
238 virtual RID convex_polygon_shape_create() = 0;
239 virtual RID concave_polygon_shape_create() = 0;
240
241 virtual void shape_set_data(RID p_shape, const Variant &p_data) = 0;
242 virtual void shape_set_custom_solver_bias(RID p_shape, real_t p_bias) = 0;
243
244 virtual ShapeType shape_get_type(RID p_shape) const = 0;
245 virtual Variant shape_get_data(RID p_shape) const = 0;
246 virtual real_t shape_get_custom_solver_bias(RID p_shape) const = 0;
247
248 //these work well, but should be used from the main thread only
249 virtual bool shape_collide(RID p_shape_A, const Transform2D &p_xform_A, const Vector2 &p_motion_A, RID p_shape_B, const Transform2D &p_xform_B, const Vector2 &p_motion_B, Vector2 *r_results, int p_result_max, int &r_result_count) = 0;
250
251 /* SPACE API */
252
253 virtual RID space_create() = 0;
254 virtual void space_set_active(RID p_space, bool p_active) = 0;
255 virtual bool space_is_active(RID p_space) const = 0;
256
257 enum SpaceParameter {
258 SPACE_PARAM_CONTACT_RECYCLE_RADIUS,
259 SPACE_PARAM_CONTACT_MAX_SEPARATION,
260 SPACE_PARAM_CONTACT_MAX_ALLOWED_PENETRATION,
261 SPACE_PARAM_CONTACT_DEFAULT_BIAS,
262 SPACE_PARAM_BODY_LINEAR_VELOCITY_SLEEP_THRESHOLD,
263 SPACE_PARAM_BODY_ANGULAR_VELOCITY_SLEEP_THRESHOLD,
264 SPACE_PARAM_BODY_TIME_TO_SLEEP,
265 SPACE_PARAM_CONSTRAINT_DEFAULT_BIAS,
266 SPACE_PARAM_SOLVER_ITERATIONS,
267 };
268
269 virtual void space_set_param(RID p_space, SpaceParameter p_param, real_t p_value) = 0;
270 virtual real_t space_get_param(RID p_space, SpaceParameter p_param) const = 0;
271
272 // this function only works on physics process, errors and returns null otherwise
273 virtual PhysicsDirectSpaceState2D *space_get_direct_state(RID p_space) = 0;
274
275 virtual void space_set_debug_contacts(RID p_space, int p_max_contacts) = 0;
276 virtual Vector<Vector2> space_get_contacts(RID p_space) const = 0;
277 virtual int space_get_contact_count(RID p_space) const = 0;
278
279 //missing space parameters
280
281 /* AREA API */
282
283 //missing attenuation? missing better override?
284
285 enum AreaParameter {
286 AREA_PARAM_GRAVITY_OVERRIDE_MODE,
287 AREA_PARAM_GRAVITY,
288 AREA_PARAM_GRAVITY_VECTOR,
289 AREA_PARAM_GRAVITY_IS_POINT,
290 AREA_PARAM_GRAVITY_POINT_UNIT_DISTANCE,
291 AREA_PARAM_LINEAR_DAMP_OVERRIDE_MODE,
292 AREA_PARAM_LINEAR_DAMP,
293 AREA_PARAM_ANGULAR_DAMP_OVERRIDE_MODE,
294 AREA_PARAM_ANGULAR_DAMP,
295 AREA_PARAM_PRIORITY
296 };
297
298 virtual RID area_create() = 0;
299
300 virtual void area_set_space(RID p_area, RID p_space) = 0;
301 virtual RID area_get_space(RID p_area) const = 0;
302
303 enum AreaSpaceOverrideMode {
304 AREA_SPACE_OVERRIDE_DISABLED,
305 AREA_SPACE_OVERRIDE_COMBINE,
306 AREA_SPACE_OVERRIDE_COMBINE_REPLACE, // Combines, then discards all subsequent calculations
307 AREA_SPACE_OVERRIDE_REPLACE,
308 AREA_SPACE_OVERRIDE_REPLACE_COMBINE // Discards all previous calculations, then keeps combining
309 };
310
311 virtual void area_add_shape(RID p_area, RID p_shape, const Transform2D &p_transform = Transform2D(), bool p_disabled = false) = 0;
312 virtual void area_set_shape(RID p_area, int p_shape_idx, RID p_shape) = 0;
313 virtual void area_set_shape_transform(RID p_area, int p_shape_idx, const Transform2D &p_transform) = 0;
314
315 virtual int area_get_shape_count(RID p_area) const = 0;
316 virtual RID area_get_shape(RID p_area, int p_shape_idx) const = 0;
317 virtual Transform2D area_get_shape_transform(RID p_area, int p_shape_idx) const = 0;
318
319 virtual void area_remove_shape(RID p_area, int p_shape_idx) = 0;
320 virtual void area_clear_shapes(RID p_area) = 0;
321
322 virtual void area_set_shape_disabled(RID p_area, int p_shape, bool p_disabled) = 0;
323
324 virtual void area_attach_object_instance_id(RID p_area, ObjectID p_id) = 0;
325 virtual ObjectID area_get_object_instance_id(RID p_area) const = 0;
326
327 virtual void area_attach_canvas_instance_id(RID p_area, ObjectID p_id) = 0;
328 virtual ObjectID area_get_canvas_instance_id(RID p_area) const = 0;
329
330 virtual void area_set_param(RID p_area, AreaParameter p_param, const Variant &p_value) = 0;
331 virtual void area_set_transform(RID p_area, const Transform2D &p_transform) = 0;
332
333 virtual Variant area_get_param(RID p_parea, AreaParameter p_param) const = 0;
334 virtual Transform2D area_get_transform(RID p_area) const = 0;
335
336 virtual void area_set_collision_layer(RID p_area, uint32_t p_layer) = 0;
337 virtual uint32_t area_get_collision_layer(RID p_area) const = 0;
338
339 virtual void area_set_collision_mask(RID p_area, uint32_t p_mask) = 0;
340 virtual uint32_t area_get_collision_mask(RID p_area) const = 0;
341
342 virtual void area_set_monitorable(RID p_area, bool p_monitorable) = 0;
343 virtual void area_set_pickable(RID p_area, bool p_pickable) = 0;
344
345 virtual void area_set_monitor_callback(RID p_area, const Callable &p_callback) = 0;
346 virtual void area_set_area_monitor_callback(RID p_area, const Callable &p_callback) = 0;
347
348 /* BODY API */
349
350 //missing ccd?
351
352 enum BodyMode {
353 BODY_MODE_STATIC,
354 BODY_MODE_KINEMATIC,
355 BODY_MODE_RIGID,
356 BODY_MODE_RIGID_LINEAR,
357 };
358
359 virtual RID body_create() = 0;
360
361 virtual void body_set_space(RID p_body, RID p_space) = 0;
362 virtual RID body_get_space(RID p_body) const = 0;
363
364 virtual void body_set_mode(RID p_body, BodyMode p_mode) = 0;
365 virtual BodyMode body_get_mode(RID p_body) const = 0;
366
367 virtual void body_add_shape(RID p_body, RID p_shape, const Transform2D &p_transform = Transform2D(), bool p_disabled = false) = 0;
368 virtual void body_set_shape(RID p_body, int p_shape_idx, RID p_shape) = 0;
369 virtual void body_set_shape_transform(RID p_body, int p_shape_idx, const Transform2D &p_transform) = 0;
370
371 virtual int body_get_shape_count(RID p_body) const = 0;
372 virtual RID body_get_shape(RID p_body, int p_shape_idx) const = 0;
373 virtual Transform2D body_get_shape_transform(RID p_body, int p_shape_idx) const = 0;
374
375 virtual void body_set_shape_disabled(RID p_body, int p_shape, bool p_disabled) = 0;
376 virtual void body_set_shape_as_one_way_collision(RID p_body, int p_shape, bool p_enabled, real_t p_margin = 0) = 0;
377
378 virtual void body_remove_shape(RID p_body, int p_shape_idx) = 0;
379 virtual void body_clear_shapes(RID p_body) = 0;
380
381 virtual void body_attach_object_instance_id(RID p_body, ObjectID p_id) = 0;
382 virtual ObjectID body_get_object_instance_id(RID p_body) const = 0;
383
384 virtual void body_attach_canvas_instance_id(RID p_body, ObjectID p_id) = 0;
385 virtual ObjectID body_get_canvas_instance_id(RID p_body) const = 0;
386
387 enum CCDMode {
388 CCD_MODE_DISABLED,
389 CCD_MODE_CAST_RAY,
390 CCD_MODE_CAST_SHAPE,
391 };
392
393 virtual void body_set_continuous_collision_detection_mode(RID p_body, CCDMode p_mode) = 0;
394 virtual CCDMode body_get_continuous_collision_detection_mode(RID p_body) const = 0;
395
396 virtual void body_set_collision_layer(RID p_body, uint32_t p_layer) = 0;
397 virtual uint32_t body_get_collision_layer(RID p_body) const = 0;
398
399 virtual void body_set_collision_mask(RID p_body, uint32_t p_mask) = 0;
400 virtual uint32_t body_get_collision_mask(RID p_body) const = 0;
401
402 virtual void body_set_collision_priority(RID p_body, real_t p_priority) = 0;
403 virtual real_t body_get_collision_priority(RID p_body) const = 0;
404
405 // common body variables
406 enum BodyParameter {
407 BODY_PARAM_BOUNCE,
408 BODY_PARAM_FRICTION,
409 BODY_PARAM_MASS, ///< unused for static, always infinite
410 BODY_PARAM_INERTIA,
411 BODY_PARAM_CENTER_OF_MASS,
412 BODY_PARAM_GRAVITY_SCALE,
413 BODY_PARAM_LINEAR_DAMP_MODE,
414 BODY_PARAM_ANGULAR_DAMP_MODE,
415 BODY_PARAM_LINEAR_DAMP,
416 BODY_PARAM_ANGULAR_DAMP,
417 BODY_PARAM_MAX,
418 };
419
420 enum BodyDampMode {
421 BODY_DAMP_MODE_COMBINE,
422 BODY_DAMP_MODE_REPLACE,
423 };
424
425 virtual void body_set_param(RID p_body, BodyParameter p_param, const Variant &p_value) = 0;
426 virtual Variant body_get_param(RID p_body, BodyParameter p_param) const = 0;
427
428 virtual void body_reset_mass_properties(RID p_body) = 0;
429
430 //state
431 enum BodyState {
432 BODY_STATE_TRANSFORM,
433 BODY_STATE_LINEAR_VELOCITY,
434 BODY_STATE_ANGULAR_VELOCITY,
435 BODY_STATE_SLEEPING,
436 BODY_STATE_CAN_SLEEP,
437 };
438
439 virtual void body_set_state(RID p_body, BodyState p_state, const Variant &p_variant) = 0;
440 virtual Variant body_get_state(RID p_body, BodyState p_state) const = 0;
441
442 virtual void body_apply_central_impulse(RID p_body, const Vector2 &p_impulse) = 0;
443 virtual void body_apply_torque_impulse(RID p_body, real_t p_torque) = 0;
444 virtual void body_apply_impulse(RID p_body, const Vector2 &p_impulse, const Vector2 &p_position = Vector2()) = 0;
445
446 virtual void body_apply_central_force(RID p_body, const Vector2 &p_force) = 0;
447 virtual void body_apply_force(RID p_body, const Vector2 &p_force, const Vector2 &p_position = Vector2()) = 0;
448 virtual void body_apply_torque(RID p_body, real_t p_torque) = 0;
449
450 virtual void body_add_constant_central_force(RID p_body, const Vector2 &p_force) = 0;
451 virtual void body_add_constant_force(RID p_body, const Vector2 &p_force, const Vector2 &p_position = Vector2()) = 0;
452 virtual void body_add_constant_torque(RID p_body, real_t p_torque) = 0;
453
454 virtual void body_set_constant_force(RID p_body, const Vector2 &p_force) = 0;
455 virtual Vector2 body_get_constant_force(RID p_body) const = 0;
456
457 virtual void body_set_constant_torque(RID p_body, real_t p_torque) = 0;
458 virtual real_t body_get_constant_torque(RID p_body) const = 0;
459
460 virtual void body_set_axis_velocity(RID p_body, const Vector2 &p_axis_velocity) = 0;
461
462 //fix
463 virtual void body_add_collision_exception(RID p_body, RID p_body_b) = 0;
464 virtual void body_remove_collision_exception(RID p_body, RID p_body_b) = 0;
465 virtual void body_get_collision_exceptions(RID p_body, List<RID> *p_exceptions) = 0;
466
467 virtual void body_set_max_contacts_reported(RID p_body, int p_contacts) = 0;
468 virtual int body_get_max_contacts_reported(RID p_body) const = 0;
469
470 //missing remove
471 virtual void body_set_contacts_reported_depth_threshold(RID p_body, real_t p_threshold) = 0;
472 virtual real_t body_get_contacts_reported_depth_threshold(RID p_body) const = 0;
473
474 virtual void body_set_omit_force_integration(RID p_body, bool p_omit) = 0;
475 virtual bool body_is_omitting_force_integration(RID p_body) const = 0;
476
477 virtual void body_set_state_sync_callback(RID p_body, const Callable &p_callable) = 0;
478 virtual void body_set_force_integration_callback(RID p_body, const Callable &p_callable, const Variant &p_udata = Variant()) = 0;
479
480 virtual bool body_collide_shape(RID p_body, int p_body_shape, RID p_shape, const Transform2D &p_shape_xform, const Vector2 &p_motion, Vector2 *r_results, int p_result_max, int &r_result_count) = 0;
481
482 virtual void body_set_pickable(RID p_body, bool p_pickable) = 0;
483
484 // this function only works on physics process, errors and returns null otherwise
485 virtual PhysicsDirectBodyState2D *body_get_direct_state(RID p_body) = 0;
486
487 struct MotionParameters {
488 Transform2D from;
489 Vector2 motion;
490 real_t margin = 0.08;
491 bool collide_separation_ray = false;
492 HashSet<RID> exclude_bodies;
493 HashSet<ObjectID> exclude_objects;
494 bool recovery_as_collision = false;
495
496 MotionParameters() {}
497
498 MotionParameters(const Transform2D &p_from, const Vector2 &p_motion, real_t p_margin = 0.08) :
499 from(p_from),
500 motion(p_motion),
501 margin(p_margin) {}
502 };
503
504 struct MotionResult {
505 Vector2 travel;
506 Vector2 remainder;
507
508 Vector2 collision_point;
509 Vector2 collision_normal;
510 Vector2 collider_velocity;
511 real_t collision_depth = 0.0;
512 real_t collision_safe_fraction = 0.0;
513 real_t collision_unsafe_fraction = 0.0;
514 int collision_local_shape = 0;
515 ObjectID collider_id;
516 RID collider;
517 int collider_shape = 0;
518
519 real_t get_angle(Vector2 p_up_direction) const {
520 return Math::acos(collision_normal.dot(p_up_direction));
521 }
522 };
523
524 virtual bool body_test_motion(RID p_body, const MotionParameters &p_parameters, MotionResult *r_result = nullptr) = 0;
525
526 /* JOINT API */
527
528 virtual RID joint_create() = 0;
529
530 virtual void joint_clear(RID p_joint) = 0;
531
532 enum JointType {
533 JOINT_TYPE_PIN,
534 JOINT_TYPE_GROOVE,
535 JOINT_TYPE_DAMPED_SPRING,
536 JOINT_TYPE_MAX
537 };
538
539 enum JointParam {
540 JOINT_PARAM_BIAS,
541 JOINT_PARAM_MAX_BIAS,
542 JOINT_PARAM_MAX_FORCE,
543 };
544
545 virtual void joint_set_param(RID p_joint, JointParam p_param, real_t p_value) = 0;
546 virtual real_t joint_get_param(RID p_joint, JointParam p_param) const = 0;
547
548 virtual void joint_disable_collisions_between_bodies(RID p_joint, const bool p_disable) = 0;
549 virtual bool joint_is_disabled_collisions_between_bodies(RID p_joint) const = 0;
550
551 virtual void joint_make_pin(RID p_joint, const Vector2 &p_anchor, RID p_body_a, RID p_body_b = RID()) = 0;
552 virtual void joint_make_groove(RID p_joint, const Vector2 &p_a_groove1, const Vector2 &p_a_groove2, const Vector2 &p_b_anchor, RID p_body_a, RID p_body_b) = 0;
553 virtual void joint_make_damped_spring(RID p_joint, const Vector2 &p_anchor_a, const Vector2 &p_anchor_b, RID p_body_a, RID p_body_b = RID()) = 0;
554
555 enum PinJointParam {
556 PIN_JOINT_SOFTNESS
557 };
558
559 virtual void pin_joint_set_param(RID p_joint, PinJointParam p_param, real_t p_value) = 0;
560 virtual real_t pin_joint_get_param(RID p_joint, PinJointParam p_param) const = 0;
561
562 enum DampedSpringParam {
563 DAMPED_SPRING_REST_LENGTH,
564 DAMPED_SPRING_STIFFNESS,
565 DAMPED_SPRING_DAMPING
566 };
567 virtual void damped_spring_joint_set_param(RID p_joint, DampedSpringParam p_param, real_t p_value) = 0;
568 virtual real_t damped_spring_joint_get_param(RID p_joint, DampedSpringParam p_param) const = 0;
569
570 virtual JointType joint_get_type(RID p_joint) const = 0;
571
572 /* QUERY API */
573
574 enum AreaBodyStatus {
575 AREA_BODY_ADDED,
576 AREA_BODY_REMOVED
577 };
578
579 /* MISC */
580
581 virtual void free(RID p_rid) = 0;
582
583 virtual void set_active(bool p_active) = 0;
584 virtual void init() = 0;
585 virtual void step(real_t p_step) = 0;
586 virtual void sync() = 0;
587 virtual void flush_queries() = 0;
588 virtual void end_sync() = 0;
589 virtual void finish() = 0;
590
591 virtual bool is_flushing_queries() const = 0;
592
593 enum ProcessInfo {
594 INFO_ACTIVE_OBJECTS,
595 INFO_COLLISION_PAIRS,
596 INFO_ISLAND_COUNT
597 };
598
599 virtual int get_process_info(ProcessInfo p_info) = 0;
600
601 PhysicsServer2D();
602 ~PhysicsServer2D();
603};
604
605class PhysicsRayQueryParameters2D : public RefCounted {
606 GDCLASS(PhysicsRayQueryParameters2D, RefCounted);
607
608 PhysicsDirectSpaceState2D::RayParameters parameters;
609
610protected:
611 static void _bind_methods();
612
613public:
614 static Ref<PhysicsRayQueryParameters2D> create(Vector2 p_from, Vector2 p_to, uint32_t p_mask, const TypedArray<RID> &p_exclude);
615 const PhysicsDirectSpaceState2D::RayParameters &get_parameters() const { return parameters; }
616
617 void set_from(const Vector2 &p_from) { parameters.from = p_from; }
618 const Vector2 &get_from() const { return parameters.from; }
619
620 void set_to(const Vector2 &p_to) { parameters.to = p_to; }
621 const Vector2 &get_to() const { return parameters.to; }
622
623 void set_collision_mask(uint32_t p_mask) { parameters.collision_mask = p_mask; }
624 uint32_t get_collision_mask() const { return parameters.collision_mask; }
625
626 void set_collide_with_bodies(bool p_enable) { parameters.collide_with_bodies = p_enable; }
627 bool is_collide_with_bodies_enabled() const { return parameters.collide_with_bodies; }
628
629 void set_collide_with_areas(bool p_enable) { parameters.collide_with_areas = p_enable; }
630 bool is_collide_with_areas_enabled() const { return parameters.collide_with_areas; }
631
632 void set_hit_from_inside(bool p_enable) { parameters.hit_from_inside = p_enable; }
633 bool is_hit_from_inside_enabled() const { return parameters.hit_from_inside; }
634
635 void set_exclude(const TypedArray<RID> &p_exclude);
636 TypedArray<RID> get_exclude() const;
637};
638
639class PhysicsPointQueryParameters2D : public RefCounted {
640 GDCLASS(PhysicsPointQueryParameters2D, RefCounted);
641
642 PhysicsDirectSpaceState2D::PointParameters parameters;
643
644protected:
645 static void _bind_methods();
646
647public:
648 const PhysicsDirectSpaceState2D::PointParameters &get_parameters() const { return parameters; }
649
650 void set_position(const Vector2 &p_position) { parameters.position = p_position; }
651 const Vector2 &get_position() const { return parameters.position; }
652
653 void set_canvas_instance_id(ObjectID p_canvas_instance_id) { parameters.canvas_instance_id = p_canvas_instance_id; }
654 ObjectID get_canvas_instance_id() const { return parameters.canvas_instance_id; }
655
656 void set_collision_mask(uint32_t p_mask) { parameters.collision_mask = p_mask; }
657 uint32_t get_collision_mask() const { return parameters.collision_mask; }
658
659 void set_collide_with_bodies(bool p_enable) { parameters.collide_with_bodies = p_enable; }
660 bool is_collide_with_bodies_enabled() const { return parameters.collide_with_bodies; }
661
662 void set_collide_with_areas(bool p_enable) { parameters.collide_with_areas = p_enable; }
663 bool is_collide_with_areas_enabled() const { return parameters.collide_with_areas; }
664
665 void set_exclude(const TypedArray<RID> &p_exclude);
666 TypedArray<RID> get_exclude() const;
667};
668
669class PhysicsShapeQueryParameters2D : public RefCounted {
670 GDCLASS(PhysicsShapeQueryParameters2D, RefCounted);
671
672 PhysicsDirectSpaceState2D::ShapeParameters parameters;
673
674 Ref<Resource> shape_ref;
675
676protected:
677 static void _bind_methods();
678
679public:
680 const PhysicsDirectSpaceState2D::ShapeParameters &get_parameters() const { return parameters; }
681
682 void set_shape(const Ref<Resource> &p_shape_ref);
683 Ref<Resource> get_shape() const { return shape_ref; }
684
685 void set_shape_rid(const RID &p_shape);
686 RID get_shape_rid() const { return parameters.shape_rid; }
687
688 void set_transform(const Transform2D &p_transform) { parameters.transform = p_transform; }
689 const Transform2D &get_transform() const { return parameters.transform; }
690
691 void set_motion(const Vector2 &p_motion) { parameters.motion = p_motion; }
692 const Vector2 &get_motion() const { return parameters.motion; }
693
694 void set_margin(real_t p_margin) { parameters.margin = p_margin; }
695 real_t get_margin() const { return parameters.margin; }
696
697 void set_collision_mask(uint32_t p_mask) { parameters.collision_mask = p_mask; }
698 uint32_t get_collision_mask() const { return parameters.collision_mask; }
699
700 void set_collide_with_bodies(bool p_enable) { parameters.collide_with_bodies = p_enable; }
701 bool is_collide_with_bodies_enabled() const { return parameters.collide_with_bodies; }
702
703 void set_collide_with_areas(bool p_enable) { parameters.collide_with_areas = p_enable; }
704 bool is_collide_with_areas_enabled() const { return parameters.collide_with_areas; }
705
706 void set_exclude(const TypedArray<RID> &p_exclude);
707 TypedArray<RID> get_exclude() const;
708};
709
710class PhysicsTestMotionParameters2D : public RefCounted {
711 GDCLASS(PhysicsTestMotionParameters2D, RefCounted);
712
713 PhysicsServer2D::MotionParameters parameters;
714
715protected:
716 static void _bind_methods();
717
718public:
719 const PhysicsServer2D::MotionParameters &get_parameters() const { return parameters; }
720
721 const Transform2D &get_from() const { return parameters.from; }
722 void set_from(const Transform2D &p_from) { parameters.from = p_from; }
723
724 const Vector2 &get_motion() const { return parameters.motion; }
725 void set_motion(const Vector2 &p_motion) { parameters.motion = p_motion; }
726
727 real_t get_margin() const { return parameters.margin; }
728 void set_margin(real_t p_margin) { parameters.margin = p_margin; }
729
730 bool is_collide_separation_ray_enabled() const { return parameters.collide_separation_ray; }
731 void set_collide_separation_ray_enabled(bool p_enabled) { parameters.collide_separation_ray = p_enabled; }
732
733 TypedArray<RID> get_exclude_bodies() const;
734 void set_exclude_bodies(const TypedArray<RID> &p_exclude);
735
736 TypedArray<uint64_t> get_exclude_objects() const;
737 void set_exclude_objects(const TypedArray<uint64_t> &p_exclude);
738
739 bool is_recovery_as_collision_enabled() const { return parameters.recovery_as_collision; }
740 void set_recovery_as_collision_enabled(bool p_enabled) { parameters.recovery_as_collision = p_enabled; }
741};
742
743class PhysicsTestMotionResult2D : public RefCounted {
744 GDCLASS(PhysicsTestMotionResult2D, RefCounted);
745
746 PhysicsServer2D::MotionResult result;
747
748protected:
749 static void _bind_methods();
750
751public:
752 PhysicsServer2D::MotionResult *get_result_ptr() const { return const_cast<PhysicsServer2D::MotionResult *>(&result); }
753
754 Vector2 get_travel() const;
755 Vector2 get_remainder() const;
756
757 Vector2 get_collision_point() const;
758 Vector2 get_collision_normal() const;
759 Vector2 get_collider_velocity() const;
760 ObjectID get_collider_id() const;
761 RID get_collider_rid() const;
762 Object *get_collider() const;
763 int get_collider_shape() const;
764 int get_collision_local_shape() const;
765 real_t get_collision_depth() const;
766 real_t get_collision_safe_fraction() const;
767 real_t get_collision_unsafe_fraction() const;
768};
769
770class PhysicsServer2DManager : public Object {
771 GDCLASS(PhysicsServer2DManager, Object);
772
773 static PhysicsServer2DManager *singleton;
774
775 struct ClassInfo {
776 String name;
777 Callable create_callback;
778
779 ClassInfo() {}
780
781 ClassInfo(String p_name, Callable p_create_callback) :
782 name(p_name),
783 create_callback(p_create_callback) {}
784
785 ClassInfo(const ClassInfo &p_ci) :
786 name(p_ci.name),
787 create_callback(p_ci.create_callback) {}
788
789 void operator=(const ClassInfo &p_ci) {
790 name = p_ci.name;
791 create_callback = p_ci.create_callback;
792 }
793 };
794
795 Vector<ClassInfo> physics_2d_servers;
796 int default_server_id = -1;
797 int default_server_priority = -1;
798
799 void on_servers_changed();
800
801protected:
802 static void _bind_methods();
803
804public:
805 static const String setting_property_name;
806
807 static PhysicsServer2DManager *get_singleton();
808
809 void register_server(const String &p_name, const Callable &p_create_callback);
810 void set_default_server(const String &p_name, int p_priority = 0);
811 int find_server_id(const String &p_name);
812 int get_servers_count();
813 String get_server_name(int p_id);
814 PhysicsServer2D *new_default_server();
815 PhysicsServer2D *new_server(const String &p_name);
816
817 PhysicsServer2DManager();
818 ~PhysicsServer2DManager();
819};
820
821VARIANT_ENUM_CAST(PhysicsServer2D::ShapeType);
822VARIANT_ENUM_CAST(PhysicsServer2D::SpaceParameter);
823VARIANT_ENUM_CAST(PhysicsServer2D::AreaParameter);
824VARIANT_ENUM_CAST(PhysicsServer2D::AreaSpaceOverrideMode);
825VARIANT_ENUM_CAST(PhysicsServer2D::BodyMode);
826VARIANT_ENUM_CAST(PhysicsServer2D::BodyParameter);
827VARIANT_ENUM_CAST(PhysicsServer2D::BodyDampMode);
828VARIANT_ENUM_CAST(PhysicsServer2D::BodyState);
829VARIANT_ENUM_CAST(PhysicsServer2D::CCDMode);
830VARIANT_ENUM_CAST(PhysicsServer2D::JointParam);
831VARIANT_ENUM_CAST(PhysicsServer2D::JointType);
832VARIANT_ENUM_CAST(PhysicsServer2D::PinJointParam);
833VARIANT_ENUM_CAST(PhysicsServer2D::DampedSpringParam);
834VARIANT_ENUM_CAST(PhysicsServer2D::AreaBodyStatus);
835VARIANT_ENUM_CAST(PhysicsServer2D::ProcessInfo);
836
837#endif // PHYSICS_SERVER_2D_H
838