Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/modules/godot_physics_3d/godot_physics_server_3d.cpp
10277 views
1
/**************************************************************************/
2
/* godot_physics_server_3d.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 "godot_physics_server_3d.h"
32
33
#include "godot_body_direct_state_3d.h"
34
#include "godot_broad_phase_3d_bvh.h"
35
#include "joints/godot_cone_twist_joint_3d.h"
36
#include "joints/godot_generic_6dof_joint_3d.h"
37
#include "joints/godot_hinge_joint_3d.h"
38
#include "joints/godot_pin_joint_3d.h"
39
#include "joints/godot_slider_joint_3d.h"
40
41
#include "core/debugger/engine_debugger.h"
42
#include "core/os/os.h"
43
44
#define FLUSH_QUERY_CHECK(m_object) \
45
ERR_FAIL_COND_MSG(m_object->get_space() && flushing_queries, "Can't change this state while flushing queries. Use call_deferred() or set_deferred() to change monitoring state instead.");
46
47
RID GodotPhysicsServer3D::world_boundary_shape_create() {
48
GodotShape3D *shape = memnew(GodotWorldBoundaryShape3D);
49
RID rid = shape_owner.make_rid(shape);
50
shape->set_self(rid);
51
return rid;
52
}
53
RID GodotPhysicsServer3D::separation_ray_shape_create() {
54
GodotShape3D *shape = memnew(GodotSeparationRayShape3D);
55
RID rid = shape_owner.make_rid(shape);
56
shape->set_self(rid);
57
return rid;
58
}
59
RID GodotPhysicsServer3D::sphere_shape_create() {
60
GodotShape3D *shape = memnew(GodotSphereShape3D);
61
RID rid = shape_owner.make_rid(shape);
62
shape->set_self(rid);
63
return rid;
64
}
65
RID GodotPhysicsServer3D::box_shape_create() {
66
GodotShape3D *shape = memnew(GodotBoxShape3D);
67
RID rid = shape_owner.make_rid(shape);
68
shape->set_self(rid);
69
return rid;
70
}
71
RID GodotPhysicsServer3D::capsule_shape_create() {
72
GodotShape3D *shape = memnew(GodotCapsuleShape3D);
73
RID rid = shape_owner.make_rid(shape);
74
shape->set_self(rid);
75
return rid;
76
}
77
RID GodotPhysicsServer3D::cylinder_shape_create() {
78
GodotShape3D *shape = memnew(GodotCylinderShape3D);
79
RID rid = shape_owner.make_rid(shape);
80
shape->set_self(rid);
81
return rid;
82
}
83
RID GodotPhysicsServer3D::convex_polygon_shape_create() {
84
GodotShape3D *shape = memnew(GodotConvexPolygonShape3D);
85
RID rid = shape_owner.make_rid(shape);
86
shape->set_self(rid);
87
return rid;
88
}
89
RID GodotPhysicsServer3D::concave_polygon_shape_create() {
90
GodotShape3D *shape = memnew(GodotConcavePolygonShape3D);
91
RID rid = shape_owner.make_rid(shape);
92
shape->set_self(rid);
93
return rid;
94
}
95
RID GodotPhysicsServer3D::heightmap_shape_create() {
96
GodotShape3D *shape = memnew(GodotHeightMapShape3D);
97
RID rid = shape_owner.make_rid(shape);
98
shape->set_self(rid);
99
return rid;
100
}
101
RID GodotPhysicsServer3D::custom_shape_create() {
102
ERR_FAIL_V(RID());
103
}
104
105
void GodotPhysicsServer3D::shape_set_data(RID p_shape, const Variant &p_data) {
106
GodotShape3D *shape = shape_owner.get_or_null(p_shape);
107
ERR_FAIL_NULL(shape);
108
shape->set_data(p_data);
109
}
110
111
void GodotPhysicsServer3D::shape_set_custom_solver_bias(RID p_shape, real_t p_bias) {
112
GodotShape3D *shape = shape_owner.get_or_null(p_shape);
113
ERR_FAIL_NULL(shape);
114
shape->set_custom_bias(p_bias);
115
}
116
117
PhysicsServer3D::ShapeType GodotPhysicsServer3D::shape_get_type(RID p_shape) const {
118
const GodotShape3D *shape = shape_owner.get_or_null(p_shape);
119
ERR_FAIL_NULL_V(shape, SHAPE_CUSTOM);
120
return shape->get_type();
121
}
122
123
Variant GodotPhysicsServer3D::shape_get_data(RID p_shape) const {
124
const GodotShape3D *shape = shape_owner.get_or_null(p_shape);
125
ERR_FAIL_NULL_V(shape, Variant());
126
ERR_FAIL_COND_V(!shape->is_configured(), Variant());
127
return shape->get_data();
128
}
129
130
void GodotPhysicsServer3D::shape_set_margin(RID p_shape, real_t p_margin) {
131
}
132
133
real_t GodotPhysicsServer3D::shape_get_margin(RID p_shape) const {
134
return 0.0;
135
}
136
137
real_t GodotPhysicsServer3D::shape_get_custom_solver_bias(RID p_shape) const {
138
const GodotShape3D *shape = shape_owner.get_or_null(p_shape);
139
ERR_FAIL_NULL_V(shape, 0);
140
return shape->get_custom_bias();
141
}
142
143
RID GodotPhysicsServer3D::space_create() {
144
GodotSpace3D *space = memnew(GodotSpace3D);
145
RID id = space_owner.make_rid(space);
146
space->set_self(id);
147
RID area_id = area_create();
148
GodotArea3D *area = area_owner.get_or_null(area_id);
149
ERR_FAIL_NULL_V(area, RID());
150
space->set_default_area(area);
151
area->set_space(space);
152
area->set_priority(-1);
153
RID sgb = body_create();
154
body_set_space(sgb, id);
155
body_set_mode(sgb, BODY_MODE_STATIC);
156
space->set_static_global_body(sgb);
157
158
return id;
159
}
160
161
void GodotPhysicsServer3D::space_set_active(RID p_space, bool p_active) {
162
GodotSpace3D *space = space_owner.get_or_null(p_space);
163
ERR_FAIL_NULL(space);
164
if (p_active) {
165
active_spaces.insert(space);
166
} else {
167
active_spaces.erase(space);
168
}
169
}
170
171
bool GodotPhysicsServer3D::space_is_active(RID p_space) const {
172
GodotSpace3D *space = space_owner.get_or_null(p_space);
173
ERR_FAIL_NULL_V(space, false);
174
175
return active_spaces.has(space);
176
}
177
178
void GodotPhysicsServer3D::space_set_param(RID p_space, SpaceParameter p_param, real_t p_value) {
179
GodotSpace3D *space = space_owner.get_or_null(p_space);
180
ERR_FAIL_NULL(space);
181
182
space->set_param(p_param, p_value);
183
}
184
185
real_t GodotPhysicsServer3D::space_get_param(RID p_space, SpaceParameter p_param) const {
186
const GodotSpace3D *space = space_owner.get_or_null(p_space);
187
ERR_FAIL_NULL_V(space, 0);
188
return space->get_param(p_param);
189
}
190
191
PhysicsDirectSpaceState3D *GodotPhysicsServer3D::space_get_direct_state(RID p_space) {
192
GodotSpace3D *space = space_owner.get_or_null(p_space);
193
ERR_FAIL_NULL_V(space, nullptr);
194
ERR_FAIL_COND_V_MSG((using_threads && !doing_sync) || space->is_locked(), nullptr, "Space state is inaccessible right now, wait for iteration or physics process notification.");
195
196
return space->get_direct_state();
197
}
198
199
void GodotPhysicsServer3D::space_set_debug_contacts(RID p_space, int p_max_contacts) {
200
GodotSpace3D *space = space_owner.get_or_null(p_space);
201
ERR_FAIL_NULL(space);
202
space->set_debug_contacts(p_max_contacts);
203
}
204
205
Vector<Vector3> GodotPhysicsServer3D::space_get_contacts(RID p_space) const {
206
GodotSpace3D *space = space_owner.get_or_null(p_space);
207
ERR_FAIL_NULL_V(space, Vector<Vector3>());
208
return space->get_debug_contacts();
209
}
210
211
int GodotPhysicsServer3D::space_get_contact_count(RID p_space) const {
212
GodotSpace3D *space = space_owner.get_or_null(p_space);
213
ERR_FAIL_NULL_V(space, 0);
214
return space->get_debug_contact_count();
215
}
216
217
RID GodotPhysicsServer3D::area_create() {
218
GodotArea3D *area = memnew(GodotArea3D);
219
RID rid = area_owner.make_rid(area);
220
area->set_self(rid);
221
return rid;
222
}
223
224
void GodotPhysicsServer3D::area_set_space(RID p_area, RID p_space) {
225
GodotArea3D *area = area_owner.get_or_null(p_area);
226
ERR_FAIL_NULL(area);
227
228
GodotSpace3D *space = nullptr;
229
if (p_space.is_valid()) {
230
space = space_owner.get_or_null(p_space);
231
ERR_FAIL_NULL(space);
232
}
233
234
if (area->get_space() == space) {
235
return; //pointless
236
}
237
238
area->clear_constraints();
239
area->set_space(space);
240
}
241
242
RID GodotPhysicsServer3D::area_get_space(RID p_area) const {
243
GodotArea3D *area = area_owner.get_or_null(p_area);
244
ERR_FAIL_NULL_V(area, RID());
245
246
GodotSpace3D *space = area->get_space();
247
if (!space) {
248
return RID();
249
}
250
return space->get_self();
251
}
252
253
void GodotPhysicsServer3D::area_add_shape(RID p_area, RID p_shape, const Transform3D &p_transform, bool p_disabled) {
254
GodotArea3D *area = area_owner.get_or_null(p_area);
255
ERR_FAIL_NULL(area);
256
257
GodotShape3D *shape = shape_owner.get_or_null(p_shape);
258
ERR_FAIL_NULL(shape);
259
260
area->add_shape(shape, p_transform, p_disabled);
261
}
262
263
void GodotPhysicsServer3D::area_set_shape(RID p_area, int p_shape_idx, RID p_shape) {
264
GodotArea3D *area = area_owner.get_or_null(p_area);
265
ERR_FAIL_NULL(area);
266
267
GodotShape3D *shape = shape_owner.get_or_null(p_shape);
268
ERR_FAIL_NULL(shape);
269
ERR_FAIL_COND(!shape->is_configured());
270
271
area->set_shape(p_shape_idx, shape);
272
}
273
274
void GodotPhysicsServer3D::area_set_shape_transform(RID p_area, int p_shape_idx, const Transform3D &p_transform) {
275
GodotArea3D *area = area_owner.get_or_null(p_area);
276
ERR_FAIL_NULL(area);
277
278
area->set_shape_transform(p_shape_idx, p_transform);
279
}
280
281
int GodotPhysicsServer3D::area_get_shape_count(RID p_area) const {
282
GodotArea3D *area = area_owner.get_or_null(p_area);
283
ERR_FAIL_NULL_V(area, -1);
284
285
return area->get_shape_count();
286
}
287
288
RID GodotPhysicsServer3D::area_get_shape(RID p_area, int p_shape_idx) const {
289
GodotArea3D *area = area_owner.get_or_null(p_area);
290
ERR_FAIL_NULL_V(area, RID());
291
292
GodotShape3D *shape = area->get_shape(p_shape_idx);
293
ERR_FAIL_NULL_V(shape, RID());
294
295
return shape->get_self();
296
}
297
298
Transform3D GodotPhysicsServer3D::area_get_shape_transform(RID p_area, int p_shape_idx) const {
299
GodotArea3D *area = area_owner.get_or_null(p_area);
300
ERR_FAIL_NULL_V(area, Transform3D());
301
302
return area->get_shape_transform(p_shape_idx);
303
}
304
305
void GodotPhysicsServer3D::area_remove_shape(RID p_area, int p_shape_idx) {
306
GodotArea3D *area = area_owner.get_or_null(p_area);
307
ERR_FAIL_NULL(area);
308
309
area->remove_shape(p_shape_idx);
310
}
311
312
void GodotPhysicsServer3D::area_clear_shapes(RID p_area) {
313
GodotArea3D *area = area_owner.get_or_null(p_area);
314
ERR_FAIL_NULL(area);
315
316
while (area->get_shape_count()) {
317
area->remove_shape(0);
318
}
319
}
320
321
void GodotPhysicsServer3D::area_set_shape_disabled(RID p_area, int p_shape_idx, bool p_disabled) {
322
GodotArea3D *area = area_owner.get_or_null(p_area);
323
ERR_FAIL_NULL(area);
324
ERR_FAIL_INDEX(p_shape_idx, area->get_shape_count());
325
FLUSH_QUERY_CHECK(area);
326
area->set_shape_disabled(p_shape_idx, p_disabled);
327
}
328
329
void GodotPhysicsServer3D::area_attach_object_instance_id(RID p_area, ObjectID p_id) {
330
if (space_owner.owns(p_area)) {
331
GodotSpace3D *space = space_owner.get_or_null(p_area);
332
p_area = space->get_default_area()->get_self();
333
}
334
GodotArea3D *area = area_owner.get_or_null(p_area);
335
ERR_FAIL_NULL(area);
336
area->set_instance_id(p_id);
337
}
338
339
ObjectID GodotPhysicsServer3D::area_get_object_instance_id(RID p_area) const {
340
if (space_owner.owns(p_area)) {
341
GodotSpace3D *space = space_owner.get_or_null(p_area);
342
p_area = space->get_default_area()->get_self();
343
}
344
GodotArea3D *area = area_owner.get_or_null(p_area);
345
ERR_FAIL_NULL_V(area, ObjectID());
346
return area->get_instance_id();
347
}
348
349
void GodotPhysicsServer3D::area_set_param(RID p_area, AreaParameter p_param, const Variant &p_value) {
350
if (space_owner.owns(p_area)) {
351
GodotSpace3D *space = space_owner.get_or_null(p_area);
352
p_area = space->get_default_area()->get_self();
353
}
354
GodotArea3D *area = area_owner.get_or_null(p_area);
355
ERR_FAIL_NULL(area);
356
area->set_param(p_param, p_value);
357
}
358
359
void GodotPhysicsServer3D::area_set_transform(RID p_area, const Transform3D &p_transform) {
360
GodotArea3D *area = area_owner.get_or_null(p_area);
361
ERR_FAIL_NULL(area);
362
area->set_transform(p_transform);
363
}
364
365
Variant GodotPhysicsServer3D::area_get_param(RID p_area, AreaParameter p_param) const {
366
if (space_owner.owns(p_area)) {
367
GodotSpace3D *space = space_owner.get_or_null(p_area);
368
p_area = space->get_default_area()->get_self();
369
}
370
GodotArea3D *area = area_owner.get_or_null(p_area);
371
ERR_FAIL_NULL_V(area, Variant());
372
373
return area->get_param(p_param);
374
}
375
376
Transform3D GodotPhysicsServer3D::area_get_transform(RID p_area) const {
377
GodotArea3D *area = area_owner.get_or_null(p_area);
378
ERR_FAIL_NULL_V(area, Transform3D());
379
380
return area->get_transform();
381
}
382
383
void GodotPhysicsServer3D::area_set_collision_layer(RID p_area, uint32_t p_layer) {
384
GodotArea3D *area = area_owner.get_or_null(p_area);
385
ERR_FAIL_NULL(area);
386
387
area->set_collision_layer(p_layer);
388
}
389
390
uint32_t GodotPhysicsServer3D::area_get_collision_layer(RID p_area) const {
391
GodotArea3D *area = area_owner.get_or_null(p_area);
392
ERR_FAIL_NULL_V(area, 0);
393
394
return area->get_collision_layer();
395
}
396
397
void GodotPhysicsServer3D::area_set_collision_mask(RID p_area, uint32_t p_mask) {
398
GodotArea3D *area = area_owner.get_or_null(p_area);
399
ERR_FAIL_NULL(area);
400
401
area->set_collision_mask(p_mask);
402
}
403
404
uint32_t GodotPhysicsServer3D::area_get_collision_mask(RID p_area) const {
405
GodotArea3D *area = area_owner.get_or_null(p_area);
406
ERR_FAIL_NULL_V(area, 0);
407
408
return area->get_collision_mask();
409
}
410
411
void GodotPhysicsServer3D::area_set_monitorable(RID p_area, bool p_monitorable) {
412
GodotArea3D *area = area_owner.get_or_null(p_area);
413
ERR_FAIL_NULL(area);
414
FLUSH_QUERY_CHECK(area);
415
416
area->set_monitorable(p_monitorable);
417
}
418
419
void GodotPhysicsServer3D::area_set_monitor_callback(RID p_area, const Callable &p_callback) {
420
GodotArea3D *area = area_owner.get_or_null(p_area);
421
ERR_FAIL_NULL(area);
422
423
area->set_monitor_callback(p_callback.is_valid() ? p_callback : Callable());
424
}
425
426
void GodotPhysicsServer3D::area_set_ray_pickable(RID p_area, bool p_enable) {
427
GodotArea3D *area = area_owner.get_or_null(p_area);
428
ERR_FAIL_NULL(area);
429
430
area->set_ray_pickable(p_enable);
431
}
432
433
void GodotPhysicsServer3D::area_set_area_monitor_callback(RID p_area, const Callable &p_callback) {
434
GodotArea3D *area = area_owner.get_or_null(p_area);
435
ERR_FAIL_NULL(area);
436
437
area->set_area_monitor_callback(p_callback.is_valid() ? p_callback : Callable());
438
}
439
440
/* BODY API */
441
442
RID GodotPhysicsServer3D::body_create() {
443
GodotBody3D *body = memnew(GodotBody3D);
444
RID rid = body_owner.make_rid(body);
445
body->set_self(rid);
446
return rid;
447
}
448
449
void GodotPhysicsServer3D::body_set_space(RID p_body, RID p_space) {
450
GodotBody3D *body = body_owner.get_or_null(p_body);
451
ERR_FAIL_NULL(body);
452
453
GodotSpace3D *space = nullptr;
454
if (p_space.is_valid()) {
455
space = space_owner.get_or_null(p_space);
456
ERR_FAIL_NULL(space);
457
}
458
459
if (body->get_space() == space) {
460
return; //pointless
461
}
462
463
body->clear_constraint_map();
464
body->set_space(space);
465
}
466
467
RID GodotPhysicsServer3D::body_get_space(RID p_body) const {
468
GodotBody3D *body = body_owner.get_or_null(p_body);
469
ERR_FAIL_NULL_V(body, RID());
470
471
GodotSpace3D *space = body->get_space();
472
if (!space) {
473
return RID();
474
}
475
return space->get_self();
476
}
477
478
void GodotPhysicsServer3D::body_set_mode(RID p_body, BodyMode p_mode) {
479
GodotBody3D *body = body_owner.get_or_null(p_body);
480
ERR_FAIL_NULL(body);
481
482
body->set_mode(p_mode);
483
}
484
485
PhysicsServer3D::BodyMode GodotPhysicsServer3D::body_get_mode(RID p_body) const {
486
GodotBody3D *body = body_owner.get_or_null(p_body);
487
ERR_FAIL_NULL_V(body, BODY_MODE_STATIC);
488
489
return body->get_mode();
490
}
491
492
void GodotPhysicsServer3D::body_add_shape(RID p_body, RID p_shape, const Transform3D &p_transform, bool p_disabled) {
493
GodotBody3D *body = body_owner.get_or_null(p_body);
494
ERR_FAIL_NULL(body);
495
496
GodotShape3D *shape = shape_owner.get_or_null(p_shape);
497
ERR_FAIL_NULL(shape);
498
499
body->add_shape(shape, p_transform, p_disabled);
500
}
501
502
void GodotPhysicsServer3D::body_set_shape(RID p_body, int p_shape_idx, RID p_shape) {
503
GodotBody3D *body = body_owner.get_or_null(p_body);
504
ERR_FAIL_NULL(body);
505
506
GodotShape3D *shape = shape_owner.get_or_null(p_shape);
507
ERR_FAIL_NULL(shape);
508
ERR_FAIL_COND(!shape->is_configured());
509
510
body->set_shape(p_shape_idx, shape);
511
}
512
void GodotPhysicsServer3D::body_set_shape_transform(RID p_body, int p_shape_idx, const Transform3D &p_transform) {
513
GodotBody3D *body = body_owner.get_or_null(p_body);
514
ERR_FAIL_NULL(body);
515
516
body->set_shape_transform(p_shape_idx, p_transform);
517
}
518
519
int GodotPhysicsServer3D::body_get_shape_count(RID p_body) const {
520
GodotBody3D *body = body_owner.get_or_null(p_body);
521
ERR_FAIL_NULL_V(body, -1);
522
523
return body->get_shape_count();
524
}
525
526
RID GodotPhysicsServer3D::body_get_shape(RID p_body, int p_shape_idx) const {
527
GodotBody3D *body = body_owner.get_or_null(p_body);
528
ERR_FAIL_NULL_V(body, RID());
529
530
GodotShape3D *shape = body->get_shape(p_shape_idx);
531
ERR_FAIL_NULL_V(shape, RID());
532
533
return shape->get_self();
534
}
535
536
void GodotPhysicsServer3D::body_set_shape_disabled(RID p_body, int p_shape_idx, bool p_disabled) {
537
GodotBody3D *body = body_owner.get_or_null(p_body);
538
ERR_FAIL_NULL(body);
539
ERR_FAIL_INDEX(p_shape_idx, body->get_shape_count());
540
FLUSH_QUERY_CHECK(body);
541
542
body->set_shape_disabled(p_shape_idx, p_disabled);
543
}
544
545
Transform3D GodotPhysicsServer3D::body_get_shape_transform(RID p_body, int p_shape_idx) const {
546
GodotBody3D *body = body_owner.get_or_null(p_body);
547
ERR_FAIL_NULL_V(body, Transform3D());
548
549
return body->get_shape_transform(p_shape_idx);
550
}
551
552
void GodotPhysicsServer3D::body_remove_shape(RID p_body, int p_shape_idx) {
553
GodotBody3D *body = body_owner.get_or_null(p_body);
554
ERR_FAIL_NULL(body);
555
556
body->remove_shape(p_shape_idx);
557
}
558
559
void GodotPhysicsServer3D::body_clear_shapes(RID p_body) {
560
GodotBody3D *body = body_owner.get_or_null(p_body);
561
ERR_FAIL_NULL(body);
562
563
while (body->get_shape_count()) {
564
body->remove_shape(0);
565
}
566
}
567
568
void GodotPhysicsServer3D::body_set_enable_continuous_collision_detection(RID p_body, bool p_enable) {
569
GodotBody3D *body = body_owner.get_or_null(p_body);
570
ERR_FAIL_NULL(body);
571
572
body->set_continuous_collision_detection(p_enable);
573
}
574
575
bool GodotPhysicsServer3D::body_is_continuous_collision_detection_enabled(RID p_body) const {
576
GodotBody3D *body = body_owner.get_or_null(p_body);
577
ERR_FAIL_NULL_V(body, false);
578
579
return body->is_continuous_collision_detection_enabled();
580
}
581
582
void GodotPhysicsServer3D::body_set_collision_layer(RID p_body, uint32_t p_layer) {
583
GodotBody3D *body = body_owner.get_or_null(p_body);
584
ERR_FAIL_NULL(body);
585
586
body->set_collision_layer(p_layer);
587
}
588
589
uint32_t GodotPhysicsServer3D::body_get_collision_layer(RID p_body) const {
590
const GodotBody3D *body = body_owner.get_or_null(p_body);
591
ERR_FAIL_NULL_V(body, 0);
592
593
return body->get_collision_layer();
594
}
595
596
void GodotPhysicsServer3D::body_set_collision_mask(RID p_body, uint32_t p_mask) {
597
GodotBody3D *body = body_owner.get_or_null(p_body);
598
ERR_FAIL_NULL(body);
599
600
body->set_collision_mask(p_mask);
601
}
602
603
uint32_t GodotPhysicsServer3D::body_get_collision_mask(RID p_body) const {
604
const GodotBody3D *body = body_owner.get_or_null(p_body);
605
ERR_FAIL_NULL_V(body, 0);
606
607
return body->get_collision_mask();
608
}
609
610
void GodotPhysicsServer3D::body_set_collision_priority(RID p_body, real_t p_priority) {
611
GodotBody3D *body = body_owner.get_or_null(p_body);
612
ERR_FAIL_NULL(body);
613
614
body->set_collision_priority(p_priority);
615
}
616
617
real_t GodotPhysicsServer3D::body_get_collision_priority(RID p_body) const {
618
const GodotBody3D *body = body_owner.get_or_null(p_body);
619
ERR_FAIL_NULL_V(body, 0);
620
621
return body->get_collision_priority();
622
}
623
624
void GodotPhysicsServer3D::body_attach_object_instance_id(RID p_body, ObjectID p_id) {
625
GodotBody3D *body = body_owner.get_or_null(p_body);
626
if (body) {
627
body->set_instance_id(p_id);
628
return;
629
}
630
631
GodotSoftBody3D *soft_body = soft_body_owner.get_or_null(p_body);
632
if (soft_body) {
633
soft_body->set_instance_id(p_id);
634
return;
635
}
636
637
ERR_FAIL_MSG("Invalid ID.");
638
}
639
640
ObjectID GodotPhysicsServer3D::body_get_object_instance_id(RID p_body) const {
641
GodotBody3D *body = body_owner.get_or_null(p_body);
642
ERR_FAIL_NULL_V(body, ObjectID());
643
644
return body->get_instance_id();
645
}
646
647
void GodotPhysicsServer3D::body_set_user_flags(RID p_body, uint32_t p_flags) {
648
GodotBody3D *body = body_owner.get_or_null(p_body);
649
ERR_FAIL_NULL(body);
650
}
651
652
uint32_t GodotPhysicsServer3D::body_get_user_flags(RID p_body) const {
653
GodotBody3D *body = body_owner.get_or_null(p_body);
654
ERR_FAIL_NULL_V(body, 0);
655
656
return 0;
657
}
658
659
void GodotPhysicsServer3D::body_set_param(RID p_body, BodyParameter p_param, const Variant &p_value) {
660
GodotBody3D *body = body_owner.get_or_null(p_body);
661
ERR_FAIL_NULL(body);
662
663
body->set_param(p_param, p_value);
664
}
665
666
Variant GodotPhysicsServer3D::body_get_param(RID p_body, BodyParameter p_param) const {
667
GodotBody3D *body = body_owner.get_or_null(p_body);
668
ERR_FAIL_NULL_V(body, 0);
669
670
return body->get_param(p_param);
671
}
672
673
void GodotPhysicsServer3D::body_reset_mass_properties(RID p_body) {
674
GodotBody3D *body = body_owner.get_or_null(p_body);
675
ERR_FAIL_NULL(body);
676
677
return body->reset_mass_properties();
678
}
679
680
void GodotPhysicsServer3D::body_set_state(RID p_body, BodyState p_state, const Variant &p_variant) {
681
GodotBody3D *body = body_owner.get_or_null(p_body);
682
ERR_FAIL_NULL(body);
683
684
body->set_state(p_state, p_variant);
685
}
686
687
Variant GodotPhysicsServer3D::body_get_state(RID p_body, BodyState p_state) const {
688
GodotBody3D *body = body_owner.get_or_null(p_body);
689
ERR_FAIL_NULL_V(body, Variant());
690
691
return body->get_state(p_state);
692
}
693
694
void GodotPhysicsServer3D::body_apply_central_impulse(RID p_body, const Vector3 &p_impulse) {
695
GodotBody3D *body = body_owner.get_or_null(p_body);
696
ERR_FAIL_NULL(body);
697
698
_update_shapes();
699
700
body->apply_central_impulse(p_impulse);
701
body->wakeup();
702
}
703
704
void GodotPhysicsServer3D::body_apply_impulse(RID p_body, const Vector3 &p_impulse, const Vector3 &p_position) {
705
GodotBody3D *body = body_owner.get_or_null(p_body);
706
ERR_FAIL_NULL(body);
707
708
_update_shapes();
709
710
body->apply_impulse(p_impulse, p_position);
711
body->wakeup();
712
}
713
714
void GodotPhysicsServer3D::body_apply_torque_impulse(RID p_body, const Vector3 &p_impulse) {
715
GodotBody3D *body = body_owner.get_or_null(p_body);
716
ERR_FAIL_NULL(body);
717
718
_update_shapes();
719
720
body->apply_torque_impulse(p_impulse);
721
body->wakeup();
722
}
723
724
void GodotPhysicsServer3D::body_apply_central_force(RID p_body, const Vector3 &p_force) {
725
GodotBody3D *body = body_owner.get_or_null(p_body);
726
ERR_FAIL_NULL(body);
727
728
body->apply_central_force(p_force);
729
body->wakeup();
730
}
731
732
void GodotPhysicsServer3D::body_apply_force(RID p_body, const Vector3 &p_force, const Vector3 &p_position) {
733
GodotBody3D *body = body_owner.get_or_null(p_body);
734
ERR_FAIL_NULL(body);
735
736
body->apply_force(p_force, p_position);
737
body->wakeup();
738
}
739
740
void GodotPhysicsServer3D::soft_body_apply_point_impulse(RID p_body, int p_point_index, const Vector3 &p_impulse) {
741
GodotSoftBody3D *soft_body = soft_body_owner.get_or_null(p_body);
742
ERR_FAIL_NULL(soft_body);
743
744
soft_body->apply_node_impulse(p_point_index, p_impulse);
745
}
746
747
void GodotPhysicsServer3D::soft_body_apply_point_force(RID p_body, int p_point_index, const Vector3 &p_force) {
748
GodotSoftBody3D *soft_body = soft_body_owner.get_or_null(p_body);
749
ERR_FAIL_NULL(soft_body);
750
751
soft_body->apply_node_force(p_point_index, p_force);
752
}
753
754
void GodotPhysicsServer3D::soft_body_apply_central_impulse(RID p_body, const Vector3 &p_impulse) {
755
GodotSoftBody3D *soft_body = soft_body_owner.get_or_null(p_body);
756
ERR_FAIL_NULL(soft_body);
757
758
soft_body->apply_central_impulse(p_impulse);
759
}
760
761
void GodotPhysicsServer3D::soft_body_apply_central_force(RID p_body, const Vector3 &p_force) {
762
GodotSoftBody3D *soft_body = soft_body_owner.get_or_null(p_body);
763
ERR_FAIL_NULL(soft_body);
764
765
soft_body->apply_central_force(p_force);
766
}
767
768
void GodotPhysicsServer3D::body_apply_torque(RID p_body, const Vector3 &p_torque) {
769
GodotBody3D *body = body_owner.get_or_null(p_body);
770
ERR_FAIL_NULL(body);
771
772
body->apply_torque(p_torque);
773
body->wakeup();
774
}
775
776
void GodotPhysicsServer3D::body_add_constant_central_force(RID p_body, const Vector3 &p_force) {
777
GodotBody3D *body = body_owner.get_or_null(p_body);
778
ERR_FAIL_NULL(body);
779
780
body->add_constant_central_force(p_force);
781
body->wakeup();
782
}
783
784
void GodotPhysicsServer3D::body_add_constant_force(RID p_body, const Vector3 &p_force, const Vector3 &p_position) {
785
GodotBody3D *body = body_owner.get_or_null(p_body);
786
ERR_FAIL_NULL(body);
787
788
body->add_constant_force(p_force, p_position);
789
body->wakeup();
790
}
791
792
void GodotPhysicsServer3D::body_add_constant_torque(RID p_body, const Vector3 &p_torque) {
793
GodotBody3D *body = body_owner.get_or_null(p_body);
794
ERR_FAIL_NULL(body);
795
796
body->add_constant_torque(p_torque);
797
body->wakeup();
798
}
799
800
void GodotPhysicsServer3D::body_set_constant_force(RID p_body, const Vector3 &p_force) {
801
GodotBody3D *body = body_owner.get_or_null(p_body);
802
ERR_FAIL_NULL(body);
803
804
body->set_constant_force(p_force);
805
if (!p_force.is_zero_approx()) {
806
body->wakeup();
807
}
808
}
809
810
Vector3 GodotPhysicsServer3D::body_get_constant_force(RID p_body) const {
811
GodotBody3D *body = body_owner.get_or_null(p_body);
812
ERR_FAIL_NULL_V(body, Vector3());
813
return body->get_constant_force();
814
}
815
816
void GodotPhysicsServer3D::body_set_constant_torque(RID p_body, const Vector3 &p_torque) {
817
GodotBody3D *body = body_owner.get_or_null(p_body);
818
ERR_FAIL_NULL(body);
819
820
body->set_constant_torque(p_torque);
821
if (!p_torque.is_zero_approx()) {
822
body->wakeup();
823
}
824
}
825
826
Vector3 GodotPhysicsServer3D::body_get_constant_torque(RID p_body) const {
827
GodotBody3D *body = body_owner.get_or_null(p_body);
828
ERR_FAIL_NULL_V(body, Vector3());
829
830
return body->get_constant_torque();
831
}
832
833
void GodotPhysicsServer3D::body_set_axis_velocity(RID p_body, const Vector3 &p_axis_velocity) {
834
GodotBody3D *body = body_owner.get_or_null(p_body);
835
ERR_FAIL_NULL(body);
836
837
_update_shapes();
838
839
Vector3 v = body->get_linear_velocity();
840
Vector3 axis = p_axis_velocity.normalized();
841
v -= axis * axis.dot(v);
842
v += p_axis_velocity;
843
body->set_linear_velocity(v);
844
body->wakeup();
845
}
846
847
void GodotPhysicsServer3D::body_set_axis_lock(RID p_body, BodyAxis p_axis, bool p_lock) {
848
GodotBody3D *body = body_owner.get_or_null(p_body);
849
ERR_FAIL_NULL(body);
850
851
body->set_axis_lock(p_axis, p_lock);
852
body->wakeup();
853
}
854
855
bool GodotPhysicsServer3D::body_is_axis_locked(RID p_body, BodyAxis p_axis) const {
856
const GodotBody3D *body = body_owner.get_or_null(p_body);
857
ERR_FAIL_NULL_V(body, false);
858
return body->is_axis_locked(p_axis);
859
}
860
861
void GodotPhysicsServer3D::body_add_collision_exception(RID p_body, RID p_body_b) {
862
GodotBody3D *body = body_owner.get_or_null(p_body);
863
ERR_FAIL_NULL(body);
864
865
body->add_exception(p_body_b);
866
body->wakeup();
867
}
868
869
void GodotPhysicsServer3D::body_remove_collision_exception(RID p_body, RID p_body_b) {
870
GodotBody3D *body = body_owner.get_or_null(p_body);
871
ERR_FAIL_NULL(body);
872
873
body->remove_exception(p_body_b);
874
body->wakeup();
875
}
876
877
void GodotPhysicsServer3D::body_get_collision_exceptions(RID p_body, List<RID> *p_exceptions) {
878
GodotBody3D *body = body_owner.get_or_null(p_body);
879
ERR_FAIL_NULL(body);
880
881
for (int i = 0; i < body->get_exceptions().size(); i++) {
882
p_exceptions->push_back(body->get_exceptions()[i]);
883
}
884
}
885
886
void GodotPhysicsServer3D::body_set_contacts_reported_depth_threshold(RID p_body, real_t p_threshold) {
887
GodotBody3D *body = body_owner.get_or_null(p_body);
888
ERR_FAIL_NULL(body);
889
}
890
891
real_t GodotPhysicsServer3D::body_get_contacts_reported_depth_threshold(RID p_body) const {
892
GodotBody3D *body = body_owner.get_or_null(p_body);
893
ERR_FAIL_NULL_V(body, 0);
894
return 0;
895
}
896
897
void GodotPhysicsServer3D::body_set_omit_force_integration(RID p_body, bool p_omit) {
898
GodotBody3D *body = body_owner.get_or_null(p_body);
899
ERR_FAIL_NULL(body);
900
901
body->set_omit_force_integration(p_omit);
902
}
903
904
bool GodotPhysicsServer3D::body_is_omitting_force_integration(RID p_body) const {
905
GodotBody3D *body = body_owner.get_or_null(p_body);
906
ERR_FAIL_NULL_V(body, false);
907
return body->get_omit_force_integration();
908
}
909
910
void GodotPhysicsServer3D::body_set_max_contacts_reported(RID p_body, int p_contacts) {
911
GodotBody3D *body = body_owner.get_or_null(p_body);
912
ERR_FAIL_NULL(body);
913
body->set_max_contacts_reported(p_contacts);
914
}
915
916
int GodotPhysicsServer3D::body_get_max_contacts_reported(RID p_body) const {
917
GodotBody3D *body = body_owner.get_or_null(p_body);
918
ERR_FAIL_NULL_V(body, -1);
919
return body->get_max_contacts_reported();
920
}
921
922
void GodotPhysicsServer3D::body_set_state_sync_callback(RID p_body, const Callable &p_callable) {
923
GodotBody3D *body = body_owner.get_or_null(p_body);
924
ERR_FAIL_NULL(body);
925
body->set_state_sync_callback(p_callable);
926
}
927
928
void GodotPhysicsServer3D::body_set_force_integration_callback(RID p_body, const Callable &p_callable, const Variant &p_udata) {
929
GodotBody3D *body = body_owner.get_or_null(p_body);
930
ERR_FAIL_NULL(body);
931
body->set_force_integration_callback(p_callable, p_udata);
932
}
933
934
void GodotPhysicsServer3D::body_set_ray_pickable(RID p_body, bool p_enable) {
935
GodotBody3D *body = body_owner.get_or_null(p_body);
936
ERR_FAIL_NULL(body);
937
body->set_ray_pickable(p_enable);
938
}
939
940
bool GodotPhysicsServer3D::body_test_motion(RID p_body, const MotionParameters &p_parameters, MotionResult *r_result) {
941
GodotBody3D *body = body_owner.get_or_null(p_body);
942
ERR_FAIL_NULL_V(body, false);
943
ERR_FAIL_NULL_V(body->get_space(), false);
944
ERR_FAIL_COND_V(body->get_space()->is_locked(), false);
945
946
_update_shapes();
947
948
return body->get_space()->test_body_motion(body, p_parameters, r_result);
949
}
950
951
PhysicsDirectBodyState3D *GodotPhysicsServer3D::body_get_direct_state(RID p_body) {
952
ERR_FAIL_COND_V_MSG((using_threads && !doing_sync), nullptr, "Body state is inaccessible right now, wait for iteration or physics process notification.");
953
954
if (!body_owner.owns(p_body)) {
955
return nullptr;
956
}
957
958
GodotBody3D *body = body_owner.get_or_null(p_body);
959
ERR_FAIL_NULL_V(body, nullptr);
960
961
if (!body->get_space()) {
962
return nullptr;
963
}
964
965
ERR_FAIL_COND_V_MSG(body->get_space()->is_locked(), nullptr, "Body state is inaccessible right now, wait for iteration or physics process notification.");
966
967
return body->get_direct_state();
968
}
969
970
/* SOFT BODY */
971
972
RID GodotPhysicsServer3D::soft_body_create() {
973
GodotSoftBody3D *soft_body = memnew(GodotSoftBody3D);
974
RID rid = soft_body_owner.make_rid(soft_body);
975
soft_body->set_self(rid);
976
return rid;
977
}
978
979
void GodotPhysicsServer3D::soft_body_update_rendering_server(RID p_body, PhysicsServer3DRenderingServerHandler *p_rendering_server_handler) {
980
GodotSoftBody3D *soft_body = soft_body_owner.get_or_null(p_body);
981
ERR_FAIL_NULL(soft_body);
982
983
soft_body->update_rendering_server(p_rendering_server_handler);
984
}
985
986
void GodotPhysicsServer3D::soft_body_set_space(RID p_body, RID p_space) {
987
GodotSoftBody3D *soft_body = soft_body_owner.get_or_null(p_body);
988
ERR_FAIL_NULL(soft_body);
989
990
GodotSpace3D *space = nullptr;
991
if (p_space.is_valid()) {
992
space = space_owner.get_or_null(p_space);
993
ERR_FAIL_NULL(space);
994
}
995
996
if (soft_body->get_space() == space) {
997
return;
998
}
999
1000
soft_body->set_space(space);
1001
}
1002
1003
RID GodotPhysicsServer3D::soft_body_get_space(RID p_body) const {
1004
GodotSoftBody3D *soft_body = soft_body_owner.get_or_null(p_body);
1005
ERR_FAIL_NULL_V(soft_body, RID());
1006
1007
GodotSpace3D *space = soft_body->get_space();
1008
if (!space) {
1009
return RID();
1010
}
1011
return space->get_self();
1012
}
1013
1014
void GodotPhysicsServer3D::soft_body_set_collision_layer(RID p_body, uint32_t p_layer) {
1015
GodotSoftBody3D *soft_body = soft_body_owner.get_or_null(p_body);
1016
ERR_FAIL_NULL(soft_body);
1017
1018
soft_body->set_collision_layer(p_layer);
1019
}
1020
1021
uint32_t GodotPhysicsServer3D::soft_body_get_collision_layer(RID p_body) const {
1022
GodotSoftBody3D *soft_body = soft_body_owner.get_or_null(p_body);
1023
ERR_FAIL_NULL_V(soft_body, 0);
1024
1025
return soft_body->get_collision_layer();
1026
}
1027
1028
void GodotPhysicsServer3D::soft_body_set_collision_mask(RID p_body, uint32_t p_mask) {
1029
GodotSoftBody3D *soft_body = soft_body_owner.get_or_null(p_body);
1030
ERR_FAIL_NULL(soft_body);
1031
1032
soft_body->set_collision_mask(p_mask);
1033
}
1034
1035
uint32_t GodotPhysicsServer3D::soft_body_get_collision_mask(RID p_body) const {
1036
GodotSoftBody3D *soft_body = soft_body_owner.get_or_null(p_body);
1037
ERR_FAIL_NULL_V(soft_body, 0);
1038
1039
return soft_body->get_collision_mask();
1040
}
1041
1042
void GodotPhysicsServer3D::soft_body_add_collision_exception(RID p_body, RID p_body_b) {
1043
GodotSoftBody3D *soft_body = soft_body_owner.get_or_null(p_body);
1044
ERR_FAIL_NULL(soft_body);
1045
1046
soft_body->add_exception(p_body_b);
1047
}
1048
1049
void GodotPhysicsServer3D::soft_body_remove_collision_exception(RID p_body, RID p_body_b) {
1050
GodotSoftBody3D *soft_body = soft_body_owner.get_or_null(p_body);
1051
ERR_FAIL_NULL(soft_body);
1052
1053
soft_body->remove_exception(p_body_b);
1054
}
1055
1056
void GodotPhysicsServer3D::soft_body_get_collision_exceptions(RID p_body, List<RID> *p_exceptions) {
1057
GodotSoftBody3D *soft_body = soft_body_owner.get_or_null(p_body);
1058
ERR_FAIL_NULL(soft_body);
1059
1060
for (int i = 0; i < soft_body->get_exceptions().size(); i++) {
1061
p_exceptions->push_back(soft_body->get_exceptions()[i]);
1062
}
1063
}
1064
1065
void GodotPhysicsServer3D::soft_body_set_state(RID p_body, BodyState p_state, const Variant &p_variant) {
1066
GodotSoftBody3D *soft_body = soft_body_owner.get_or_null(p_body);
1067
ERR_FAIL_NULL(soft_body);
1068
1069
soft_body->set_state(p_state, p_variant);
1070
}
1071
1072
Variant GodotPhysicsServer3D::soft_body_get_state(RID p_body, BodyState p_state) const {
1073
GodotSoftBody3D *soft_body = soft_body_owner.get_or_null(p_body);
1074
ERR_FAIL_NULL_V(soft_body, Variant());
1075
1076
return soft_body->get_state(p_state);
1077
}
1078
1079
void GodotPhysicsServer3D::soft_body_set_transform(RID p_body, const Transform3D &p_transform) {
1080
GodotSoftBody3D *soft_body = soft_body_owner.get_or_null(p_body);
1081
ERR_FAIL_NULL(soft_body);
1082
1083
soft_body->set_state(BODY_STATE_TRANSFORM, p_transform);
1084
}
1085
1086
void GodotPhysicsServer3D::soft_body_set_ray_pickable(RID p_body, bool p_enable) {
1087
GodotSoftBody3D *soft_body = soft_body_owner.get_or_null(p_body);
1088
ERR_FAIL_NULL(soft_body);
1089
1090
soft_body->set_ray_pickable(p_enable);
1091
}
1092
1093
void GodotPhysicsServer3D::soft_body_set_simulation_precision(RID p_body, int p_simulation_precision) {
1094
GodotSoftBody3D *soft_body = soft_body_owner.get_or_null(p_body);
1095
ERR_FAIL_NULL(soft_body);
1096
1097
soft_body->set_iteration_count(p_simulation_precision);
1098
}
1099
1100
int GodotPhysicsServer3D::soft_body_get_simulation_precision(RID p_body) const {
1101
GodotSoftBody3D *soft_body = soft_body_owner.get_or_null(p_body);
1102
ERR_FAIL_NULL_V(soft_body, 0.f);
1103
1104
return soft_body->get_iteration_count();
1105
}
1106
1107
void GodotPhysicsServer3D::soft_body_set_total_mass(RID p_body, real_t p_total_mass) {
1108
GodotSoftBody3D *soft_body = soft_body_owner.get_or_null(p_body);
1109
ERR_FAIL_NULL(soft_body);
1110
1111
soft_body->set_total_mass(p_total_mass);
1112
}
1113
1114
real_t GodotPhysicsServer3D::soft_body_get_total_mass(RID p_body) const {
1115
GodotSoftBody3D *soft_body = soft_body_owner.get_or_null(p_body);
1116
ERR_FAIL_NULL_V(soft_body, 0.f);
1117
1118
return soft_body->get_total_mass();
1119
}
1120
1121
void GodotPhysicsServer3D::soft_body_set_linear_stiffness(RID p_body, real_t p_stiffness) {
1122
GodotSoftBody3D *soft_body = soft_body_owner.get_or_null(p_body);
1123
ERR_FAIL_NULL(soft_body);
1124
1125
soft_body->set_linear_stiffness(p_stiffness);
1126
}
1127
1128
real_t GodotPhysicsServer3D::soft_body_get_linear_stiffness(RID p_body) const {
1129
GodotSoftBody3D *soft_body = soft_body_owner.get_or_null(p_body);
1130
ERR_FAIL_NULL_V(soft_body, 0.f);
1131
1132
return soft_body->get_linear_stiffness();
1133
}
1134
1135
void GodotPhysicsServer3D::soft_body_set_shrinking_factor(RID p_body, real_t p_shrinking_factor) {
1136
GodotSoftBody3D *soft_body = soft_body_owner.get_or_null(p_body);
1137
ERR_FAIL_NULL(soft_body);
1138
1139
soft_body->set_shrinking_factor(p_shrinking_factor);
1140
}
1141
1142
real_t GodotPhysicsServer3D::soft_body_get_shrinking_factor(RID p_body) const {
1143
GodotSoftBody3D *soft_body = soft_body_owner.get_or_null(p_body);
1144
ERR_FAIL_NULL_V(soft_body, 0.f);
1145
1146
return soft_body->get_shrinking_factor();
1147
}
1148
1149
void GodotPhysicsServer3D::soft_body_set_pressure_coefficient(RID p_body, real_t p_pressure_coefficient) {
1150
GodotSoftBody3D *soft_body = soft_body_owner.get_or_null(p_body);
1151
ERR_FAIL_NULL(soft_body);
1152
1153
soft_body->set_pressure_coefficient(p_pressure_coefficient);
1154
}
1155
1156
real_t GodotPhysicsServer3D::soft_body_get_pressure_coefficient(RID p_body) const {
1157
GodotSoftBody3D *soft_body = soft_body_owner.get_or_null(p_body);
1158
ERR_FAIL_NULL_V(soft_body, 0.f);
1159
1160
return soft_body->get_pressure_coefficient();
1161
}
1162
1163
void GodotPhysicsServer3D::soft_body_set_damping_coefficient(RID p_body, real_t p_damping_coefficient) {
1164
GodotSoftBody3D *soft_body = soft_body_owner.get_or_null(p_body);
1165
ERR_FAIL_NULL(soft_body);
1166
1167
soft_body->set_damping_coefficient(p_damping_coefficient);
1168
}
1169
1170
real_t GodotPhysicsServer3D::soft_body_get_damping_coefficient(RID p_body) const {
1171
GodotSoftBody3D *soft_body = soft_body_owner.get_or_null(p_body);
1172
ERR_FAIL_NULL_V(soft_body, 0.f);
1173
1174
return soft_body->get_damping_coefficient();
1175
}
1176
1177
void GodotPhysicsServer3D::soft_body_set_drag_coefficient(RID p_body, real_t p_drag_coefficient) {
1178
GodotSoftBody3D *soft_body = soft_body_owner.get_or_null(p_body);
1179
ERR_FAIL_NULL(soft_body);
1180
1181
soft_body->set_drag_coefficient(p_drag_coefficient);
1182
}
1183
1184
real_t GodotPhysicsServer3D::soft_body_get_drag_coefficient(RID p_body) const {
1185
GodotSoftBody3D *soft_body = soft_body_owner.get_or_null(p_body);
1186
ERR_FAIL_NULL_V(soft_body, 0.f);
1187
1188
return soft_body->get_drag_coefficient();
1189
}
1190
1191
void GodotPhysicsServer3D::soft_body_set_mesh(RID p_body, RID p_mesh) {
1192
GodotSoftBody3D *soft_body = soft_body_owner.get_or_null(p_body);
1193
ERR_FAIL_NULL(soft_body);
1194
1195
soft_body->set_mesh(p_mesh);
1196
}
1197
1198
AABB GodotPhysicsServer3D::soft_body_get_bounds(RID p_body) const {
1199
GodotSoftBody3D *soft_body = soft_body_owner.get_or_null(p_body);
1200
ERR_FAIL_NULL_V(soft_body, AABB());
1201
1202
return soft_body->get_bounds();
1203
}
1204
1205
void GodotPhysicsServer3D::soft_body_move_point(RID p_body, int p_point_index, const Vector3 &p_global_position) {
1206
GodotSoftBody3D *soft_body = soft_body_owner.get_or_null(p_body);
1207
ERR_FAIL_NULL(soft_body);
1208
1209
soft_body->set_vertex_position(p_point_index, p_global_position);
1210
}
1211
1212
Vector3 GodotPhysicsServer3D::soft_body_get_point_global_position(RID p_body, int p_point_index) const {
1213
GodotSoftBody3D *soft_body = soft_body_owner.get_or_null(p_body);
1214
ERR_FAIL_NULL_V(soft_body, Vector3());
1215
1216
return soft_body->get_vertex_position(p_point_index);
1217
}
1218
1219
void GodotPhysicsServer3D::soft_body_remove_all_pinned_points(RID p_body) {
1220
GodotSoftBody3D *soft_body = soft_body_owner.get_or_null(p_body);
1221
ERR_FAIL_NULL(soft_body);
1222
1223
soft_body->unpin_all_vertices();
1224
}
1225
1226
void GodotPhysicsServer3D::soft_body_pin_point(RID p_body, int p_point_index, bool p_pin) {
1227
GodotSoftBody3D *soft_body = soft_body_owner.get_or_null(p_body);
1228
ERR_FAIL_NULL(soft_body);
1229
1230
if (p_pin) {
1231
soft_body->pin_vertex(p_point_index);
1232
} else {
1233
soft_body->unpin_vertex(p_point_index);
1234
}
1235
}
1236
1237
bool GodotPhysicsServer3D::soft_body_is_point_pinned(RID p_body, int p_point_index) const {
1238
GodotSoftBody3D *soft_body = soft_body_owner.get_or_null(p_body);
1239
ERR_FAIL_NULL_V(soft_body, false);
1240
1241
return soft_body->is_vertex_pinned(p_point_index);
1242
}
1243
1244
/* JOINT API */
1245
1246
RID GodotPhysicsServer3D::joint_create() {
1247
GodotJoint3D *joint = memnew(GodotJoint3D);
1248
RID rid = joint_owner.make_rid(joint);
1249
joint->set_self(rid);
1250
return rid;
1251
}
1252
1253
void GodotPhysicsServer3D::joint_clear(RID p_joint) {
1254
GodotJoint3D *joint = joint_owner.get_or_null(p_joint);
1255
ERR_FAIL_NULL(joint);
1256
if (joint->get_type() != JOINT_TYPE_MAX) {
1257
GodotJoint3D *empty_joint = memnew(GodotJoint3D);
1258
empty_joint->copy_settings_from(joint);
1259
1260
joint_owner.replace(p_joint, empty_joint);
1261
memdelete(joint);
1262
}
1263
}
1264
1265
void GodotPhysicsServer3D::joint_make_pin(RID p_joint, RID p_body_A, const Vector3 &p_local_A, RID p_body_B, const Vector3 &p_local_B) {
1266
GodotBody3D *body_A = body_owner.get_or_null(p_body_A);
1267
ERR_FAIL_NULL(body_A);
1268
1269
if (!p_body_B.is_valid()) {
1270
ERR_FAIL_NULL(body_A->get_space());
1271
p_body_B = body_A->get_space()->get_static_global_body();
1272
}
1273
1274
GodotBody3D *body_B = body_owner.get_or_null(p_body_B);
1275
ERR_FAIL_NULL(body_B);
1276
1277
ERR_FAIL_COND(body_A == body_B);
1278
1279
GodotJoint3D *prev_joint = joint_owner.get_or_null(p_joint);
1280
ERR_FAIL_NULL(prev_joint);
1281
1282
GodotJoint3D *joint = memnew(GodotPinJoint3D(body_A, p_local_A, body_B, p_local_B));
1283
1284
joint->copy_settings_from(prev_joint);
1285
joint_owner.replace(p_joint, joint);
1286
memdelete(prev_joint);
1287
}
1288
1289
void GodotPhysicsServer3D::pin_joint_set_param(RID p_joint, PinJointParam p_param, real_t p_value) {
1290
GodotJoint3D *joint = joint_owner.get_or_null(p_joint);
1291
ERR_FAIL_NULL(joint);
1292
ERR_FAIL_COND(joint->get_type() != JOINT_TYPE_PIN);
1293
GodotPinJoint3D *pin_joint = static_cast<GodotPinJoint3D *>(joint);
1294
pin_joint->set_param(p_param, p_value);
1295
}
1296
1297
real_t GodotPhysicsServer3D::pin_joint_get_param(RID p_joint, PinJointParam p_param) const {
1298
GodotJoint3D *joint = joint_owner.get_or_null(p_joint);
1299
ERR_FAIL_NULL_V(joint, 0);
1300
ERR_FAIL_COND_V(joint->get_type() != JOINT_TYPE_PIN, 0);
1301
GodotPinJoint3D *pin_joint = static_cast<GodotPinJoint3D *>(joint);
1302
return pin_joint->get_param(p_param);
1303
}
1304
1305
void GodotPhysicsServer3D::pin_joint_set_local_a(RID p_joint, const Vector3 &p_A) {
1306
GodotJoint3D *joint = joint_owner.get_or_null(p_joint);
1307
ERR_FAIL_NULL(joint);
1308
ERR_FAIL_COND(joint->get_type() != JOINT_TYPE_PIN);
1309
GodotPinJoint3D *pin_joint = static_cast<GodotPinJoint3D *>(joint);
1310
pin_joint->set_pos_a(p_A);
1311
}
1312
1313
Vector3 GodotPhysicsServer3D::pin_joint_get_local_a(RID p_joint) const {
1314
GodotJoint3D *joint = joint_owner.get_or_null(p_joint);
1315
ERR_FAIL_NULL_V(joint, Vector3());
1316
ERR_FAIL_COND_V(joint->get_type() != JOINT_TYPE_PIN, Vector3());
1317
GodotPinJoint3D *pin_joint = static_cast<GodotPinJoint3D *>(joint);
1318
return pin_joint->get_position_a();
1319
}
1320
1321
void GodotPhysicsServer3D::pin_joint_set_local_b(RID p_joint, const Vector3 &p_B) {
1322
GodotJoint3D *joint = joint_owner.get_or_null(p_joint);
1323
ERR_FAIL_NULL(joint);
1324
ERR_FAIL_COND(joint->get_type() != JOINT_TYPE_PIN);
1325
GodotPinJoint3D *pin_joint = static_cast<GodotPinJoint3D *>(joint);
1326
pin_joint->set_pos_b(p_B);
1327
}
1328
1329
Vector3 GodotPhysicsServer3D::pin_joint_get_local_b(RID p_joint) const {
1330
GodotJoint3D *joint = joint_owner.get_or_null(p_joint);
1331
ERR_FAIL_NULL_V(joint, Vector3());
1332
ERR_FAIL_COND_V(joint->get_type() != JOINT_TYPE_PIN, Vector3());
1333
GodotPinJoint3D *pin_joint = static_cast<GodotPinJoint3D *>(joint);
1334
return pin_joint->get_position_b();
1335
}
1336
1337
void GodotPhysicsServer3D::joint_make_hinge(RID p_joint, RID p_body_A, const Transform3D &p_frame_A, RID p_body_B, const Transform3D &p_frame_B) {
1338
GodotBody3D *body_A = body_owner.get_or_null(p_body_A);
1339
ERR_FAIL_NULL(body_A);
1340
1341
if (!p_body_B.is_valid()) {
1342
ERR_FAIL_NULL(body_A->get_space());
1343
p_body_B = body_A->get_space()->get_static_global_body();
1344
}
1345
1346
GodotBody3D *body_B = body_owner.get_or_null(p_body_B);
1347
ERR_FAIL_NULL(body_B);
1348
1349
ERR_FAIL_COND(body_A == body_B);
1350
1351
GodotJoint3D *prev_joint = joint_owner.get_or_null(p_joint);
1352
ERR_FAIL_NULL(prev_joint);
1353
1354
GodotJoint3D *joint = memnew(GodotHingeJoint3D(body_A, body_B, p_frame_A, p_frame_B));
1355
1356
joint->copy_settings_from(prev_joint);
1357
joint_owner.replace(p_joint, joint);
1358
memdelete(prev_joint);
1359
}
1360
1361
void GodotPhysicsServer3D::joint_make_hinge_simple(RID p_joint, RID p_body_A, const Vector3 &p_pivot_A, const Vector3 &p_axis_A, RID p_body_B, const Vector3 &p_pivot_B, const Vector3 &p_axis_B) {
1362
GodotBody3D *body_A = body_owner.get_or_null(p_body_A);
1363
ERR_FAIL_NULL(body_A);
1364
1365
if (!p_body_B.is_valid()) {
1366
ERR_FAIL_NULL(body_A->get_space());
1367
p_body_B = body_A->get_space()->get_static_global_body();
1368
}
1369
1370
GodotBody3D *body_B = body_owner.get_or_null(p_body_B);
1371
ERR_FAIL_NULL(body_B);
1372
1373
ERR_FAIL_COND(body_A == body_B);
1374
1375
GodotJoint3D *prev_joint = joint_owner.get_or_null(p_joint);
1376
ERR_FAIL_NULL(prev_joint);
1377
1378
GodotJoint3D *joint = memnew(GodotHingeJoint3D(body_A, body_B, p_pivot_A, p_pivot_B, p_axis_A, p_axis_B));
1379
1380
joint->copy_settings_from(prev_joint);
1381
joint_owner.replace(p_joint, joint);
1382
memdelete(prev_joint);
1383
}
1384
1385
void GodotPhysicsServer3D::hinge_joint_set_param(RID p_joint, HingeJointParam p_param, real_t p_value) {
1386
GodotJoint3D *joint = joint_owner.get_or_null(p_joint);
1387
ERR_FAIL_NULL(joint);
1388
ERR_FAIL_COND(joint->get_type() != JOINT_TYPE_HINGE);
1389
GodotHingeJoint3D *hinge_joint = static_cast<GodotHingeJoint3D *>(joint);
1390
hinge_joint->set_param(p_param, p_value);
1391
}
1392
1393
real_t GodotPhysicsServer3D::hinge_joint_get_param(RID p_joint, HingeJointParam p_param) const {
1394
GodotJoint3D *joint = joint_owner.get_or_null(p_joint);
1395
ERR_FAIL_NULL_V(joint, 0);
1396
ERR_FAIL_COND_V(joint->get_type() != JOINT_TYPE_HINGE, 0);
1397
GodotHingeJoint3D *hinge_joint = static_cast<GodotHingeJoint3D *>(joint);
1398
return hinge_joint->get_param(p_param);
1399
}
1400
1401
void GodotPhysicsServer3D::hinge_joint_set_flag(RID p_joint, HingeJointFlag p_flag, bool p_enabled) {
1402
GodotJoint3D *joint = joint_owner.get_or_null(p_joint);
1403
ERR_FAIL_NULL(joint);
1404
ERR_FAIL_COND(joint->get_type() != JOINT_TYPE_HINGE);
1405
GodotHingeJoint3D *hinge_joint = static_cast<GodotHingeJoint3D *>(joint);
1406
hinge_joint->set_flag(p_flag, p_enabled);
1407
}
1408
1409
bool GodotPhysicsServer3D::hinge_joint_get_flag(RID p_joint, HingeJointFlag p_flag) const {
1410
GodotJoint3D *joint = joint_owner.get_or_null(p_joint);
1411
ERR_FAIL_NULL_V(joint, false);
1412
ERR_FAIL_COND_V(joint->get_type() != JOINT_TYPE_HINGE, false);
1413
GodotHingeJoint3D *hinge_joint = static_cast<GodotHingeJoint3D *>(joint);
1414
return hinge_joint->get_flag(p_flag);
1415
}
1416
1417
void GodotPhysicsServer3D::joint_set_solver_priority(RID p_joint, int p_priority) {
1418
GodotJoint3D *joint = joint_owner.get_or_null(p_joint);
1419
ERR_FAIL_NULL(joint);
1420
joint->set_priority(p_priority);
1421
}
1422
1423
int GodotPhysicsServer3D::joint_get_solver_priority(RID p_joint) const {
1424
GodotJoint3D *joint = joint_owner.get_or_null(p_joint);
1425
ERR_FAIL_NULL_V(joint, 0);
1426
return joint->get_priority();
1427
}
1428
1429
void GodotPhysicsServer3D::joint_disable_collisions_between_bodies(RID p_joint, bool p_disable) {
1430
GodotJoint3D *joint = joint_owner.get_or_null(p_joint);
1431
ERR_FAIL_NULL(joint);
1432
1433
joint->disable_collisions_between_bodies(p_disable);
1434
1435
if (2 == joint->get_body_count()) {
1436
GodotBody3D *body_a = *joint->get_body_ptr();
1437
GodotBody3D *body_b = *(joint->get_body_ptr() + 1);
1438
1439
if (p_disable) {
1440
body_add_collision_exception(body_a->get_self(), body_b->get_self());
1441
body_add_collision_exception(body_b->get_self(), body_a->get_self());
1442
} else {
1443
body_remove_collision_exception(body_a->get_self(), body_b->get_self());
1444
body_remove_collision_exception(body_b->get_self(), body_a->get_self());
1445
}
1446
}
1447
}
1448
1449
bool GodotPhysicsServer3D::joint_is_disabled_collisions_between_bodies(RID p_joint) const {
1450
GodotJoint3D *joint = joint_owner.get_or_null(p_joint);
1451
ERR_FAIL_NULL_V(joint, true);
1452
1453
return joint->is_disabled_collisions_between_bodies();
1454
}
1455
1456
GodotPhysicsServer3D::JointType GodotPhysicsServer3D::joint_get_type(RID p_joint) const {
1457
GodotJoint3D *joint = joint_owner.get_or_null(p_joint);
1458
ERR_FAIL_NULL_V(joint, JOINT_TYPE_PIN);
1459
return joint->get_type();
1460
}
1461
1462
void GodotPhysicsServer3D::joint_make_slider(RID p_joint, RID p_body_A, const Transform3D &p_local_frame_A, RID p_body_B, const Transform3D &p_local_frame_B) {
1463
GodotBody3D *body_A = body_owner.get_or_null(p_body_A);
1464
ERR_FAIL_NULL(body_A);
1465
1466
if (!p_body_B.is_valid()) {
1467
ERR_FAIL_NULL(body_A->get_space());
1468
p_body_B = body_A->get_space()->get_static_global_body();
1469
}
1470
1471
GodotBody3D *body_B = body_owner.get_or_null(p_body_B);
1472
ERR_FAIL_NULL(body_B);
1473
1474
ERR_FAIL_COND(body_A == body_B);
1475
1476
GodotJoint3D *prev_joint = joint_owner.get_or_null(p_joint);
1477
ERR_FAIL_NULL(prev_joint);
1478
1479
GodotJoint3D *joint = memnew(GodotSliderJoint3D(body_A, body_B, p_local_frame_A, p_local_frame_B));
1480
1481
joint->copy_settings_from(prev_joint);
1482
joint_owner.replace(p_joint, joint);
1483
memdelete(prev_joint);
1484
}
1485
1486
void GodotPhysicsServer3D::slider_joint_set_param(RID p_joint, SliderJointParam p_param, real_t p_value) {
1487
GodotJoint3D *joint = joint_owner.get_or_null(p_joint);
1488
ERR_FAIL_NULL(joint);
1489
ERR_FAIL_COND(joint->get_type() != JOINT_TYPE_SLIDER);
1490
GodotSliderJoint3D *slider_joint = static_cast<GodotSliderJoint3D *>(joint);
1491
slider_joint->set_param(p_param, p_value);
1492
}
1493
1494
real_t GodotPhysicsServer3D::slider_joint_get_param(RID p_joint, SliderJointParam p_param) const {
1495
GodotJoint3D *joint = joint_owner.get_or_null(p_joint);
1496
ERR_FAIL_NULL_V(joint, 0);
1497
ERR_FAIL_COND_V(joint->get_type() != JOINT_TYPE_CONE_TWIST, 0);
1498
GodotSliderJoint3D *slider_joint = static_cast<GodotSliderJoint3D *>(joint);
1499
return slider_joint->get_param(p_param);
1500
}
1501
1502
void GodotPhysicsServer3D::joint_make_cone_twist(RID p_joint, RID p_body_A, const Transform3D &p_local_frame_A, RID p_body_B, const Transform3D &p_local_frame_B) {
1503
GodotBody3D *body_A = body_owner.get_or_null(p_body_A);
1504
ERR_FAIL_NULL(body_A);
1505
1506
if (!p_body_B.is_valid()) {
1507
ERR_FAIL_NULL(body_A->get_space());
1508
p_body_B = body_A->get_space()->get_static_global_body();
1509
}
1510
1511
GodotBody3D *body_B = body_owner.get_or_null(p_body_B);
1512
ERR_FAIL_NULL(body_B);
1513
1514
ERR_FAIL_COND(body_A == body_B);
1515
1516
GodotJoint3D *prev_joint = joint_owner.get_or_null(p_joint);
1517
ERR_FAIL_NULL(prev_joint);
1518
1519
GodotJoint3D *joint = memnew(GodotConeTwistJoint3D(body_A, body_B, p_local_frame_A, p_local_frame_B));
1520
1521
joint->copy_settings_from(prev_joint);
1522
joint_owner.replace(p_joint, joint);
1523
memdelete(prev_joint);
1524
}
1525
1526
void GodotPhysicsServer3D::cone_twist_joint_set_param(RID p_joint, ConeTwistJointParam p_param, real_t p_value) {
1527
GodotJoint3D *joint = joint_owner.get_or_null(p_joint);
1528
ERR_FAIL_NULL(joint);
1529
ERR_FAIL_COND(joint->get_type() != JOINT_TYPE_CONE_TWIST);
1530
GodotConeTwistJoint3D *cone_twist_joint = static_cast<GodotConeTwistJoint3D *>(joint);
1531
cone_twist_joint->set_param(p_param, p_value);
1532
}
1533
1534
real_t GodotPhysicsServer3D::cone_twist_joint_get_param(RID p_joint, ConeTwistJointParam p_param) const {
1535
GodotJoint3D *joint = joint_owner.get_or_null(p_joint);
1536
ERR_FAIL_NULL_V(joint, 0);
1537
ERR_FAIL_COND_V(joint->get_type() != JOINT_TYPE_CONE_TWIST, 0);
1538
GodotConeTwistJoint3D *cone_twist_joint = static_cast<GodotConeTwistJoint3D *>(joint);
1539
return cone_twist_joint->get_param(p_param);
1540
}
1541
1542
void GodotPhysicsServer3D::joint_make_generic_6dof(RID p_joint, RID p_body_A, const Transform3D &p_local_frame_A, RID p_body_B, const Transform3D &p_local_frame_B) {
1543
GodotBody3D *body_A = body_owner.get_or_null(p_body_A);
1544
ERR_FAIL_NULL(body_A);
1545
1546
if (!p_body_B.is_valid()) {
1547
ERR_FAIL_NULL(body_A->get_space());
1548
p_body_B = body_A->get_space()->get_static_global_body();
1549
}
1550
1551
GodotBody3D *body_B = body_owner.get_or_null(p_body_B);
1552
ERR_FAIL_NULL(body_B);
1553
1554
ERR_FAIL_COND(body_A == body_B);
1555
1556
GodotJoint3D *prev_joint = joint_owner.get_or_null(p_joint);
1557
ERR_FAIL_NULL(prev_joint);
1558
1559
GodotJoint3D *joint = memnew(GodotGeneric6DOFJoint3D(body_A, body_B, p_local_frame_A, p_local_frame_B, true));
1560
1561
joint->copy_settings_from(prev_joint);
1562
joint_owner.replace(p_joint, joint);
1563
memdelete(prev_joint);
1564
}
1565
1566
void GodotPhysicsServer3D::generic_6dof_joint_set_param(RID p_joint, Vector3::Axis p_axis, G6DOFJointAxisParam p_param, real_t p_value) {
1567
GodotJoint3D *joint = joint_owner.get_or_null(p_joint);
1568
ERR_FAIL_NULL(joint);
1569
ERR_FAIL_COND(joint->get_type() != JOINT_TYPE_6DOF);
1570
GodotGeneric6DOFJoint3D *generic_6dof_joint = static_cast<GodotGeneric6DOFJoint3D *>(joint);
1571
generic_6dof_joint->set_param(p_axis, p_param, p_value);
1572
}
1573
1574
real_t GodotPhysicsServer3D::generic_6dof_joint_get_param(RID p_joint, Vector3::Axis p_axis, G6DOFJointAxisParam p_param) const {
1575
GodotJoint3D *joint = joint_owner.get_or_null(p_joint);
1576
ERR_FAIL_NULL_V(joint, 0);
1577
ERR_FAIL_COND_V(joint->get_type() != JOINT_TYPE_6DOF, 0);
1578
GodotGeneric6DOFJoint3D *generic_6dof_joint = static_cast<GodotGeneric6DOFJoint3D *>(joint);
1579
return generic_6dof_joint->get_param(p_axis, p_param);
1580
}
1581
1582
void GodotPhysicsServer3D::generic_6dof_joint_set_flag(RID p_joint, Vector3::Axis p_axis, G6DOFJointAxisFlag p_flag, bool p_enable) {
1583
GodotJoint3D *joint = joint_owner.get_or_null(p_joint);
1584
ERR_FAIL_NULL(joint);
1585
ERR_FAIL_COND(joint->get_type() != JOINT_TYPE_6DOF);
1586
GodotGeneric6DOFJoint3D *generic_6dof_joint = static_cast<GodotGeneric6DOFJoint3D *>(joint);
1587
generic_6dof_joint->set_flag(p_axis, p_flag, p_enable);
1588
}
1589
1590
bool GodotPhysicsServer3D::generic_6dof_joint_get_flag(RID p_joint, Vector3::Axis p_axis, G6DOFJointAxisFlag p_flag) const {
1591
GodotJoint3D *joint = joint_owner.get_or_null(p_joint);
1592
ERR_FAIL_NULL_V(joint, false);
1593
ERR_FAIL_COND_V(joint->get_type() != JOINT_TYPE_6DOF, false);
1594
GodotGeneric6DOFJoint3D *generic_6dof_joint = static_cast<GodotGeneric6DOFJoint3D *>(joint);
1595
return generic_6dof_joint->get_flag(p_axis, p_flag);
1596
}
1597
1598
void GodotPhysicsServer3D::free(RID p_rid) {
1599
_update_shapes(); //just in case
1600
1601
if (shape_owner.owns(p_rid)) {
1602
GodotShape3D *shape = shape_owner.get_or_null(p_rid);
1603
1604
while (shape->get_owners().size()) {
1605
GodotShapeOwner3D *so = shape->get_owners().begin()->key;
1606
so->remove_shape(shape);
1607
}
1608
1609
shape_owner.free(p_rid);
1610
memdelete(shape);
1611
} else if (body_owner.owns(p_rid)) {
1612
GodotBody3D *body = body_owner.get_or_null(p_rid);
1613
1614
body->set_space(nullptr);
1615
1616
while (body->get_shape_count()) {
1617
body->remove_shape(0);
1618
}
1619
1620
body_owner.free(p_rid);
1621
memdelete(body);
1622
} else if (soft_body_owner.owns(p_rid)) {
1623
GodotSoftBody3D *soft_body = soft_body_owner.get_or_null(p_rid);
1624
1625
soft_body->set_space(nullptr);
1626
1627
soft_body_owner.free(p_rid);
1628
memdelete(soft_body);
1629
} else if (area_owner.owns(p_rid)) {
1630
GodotArea3D *area = area_owner.get_or_null(p_rid);
1631
1632
area->set_space(nullptr);
1633
1634
while (area->get_shape_count()) {
1635
area->remove_shape(0);
1636
}
1637
1638
area_owner.free(p_rid);
1639
memdelete(area);
1640
} else if (space_owner.owns(p_rid)) {
1641
GodotSpace3D *space = space_owner.get_or_null(p_rid);
1642
1643
while (space->get_objects().size()) {
1644
GodotCollisionObject3D *co = static_cast<GodotCollisionObject3D *>(*space->get_objects().begin());
1645
co->set_space(nullptr);
1646
}
1647
1648
active_spaces.erase(space);
1649
free(space->get_default_area()->get_self());
1650
free(space->get_static_global_body());
1651
1652
space_owner.free(p_rid);
1653
memdelete(space);
1654
} else if (joint_owner.owns(p_rid)) {
1655
GodotJoint3D *joint = joint_owner.get_or_null(p_rid);
1656
1657
joint_owner.free(p_rid);
1658
memdelete(joint);
1659
1660
} else {
1661
ERR_FAIL_MSG("Invalid ID.");
1662
}
1663
}
1664
1665
void GodotPhysicsServer3D::set_active(bool p_active) {
1666
active = p_active;
1667
}
1668
1669
void GodotPhysicsServer3D::init() {
1670
stepper = memnew(GodotStep3D);
1671
}
1672
1673
void GodotPhysicsServer3D::step(real_t p_step) {
1674
if (!active) {
1675
return;
1676
}
1677
1678
_update_shapes();
1679
1680
island_count = 0;
1681
active_objects = 0;
1682
collision_pairs = 0;
1683
for (GodotSpace3D *E : active_spaces) {
1684
stepper->step(E, p_step);
1685
island_count += E->get_island_count();
1686
active_objects += E->get_active_objects();
1687
collision_pairs += E->get_collision_pairs();
1688
}
1689
}
1690
1691
void GodotPhysicsServer3D::sync() {
1692
doing_sync = true;
1693
}
1694
1695
void GodotPhysicsServer3D::flush_queries() {
1696
if (!active) {
1697
return;
1698
}
1699
1700
flushing_queries = true;
1701
1702
uint64_t time_beg = OS::get_singleton()->get_ticks_usec();
1703
1704
for (GodotSpace3D *E : active_spaces) {
1705
GodotSpace3D *space = E;
1706
space->call_queries();
1707
}
1708
1709
flushing_queries = false;
1710
1711
if (EngineDebugger::is_profiling("servers")) {
1712
uint64_t total_time[GodotSpace3D::ELAPSED_TIME_MAX];
1713
static const char *time_name[GodotSpace3D::ELAPSED_TIME_MAX] = {
1714
"integrate_forces",
1715
"generate_islands",
1716
"setup_constraints",
1717
"solve_constraints",
1718
"integrate_velocities"
1719
};
1720
1721
for (int i = 0; i < GodotSpace3D::ELAPSED_TIME_MAX; i++) {
1722
total_time[i] = 0;
1723
}
1724
1725
for (const GodotSpace3D *E : active_spaces) {
1726
for (int i = 0; i < GodotSpace3D::ELAPSED_TIME_MAX; i++) {
1727
total_time[i] += E->get_elapsed_time(GodotSpace3D::ElapsedTime(i));
1728
}
1729
}
1730
1731
Array values;
1732
values.resize(GodotSpace3D::ELAPSED_TIME_MAX * 2);
1733
for (int i = 0; i < GodotSpace3D::ELAPSED_TIME_MAX; i++) {
1734
values[i * 2 + 0] = time_name[i];
1735
values[i * 2 + 1] = USEC_TO_SEC(total_time[i]);
1736
}
1737
values.push_back("flush_queries");
1738
values.push_back(USEC_TO_SEC(OS::get_singleton()->get_ticks_usec() - time_beg));
1739
1740
values.push_front("physics_3d");
1741
EngineDebugger::profiler_add_frame_data("servers", values);
1742
}
1743
}
1744
1745
void GodotPhysicsServer3D::end_sync() {
1746
doing_sync = false;
1747
}
1748
1749
void GodotPhysicsServer3D::finish() {
1750
memdelete(stepper);
1751
}
1752
1753
int GodotPhysicsServer3D::get_process_info(ProcessInfo p_info) {
1754
switch (p_info) {
1755
case INFO_ACTIVE_OBJECTS: {
1756
return active_objects;
1757
} break;
1758
case INFO_COLLISION_PAIRS: {
1759
return collision_pairs;
1760
} break;
1761
case INFO_ISLAND_COUNT: {
1762
return island_count;
1763
} break;
1764
}
1765
1766
return 0;
1767
}
1768
1769
void GodotPhysicsServer3D::_update_shapes() {
1770
while (pending_shape_update_list.first()) {
1771
pending_shape_update_list.first()->self()->_shape_changed();
1772
pending_shape_update_list.remove(pending_shape_update_list.first());
1773
}
1774
}
1775
1776
void GodotPhysicsServer3D::_shape_col_cbk(const Vector3 &p_point_A, int p_index_A, const Vector3 &p_point_B, int p_index_B, const Vector3 &normal, void *p_userdata) {
1777
CollCbkData *cbk = static_cast<CollCbkData *>(p_userdata);
1778
1779
if (cbk->max == 0) {
1780
return;
1781
}
1782
1783
if (cbk->amount == cbk->max) {
1784
//find least deep
1785
real_t min_depth = 1e20;
1786
int min_depth_idx = 0;
1787
for (int i = 0; i < cbk->amount; i++) {
1788
real_t d = cbk->ptr[i * 2 + 0].distance_squared_to(cbk->ptr[i * 2 + 1]);
1789
if (d < min_depth) {
1790
min_depth = d;
1791
min_depth_idx = i;
1792
}
1793
}
1794
1795
real_t d = p_point_A.distance_squared_to(p_point_B);
1796
if (d < min_depth) {
1797
return;
1798
}
1799
cbk->ptr[min_depth_idx * 2 + 0] = p_point_A;
1800
cbk->ptr[min_depth_idx * 2 + 1] = p_point_B;
1801
1802
} else {
1803
cbk->ptr[cbk->amount * 2 + 0] = p_point_A;
1804
cbk->ptr[cbk->amount * 2 + 1] = p_point_B;
1805
cbk->amount++;
1806
}
1807
}
1808
1809
GodotPhysicsServer3D *GodotPhysicsServer3D::godot_singleton = nullptr;
1810
GodotPhysicsServer3D::GodotPhysicsServer3D(bool p_using_threads) {
1811
godot_singleton = this;
1812
GodotBroadPhase3D::create_func = GodotBroadPhase3DBVH::_create;
1813
1814
using_threads = p_using_threads;
1815
}
1816
1817