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