1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
|
/*************************************************************************/
/* shape_bullet.cpp */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/*************************************************************************/
/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/
#include "shape_bullet.h"
#include "btRayShape.h"
#include "bullet_physics_server.h"
#include "bullet_types_converter.h"
#include "bullet_utilities.h"
#include "core/project_settings.h"
#include "shape_owner_bullet.h"
#include <BulletCollision/CollisionDispatch/btInternalEdgeUtility.h>
#include <BulletCollision/CollisionShapes/btConvexPointCloudShape.h>
#include <BulletCollision/CollisionShapes/btHeightfieldTerrainShape.h>
#include <btBulletCollisionCommon.h>
/**
@author AndreaCatania
*/
ShapeBullet::ShapeBullet() {}
ShapeBullet::~ShapeBullet() {}
btCollisionShape *ShapeBullet::create_bt_shape(const Vector3 &p_implicit_scale, real_t p_extra_edge) {
btVector3 s;
G_TO_B(p_implicit_scale, s);
return create_bt_shape(s, p_extra_edge);
}
btCollisionShape *ShapeBullet::prepare(btCollisionShape *p_btShape) const {
p_btShape->setUserPointer(const_cast<ShapeBullet *>(this));
p_btShape->setMargin(margin);
return p_btShape;
}
void ShapeBullet::notifyShapeChanged() {
for (Map<ShapeOwnerBullet *, int>::Element *E = owners.front(); E; E = E->next()) {
ShapeOwnerBullet *owner = static_cast<ShapeOwnerBullet *>(E->key());
owner->shape_changed(owner->find_shape(this));
}
}
void ShapeBullet::add_owner(ShapeOwnerBullet *p_owner) {
Map<ShapeOwnerBullet *, int>::Element *E = owners.find(p_owner);
if (E) {
E->get()++;
} else {
owners[p_owner] = 1; // add new owner
}
}
void ShapeBullet::remove_owner(ShapeOwnerBullet *p_owner, bool p_permanentlyFromThisBody) {
Map<ShapeOwnerBullet *, int>::Element *E = owners.find(p_owner);
if (!E)
return;
E->get()--;
if (p_permanentlyFromThisBody || 0 >= E->get()) {
owners.erase(E);
}
}
bool ShapeBullet::is_owner(ShapeOwnerBullet *p_owner) const {
return owners.has(p_owner);
}
const Map<ShapeOwnerBullet *, int> &ShapeBullet::get_owners() const {
return owners;
}
void ShapeBullet::set_margin(real_t p_margin) {
margin = p_margin;
notifyShapeChanged();
}
real_t ShapeBullet::get_margin() const {
return margin;
}
btEmptyShape *ShapeBullet::create_shape_empty() {
return bulletnew(btEmptyShape);
}
btStaticPlaneShape *ShapeBullet::create_shape_plane(const btVector3 &planeNormal, btScalar planeConstant) {
return bulletnew(btStaticPlaneShape(planeNormal, planeConstant));
}
btSphereShape *ShapeBullet::create_shape_sphere(btScalar radius) {
return bulletnew(btSphereShape(radius));
}
btBoxShape *ShapeBullet::create_shape_box(const btVector3 &boxHalfExtents) {
return bulletnew(btBoxShape(boxHalfExtents));
}
btCapsuleShape *ShapeBullet::create_shape_capsule(btScalar radius, btScalar height) {
return bulletnew(btCapsuleShape(radius, height));
}
btCylinderShape *ShapeBullet::create_shape_cylinder(btScalar radius, btScalar height) {
return bulletnew(btCylinderShape(btVector3(radius, height / 2.0, radius)));
}
btConvexPointCloudShape *ShapeBullet::create_shape_convex(btAlignedObjectArray<btVector3> &p_vertices, const btVector3 &p_local_scaling) {
return bulletnew(btConvexPointCloudShape(&p_vertices[0], p_vertices.size(), p_local_scaling));
}
btScaledBvhTriangleMeshShape *ShapeBullet::create_shape_concave(btBvhTriangleMeshShape *p_mesh_shape, const btVector3 &p_local_scaling) {
if (p_mesh_shape) {
return bulletnew(btScaledBvhTriangleMeshShape(p_mesh_shape, p_local_scaling));
} else {
return nullptr;
}
}
btHeightfieldTerrainShape *ShapeBullet::create_shape_height_field(Vector<real_t> &p_heights, int p_width, int p_depth, real_t p_min_height, real_t p_max_height) {
const btScalar ignoredHeightScale(1);
const int YAxis = 1; // 0=X, 1=Y, 2=Z
const bool flipQuadEdges = false;
const void *heightsPtr = p_heights.ptr();
btHeightfieldTerrainShape *heightfield = bulletnew(btHeightfieldTerrainShape(p_width, p_depth, heightsPtr, ignoredHeightScale, p_min_height, p_max_height, YAxis, PHY_FLOAT, flipQuadEdges));
// The shape can be created without params when you do PhysicsServer3D.shape_create(PhysicsServer3D.SHAPE_HEIGHTMAP)
if (heightsPtr)
heightfield->buildAccelerator(16);
return heightfield;
}
btRayShape *ShapeBullet::create_shape_ray(real_t p_length, bool p_slips_on_slope) {
btRayShape *r(bulletnew(btRayShape(p_length)));
r->setSlipsOnSlope(p_slips_on_slope);
return r;
}
/* PLANE */
PlaneShapeBullet::PlaneShapeBullet() :
ShapeBullet() {}
void PlaneShapeBullet::set_data(const Variant &p_data) {
setup(p_data);
}
Variant PlaneShapeBullet::get_data() const {
return plane;
}
PhysicsServer3D::ShapeType PlaneShapeBullet::get_type() const {
return PhysicsServer3D::SHAPE_PLANE;
}
void PlaneShapeBullet::setup(const Plane &p_plane) {
plane = p_plane;
notifyShapeChanged();
}
btCollisionShape *PlaneShapeBullet::create_bt_shape(const btVector3 &p_implicit_scale, real_t p_extra_edge) {
btVector3 btPlaneNormal;
G_TO_B(plane.normal, btPlaneNormal);
return prepare(PlaneShapeBullet::create_shape_plane(btPlaneNormal, plane.d));
}
/* Sphere */
SphereShapeBullet::SphereShapeBullet() :
ShapeBullet() {}
void SphereShapeBullet::set_data(const Variant &p_data) {
setup(p_data);
}
Variant SphereShapeBullet::get_data() const {
return radius;
}
PhysicsServer3D::ShapeType SphereShapeBullet::get_type() const {
return PhysicsServer3D::SHAPE_SPHERE;
}
void SphereShapeBullet::setup(real_t p_radius) {
radius = p_radius;
notifyShapeChanged();
}
btCollisionShape *SphereShapeBullet::create_bt_shape(const btVector3 &p_implicit_scale, real_t p_extra_edge) {
return prepare(ShapeBullet::create_shape_sphere(radius * p_implicit_scale[0] + p_extra_edge));
}
/* Box */
BoxShapeBullet::BoxShapeBullet() :
ShapeBullet() {}
void BoxShapeBullet::set_data(const Variant &p_data) {
setup(p_data);
}
Variant BoxShapeBullet::get_data() const {
Vector3 g_half_extents;
B_TO_G(half_extents, g_half_extents);
return g_half_extents;
}
PhysicsServer3D::ShapeType BoxShapeBullet::get_type() const {
return PhysicsServer3D::SHAPE_BOX;
}
void BoxShapeBullet::setup(const Vector3 &p_half_extents) {
G_TO_B(p_half_extents, half_extents);
notifyShapeChanged();
}
btCollisionShape *BoxShapeBullet::create_bt_shape(const btVector3 &p_implicit_scale, real_t p_extra_edge) {
return prepare(ShapeBullet::create_shape_box((half_extents * p_implicit_scale) + btVector3(p_extra_edge, p_extra_edge, p_extra_edge)));
}
/* Capsule */
CapsuleShapeBullet::CapsuleShapeBullet() :
ShapeBullet() {}
void CapsuleShapeBullet::set_data(const Variant &p_data) {
Dictionary d = p_data;
ERR_FAIL_COND(!d.has("radius"));
ERR_FAIL_COND(!d.has("height"));
setup(d["height"], d["radius"]);
}
Variant CapsuleShapeBullet::get_data() const {
Dictionary d;
d["radius"] = radius;
d["height"] = height;
return d;
}
PhysicsServer3D::ShapeType CapsuleShapeBullet::get_type() const {
return PhysicsServer3D::SHAPE_CAPSULE;
}
void CapsuleShapeBullet::setup(real_t p_height, real_t p_radius) {
radius = p_radius;
height = p_height;
notifyShapeChanged();
}
btCollisionShape *CapsuleShapeBullet::create_bt_shape(const btVector3 &p_implicit_scale, real_t p_extra_edge) {
return prepare(ShapeBullet::create_shape_capsule(radius * p_implicit_scale[0] + p_extra_edge, height * p_implicit_scale[1] + p_extra_edge));
}
/* Cylinder */
CylinderShapeBullet::CylinderShapeBullet() :
ShapeBullet() {}
void CylinderShapeBullet::set_data(const Variant &p_data) {
Dictionary d = p_data;
ERR_FAIL_COND(!d.has("radius"));
ERR_FAIL_COND(!d.has("height"));
setup(d["height"], d["radius"]);
}
Variant CylinderShapeBullet::get_data() const {
Dictionary d;
d["radius"] = radius;
d["height"] = height;
return d;
}
PhysicsServer3D::ShapeType CylinderShapeBullet::get_type() const {
return PhysicsServer3D::SHAPE_CYLINDER;
}
void CylinderShapeBullet::setup(real_t p_height, real_t p_radius) {
radius = p_radius;
height = p_height;
notifyShapeChanged();
}
btCollisionShape *CylinderShapeBullet::create_bt_shape(const btVector3 &p_implicit_scale, real_t p_margin) {
return prepare(ShapeBullet::create_shape_cylinder(radius * p_implicit_scale[0] + p_margin, height * p_implicit_scale[1] + p_margin));
}
/* Convex polygon */
ConvexPolygonShapeBullet::ConvexPolygonShapeBullet() :
ShapeBullet() {}
void ConvexPolygonShapeBullet::set_data(const Variant &p_data) {
setup(p_data);
}
void ConvexPolygonShapeBullet::get_vertices(Vector<Vector3> &out_vertices) {
const int n_of_vertices = vertices.size();
out_vertices.resize(n_of_vertices);
for (int i = n_of_vertices - 1; 0 <= i; --i) {
B_TO_G(vertices[i], out_vertices.write[i]);
}
}
Variant ConvexPolygonShapeBullet::get_data() const {
ConvexPolygonShapeBullet *variable_self = const_cast<ConvexPolygonShapeBullet *>(this);
Vector<Vector3> out_vertices;
variable_self->get_vertices(out_vertices);
return out_vertices;
}
PhysicsServer3D::ShapeType ConvexPolygonShapeBullet::get_type() const {
return PhysicsServer3D::SHAPE_CONVEX_POLYGON;
}
void ConvexPolygonShapeBullet::setup(const Vector<Vector3> &p_vertices) {
// Make a copy of vertices
const int n_of_vertices = p_vertices.size();
vertices.resize(n_of_vertices);
for (int i = n_of_vertices - 1; 0 <= i; --i) {
G_TO_B(p_vertices[i], vertices[i]);
}
notifyShapeChanged();
}
btCollisionShape *ConvexPolygonShapeBullet::create_bt_shape(const btVector3 &p_implicit_scale, real_t p_extra_edge) {
if (!vertices.size())
// This is necessary since 0 vertices
return prepare(ShapeBullet::create_shape_empty());
btCollisionShape *cs(ShapeBullet::create_shape_convex(vertices));
cs->setLocalScaling(p_implicit_scale);
prepare(cs);
return cs;
}
/* Concave polygon */
ConcavePolygonShapeBullet::ConcavePolygonShapeBullet() :
ShapeBullet() {}
ConcavePolygonShapeBullet::~ConcavePolygonShapeBullet() {
if (meshShape) {
delete meshShape->getMeshInterface();
delete meshShape->getTriangleInfoMap();
bulletdelete(meshShape);
}
faces = Vector<Vector3>();
}
void ConcavePolygonShapeBullet::set_data(const Variant &p_data) {
setup(p_data);
}
Variant ConcavePolygonShapeBullet::get_data() const {
return faces;
}
PhysicsServer3D::ShapeType ConcavePolygonShapeBullet::get_type() const {
return PhysicsServer3D::SHAPE_CONCAVE_POLYGON;
}
void ConcavePolygonShapeBullet::setup(Vector<Vector3> p_faces) {
faces = p_faces;
if (meshShape) {
/// Clear previous created shape
delete meshShape->getMeshInterface();
delete meshShape->getTriangleInfoMap();
bulletdelete(meshShape);
}
int src_face_count = faces.size();
if (0 < src_face_count) {
// It counts the faces and assert the array contains the correct number of vertices.
ERR_FAIL_COND(src_face_count % 3);
btTriangleMesh *shapeInterface = bulletnew(btTriangleMesh);
src_face_count /= 3;
const Vector3 *r = p_faces.ptr();
const Vector3 *facesr = r;
btVector3 supVec_0;
btVector3 supVec_1;
btVector3 supVec_2;
for (int i = 0; i < src_face_count; ++i) {
G_TO_B(facesr[i * 3 + 0], supVec_0);
G_TO_B(facesr[i * 3 + 1], supVec_1);
G_TO_B(facesr[i * 3 + 2], supVec_2);
// Inverted from standard godot otherwise btGenerateInternalEdgeInfo generates wrong edge info
shapeInterface->addTriangle(supVec_2, supVec_1, supVec_0);
}
const bool useQuantizedAabbCompression = true;
meshShape = bulletnew(btBvhTriangleMeshShape(shapeInterface, useQuantizedAabbCompression));
if (GLOBAL_DEF("physics/3d/smooth_trimesh_collision", false)) {
btTriangleInfoMap *triangleInfoMap = new btTriangleInfoMap();
btGenerateInternalEdgeInfo(meshShape, triangleInfoMap);
}
} else {
meshShape = nullptr;
ERR_PRINT("The faces count are 0, the mesh shape cannot be created");
}
notifyShapeChanged();
}
btCollisionShape *ConcavePolygonShapeBullet::create_bt_shape(const btVector3 &p_implicit_scale, real_t p_extra_edge) {
btCollisionShape *cs = ShapeBullet::create_shape_concave(meshShape);
if (!cs)
// This is necessary since if 0 faces the creation of concave return null
cs = ShapeBullet::create_shape_empty();
cs->setLocalScaling(p_implicit_scale);
prepare(cs);
cs->setMargin(0);
return cs;
}
/* Height map shape */
HeightMapShapeBullet::HeightMapShapeBullet() :
ShapeBullet() {}
void HeightMapShapeBullet::set_data(const Variant &p_data) {
ERR_FAIL_COND(p_data.get_type() != Variant::DICTIONARY);
Dictionary d = p_data;
ERR_FAIL_COND(!d.has("width"));
ERR_FAIL_COND(!d.has("depth"));
ERR_FAIL_COND(!d.has("heights"));
real_t l_min_height = 0.0;
real_t l_max_height = 0.0;
// If specified, min and max height will be used as precomputed values
if (d.has("min_height"))
l_min_height = d["min_height"];
if (d.has("max_height"))
l_max_height = d["max_height"];
ERR_FAIL_COND(l_min_height > l_max_height);
int l_width = d["width"];
int l_depth = d["depth"];
// TODO This code will need adjustments if real_t is set to `double`,
// because that precision is unnecessary for a heightmap and Bullet doesn't support it...
Vector<real_t> l_heights;
Variant l_heights_v = d["heights"];
if (l_heights_v.get_type() == Variant::PACKED_FLOAT32_ARRAY) {
// Ready-to-use heights can be passed
l_heights = l_heights_v;
} else if (l_heights_v.get_type() == Variant::OBJECT) {
// If an image is passed, we have to convert it to a format Bullet supports.
// this would be expensive to do with a script, so it's nice to have it here.
Ref<Image> l_image = l_heights_v;
ERR_FAIL_COND(l_image.is_null());
// Float is the only common format between Godot and Bullet that can be used for decent collision.
// (Int16 would be nice too but we still don't have it)
// We could convert here automatically but it's better to not be intrusive and let the caller do it if necessary.
ERR_FAIL_COND(l_image->get_format() != Image::FORMAT_RF);
PackedByteArray im_data = l_image->get_data();
l_heights.resize(l_image->get_width() * l_image->get_height());
real_t *w = l_heights.ptrw();
const uint8_t *r = im_data.ptr();
float *rp = (float *)r;
// At this point, `rp` could be used directly for Bullet, but I don't know how safe it would be.
for (int i = 0; i < l_heights.size(); ++i) {
w[i] = rp[i];
}
} else {
ERR_FAIL_MSG("Expected PackedFloat32Array or float Image.");
}
ERR_FAIL_COND(l_width <= 0);
ERR_FAIL_COND(l_depth <= 0);
ERR_FAIL_COND(l_heights.size() != (l_width * l_depth));
// Compute min and max heights if not specified.
if (!d.has("min_height") && !d.has("max_height")) {
const real_t *r = l_heights.ptr();
int heights_size = l_heights.size();
for (int i = 0; i < heights_size; ++i) {
real_t h = r[i];
if (h < l_min_height) {
l_min_height = h;
} else if (h > l_max_height) {
l_max_height = h;
}
}
}
setup(l_heights, l_width, l_depth, l_min_height, l_max_height);
}
Variant HeightMapShapeBullet::get_data() const {
ERR_FAIL_V(Variant());
}
PhysicsServer3D::ShapeType HeightMapShapeBullet::get_type() const {
return PhysicsServer3D::SHAPE_HEIGHTMAP;
}
void HeightMapShapeBullet::setup(Vector<real_t> &p_heights, int p_width, int p_depth, real_t p_min_height, real_t p_max_height) {
// TODO cell size must be tweaked using localScaling, which is a shared property for all Bullet shapes
// If this array is resized outside of here, it should be preserved due to CoW
heights = p_heights;
width = p_width;
depth = p_depth;
min_height = p_min_height;
max_height = p_max_height;
notifyShapeChanged();
}
btCollisionShape *HeightMapShapeBullet::create_bt_shape(const btVector3 &p_implicit_scale, real_t p_extra_edge) {
btCollisionShape *cs(ShapeBullet::create_shape_height_field(heights, width, depth, min_height, max_height));
cs->setLocalScaling(p_implicit_scale);
prepare(cs);
return cs;
}
/* Ray shape */
RayShapeBullet::RayShapeBullet() :
ShapeBullet() {}
void RayShapeBullet::set_data(const Variant &p_data) {
Dictionary d = p_data;
setup(d["length"], d["slips_on_slope"]);
}
Variant RayShapeBullet::get_data() const {
Dictionary d;
d["length"] = length;
d["slips_on_slope"] = slips_on_slope;
return d;
}
PhysicsServer3D::ShapeType RayShapeBullet::get_type() const {
return PhysicsServer3D::SHAPE_RAY;
}
void RayShapeBullet::setup(real_t p_length, bool p_slips_on_slope) {
length = p_length;
slips_on_slope = p_slips_on_slope;
notifyShapeChanged();
}
btCollisionShape *RayShapeBullet::create_bt_shape(const btVector3 &p_implicit_scale, real_t p_extra_edge) {
return prepare(ShapeBullet::create_shape_ray(length * p_implicit_scale[1] + p_extra_edge, slips_on_slope));
}
|