shape_bullet.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  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. 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. 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. return heightfield;
  131. }
  132. btRayShape *ShapeBullet::create_shape_ray(real_t p_length, bool p_slips_on_slope) {
  133. btRayShape *r(bulletnew(btRayShape(p_length)));
  134. r->setSlipsOnSlope(p_slips_on_slope);
  135. return r;
  136. }
  137. /* PLANE */
  138. PlaneShapeBullet::PlaneShapeBullet() :
  139. ShapeBullet() {}
  140. void PlaneShapeBullet::set_data(const Variant &p_data) {
  141. setup(p_data);
  142. }
  143. Variant PlaneShapeBullet::get_data() const {
  144. return plane;
  145. }
  146. PhysicsServer3D::ShapeType PlaneShapeBullet::get_type() const {
  147. return PhysicsServer3D::SHAPE_PLANE;
  148. }
  149. void PlaneShapeBullet::setup(const Plane &p_plane) {
  150. plane = p_plane;
  151. notifyShapeChanged();
  152. }
  153. btCollisionShape *PlaneShapeBullet::create_bt_shape(const btVector3 &p_implicit_scale, real_t p_extra_edge) {
  154. btVector3 btPlaneNormal;
  155. G_TO_B(plane.normal, btPlaneNormal);
  156. return prepare(PlaneShapeBullet::create_shape_plane(btPlaneNormal, plane.distance));
  157. }
  158. /* Sphere */
  159. SphereShapeBullet::SphereShapeBullet() :
  160. ShapeBullet() {}
  161. void SphereShapeBullet::set_data(const Variant &p_data) {
  162. setup(p_data);
  163. }
  164. Variant SphereShapeBullet::get_data() const {
  165. return radius;
  166. }
  167. PhysicsServer3D::ShapeType SphereShapeBullet::get_type() const {
  168. return PhysicsServer3D::SHAPE_SPHERE;
  169. }
  170. void SphereShapeBullet::setup(real_t p_radius) {
  171. radius = p_radius;
  172. notifyShapeChanged();
  173. }
  174. btCollisionShape *SphereShapeBullet::create_bt_shape(const btVector3 &p_implicit_scale, real_t p_extra_edge) {
  175. return prepare(ShapeBullet::create_shape_sphere(radius * p_implicit_scale[0] + p_extra_edge));
  176. }
  177. /* Box */
  178. BoxShapeBullet::BoxShapeBullet() :
  179. ShapeBullet() {}
  180. void BoxShapeBullet::set_data(const Variant &p_data) {
  181. setup(p_data);
  182. }
  183. Variant BoxShapeBullet::get_data() const {
  184. Vector3 g_half_extents;
  185. B_TO_G(half_extents, g_half_extents);
  186. return g_half_extents;
  187. }
  188. PhysicsServer3D::ShapeType BoxShapeBullet::get_type() const {
  189. return PhysicsServer3D::SHAPE_BOX;
  190. }
  191. void BoxShapeBullet::setup(const Vector3 &p_half_extents) {
  192. G_TO_B(p_half_extents, half_extents);
  193. notifyShapeChanged();
  194. }
  195. btCollisionShape *BoxShapeBullet::create_bt_shape(const btVector3 &p_implicit_scale, real_t p_extra_edge) {
  196. return prepare(ShapeBullet::create_shape_box((half_extents * p_implicit_scale) + btVector3(p_extra_edge, p_extra_edge, p_extra_edge)));
  197. }
  198. /* Capsule */
  199. CapsuleShapeBullet::CapsuleShapeBullet() :
  200. ShapeBullet() {}
  201. void CapsuleShapeBullet::set_data(const Variant &p_data) {
  202. Dictionary d = p_data;
  203. ERR_FAIL_COND(!d.has("radius"));
  204. ERR_FAIL_COND(!d.has("height"));
  205. setup(d["height"], d["radius"]);
  206. }
  207. Variant CapsuleShapeBullet::get_data() const {
  208. Dictionary d;
  209. d["radius"] = radius;
  210. d["height"] = height;
  211. return d;
  212. }
  213. PhysicsServer3D::ShapeType CapsuleShapeBullet::get_type() const {
  214. return PhysicsServer3D::SHAPE_CAPSULE;
  215. }
  216. void CapsuleShapeBullet::setup(real_t p_height, real_t p_radius) {
  217. radius = p_radius;
  218. height = p_height;
  219. notifyShapeChanged();
  220. }
  221. btCollisionShape *CapsuleShapeBullet::create_bt_shape(const btVector3 &p_implicit_scale, real_t p_extra_edge) {
  222. return prepare(ShapeBullet::create_shape_capsule(radius * p_implicit_scale[0] + p_extra_edge, height * p_implicit_scale[1] + p_extra_edge));
  223. }
  224. /* Cylinder */
  225. CylinderShapeBullet::CylinderShapeBullet() :
  226. ShapeBullet() {}
  227. void CylinderShapeBullet::set_data(const Variant &p_data) {
  228. Dictionary d = p_data;
  229. ERR_FAIL_COND(!d.has("radius"));
  230. ERR_FAIL_COND(!d.has("height"));
  231. setup(d["height"], d["radius"]);
  232. }
  233. Variant CylinderShapeBullet::get_data() const {
  234. Dictionary d;
  235. d["radius"] = radius;
  236. d["height"] = height;
  237. return d;
  238. }
  239. PhysicsServer3D::ShapeType CylinderShapeBullet::get_type() const {
  240. return PhysicsServer3D::SHAPE_CYLINDER;
  241. }
  242. void CylinderShapeBullet::setup(real_t p_height, real_t p_radius) {
  243. radius = p_radius;
  244. height = p_height;
  245. notifyShapeChanged();
  246. }
  247. btCollisionShape *CylinderShapeBullet::create_bt_shape(const btVector3 &p_implicit_scale, real_t p_margin) {
  248. return prepare(ShapeBullet::create_shape_cylinder(radius * p_implicit_scale[0] + p_margin, height * p_implicit_scale[1] + p_margin));
  249. }
  250. /* Convex polygon */
  251. ConvexPolygonShapeBullet::ConvexPolygonShapeBullet() :
  252. ShapeBullet() {}
  253. void ConvexPolygonShapeBullet::set_data(const Variant &p_data) {
  254. setup(p_data);
  255. }
  256. void ConvexPolygonShapeBullet::get_vertices(Vector<Vector3> &out_vertices) {
  257. const int n_of_vertices = vertices.size();
  258. out_vertices.resize(n_of_vertices);
  259. for (int i = n_of_vertices - 1; 0 <= i; --i) {
  260. B_TO_G(vertices[i], out_vertices.write[i]);
  261. }
  262. }
  263. Variant ConvexPolygonShapeBullet::get_data() const {
  264. ConvexPolygonShapeBullet *variable_self = const_cast<ConvexPolygonShapeBullet *>(this);
  265. Vector<Vector3> out_vertices;
  266. variable_self->get_vertices(out_vertices);
  267. return out_vertices;
  268. }
  269. PhysicsServer3D::ShapeType ConvexPolygonShapeBullet::get_type() const {
  270. return PhysicsServer3D::SHAPE_CONVEX_POLYGON;
  271. }
  272. void ConvexPolygonShapeBullet::setup(const Vector<Vector3> &p_vertices) {
  273. // Make a copy of vertices
  274. const int n_of_vertices = p_vertices.size();
  275. vertices.resize(n_of_vertices);
  276. for (int i = n_of_vertices - 1; 0 <= i; --i) {
  277. G_TO_B(p_vertices[i], vertices[i]);
  278. }
  279. notifyShapeChanged();
  280. }
  281. btCollisionShape *ConvexPolygonShapeBullet::create_bt_shape(const btVector3 &p_implicit_scale, real_t p_extra_edge) {
  282. if (!vertices.size())
  283. // This is necessary since 0 vertices
  284. return prepare(ShapeBullet::create_shape_empty());
  285. btCollisionShape *cs(ShapeBullet::create_shape_convex(vertices));
  286. cs->setLocalScaling(p_implicit_scale);
  287. prepare(cs);
  288. return cs;
  289. }
  290. /* Concave polygon */
  291. ConcavePolygonShapeBullet::ConcavePolygonShapeBullet() :
  292. ShapeBullet(),
  293. meshShape(nullptr) {}
  294. ConcavePolygonShapeBullet::~ConcavePolygonShapeBullet() {
  295. if (meshShape) {
  296. delete meshShape->getMeshInterface();
  297. delete meshShape->getTriangleInfoMap();
  298. bulletdelete(meshShape);
  299. }
  300. faces = Vector<Vector3>();
  301. }
  302. void ConcavePolygonShapeBullet::set_data(const Variant &p_data) {
  303. setup(p_data);
  304. }
  305. Variant ConcavePolygonShapeBullet::get_data() const {
  306. return faces;
  307. }
  308. PhysicsServer3D::ShapeType ConcavePolygonShapeBullet::get_type() const {
  309. return PhysicsServer3D::SHAPE_CONCAVE_POLYGON;
  310. }
  311. void ConcavePolygonShapeBullet::setup(Vector<Vector3> p_faces) {
  312. faces = p_faces;
  313. if (meshShape) {
  314. /// Clear previous created shape
  315. delete meshShape->getMeshInterface();
  316. delete meshShape->getTriangleInfoMap();
  317. bulletdelete(meshShape);
  318. }
  319. int src_face_count = faces.size();
  320. if (0 < src_face_count) {
  321. // It counts the faces and assert the array contains the correct number of vertices.
  322. ERR_FAIL_COND(src_face_count % 3);
  323. btTriangleMesh *shapeInterface = bulletnew(btTriangleMesh);
  324. src_face_count /= 3;
  325. const Vector3 *r = p_faces.ptr();
  326. const Vector3 *facesr = r;
  327. btVector3 supVec_0;
  328. btVector3 supVec_1;
  329. btVector3 supVec_2;
  330. for (int i = 0; i < src_face_count; ++i) {
  331. G_TO_B(facesr[i * 3 + 0], supVec_0);
  332. G_TO_B(facesr[i * 3 + 1], supVec_1);
  333. G_TO_B(facesr[i * 3 + 2], supVec_2);
  334. // Inverted from standard godot otherwise btGenerateInternalEdgeInfo generates wrong edge info
  335. shapeInterface->addTriangle(supVec_2, supVec_1, supVec_0);
  336. }
  337. const bool useQuantizedAabbCompression = true;
  338. meshShape = bulletnew(btBvhTriangleMeshShape(shapeInterface, useQuantizedAabbCompression));
  339. if (GLOBAL_DEF("physics/3d/smooth_trimesh_collision", false)) {
  340. btTriangleInfoMap *triangleInfoMap = new btTriangleInfoMap();
  341. btGenerateInternalEdgeInfo(meshShape, triangleInfoMap);
  342. }
  343. } else {
  344. meshShape = nullptr;
  345. ERR_PRINT("The faces count are 0, the mesh shape cannot be created");
  346. }
  347. notifyShapeChanged();
  348. }
  349. btCollisionShape *ConcavePolygonShapeBullet::create_bt_shape(const btVector3 &p_implicit_scale, real_t p_extra_edge) {
  350. btCollisionShape *cs = ShapeBullet::create_shape_concave(meshShape);
  351. if (!cs)
  352. // This is necessary since if 0 faces the creation of concave return null
  353. cs = ShapeBullet::create_shape_empty();
  354. cs->setLocalScaling(p_implicit_scale);
  355. prepare(cs);
  356. cs->setMargin(0);
  357. return cs;
  358. }
  359. /* Height map shape */
  360. HeightMapShapeBullet::HeightMapShapeBullet() :
  361. ShapeBullet() {}
  362. void HeightMapShapeBullet::set_data(const Variant &p_data) {
  363. ERR_FAIL_COND(p_data.get_type() != Variant::DICTIONARY);
  364. Dictionary d = p_data;
  365. ERR_FAIL_COND(!d.has("width"));
  366. ERR_FAIL_COND(!d.has("depth"));
  367. ERR_FAIL_COND(!d.has("heights"));
  368. real_t l_min_height = 0.0;
  369. real_t l_max_height = 0.0;
  370. // If specified, min and max height will be used as precomputed values
  371. if (d.has("min_height"))
  372. l_min_height = d["min_height"];
  373. if (d.has("max_height"))
  374. l_max_height = d["max_height"];
  375. ERR_FAIL_COND(l_min_height > l_max_height);
  376. int l_width = d["width"];
  377. int l_depth = d["depth"];
  378. // TODO This code will need adjustments if real_t is set to `double`,
  379. // because that precision is unnecessary for a heightmap and Bullet doesn't support it...
  380. Vector<real_t> l_heights;
  381. Variant l_heights_v = d["heights"];
  382. if (l_heights_v.get_type() == Variant::PACKED_FLOAT32_ARRAY) {
  383. // Ready-to-use heights can be passed
  384. l_heights = l_heights_v;
  385. } else if (l_heights_v.get_type() == Variant::OBJECT) {
  386. // If an image is passed, we have to convert it to a format Bullet supports.
  387. // this would be expensive to do with a script, so it's nice to have it here.
  388. Ref<Image> l_image = l_heights_v;
  389. ERR_FAIL_COND(l_image.is_null());
  390. // Float is the only common format between Godot and Bullet that can be used for decent collision.
  391. // (Int16 would be nice too but we still don't have it)
  392. // We could convert here automatically but it's better to not be intrusive and let the caller do it if necessary.
  393. ERR_FAIL_COND(l_image->get_format() != Image::FORMAT_RF);
  394. PackedByteArray im_data = l_image->get_data();
  395. l_heights.resize(l_image->get_width() * l_image->get_height());
  396. real_t *w = l_heights.ptrw();
  397. const uint8_t *r = im_data.ptr();
  398. float *rp = (float *)r;
  399. // At this point, `rp` could be used directly for Bullet, but I don't know how safe it would be.
  400. for (int i = 0; i < l_heights.size(); ++i) {
  401. w[i] = rp[i];
  402. }
  403. } else {
  404. ERR_FAIL_MSG("Expected PackedFloat32Array or float Image.");
  405. }
  406. ERR_FAIL_COND(l_width <= 0);
  407. ERR_FAIL_COND(l_depth <= 0);
  408. ERR_FAIL_COND(l_heights.size() != (l_width * l_depth));
  409. // Compute min and max heights if not specified.
  410. if (!d.has("min_height") && !d.has("max_height")) {
  411. const real_t *r = l_heights.ptr();
  412. int heights_size = l_heights.size();
  413. for (int i = 0; i < heights_size; ++i) {
  414. real_t h = r[i];
  415. if (h < l_min_height) {
  416. l_min_height = h;
  417. } else if (h > l_max_height) {
  418. l_max_height = h;
  419. }
  420. }
  421. }
  422. setup(l_heights, l_width, l_depth, l_min_height, l_max_height);
  423. }
  424. Variant HeightMapShapeBullet::get_data() const {
  425. ERR_FAIL_V(Variant());
  426. }
  427. PhysicsServer3D::ShapeType HeightMapShapeBullet::get_type() const {
  428. return PhysicsServer3D::SHAPE_HEIGHTMAP;
  429. }
  430. void HeightMapShapeBullet::setup(Vector<real_t> &p_heights, int p_width, int p_depth, real_t p_min_height, real_t p_max_height) {
  431. // TODO cell size must be tweaked using localScaling, which is a shared property for all Bullet shapes
  432. // If this array is resized outside of here, it should be preserved due to CoW
  433. heights = p_heights;
  434. width = p_width;
  435. depth = p_depth;
  436. min_height = p_min_height;
  437. max_height = p_max_height;
  438. notifyShapeChanged();
  439. }
  440. btCollisionShape *HeightMapShapeBullet::create_bt_shape(const btVector3 &p_implicit_scale, real_t p_extra_edge) {
  441. btCollisionShape *cs(ShapeBullet::create_shape_height_field(heights, width, depth, min_height, max_height));
  442. cs->setLocalScaling(p_implicit_scale);
  443. prepare(cs);
  444. return cs;
  445. }
  446. /* Ray shape */
  447. RayShapeBullet::RayShapeBullet() :
  448. ShapeBullet(),
  449. length(1),
  450. slips_on_slope(false) {}
  451. void RayShapeBullet::set_data(const Variant &p_data) {
  452. Dictionary d = p_data;
  453. setup(d["length"], d["slips_on_slope"]);
  454. }
  455. Variant RayShapeBullet::get_data() const {
  456. Dictionary d;
  457. d["length"] = length;
  458. d["slips_on_slope"] = slips_on_slope;
  459. return d;
  460. }
  461. PhysicsServer3D::ShapeType RayShapeBullet::get_type() const {
  462. return PhysicsServer3D::SHAPE_RAY;
  463. }
  464. void RayShapeBullet::setup(real_t p_length, bool p_slips_on_slope) {
  465. length = p_length;
  466. slips_on_slope = p_slips_on_slope;
  467. notifyShapeChanged();
  468. }
  469. btCollisionShape *RayShapeBullet::create_bt_shape(const btVector3 &p_implicit_scale, real_t p_extra_edge) {
  470. return prepare(ShapeBullet::create_shape_ray(length * p_implicit_scale[1] + p_extra_edge, slips_on_slope));
  471. }