shape_bullet.cpp 19 KB

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