shape_bullet.cpp 18 KB

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