1/**************************************************************************/
2/* physics_server_2d.cpp */
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#include "physics_server_2d.h"
32
33#include "core/config/project_settings.h"
34#include "core/string/print_string.h"
35#include "core/variant/typed_array.h"
36
37PhysicsServer2D *PhysicsServer2D::singleton = nullptr;
38
39void PhysicsDirectBodyState2D::integrate_forces() {
40 real_t step = get_step();
41 Vector2 lv = get_linear_velocity();
42 lv += get_total_gravity() * step;
43
44 real_t av = get_angular_velocity();
45
46 real_t damp = 1.0 - step * get_total_linear_damp();
47
48 if (damp < 0) { // reached zero in the given time
49 damp = 0;
50 }
51
52 lv *= damp;
53
54 damp = 1.0 - step * get_total_angular_damp();
55
56 if (damp < 0) { // reached zero in the given time
57 damp = 0;
58 }
59
60 av *= damp;
61
62 set_linear_velocity(lv);
63 set_angular_velocity(av);
64}
65
66Object *PhysicsDirectBodyState2D::get_contact_collider_object(int p_contact_idx) const {
67 ObjectID objid = get_contact_collider_id(p_contact_idx);
68 Object *obj = ObjectDB::get_instance(objid);
69 return obj;
70}
71
72PhysicsServer2D *PhysicsServer2D::get_singleton() {
73 return singleton;
74}
75
76void PhysicsDirectBodyState2D::_bind_methods() {
77 ClassDB::bind_method(D_METHOD("get_total_gravity"), &PhysicsDirectBodyState2D::get_total_gravity);
78 ClassDB::bind_method(D_METHOD("get_total_linear_damp"), &PhysicsDirectBodyState2D::get_total_linear_damp);
79 ClassDB::bind_method(D_METHOD("get_total_angular_damp"), &PhysicsDirectBodyState2D::get_total_angular_damp);
80
81 ClassDB::bind_method(D_METHOD("get_center_of_mass"), &PhysicsDirectBodyState2D::get_center_of_mass);
82 ClassDB::bind_method(D_METHOD("get_center_of_mass_local"), &PhysicsDirectBodyState2D::get_center_of_mass_local);
83 ClassDB::bind_method(D_METHOD("get_inverse_mass"), &PhysicsDirectBodyState2D::get_inverse_mass);
84 ClassDB::bind_method(D_METHOD("get_inverse_inertia"), &PhysicsDirectBodyState2D::get_inverse_inertia);
85
86 ClassDB::bind_method(D_METHOD("set_linear_velocity", "velocity"), &PhysicsDirectBodyState2D::set_linear_velocity);
87 ClassDB::bind_method(D_METHOD("get_linear_velocity"), &PhysicsDirectBodyState2D::get_linear_velocity);
88
89 ClassDB::bind_method(D_METHOD("set_angular_velocity", "velocity"), &PhysicsDirectBodyState2D::set_angular_velocity);
90 ClassDB::bind_method(D_METHOD("get_angular_velocity"), &PhysicsDirectBodyState2D::get_angular_velocity);
91
92 ClassDB::bind_method(D_METHOD("set_transform", "transform"), &PhysicsDirectBodyState2D::set_transform);
93 ClassDB::bind_method(D_METHOD("get_transform"), &PhysicsDirectBodyState2D::get_transform);
94
95 ClassDB::bind_method(D_METHOD("get_velocity_at_local_position", "local_position"), &PhysicsDirectBodyState2D::get_velocity_at_local_position);
96
97 ClassDB::bind_method(D_METHOD("apply_central_impulse", "impulse"), &PhysicsDirectBodyState2D::apply_central_impulse);
98 ClassDB::bind_method(D_METHOD("apply_torque_impulse", "impulse"), &PhysicsDirectBodyState2D::apply_torque_impulse);
99 ClassDB::bind_method(D_METHOD("apply_impulse", "impulse", "position"), &PhysicsDirectBodyState2D::apply_impulse, Vector2());
100
101 ClassDB::bind_method(D_METHOD("apply_central_force", "force"), &PhysicsDirectBodyState2D::apply_central_force, Vector2());
102 ClassDB::bind_method(D_METHOD("apply_force", "force", "position"), &PhysicsDirectBodyState2D::apply_force, Vector2());
103 ClassDB::bind_method(D_METHOD("apply_torque", "torque"), &PhysicsDirectBodyState2D::apply_torque);
104
105 ClassDB::bind_method(D_METHOD("add_constant_central_force", "force"), &PhysicsDirectBodyState2D::add_constant_central_force, Vector2());
106 ClassDB::bind_method(D_METHOD("add_constant_force", "force", "position"), &PhysicsDirectBodyState2D::add_constant_force, Vector2());
107 ClassDB::bind_method(D_METHOD("add_constant_torque", "torque"), &PhysicsDirectBodyState2D::add_constant_torque);
108
109 ClassDB::bind_method(D_METHOD("set_constant_force", "force"), &PhysicsDirectBodyState2D::set_constant_force);
110 ClassDB::bind_method(D_METHOD("get_constant_force"), &PhysicsDirectBodyState2D::get_constant_force);
111
112 ClassDB::bind_method(D_METHOD("set_constant_torque", "torque"), &PhysicsDirectBodyState2D::set_constant_torque);
113 ClassDB::bind_method(D_METHOD("get_constant_torque"), &PhysicsDirectBodyState2D::get_constant_torque);
114
115 ClassDB::bind_method(D_METHOD("set_sleep_state", "enabled"), &PhysicsDirectBodyState2D::set_sleep_state);
116 ClassDB::bind_method(D_METHOD("is_sleeping"), &PhysicsDirectBodyState2D::is_sleeping);
117
118 ClassDB::bind_method(D_METHOD("get_contact_count"), &PhysicsDirectBodyState2D::get_contact_count);
119
120 ClassDB::bind_method(D_METHOD("get_contact_local_position", "contact_idx"), &PhysicsDirectBodyState2D::get_contact_local_position);
121 ClassDB::bind_method(D_METHOD("get_contact_local_normal", "contact_idx"), &PhysicsDirectBodyState2D::get_contact_local_normal);
122 ClassDB::bind_method(D_METHOD("get_contact_local_shape", "contact_idx"), &PhysicsDirectBodyState2D::get_contact_local_shape);
123 ClassDB::bind_method(D_METHOD("get_contact_local_velocity_at_position", "contact_idx"), &PhysicsDirectBodyState2D::get_contact_local_velocity_at_position);
124 ClassDB::bind_method(D_METHOD("get_contact_collider", "contact_idx"), &PhysicsDirectBodyState2D::get_contact_collider);
125 ClassDB::bind_method(D_METHOD("get_contact_collider_position", "contact_idx"), &PhysicsDirectBodyState2D::get_contact_collider_position);
126 ClassDB::bind_method(D_METHOD("get_contact_collider_id", "contact_idx"), &PhysicsDirectBodyState2D::get_contact_collider_id);
127 ClassDB::bind_method(D_METHOD("get_contact_collider_object", "contact_idx"), &PhysicsDirectBodyState2D::get_contact_collider_object);
128 ClassDB::bind_method(D_METHOD("get_contact_collider_shape", "contact_idx"), &PhysicsDirectBodyState2D::get_contact_collider_shape);
129 ClassDB::bind_method(D_METHOD("get_contact_collider_velocity_at_position", "contact_idx"), &PhysicsDirectBodyState2D::get_contact_collider_velocity_at_position);
130 ClassDB::bind_method(D_METHOD("get_contact_impulse", "contact_idx"), &PhysicsDirectBodyState2D::get_contact_impulse);
131 ClassDB::bind_method(D_METHOD("get_step"), &PhysicsDirectBodyState2D::get_step);
132 ClassDB::bind_method(D_METHOD("integrate_forces"), &PhysicsDirectBodyState2D::integrate_forces);
133 ClassDB::bind_method(D_METHOD("get_space_state"), &PhysicsDirectBodyState2D::get_space_state);
134
135 ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "step"), "", "get_step");
136 ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "inverse_mass"), "", "get_inverse_mass");
137 ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "inverse_inertia"), "", "get_inverse_inertia");
138 ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "total_angular_damp"), "", "get_total_angular_damp");
139 ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "total_linear_damp"), "", "get_total_linear_damp");
140 ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "total_gravity"), "", "get_total_gravity");
141 ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "center_of_mass"), "", "get_center_of_mass");
142 ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "center_of_mass_local"), "", "get_center_of_mass_local");
143 ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "angular_velocity"), "set_angular_velocity", "get_angular_velocity");
144 ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "linear_velocity"), "set_linear_velocity", "get_linear_velocity");
145 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "sleeping"), "set_sleep_state", "is_sleeping");
146 ADD_PROPERTY(PropertyInfo(Variant::TRANSFORM2D, "transform"), "set_transform", "get_transform");
147}
148
149PhysicsDirectBodyState2D::PhysicsDirectBodyState2D() {}
150
151///////////////////////////////////////////////////////
152
153Ref<PhysicsRayQueryParameters2D> PhysicsRayQueryParameters2D::create(Vector2 p_from, Vector2 p_to, uint32_t p_mask, const TypedArray<RID> &p_exclude) {
154 Ref<PhysicsRayQueryParameters2D> params;
155 params.instantiate();
156 params->set_from(p_from);
157 params->set_to(p_to);
158 params->set_collision_mask(p_mask);
159 params->set_exclude(p_exclude);
160 return params;
161}
162
163void PhysicsRayQueryParameters2D::set_exclude(const TypedArray<RID> &p_exclude) {
164 parameters.exclude.clear();
165 for (int i = 0; i < p_exclude.size(); i++) {
166 parameters.exclude.insert(p_exclude[i]);
167 }
168}
169
170TypedArray<RID> PhysicsRayQueryParameters2D::get_exclude() const {
171 TypedArray<RID> ret;
172 ret.resize(parameters.exclude.size());
173 int idx = 0;
174 for (const RID &E : parameters.exclude) {
175 ret[idx++] = E;
176 }
177 return ret;
178}
179
180void PhysicsRayQueryParameters2D::_bind_methods() {
181 ClassDB::bind_static_method("PhysicsRayQueryParameters2D", D_METHOD("create", "from", "to", "collision_mask", "exclude"), &PhysicsRayQueryParameters2D::create, DEFVAL(UINT32_MAX), DEFVAL(TypedArray<RID>()));
182
183 ClassDB::bind_method(D_METHOD("set_from", "from"), &PhysicsRayQueryParameters2D::set_from);
184 ClassDB::bind_method(D_METHOD("get_from"), &PhysicsRayQueryParameters2D::get_from);
185
186 ClassDB::bind_method(D_METHOD("set_to", "to"), &PhysicsRayQueryParameters2D::set_to);
187 ClassDB::bind_method(D_METHOD("get_to"), &PhysicsRayQueryParameters2D::get_to);
188
189 ClassDB::bind_method(D_METHOD("set_collision_mask", "collision_mask"), &PhysicsRayQueryParameters2D::set_collision_mask);
190 ClassDB::bind_method(D_METHOD("get_collision_mask"), &PhysicsRayQueryParameters2D::get_collision_mask);
191
192 ClassDB::bind_method(D_METHOD("set_exclude", "exclude"), &PhysicsRayQueryParameters2D::set_exclude);
193 ClassDB::bind_method(D_METHOD("get_exclude"), &PhysicsRayQueryParameters2D::get_exclude);
194
195 ClassDB::bind_method(D_METHOD("set_collide_with_bodies", "enable"), &PhysicsRayQueryParameters2D::set_collide_with_bodies);
196 ClassDB::bind_method(D_METHOD("is_collide_with_bodies_enabled"), &PhysicsRayQueryParameters2D::is_collide_with_bodies_enabled);
197
198 ClassDB::bind_method(D_METHOD("set_collide_with_areas", "enable"), &PhysicsRayQueryParameters2D::set_collide_with_areas);
199 ClassDB::bind_method(D_METHOD("is_collide_with_areas_enabled"), &PhysicsRayQueryParameters2D::is_collide_with_areas_enabled);
200
201 ClassDB::bind_method(D_METHOD("set_hit_from_inside", "enable"), &PhysicsRayQueryParameters2D::set_hit_from_inside);
202 ClassDB::bind_method(D_METHOD("is_hit_from_inside_enabled"), &PhysicsRayQueryParameters2D::is_hit_from_inside_enabled);
203
204 ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "from"), "set_from", "get_from");
205 ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "to"), "set_to", "get_to");
206 ADD_PROPERTY(PropertyInfo(Variant::INT, "collision_mask", PROPERTY_HINT_LAYERS_2D_PHYSICS), "set_collision_mask", "get_collision_mask");
207 ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "exclude", PROPERTY_HINT_ARRAY_TYPE, "RID"), "set_exclude", "get_exclude");
208 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "collide_with_bodies"), "set_collide_with_bodies", "is_collide_with_bodies_enabled");
209 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "collide_with_areas"), "set_collide_with_areas", "is_collide_with_areas_enabled");
210 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "hit_from_inside"), "set_hit_from_inside", "is_hit_from_inside_enabled");
211}
212
213///////////////////////////////////////////////////////
214
215void PhysicsPointQueryParameters2D::set_exclude(const TypedArray<RID> &p_exclude) {
216 parameters.exclude.clear();
217 for (int i = 0; i < p_exclude.size(); i++) {
218 parameters.exclude.insert(p_exclude[i]);
219 }
220}
221
222TypedArray<RID> PhysicsPointQueryParameters2D::get_exclude() const {
223 TypedArray<RID> ret;
224 ret.resize(parameters.exclude.size());
225 int idx = 0;
226 for (const RID &E : parameters.exclude) {
227 ret[idx++] = E;
228 }
229 return ret;
230}
231
232void PhysicsPointQueryParameters2D::_bind_methods() {
233 ClassDB::bind_method(D_METHOD("set_position", "position"), &PhysicsPointQueryParameters2D::set_position);
234 ClassDB::bind_method(D_METHOD("get_position"), &PhysicsPointQueryParameters2D::get_position);
235
236 ClassDB::bind_method(D_METHOD("set_canvas_instance_id", "canvas_instance_id"), &PhysicsPointQueryParameters2D::set_canvas_instance_id);
237 ClassDB::bind_method(D_METHOD("get_canvas_instance_id"), &PhysicsPointQueryParameters2D::get_canvas_instance_id);
238
239 ClassDB::bind_method(D_METHOD("set_collision_mask", "collision_mask"), &PhysicsPointQueryParameters2D::set_collision_mask);
240 ClassDB::bind_method(D_METHOD("get_collision_mask"), &PhysicsPointQueryParameters2D::get_collision_mask);
241
242 ClassDB::bind_method(D_METHOD("set_exclude", "exclude"), &PhysicsPointQueryParameters2D::set_exclude);
243 ClassDB::bind_method(D_METHOD("get_exclude"), &PhysicsPointQueryParameters2D::get_exclude);
244
245 ClassDB::bind_method(D_METHOD("set_collide_with_bodies", "enable"), &PhysicsPointQueryParameters2D::set_collide_with_bodies);
246 ClassDB::bind_method(D_METHOD("is_collide_with_bodies_enabled"), &PhysicsPointQueryParameters2D::is_collide_with_bodies_enabled);
247
248 ClassDB::bind_method(D_METHOD("set_collide_with_areas", "enable"), &PhysicsPointQueryParameters2D::set_collide_with_areas);
249 ClassDB::bind_method(D_METHOD("is_collide_with_areas_enabled"), &PhysicsPointQueryParameters2D::is_collide_with_areas_enabled);
250
251 ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "position"), "set_position", "get_position");
252 ADD_PROPERTY(PropertyInfo(Variant::INT, "canvas_instance_id", PROPERTY_HINT_OBJECT_ID), "set_canvas_instance_id", "get_canvas_instance_id");
253 ADD_PROPERTY(PropertyInfo(Variant::INT, "collision_mask", PROPERTY_HINT_LAYERS_2D_PHYSICS), "set_collision_mask", "get_collision_mask");
254 ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "exclude", PROPERTY_HINT_ARRAY_TYPE, "RID"), "set_exclude", "get_exclude");
255 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "collide_with_bodies"), "set_collide_with_bodies", "is_collide_with_bodies_enabled");
256 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "collide_with_areas"), "set_collide_with_areas", "is_collide_with_areas_enabled");
257}
258
259///////////////////////////////////////////////////////
260
261void PhysicsShapeQueryParameters2D::set_shape(const Ref<Resource> &p_shape_ref) {
262 ERR_FAIL_COND(p_shape_ref.is_null());
263 shape_ref = p_shape_ref;
264 parameters.shape_rid = p_shape_ref->get_rid();
265}
266
267void PhysicsShapeQueryParameters2D::set_shape_rid(const RID &p_shape) {
268 if (parameters.shape_rid != p_shape) {
269 shape_ref = Ref<Resource>();
270 parameters.shape_rid = p_shape;
271 }
272}
273
274void PhysicsShapeQueryParameters2D::set_exclude(const TypedArray<RID> &p_exclude) {
275 parameters.exclude.clear();
276 for (int i = 0; i < p_exclude.size(); i++) {
277 parameters.exclude.insert(p_exclude[i]);
278 }
279}
280
281TypedArray<RID> PhysicsShapeQueryParameters2D::get_exclude() const {
282 TypedArray<RID> ret;
283 ret.resize(parameters.exclude.size());
284 int idx = 0;
285 for (const RID &E : parameters.exclude) {
286 ret[idx++] = E;
287 }
288 return ret;
289}
290
291void PhysicsShapeQueryParameters2D::_bind_methods() {
292 ClassDB::bind_method(D_METHOD("set_shape", "shape"), &PhysicsShapeQueryParameters2D::set_shape);
293 ClassDB::bind_method(D_METHOD("get_shape"), &PhysicsShapeQueryParameters2D::get_shape);
294
295 ClassDB::bind_method(D_METHOD("set_shape_rid", "shape"), &PhysicsShapeQueryParameters2D::set_shape_rid);
296 ClassDB::bind_method(D_METHOD("get_shape_rid"), &PhysicsShapeQueryParameters2D::get_shape_rid);
297
298 ClassDB::bind_method(D_METHOD("set_transform", "transform"), &PhysicsShapeQueryParameters2D::set_transform);
299 ClassDB::bind_method(D_METHOD("get_transform"), &PhysicsShapeQueryParameters2D::get_transform);
300
301 ClassDB::bind_method(D_METHOD("set_motion", "motion"), &PhysicsShapeQueryParameters2D::set_motion);
302 ClassDB::bind_method(D_METHOD("get_motion"), &PhysicsShapeQueryParameters2D::get_motion);
303
304 ClassDB::bind_method(D_METHOD("set_margin", "margin"), &PhysicsShapeQueryParameters2D::set_margin);
305 ClassDB::bind_method(D_METHOD("get_margin"), &PhysicsShapeQueryParameters2D::get_margin);
306
307 ClassDB::bind_method(D_METHOD("set_collision_mask", "collision_mask"), &PhysicsShapeQueryParameters2D::set_collision_mask);
308 ClassDB::bind_method(D_METHOD("get_collision_mask"), &PhysicsShapeQueryParameters2D::get_collision_mask);
309
310 ClassDB::bind_method(D_METHOD("set_exclude", "exclude"), &PhysicsShapeQueryParameters2D::set_exclude);
311 ClassDB::bind_method(D_METHOD("get_exclude"), &PhysicsShapeQueryParameters2D::get_exclude);
312
313 ClassDB::bind_method(D_METHOD("set_collide_with_bodies", "enable"), &PhysicsShapeQueryParameters2D::set_collide_with_bodies);
314 ClassDB::bind_method(D_METHOD("is_collide_with_bodies_enabled"), &PhysicsShapeQueryParameters2D::is_collide_with_bodies_enabled);
315
316 ClassDB::bind_method(D_METHOD("set_collide_with_areas", "enable"), &PhysicsShapeQueryParameters2D::set_collide_with_areas);
317 ClassDB::bind_method(D_METHOD("is_collide_with_areas_enabled"), &PhysicsShapeQueryParameters2D::is_collide_with_areas_enabled);
318
319 ADD_PROPERTY(PropertyInfo(Variant::INT, "collision_mask", PROPERTY_HINT_LAYERS_2D_PHYSICS), "set_collision_mask", "get_collision_mask");
320 ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "exclude", PROPERTY_HINT_ARRAY_TYPE, "RID"), "set_exclude", "get_exclude");
321 ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "margin", PROPERTY_HINT_RANGE, "0,100,0.01"), "set_margin", "get_margin");
322 ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "motion"), "set_motion", "get_motion");
323 ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "shape", PROPERTY_HINT_RESOURCE_TYPE, "Shape2D"), "set_shape", "get_shape");
324 ADD_PROPERTY(PropertyInfo(Variant::RID, "shape_rid"), "set_shape_rid", "get_shape_rid");
325 ADD_PROPERTY(PropertyInfo(Variant::TRANSFORM2D, "transform"), "set_transform", "get_transform");
326 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "collide_with_bodies"), "set_collide_with_bodies", "is_collide_with_bodies_enabled");
327 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "collide_with_areas"), "set_collide_with_areas", "is_collide_with_areas_enabled");
328}
329
330///////////////////////////////////////////////////////
331
332Dictionary PhysicsDirectSpaceState2D::_intersect_ray(const Ref<PhysicsRayQueryParameters2D> &p_ray_query) {
333 ERR_FAIL_COND_V(!p_ray_query.is_valid(), Dictionary());
334
335 RayResult result;
336 bool res = intersect_ray(p_ray_query->get_parameters(), result);
337
338 if (!res) {
339 return Dictionary();
340 }
341
342 Dictionary d;
343 d["position"] = result.position;
344 d["normal"] = result.normal;
345 d["collider_id"] = result.collider_id;
346 d["collider"] = result.collider;
347 d["shape"] = result.shape;
348 d["rid"] = result.rid;
349
350 return d;
351}
352
353TypedArray<Dictionary> PhysicsDirectSpaceState2D::_intersect_point(const Ref<PhysicsPointQueryParameters2D> &p_point_query, int p_max_results) {
354 ERR_FAIL_COND_V(p_point_query.is_null(), Array());
355
356 Vector<ShapeResult> ret;
357 ret.resize(p_max_results);
358
359 int rc = intersect_point(p_point_query->get_parameters(), ret.ptrw(), ret.size());
360
361 if (rc == 0) {
362 return TypedArray<Dictionary>();
363 }
364
365 TypedArray<Dictionary> r;
366 r.resize(rc);
367 for (int i = 0; i < rc; i++) {
368 Dictionary d;
369 d["rid"] = ret[i].rid;
370 d["collider_id"] = ret[i].collider_id;
371 d["collider"] = ret[i].collider;
372 d["shape"] = ret[i].shape;
373 r[i] = d;
374 }
375 return r;
376}
377
378TypedArray<Dictionary> PhysicsDirectSpaceState2D::_intersect_shape(const Ref<PhysicsShapeQueryParameters2D> &p_shape_query, int p_max_results) {
379 ERR_FAIL_COND_V(!p_shape_query.is_valid(), TypedArray<Dictionary>());
380
381 Vector<ShapeResult> sr;
382 sr.resize(p_max_results);
383 int rc = intersect_shape(p_shape_query->get_parameters(), sr.ptrw(), sr.size());
384 TypedArray<Dictionary> ret;
385 ret.resize(rc);
386 for (int i = 0; i < rc; i++) {
387 Dictionary d;
388 d["rid"] = sr[i].rid;
389 d["collider_id"] = sr[i].collider_id;
390 d["collider"] = sr[i].collider;
391 d["shape"] = sr[i].shape;
392 ret[i] = d;
393 }
394
395 return ret;
396}
397
398Vector<real_t> PhysicsDirectSpaceState2D::_cast_motion(const Ref<PhysicsShapeQueryParameters2D> &p_shape_query) {
399 ERR_FAIL_COND_V(!p_shape_query.is_valid(), Vector<real_t>());
400
401 real_t closest_safe, closest_unsafe;
402 bool res = cast_motion(p_shape_query->get_parameters(), closest_safe, closest_unsafe);
403 if (!res) {
404 return Vector<real_t>();
405 }
406 Vector<real_t> ret;
407 ret.resize(2);
408 ret.write[0] = closest_safe;
409 ret.write[1] = closest_unsafe;
410 return ret;
411}
412
413TypedArray<Vector2> PhysicsDirectSpaceState2D::_collide_shape(const Ref<PhysicsShapeQueryParameters2D> &p_shape_query, int p_max_results) {
414 ERR_FAIL_COND_V(!p_shape_query.is_valid(), TypedArray<Vector2>());
415
416 Vector<Vector2> ret;
417 ret.resize(p_max_results * 2);
418 int rc = 0;
419 bool res = collide_shape(p_shape_query->get_parameters(), ret.ptrw(), p_max_results, rc);
420 if (!res) {
421 return TypedArray<Vector2>();
422 }
423 TypedArray<Vector2> r;
424 r.resize(rc * 2);
425 for (int i = 0; i < rc * 2; i++) {
426 r[i] = ret[i];
427 }
428 return r;
429}
430
431Dictionary PhysicsDirectSpaceState2D::_get_rest_info(const Ref<PhysicsShapeQueryParameters2D> &p_shape_query) {
432 ERR_FAIL_COND_V(!p_shape_query.is_valid(), Dictionary());
433
434 ShapeRestInfo sri;
435
436 bool res = rest_info(p_shape_query->get_parameters(), &sri);
437 Dictionary r;
438 if (!res) {
439 return r;
440 }
441
442 r["point"] = sri.point;
443 r["normal"] = sri.normal;
444 r["rid"] = sri.rid;
445 r["collider_id"] = sri.collider_id;
446 r["shape"] = sri.shape;
447 r["linear_velocity"] = sri.linear_velocity;
448
449 return r;
450}
451
452PhysicsDirectSpaceState2D::PhysicsDirectSpaceState2D() {
453}
454
455void PhysicsDirectSpaceState2D::_bind_methods() {
456 ClassDB::bind_method(D_METHOD("intersect_point", "parameters", "max_results"), &PhysicsDirectSpaceState2D::_intersect_point, DEFVAL(32));
457 ClassDB::bind_method(D_METHOD("intersect_ray", "parameters"), &PhysicsDirectSpaceState2D::_intersect_ray);
458 ClassDB::bind_method(D_METHOD("intersect_shape", "parameters", "max_results"), &PhysicsDirectSpaceState2D::_intersect_shape, DEFVAL(32));
459 ClassDB::bind_method(D_METHOD("cast_motion", "parameters"), &PhysicsDirectSpaceState2D::_cast_motion);
460 ClassDB::bind_method(D_METHOD("collide_shape", "parameters", "max_results"), &PhysicsDirectSpaceState2D::_collide_shape, DEFVAL(32));
461 ClassDB::bind_method(D_METHOD("get_rest_info", "parameters"), &PhysicsDirectSpaceState2D::_get_rest_info);
462}
463
464///////////////////////////////
465
466TypedArray<RID> PhysicsTestMotionParameters2D::get_exclude_bodies() const {
467 TypedArray<RID> exclude;
468 exclude.resize(parameters.exclude_bodies.size());
469
470 int body_index = 0;
471 for (RID body : parameters.exclude_bodies) {
472 exclude[body_index++] = body;
473 }
474
475 return exclude;
476}
477
478void PhysicsTestMotionParameters2D::set_exclude_bodies(const TypedArray<RID> &p_exclude) {
479 for (int i = 0; i < p_exclude.size(); i++) {
480 parameters.exclude_bodies.insert(p_exclude[i]);
481 }
482}
483
484TypedArray<uint64_t> PhysicsTestMotionParameters2D::get_exclude_objects() const {
485 TypedArray<uint64_t> exclude;
486 exclude.resize(parameters.exclude_objects.size());
487
488 int object_index = 0;
489 for (ObjectID object_id : parameters.exclude_objects) {
490 exclude[object_index++] = object_id;
491 }
492
493 return exclude;
494}
495
496void PhysicsTestMotionParameters2D::set_exclude_objects(const TypedArray<uint64_t> &p_exclude) {
497 for (int i = 0; i < p_exclude.size(); ++i) {
498 ObjectID object_id = p_exclude[i];
499 ERR_CONTINUE(object_id.is_null());
500 parameters.exclude_objects.insert(object_id);
501 }
502}
503
504void PhysicsTestMotionParameters2D::_bind_methods() {
505 ClassDB::bind_method(D_METHOD("get_from"), &PhysicsTestMotionParameters2D::get_from);
506 ClassDB::bind_method(D_METHOD("set_from", "from"), &PhysicsTestMotionParameters2D::set_from);
507
508 ClassDB::bind_method(D_METHOD("get_motion"), &PhysicsTestMotionParameters2D::get_motion);
509 ClassDB::bind_method(D_METHOD("set_motion", "motion"), &PhysicsTestMotionParameters2D::set_motion);
510
511 ClassDB::bind_method(D_METHOD("get_margin"), &PhysicsTestMotionParameters2D::get_margin);
512 ClassDB::bind_method(D_METHOD("set_margin", "margin"), &PhysicsTestMotionParameters2D::set_margin);
513
514 ClassDB::bind_method(D_METHOD("is_collide_separation_ray_enabled"), &PhysicsTestMotionParameters2D::is_collide_separation_ray_enabled);
515 ClassDB::bind_method(D_METHOD("set_collide_separation_ray_enabled", "enabled"), &PhysicsTestMotionParameters2D::set_collide_separation_ray_enabled);
516
517 ClassDB::bind_method(D_METHOD("get_exclude_bodies"), &PhysicsTestMotionParameters2D::get_exclude_bodies);
518 ClassDB::bind_method(D_METHOD("set_exclude_bodies", "exclude_list"), &PhysicsTestMotionParameters2D::set_exclude_bodies);
519
520 ClassDB::bind_method(D_METHOD("get_exclude_objects"), &PhysicsTestMotionParameters2D::get_exclude_objects);
521 ClassDB::bind_method(D_METHOD("set_exclude_objects", "exclude_list"), &PhysicsTestMotionParameters2D::set_exclude_objects);
522
523 ClassDB::bind_method(D_METHOD("is_recovery_as_collision_enabled"), &PhysicsTestMotionParameters2D::is_recovery_as_collision_enabled);
524 ClassDB::bind_method(D_METHOD("set_recovery_as_collision_enabled", "enabled"), &PhysicsTestMotionParameters2D::set_recovery_as_collision_enabled);
525
526 ADD_PROPERTY(PropertyInfo(Variant::TRANSFORM2D, "from"), "set_from", "get_from");
527 ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "motion"), "set_motion", "get_motion");
528 ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "margin"), "set_margin", "get_margin");
529 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "collide_separation_ray"), "set_collide_separation_ray_enabled", "is_collide_separation_ray_enabled");
530 ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "exclude_bodies", PROPERTY_HINT_ARRAY_TYPE, "RID"), "set_exclude_bodies", "get_exclude_bodies");
531 ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "exclude_objects"), "set_exclude_objects", "get_exclude_objects");
532 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "recovery_as_collision"), "set_recovery_as_collision_enabled", "is_recovery_as_collision_enabled");
533}
534
535///////////////////////////////
536
537Vector2 PhysicsTestMotionResult2D::get_travel() const {
538 return result.travel;
539}
540
541Vector2 PhysicsTestMotionResult2D::get_remainder() const {
542 return result.remainder;
543}
544
545Vector2 PhysicsTestMotionResult2D::get_collision_point() const {
546 return result.collision_point;
547}
548
549Vector2 PhysicsTestMotionResult2D::get_collision_normal() const {
550 return result.collision_normal;
551}
552
553Vector2 PhysicsTestMotionResult2D::get_collider_velocity() const {
554 return result.collider_velocity;
555}
556
557ObjectID PhysicsTestMotionResult2D::get_collider_id() const {
558 return result.collider_id;
559}
560
561RID PhysicsTestMotionResult2D::get_collider_rid() const {
562 return result.collider;
563}
564
565Object *PhysicsTestMotionResult2D::get_collider() const {
566 return ObjectDB::get_instance(result.collider_id);
567}
568
569int PhysicsTestMotionResult2D::get_collider_shape() const {
570 return result.collider_shape;
571}
572
573int PhysicsTestMotionResult2D::get_collision_local_shape() const {
574 return result.collision_local_shape;
575}
576
577real_t PhysicsTestMotionResult2D::get_collision_depth() const {
578 return result.collision_depth;
579}
580
581real_t PhysicsTestMotionResult2D::get_collision_safe_fraction() const {
582 return result.collision_safe_fraction;
583}
584
585real_t PhysicsTestMotionResult2D::get_collision_unsafe_fraction() const {
586 return result.collision_unsafe_fraction;
587}
588
589void PhysicsTestMotionResult2D::_bind_methods() {
590 ClassDB::bind_method(D_METHOD("get_travel"), &PhysicsTestMotionResult2D::get_travel);
591 ClassDB::bind_method(D_METHOD("get_remainder"), &PhysicsTestMotionResult2D::get_remainder);
592 ClassDB::bind_method(D_METHOD("get_collision_point"), &PhysicsTestMotionResult2D::get_collision_point);
593 ClassDB::bind_method(D_METHOD("get_collision_normal"), &PhysicsTestMotionResult2D::get_collision_normal);
594 ClassDB::bind_method(D_METHOD("get_collider_velocity"), &PhysicsTestMotionResult2D::get_collider_velocity);
595 ClassDB::bind_method(D_METHOD("get_collider_id"), &PhysicsTestMotionResult2D::get_collider_id);
596 ClassDB::bind_method(D_METHOD("get_collider_rid"), &PhysicsTestMotionResult2D::get_collider_rid);
597 ClassDB::bind_method(D_METHOD("get_collider"), &PhysicsTestMotionResult2D::get_collider);
598 ClassDB::bind_method(D_METHOD("get_collider_shape"), &PhysicsTestMotionResult2D::get_collider_shape);
599 ClassDB::bind_method(D_METHOD("get_collision_local_shape"), &PhysicsTestMotionResult2D::get_collision_local_shape);
600 ClassDB::bind_method(D_METHOD("get_collision_depth"), &PhysicsTestMotionResult2D::get_collision_depth);
601 ClassDB::bind_method(D_METHOD("get_collision_safe_fraction"), &PhysicsTestMotionResult2D::get_collision_safe_fraction);
602 ClassDB::bind_method(D_METHOD("get_collision_unsafe_fraction"), &PhysicsTestMotionResult2D::get_collision_unsafe_fraction);
603}
604
605///////////////////////////////////////
606
607bool PhysicsServer2D::_body_test_motion(RID p_body, const Ref<PhysicsTestMotionParameters2D> &p_parameters, const Ref<PhysicsTestMotionResult2D> &p_result) {
608 ERR_FAIL_COND_V(!p_parameters.is_valid(), false);
609
610 MotionResult *result_ptr = nullptr;
611 if (p_result.is_valid()) {
612 result_ptr = p_result->get_result_ptr();
613 }
614
615 return body_test_motion(p_body, p_parameters->get_parameters(), result_ptr);
616}
617
618void PhysicsServer2D::_bind_methods() {
619 ClassDB::bind_method(D_METHOD("world_boundary_shape_create"), &PhysicsServer2D::world_boundary_shape_create);
620 ClassDB::bind_method(D_METHOD("separation_ray_shape_create"), &PhysicsServer2D::separation_ray_shape_create);
621 ClassDB::bind_method(D_METHOD("segment_shape_create"), &PhysicsServer2D::segment_shape_create);
622 ClassDB::bind_method(D_METHOD("circle_shape_create"), &PhysicsServer2D::circle_shape_create);
623 ClassDB::bind_method(D_METHOD("rectangle_shape_create"), &PhysicsServer2D::rectangle_shape_create);
624 ClassDB::bind_method(D_METHOD("capsule_shape_create"), &PhysicsServer2D::capsule_shape_create);
625 ClassDB::bind_method(D_METHOD("convex_polygon_shape_create"), &PhysicsServer2D::convex_polygon_shape_create);
626 ClassDB::bind_method(D_METHOD("concave_polygon_shape_create"), &PhysicsServer2D::concave_polygon_shape_create);
627
628 ClassDB::bind_method(D_METHOD("shape_set_data", "shape", "data"), &PhysicsServer2D::shape_set_data);
629
630 ClassDB::bind_method(D_METHOD("shape_get_type", "shape"), &PhysicsServer2D::shape_get_type);
631 ClassDB::bind_method(D_METHOD("shape_get_data", "shape"), &PhysicsServer2D::shape_get_data);
632
633 ClassDB::bind_method(D_METHOD("space_create"), &PhysicsServer2D::space_create);
634 ClassDB::bind_method(D_METHOD("space_set_active", "space", "active"), &PhysicsServer2D::space_set_active);
635 ClassDB::bind_method(D_METHOD("space_is_active", "space"), &PhysicsServer2D::space_is_active);
636 ClassDB::bind_method(D_METHOD("space_set_param", "space", "param", "value"), &PhysicsServer2D::space_set_param);
637 ClassDB::bind_method(D_METHOD("space_get_param", "space", "param"), &PhysicsServer2D::space_get_param);
638 ClassDB::bind_method(D_METHOD("space_get_direct_state", "space"), &PhysicsServer2D::space_get_direct_state);
639
640 ClassDB::bind_method(D_METHOD("area_create"), &PhysicsServer2D::area_create);
641 ClassDB::bind_method(D_METHOD("area_set_space", "area", "space"), &PhysicsServer2D::area_set_space);
642 ClassDB::bind_method(D_METHOD("area_get_space", "area"), &PhysicsServer2D::area_get_space);
643
644 ClassDB::bind_method(D_METHOD("area_add_shape", "area", "shape", "transform", "disabled"), &PhysicsServer2D::area_add_shape, DEFVAL(Transform2D()), DEFVAL(false));
645 ClassDB::bind_method(D_METHOD("area_set_shape", "area", "shape_idx", "shape"), &PhysicsServer2D::area_set_shape);
646 ClassDB::bind_method(D_METHOD("area_set_shape_transform", "area", "shape_idx", "transform"), &PhysicsServer2D::area_set_shape_transform);
647 ClassDB::bind_method(D_METHOD("area_set_shape_disabled", "area", "shape_idx", "disabled"), &PhysicsServer2D::area_set_shape_disabled);
648
649 ClassDB::bind_method(D_METHOD("area_get_shape_count", "area"), &PhysicsServer2D::area_get_shape_count);
650 ClassDB::bind_method(D_METHOD("area_get_shape", "area", "shape_idx"), &PhysicsServer2D::area_get_shape);
651 ClassDB::bind_method(D_METHOD("area_get_shape_transform", "area", "shape_idx"), &PhysicsServer2D::area_get_shape_transform);
652
653 ClassDB::bind_method(D_METHOD("area_remove_shape", "area", "shape_idx"), &PhysicsServer2D::area_remove_shape);
654 ClassDB::bind_method(D_METHOD("area_clear_shapes", "area"), &PhysicsServer2D::area_clear_shapes);
655
656 ClassDB::bind_method(D_METHOD("area_set_collision_layer", "area", "layer"), &PhysicsServer2D::area_set_collision_layer);
657 ClassDB::bind_method(D_METHOD("area_get_collision_layer", "area"), &PhysicsServer2D::area_get_collision_layer);
658
659 ClassDB::bind_method(D_METHOD("area_set_collision_mask", "area", "mask"), &PhysicsServer2D::area_set_collision_mask);
660 ClassDB::bind_method(D_METHOD("area_get_collision_mask", "area"), &PhysicsServer2D::area_get_collision_mask);
661
662 ClassDB::bind_method(D_METHOD("area_set_param", "area", "param", "value"), &PhysicsServer2D::area_set_param);
663 ClassDB::bind_method(D_METHOD("area_set_transform", "area", "transform"), &PhysicsServer2D::area_set_transform);
664
665 ClassDB::bind_method(D_METHOD("area_get_param", "area", "param"), &PhysicsServer2D::area_get_param);
666 ClassDB::bind_method(D_METHOD("area_get_transform", "area"), &PhysicsServer2D::area_get_transform);
667
668 ClassDB::bind_method(D_METHOD("area_attach_object_instance_id", "area", "id"), &PhysicsServer2D::area_attach_object_instance_id);
669 ClassDB::bind_method(D_METHOD("area_get_object_instance_id", "area"), &PhysicsServer2D::area_get_object_instance_id);
670
671 ClassDB::bind_method(D_METHOD("area_attach_canvas_instance_id", "area", "id"), &PhysicsServer2D::area_attach_canvas_instance_id);
672 ClassDB::bind_method(D_METHOD("area_get_canvas_instance_id", "area"), &PhysicsServer2D::area_get_canvas_instance_id);
673
674 ClassDB::bind_method(D_METHOD("area_set_monitor_callback", "area", "callback"), &PhysicsServer2D::area_set_monitor_callback);
675 ClassDB::bind_method(D_METHOD("area_set_area_monitor_callback", "area", "callback"), &PhysicsServer2D::area_set_area_monitor_callback);
676 ClassDB::bind_method(D_METHOD("area_set_monitorable", "area", "monitorable"), &PhysicsServer2D::area_set_monitorable);
677
678 ClassDB::bind_method(D_METHOD("body_create"), &PhysicsServer2D::body_create);
679
680 ClassDB::bind_method(D_METHOD("body_set_space", "body", "space"), &PhysicsServer2D::body_set_space);
681 ClassDB::bind_method(D_METHOD("body_get_space", "body"), &PhysicsServer2D::body_get_space);
682
683 ClassDB::bind_method(D_METHOD("body_set_mode", "body", "mode"), &PhysicsServer2D::body_set_mode);
684 ClassDB::bind_method(D_METHOD("body_get_mode", "body"), &PhysicsServer2D::body_get_mode);
685
686 ClassDB::bind_method(D_METHOD("body_add_shape", "body", "shape", "transform", "disabled"), &PhysicsServer2D::body_add_shape, DEFVAL(Transform2D()), DEFVAL(false));
687 ClassDB::bind_method(D_METHOD("body_set_shape", "body", "shape_idx", "shape"), &PhysicsServer2D::body_set_shape);
688 ClassDB::bind_method(D_METHOD("body_set_shape_transform", "body", "shape_idx", "transform"), &PhysicsServer2D::body_set_shape_transform);
689
690 ClassDB::bind_method(D_METHOD("body_get_shape_count", "body"), &PhysicsServer2D::body_get_shape_count);
691 ClassDB::bind_method(D_METHOD("body_get_shape", "body", "shape_idx"), &PhysicsServer2D::body_get_shape);
692 ClassDB::bind_method(D_METHOD("body_get_shape_transform", "body", "shape_idx"), &PhysicsServer2D::body_get_shape_transform);
693
694 ClassDB::bind_method(D_METHOD("body_remove_shape", "body", "shape_idx"), &PhysicsServer2D::body_remove_shape);
695 ClassDB::bind_method(D_METHOD("body_clear_shapes", "body"), &PhysicsServer2D::body_clear_shapes);
696
697 ClassDB::bind_method(D_METHOD("body_set_shape_disabled", "body", "shape_idx", "disabled"), &PhysicsServer2D::body_set_shape_disabled);
698 ClassDB::bind_method(D_METHOD("body_set_shape_as_one_way_collision", "body", "shape_idx", "enable", "margin"), &PhysicsServer2D::body_set_shape_as_one_way_collision);
699
700 ClassDB::bind_method(D_METHOD("body_attach_object_instance_id", "body", "id"), &PhysicsServer2D::body_attach_object_instance_id);
701 ClassDB::bind_method(D_METHOD("body_get_object_instance_id", "body"), &PhysicsServer2D::body_get_object_instance_id);
702
703 ClassDB::bind_method(D_METHOD("body_attach_canvas_instance_id", "body", "id"), &PhysicsServer2D::body_attach_canvas_instance_id);
704 ClassDB::bind_method(D_METHOD("body_get_canvas_instance_id", "body"), &PhysicsServer2D::body_get_canvas_instance_id);
705
706 ClassDB::bind_method(D_METHOD("body_set_continuous_collision_detection_mode", "body", "mode"), &PhysicsServer2D::body_set_continuous_collision_detection_mode);
707 ClassDB::bind_method(D_METHOD("body_get_continuous_collision_detection_mode", "body"), &PhysicsServer2D::body_get_continuous_collision_detection_mode);
708
709 ClassDB::bind_method(D_METHOD("body_set_collision_layer", "body", "layer"), &PhysicsServer2D::body_set_collision_layer);
710 ClassDB::bind_method(D_METHOD("body_get_collision_layer", "body"), &PhysicsServer2D::body_get_collision_layer);
711
712 ClassDB::bind_method(D_METHOD("body_set_collision_mask", "body", "mask"), &PhysicsServer2D::body_set_collision_mask);
713 ClassDB::bind_method(D_METHOD("body_get_collision_mask", "body"), &PhysicsServer2D::body_get_collision_mask);
714
715 ClassDB::bind_method(D_METHOD("body_set_collision_priority", "body", "priority"), &PhysicsServer2D::body_set_collision_priority);
716 ClassDB::bind_method(D_METHOD("body_get_collision_priority", "body"), &PhysicsServer2D::body_get_collision_priority);
717
718 ClassDB::bind_method(D_METHOD("body_set_param", "body", "param", "value"), &PhysicsServer2D::body_set_param);
719 ClassDB::bind_method(D_METHOD("body_get_param", "body", "param"), &PhysicsServer2D::body_get_param);
720
721 ClassDB::bind_method(D_METHOD("body_reset_mass_properties", "body"), &PhysicsServer2D::body_reset_mass_properties);
722
723 ClassDB::bind_method(D_METHOD("body_set_state", "body", "state", "value"), &PhysicsServer2D::body_set_state);
724 ClassDB::bind_method(D_METHOD("body_get_state", "body", "state"), &PhysicsServer2D::body_get_state);
725
726 ClassDB::bind_method(D_METHOD("body_apply_central_impulse", "body", "impulse"), &PhysicsServer2D::body_apply_central_impulse);
727 ClassDB::bind_method(D_METHOD("body_apply_torque_impulse", "body", "impulse"), &PhysicsServer2D::body_apply_torque_impulse);
728 ClassDB::bind_method(D_METHOD("body_apply_impulse", "body", "impulse", "position"), &PhysicsServer2D::body_apply_impulse, Vector2());
729
730 ClassDB::bind_method(D_METHOD("body_apply_central_force", "body", "force"), &PhysicsServer2D::body_apply_central_force);
731 ClassDB::bind_method(D_METHOD("body_apply_force", "body", "force", "position"), &PhysicsServer2D::body_apply_force, Vector2());
732 ClassDB::bind_method(D_METHOD("body_apply_torque", "body", "torque"), &PhysicsServer2D::body_apply_torque);
733
734 ClassDB::bind_method(D_METHOD("body_add_constant_central_force", "body", "force"), &PhysicsServer2D::body_add_constant_central_force);
735 ClassDB::bind_method(D_METHOD("body_add_constant_force", "body", "force", "position"), &PhysicsServer2D::body_add_constant_force, Vector2());
736 ClassDB::bind_method(D_METHOD("body_add_constant_torque", "body", "torque"), &PhysicsServer2D::body_add_constant_torque);
737
738 ClassDB::bind_method(D_METHOD("body_set_constant_force", "body", "force"), &PhysicsServer2D::body_set_constant_force);
739 ClassDB::bind_method(D_METHOD("body_get_constant_force", "body"), &PhysicsServer2D::body_get_constant_force);
740
741 ClassDB::bind_method(D_METHOD("body_set_constant_torque", "body", "torque"), &PhysicsServer2D::body_set_constant_torque);
742 ClassDB::bind_method(D_METHOD("body_get_constant_torque", "body"), &PhysicsServer2D::body_get_constant_torque);
743
744 ClassDB::bind_method(D_METHOD("body_set_axis_velocity", "body", "axis_velocity"), &PhysicsServer2D::body_set_axis_velocity);
745
746 ClassDB::bind_method(D_METHOD("body_add_collision_exception", "body", "excepted_body"), &PhysicsServer2D::body_add_collision_exception);
747 ClassDB::bind_method(D_METHOD("body_remove_collision_exception", "body", "excepted_body"), &PhysicsServer2D::body_remove_collision_exception);
748
749 ClassDB::bind_method(D_METHOD("body_set_max_contacts_reported", "body", "amount"), &PhysicsServer2D::body_set_max_contacts_reported);
750 ClassDB::bind_method(D_METHOD("body_get_max_contacts_reported", "body"), &PhysicsServer2D::body_get_max_contacts_reported);
751
752 ClassDB::bind_method(D_METHOD("body_set_omit_force_integration", "body", "enable"), &PhysicsServer2D::body_set_omit_force_integration);
753 ClassDB::bind_method(D_METHOD("body_is_omitting_force_integration", "body"), &PhysicsServer2D::body_is_omitting_force_integration);
754
755 ClassDB::bind_method(D_METHOD("body_set_force_integration_callback", "body", "callable", "userdata"), &PhysicsServer2D::body_set_force_integration_callback, DEFVAL(Variant()));
756
757 ClassDB::bind_method(D_METHOD("body_test_motion", "body", "parameters", "result"), &PhysicsServer2D::_body_test_motion, DEFVAL(Variant()));
758
759 ClassDB::bind_method(D_METHOD("body_get_direct_state", "body"), &PhysicsServer2D::body_get_direct_state);
760
761 /* JOINT API */
762
763 ClassDB::bind_method(D_METHOD("joint_create"), &PhysicsServer2D::joint_create);
764
765 ClassDB::bind_method(D_METHOD("joint_clear", "joint"), &PhysicsServer2D::joint_clear);
766
767 ClassDB::bind_method(D_METHOD("joint_set_param", "joint", "param", "value"), &PhysicsServer2D::joint_set_param);
768 ClassDB::bind_method(D_METHOD("joint_get_param", "joint", "param"), &PhysicsServer2D::joint_get_param);
769
770 ClassDB::bind_method(D_METHOD("joint_disable_collisions_between_bodies", "joint", "disable"), &PhysicsServer2D::joint_disable_collisions_between_bodies);
771 ClassDB::bind_method(D_METHOD("joint_is_disabled_collisions_between_bodies", "joint"), &PhysicsServer2D::joint_is_disabled_collisions_between_bodies);
772
773 ClassDB::bind_method(D_METHOD("joint_make_pin", "joint", "anchor", "body_a", "body_b"), &PhysicsServer2D::joint_make_pin, DEFVAL(RID()));
774 ClassDB::bind_method(D_METHOD("joint_make_groove", "joint", "groove1_a", "groove2_a", "anchor_b", "body_a", "body_b"), &PhysicsServer2D::joint_make_groove, DEFVAL(RID()), DEFVAL(RID()));
775 ClassDB::bind_method(D_METHOD("joint_make_damped_spring", "joint", "anchor_a", "anchor_b", "body_a", "body_b"), &PhysicsServer2D::joint_make_damped_spring, DEFVAL(RID()));
776
777 ClassDB::bind_method(D_METHOD("pin_joint_set_param", "joint", "param", "value"), &PhysicsServer2D::pin_joint_set_param);
778 ClassDB::bind_method(D_METHOD("pin_joint_get_param", "joint", "param"), &PhysicsServer2D::pin_joint_get_param);
779
780 ClassDB::bind_method(D_METHOD("damped_spring_joint_set_param", "joint", "param", "value"), &PhysicsServer2D::damped_spring_joint_set_param);
781 ClassDB::bind_method(D_METHOD("damped_spring_joint_get_param", "joint", "param"), &PhysicsServer2D::damped_spring_joint_get_param);
782
783 ClassDB::bind_method(D_METHOD("joint_get_type", "joint"), &PhysicsServer2D::joint_get_type);
784
785 ClassDB::bind_method(D_METHOD("free_rid", "rid"), &PhysicsServer2D::free);
786
787 ClassDB::bind_method(D_METHOD("set_active", "active"), &PhysicsServer2D::set_active);
788
789 ClassDB::bind_method(D_METHOD("get_process_info", "process_info"), &PhysicsServer2D::get_process_info);
790
791 BIND_ENUM_CONSTANT(SPACE_PARAM_CONTACT_RECYCLE_RADIUS);
792 BIND_ENUM_CONSTANT(SPACE_PARAM_CONTACT_MAX_SEPARATION);
793 BIND_ENUM_CONSTANT(SPACE_PARAM_CONTACT_MAX_ALLOWED_PENETRATION);
794 BIND_ENUM_CONSTANT(SPACE_PARAM_CONTACT_DEFAULT_BIAS);
795 BIND_ENUM_CONSTANT(SPACE_PARAM_BODY_LINEAR_VELOCITY_SLEEP_THRESHOLD);
796 BIND_ENUM_CONSTANT(SPACE_PARAM_BODY_ANGULAR_VELOCITY_SLEEP_THRESHOLD);
797 BIND_ENUM_CONSTANT(SPACE_PARAM_BODY_TIME_TO_SLEEP);
798 BIND_ENUM_CONSTANT(SPACE_PARAM_CONSTRAINT_DEFAULT_BIAS);
799 BIND_ENUM_CONSTANT(SPACE_PARAM_SOLVER_ITERATIONS);
800
801 BIND_ENUM_CONSTANT(SHAPE_WORLD_BOUNDARY);
802 BIND_ENUM_CONSTANT(SHAPE_SEPARATION_RAY);
803 BIND_ENUM_CONSTANT(SHAPE_SEGMENT);
804 BIND_ENUM_CONSTANT(SHAPE_CIRCLE);
805 BIND_ENUM_CONSTANT(SHAPE_RECTANGLE);
806 BIND_ENUM_CONSTANT(SHAPE_CAPSULE);
807 BIND_ENUM_CONSTANT(SHAPE_CONVEX_POLYGON);
808 BIND_ENUM_CONSTANT(SHAPE_CONCAVE_POLYGON);
809 BIND_ENUM_CONSTANT(SHAPE_CUSTOM);
810
811 BIND_ENUM_CONSTANT(AREA_PARAM_GRAVITY_OVERRIDE_MODE);
812 BIND_ENUM_CONSTANT(AREA_PARAM_GRAVITY);
813 BIND_ENUM_CONSTANT(AREA_PARAM_GRAVITY_VECTOR);
814 BIND_ENUM_CONSTANT(AREA_PARAM_GRAVITY_IS_POINT);
815 BIND_ENUM_CONSTANT(AREA_PARAM_GRAVITY_POINT_UNIT_DISTANCE);
816 BIND_ENUM_CONSTANT(AREA_PARAM_LINEAR_DAMP_OVERRIDE_MODE);
817 BIND_ENUM_CONSTANT(AREA_PARAM_LINEAR_DAMP);
818 BIND_ENUM_CONSTANT(AREA_PARAM_ANGULAR_DAMP_OVERRIDE_MODE);
819 BIND_ENUM_CONSTANT(AREA_PARAM_ANGULAR_DAMP);
820 BIND_ENUM_CONSTANT(AREA_PARAM_PRIORITY);
821
822 BIND_ENUM_CONSTANT(AREA_SPACE_OVERRIDE_DISABLED);
823 BIND_ENUM_CONSTANT(AREA_SPACE_OVERRIDE_COMBINE);
824 BIND_ENUM_CONSTANT(AREA_SPACE_OVERRIDE_COMBINE_REPLACE);
825 BIND_ENUM_CONSTANT(AREA_SPACE_OVERRIDE_REPLACE);
826 BIND_ENUM_CONSTANT(AREA_SPACE_OVERRIDE_REPLACE_COMBINE);
827
828 BIND_ENUM_CONSTANT(BODY_MODE_STATIC);
829 BIND_ENUM_CONSTANT(BODY_MODE_KINEMATIC);
830 BIND_ENUM_CONSTANT(BODY_MODE_RIGID);
831 BIND_ENUM_CONSTANT(BODY_MODE_RIGID_LINEAR);
832
833 BIND_ENUM_CONSTANT(BODY_PARAM_BOUNCE);
834 BIND_ENUM_CONSTANT(BODY_PARAM_FRICTION);
835 BIND_ENUM_CONSTANT(BODY_PARAM_MASS);
836 BIND_ENUM_CONSTANT(BODY_PARAM_INERTIA);
837 BIND_ENUM_CONSTANT(BODY_PARAM_CENTER_OF_MASS);
838 BIND_ENUM_CONSTANT(BODY_PARAM_GRAVITY_SCALE);
839 BIND_ENUM_CONSTANT(BODY_PARAM_LINEAR_DAMP_MODE);
840 BIND_ENUM_CONSTANT(BODY_PARAM_ANGULAR_DAMP_MODE);
841 BIND_ENUM_CONSTANT(BODY_PARAM_LINEAR_DAMP);
842 BIND_ENUM_CONSTANT(BODY_PARAM_ANGULAR_DAMP);
843 BIND_ENUM_CONSTANT(BODY_PARAM_MAX);
844
845 BIND_ENUM_CONSTANT(BODY_DAMP_MODE_COMBINE);
846 BIND_ENUM_CONSTANT(BODY_DAMP_MODE_REPLACE);
847
848 BIND_ENUM_CONSTANT(BODY_STATE_TRANSFORM);
849 BIND_ENUM_CONSTANT(BODY_STATE_LINEAR_VELOCITY);
850 BIND_ENUM_CONSTANT(BODY_STATE_ANGULAR_VELOCITY);
851 BIND_ENUM_CONSTANT(BODY_STATE_SLEEPING);
852 BIND_ENUM_CONSTANT(BODY_STATE_CAN_SLEEP);
853
854 BIND_ENUM_CONSTANT(JOINT_TYPE_PIN);
855 BIND_ENUM_CONSTANT(JOINT_TYPE_GROOVE);
856 BIND_ENUM_CONSTANT(JOINT_TYPE_DAMPED_SPRING);
857 BIND_ENUM_CONSTANT(JOINT_TYPE_MAX);
858
859 BIND_ENUM_CONSTANT(JOINT_PARAM_BIAS);
860 BIND_ENUM_CONSTANT(JOINT_PARAM_MAX_BIAS);
861 BIND_ENUM_CONSTANT(JOINT_PARAM_MAX_FORCE);
862
863 BIND_ENUM_CONSTANT(PIN_JOINT_SOFTNESS);
864
865 BIND_ENUM_CONSTANT(DAMPED_SPRING_REST_LENGTH);
866 BIND_ENUM_CONSTANT(DAMPED_SPRING_STIFFNESS);
867 BIND_ENUM_CONSTANT(DAMPED_SPRING_DAMPING);
868
869 BIND_ENUM_CONSTANT(CCD_MODE_DISABLED);
870 BIND_ENUM_CONSTANT(CCD_MODE_CAST_RAY);
871 BIND_ENUM_CONSTANT(CCD_MODE_CAST_SHAPE);
872
873 BIND_ENUM_CONSTANT(AREA_BODY_ADDED);
874 BIND_ENUM_CONSTANT(AREA_BODY_REMOVED);
875
876 BIND_ENUM_CONSTANT(INFO_ACTIVE_OBJECTS);
877 BIND_ENUM_CONSTANT(INFO_COLLISION_PAIRS);
878 BIND_ENUM_CONSTANT(INFO_ISLAND_COUNT);
879}
880
881PhysicsServer2D::PhysicsServer2D() {
882 singleton = this;
883
884 // World2D physics space
885 GLOBAL_DEF_BASIC("physics/2d/default_gravity", 980.0);
886 GLOBAL_DEF_BASIC("physics/2d/default_gravity_vector", Vector2(0, 1));
887 GLOBAL_DEF(PropertyInfo(Variant::FLOAT, "physics/2d/default_linear_damp", PROPERTY_HINT_RANGE, "-1,100,0.001,or_greater"), 0.1);
888 GLOBAL_DEF(PropertyInfo(Variant::FLOAT, "physics/2d/default_angular_damp", PROPERTY_HINT_RANGE, "-1,100,0.001,or_greater"), 1.0);
889
890 // PhysicsServer2D
891 GLOBAL_DEF("physics/2d/sleep_threshold_linear", 2.0);
892 GLOBAL_DEF("physics/2d/sleep_threshold_angular", Math::deg_to_rad(8.0));
893 GLOBAL_DEF(PropertyInfo(Variant::FLOAT, "physics/2d/time_before_sleep", PROPERTY_HINT_RANGE, "0,5,0.01,or_greater"), 0.5);
894 GLOBAL_DEF(PropertyInfo(Variant::INT, "physics/2d/solver/solver_iterations", PROPERTY_HINT_RANGE, "1,32,1,or_greater"), 16);
895 GLOBAL_DEF(PropertyInfo(Variant::FLOAT, "physics/2d/solver/contact_recycle_radius", PROPERTY_HINT_RANGE, "0,10,0.01,or_greater"), 1.0);
896 GLOBAL_DEF(PropertyInfo(Variant::FLOAT, "physics/2d/solver/contact_max_separation", PROPERTY_HINT_RANGE, "0,10,0.01,or_greater"), 1.5);
897 GLOBAL_DEF(PropertyInfo(Variant::FLOAT, "physics/2d/solver/contact_max_allowed_penetration", PROPERTY_HINT_RANGE, "0.01,10,0.01,or_greater"), 0.3);
898 GLOBAL_DEF(PropertyInfo(Variant::FLOAT, "physics/2d/solver/default_contact_bias", PROPERTY_HINT_RANGE, "0,1,0.01"), 0.8);
899 GLOBAL_DEF(PropertyInfo(Variant::FLOAT, "physics/2d/solver/default_constraint_bias", PROPERTY_HINT_RANGE, "0,1,0.01"), 0.2);
900}
901
902PhysicsServer2D::~PhysicsServer2D() {
903 singleton = nullptr;
904}
905
906PhysicsServer2DManager *PhysicsServer2DManager::singleton = nullptr;
907const String PhysicsServer2DManager::setting_property_name(PNAME("physics/2d/physics_engine"));
908
909void PhysicsServer2DManager::on_servers_changed() {
910 String physics_servers("DEFAULT");
911 for (int i = get_servers_count() - 1; 0 <= i; --i) {
912 physics_servers += "," + get_server_name(i);
913 }
914 ProjectSettings::get_singleton()->set_custom_property_info(PropertyInfo(Variant::STRING, setting_property_name, PROPERTY_HINT_ENUM, physics_servers));
915 ProjectSettings::get_singleton()->set_restart_if_changed(setting_property_name, true);
916}
917
918void PhysicsServer2DManager::_bind_methods() {
919 ClassDB::bind_method(D_METHOD("register_server", "name", "create_callback"), &PhysicsServer2DManager::register_server);
920 ClassDB::bind_method(D_METHOD("set_default_server", "name", "priority"), &PhysicsServer2DManager::set_default_server);
921}
922
923PhysicsServer2DManager *PhysicsServer2DManager::get_singleton() {
924 return singleton;
925}
926
927void PhysicsServer2DManager::register_server(const String &p_name, const Callable &p_create_callback) {
928 //ERR_FAIL_COND(!p_create_callback.is_valid());
929 ERR_FAIL_COND(find_server_id(p_name) != -1);
930 physics_2d_servers.push_back(ClassInfo(p_name, p_create_callback));
931 on_servers_changed();
932}
933
934void PhysicsServer2DManager::set_default_server(const String &p_name, int p_priority) {
935 const int id = find_server_id(p_name);
936 ERR_FAIL_COND(id == -1); // Not found
937 if (default_server_priority < p_priority) {
938 default_server_id = id;
939 default_server_priority = p_priority;
940 }
941}
942
943int PhysicsServer2DManager::find_server_id(const String &p_name) {
944 for (int i = physics_2d_servers.size() - 1; 0 <= i; --i) {
945 if (p_name == physics_2d_servers[i].name) {
946 return i;
947 }
948 }
949 return -1;
950}
951
952int PhysicsServer2DManager::get_servers_count() {
953 return physics_2d_servers.size();
954}
955
956String PhysicsServer2DManager::get_server_name(int p_id) {
957 ERR_FAIL_INDEX_V(p_id, get_servers_count(), "");
958 return physics_2d_servers[p_id].name;
959}
960
961PhysicsServer2D *PhysicsServer2DManager::new_default_server() {
962 ERR_FAIL_COND_V(default_server_id == -1, nullptr);
963 Variant ret;
964 Callable::CallError ce;
965 physics_2d_servers[default_server_id].create_callback.callp(nullptr, 0, ret, ce);
966 ERR_FAIL_COND_V(ce.error != Callable::CallError::CALL_OK, nullptr);
967 return Object::cast_to<PhysicsServer2D>(ret.get_validated_object());
968}
969
970PhysicsServer2D *PhysicsServer2DManager::new_server(const String &p_name) {
971 int id = find_server_id(p_name);
972 if (id == -1) {
973 return nullptr;
974 } else {
975 Variant ret;
976 Callable::CallError ce;
977 physics_2d_servers[id].create_callback.callp(nullptr, 0, ret, ce);
978 ERR_FAIL_COND_V(ce.error != Callable::CallError::CALL_OK, nullptr);
979 return Object::cast_to<PhysicsServer2D>(ret.get_validated_object());
980 }
981}
982
983PhysicsServer2DManager::PhysicsServer2DManager() {
984 singleton = this;
985}
986
987PhysicsServer2DManager::~PhysicsServer2DManager() {
988 singleton = nullptr;
989}
990