shape_bullet.cpp 19 KB

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