shape_bullet.cpp 18 KB

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