shape_bullet.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. /*************************************************************************/
  2. /* shape_bullet.cpp */
  3. /* Author: AndreaCatania */
  4. /*************************************************************************/
  5. /* This file is part of: */
  6. /* GODOT ENGINE */
  7. /* http://www.godotengine.org */
  8. /*************************************************************************/
  9. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  10. /* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */
  11. /* */
  12. /* Permission is hereby granted, free of charge, to any person obtaining */
  13. /* a copy of this software and associated documentation files (the */
  14. /* "Software"), to deal in the Software without restriction, including */
  15. /* without limitation the rights to use, copy, modify, merge, publish, */
  16. /* distribute, sublicense, and/or sell copies of the Software, and to */
  17. /* permit persons to whom the Software is furnished to do so, subject to */
  18. /* the following conditions: */
  19. /* */
  20. /* The above copyright notice and this permission notice shall be */
  21. /* included in all copies or substantial portions of the Software. */
  22. /* */
  23. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  24. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  25. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  26. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  27. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  28. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  29. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  30. /*************************************************************************/
  31. #include "shape_bullet.h"
  32. #include "BulletCollision/CollisionShapes/btConvexPointCloudShape.h"
  33. #include "BulletCollision/CollisionShapes/btHeightfieldTerrainShape.h"
  34. #include "btBulletCollisionCommon.h"
  35. #include "btRayShape.h"
  36. #include "bullet_physics_server.h"
  37. #include "bullet_types_converter.h"
  38. #include "bullet_utilities.h"
  39. #include "shape_owner_bullet.h"
  40. ShapeBullet::ShapeBullet() {}
  41. ShapeBullet::~ShapeBullet() {}
  42. btCollisionShape *ShapeBullet::prepare(btCollisionShape *p_btShape) const {
  43. p_btShape->setUserPointer(const_cast<ShapeBullet *>(this));
  44. return p_btShape;
  45. }
  46. void ShapeBullet::notifyShapeChanged() {
  47. for (Map<ShapeOwnerBullet *, int>::Element *E = owners.front(); E; E = E->next()) {
  48. static_cast<ShapeOwnerBullet *>(E->key())->on_shape_changed(this);
  49. }
  50. }
  51. void ShapeBullet::add_owner(ShapeOwnerBullet *p_owner) {
  52. Map<ShapeOwnerBullet *, int>::Element *E = owners.find(p_owner);
  53. if (E) {
  54. E->get()++;
  55. } else {
  56. owners[p_owner] = 1; // add new owner
  57. }
  58. }
  59. void ShapeBullet::remove_owner(ShapeOwnerBullet *p_owner, bool p_permanentlyFromThisBody) {
  60. Map<ShapeOwnerBullet *, int>::Element *E = owners.find(p_owner);
  61. ERR_FAIL_COND(!E);
  62. E->get()--;
  63. if (p_permanentlyFromThisBody || 0 >= E->get()) {
  64. owners.erase(E);
  65. }
  66. }
  67. bool ShapeBullet::is_owner(ShapeOwnerBullet *p_owner) const {
  68. return owners.has(p_owner);
  69. }
  70. const Map<ShapeOwnerBullet *, int> &ShapeBullet::get_owners() const {
  71. return owners;
  72. }
  73. btEmptyShape *ShapeBullet::create_shape_empty() {
  74. return bulletnew(btEmptyShape);
  75. }
  76. btStaticPlaneShape *ShapeBullet::create_shape_plane(const btVector3 &planeNormal, btScalar planeConstant) {
  77. return bulletnew(btStaticPlaneShape(planeNormal, planeConstant));
  78. }
  79. btSphereShape *ShapeBullet::create_shape_sphere(btScalar radius) {
  80. return bulletnew(btSphereShape(radius));
  81. }
  82. btBoxShape *ShapeBullet::create_shape_box(const btVector3 &boxHalfExtents) {
  83. return bulletnew(btBoxShape(boxHalfExtents));
  84. }
  85. btCapsuleShapeZ *ShapeBullet::create_shape_capsule(btScalar radius, btScalar height) {
  86. return bulletnew(btCapsuleShapeZ(radius, height));
  87. }
  88. btConvexPointCloudShape *ShapeBullet::create_shape_convex(btAlignedObjectArray<btVector3> &p_vertices, const btVector3 &p_local_scaling) {
  89. return bulletnew(btConvexPointCloudShape(&p_vertices[0], p_vertices.size(), p_local_scaling));
  90. }
  91. btScaledBvhTriangleMeshShape *ShapeBullet::create_shape_concave(btBvhTriangleMeshShape *p_mesh_shape, const btVector3 &p_local_scaling) {
  92. if (p_mesh_shape) {
  93. return bulletnew(btScaledBvhTriangleMeshShape(p_mesh_shape, p_local_scaling));
  94. } else {
  95. return NULL;
  96. }
  97. }
  98. btHeightfieldTerrainShape *ShapeBullet::create_shape_height_field(PoolVector<real_t> &p_heights, int p_width, int p_depth, real_t p_cell_size) {
  99. const btScalar ignoredHeightScale(1);
  100. const btScalar fieldHeight(500); // Meters
  101. const int YAxis = 1; // 0=X, 1=Y, 2=Z
  102. const bool flipQuadEdges = false;
  103. const void *heightsPtr = p_heights.read().ptr();
  104. return bulletnew(btHeightfieldTerrainShape(p_width, p_depth, heightsPtr, ignoredHeightScale, -fieldHeight, fieldHeight, YAxis, PHY_FLOAT, flipQuadEdges));
  105. }
  106. btRayShape *ShapeBullet::create_shape_ray(real_t p_length) {
  107. return bulletnew(btRayShape(p_length));
  108. }
  109. /* PLANE */
  110. PlaneShapeBullet::PlaneShapeBullet()
  111. : ShapeBullet() {}
  112. void PlaneShapeBullet::set_data(const Variant &p_data) {
  113. setup(p_data);
  114. }
  115. Variant PlaneShapeBullet::get_data() const {
  116. return plane;
  117. }
  118. PhysicsServer::ShapeType PlaneShapeBullet::get_type() const {
  119. return PhysicsServer::SHAPE_PLANE;
  120. }
  121. void PlaneShapeBullet::setup(const Plane &p_plane) {
  122. plane = p_plane;
  123. notifyShapeChanged();
  124. }
  125. btCollisionShape *PlaneShapeBullet::create_bt_shape() {
  126. btVector3 btPlaneNormal;
  127. G_TO_B(plane.normal, btPlaneNormal);
  128. return prepare(PlaneShapeBullet::create_shape_plane(btPlaneNormal, plane.d));
  129. }
  130. /* Sphere */
  131. SphereShapeBullet::SphereShapeBullet()
  132. : ShapeBullet() {}
  133. void SphereShapeBullet::set_data(const Variant &p_data) {
  134. setup(p_data);
  135. }
  136. Variant SphereShapeBullet::get_data() const {
  137. return radius;
  138. }
  139. PhysicsServer::ShapeType SphereShapeBullet::get_type() const {
  140. return PhysicsServer::SHAPE_SPHERE;
  141. }
  142. void SphereShapeBullet::setup(real_t p_radius) {
  143. radius = p_radius;
  144. notifyShapeChanged();
  145. }
  146. btCollisionShape *SphereShapeBullet::create_bt_shape() {
  147. return prepare(ShapeBullet::create_shape_sphere(radius));
  148. }
  149. /* Box */
  150. BoxShapeBullet::BoxShapeBullet()
  151. : ShapeBullet() {}
  152. void BoxShapeBullet::set_data(const Variant &p_data) {
  153. setup(p_data);
  154. }
  155. Variant BoxShapeBullet::get_data() const {
  156. Vector3 g_half_extents;
  157. B_TO_G(half_extents, g_half_extents);
  158. return g_half_extents;
  159. }
  160. PhysicsServer::ShapeType BoxShapeBullet::get_type() const {
  161. return PhysicsServer::SHAPE_BOX;
  162. }
  163. void BoxShapeBullet::setup(const Vector3 &p_half_extents) {
  164. G_TO_B(p_half_extents, half_extents);
  165. notifyShapeChanged();
  166. }
  167. btCollisionShape *BoxShapeBullet::create_bt_shape() {
  168. return prepare(ShapeBullet::create_shape_box(half_extents));
  169. }
  170. /* Capsule */
  171. CapsuleShapeBullet::CapsuleShapeBullet()
  172. : ShapeBullet() {}
  173. void CapsuleShapeBullet::set_data(const Variant &p_data) {
  174. Dictionary d = p_data;
  175. ERR_FAIL_COND(!d.has("radius"));
  176. ERR_FAIL_COND(!d.has("height"));
  177. setup(d["height"], d["radius"]);
  178. }
  179. Variant CapsuleShapeBullet::get_data() const {
  180. Dictionary d;
  181. d["radius"] = radius;
  182. d["height"] = height;
  183. return d;
  184. }
  185. PhysicsServer::ShapeType CapsuleShapeBullet::get_type() const {
  186. return PhysicsServer::SHAPE_CAPSULE;
  187. }
  188. void CapsuleShapeBullet::setup(real_t p_height, real_t p_radius) {
  189. radius = p_radius;
  190. height = p_height;
  191. notifyShapeChanged();
  192. }
  193. btCollisionShape *CapsuleShapeBullet::create_bt_shape() {
  194. return prepare(ShapeBullet::create_shape_capsule(radius, height));
  195. }
  196. /* Convex polygon */
  197. ConvexPolygonShapeBullet::ConvexPolygonShapeBullet()
  198. : ShapeBullet() {}
  199. void ConvexPolygonShapeBullet::set_data(const Variant &p_data) {
  200. setup(p_data);
  201. }
  202. void ConvexPolygonShapeBullet::get_vertices(Vector<Vector3> &out_vertices) {
  203. const int n_of_vertices = vertices.size();
  204. out_vertices.resize(n_of_vertices);
  205. for (int i = n_of_vertices - 1; 0 <= i; --i) {
  206. B_TO_G(vertices[i], out_vertices[i]);
  207. }
  208. }
  209. Variant ConvexPolygonShapeBullet::get_data() const {
  210. ConvexPolygonShapeBullet *variable_self = const_cast<ConvexPolygonShapeBullet *>(this);
  211. Vector<Vector3> out_vertices;
  212. variable_self->get_vertices(out_vertices);
  213. return out_vertices;
  214. }
  215. PhysicsServer::ShapeType ConvexPolygonShapeBullet::get_type() const {
  216. return PhysicsServer::SHAPE_CONVEX_POLYGON;
  217. }
  218. void ConvexPolygonShapeBullet::setup(const Vector<Vector3> &p_vertices) {
  219. // Make a copy of verticies
  220. const int n_of_vertices = p_vertices.size();
  221. vertices.resize(n_of_vertices);
  222. for (int i = n_of_vertices - 1; 0 <= i; --i) {
  223. G_TO_B(p_vertices[i], vertices[i]);
  224. }
  225. notifyShapeChanged();
  226. }
  227. btCollisionShape *ConvexPolygonShapeBullet::create_bt_shape() {
  228. return prepare(ShapeBullet::create_shape_convex(vertices));
  229. }
  230. /* Concave polygon */
  231. ConcavePolygonShapeBullet::ConcavePolygonShapeBullet()
  232. : ShapeBullet(), meshShape(NULL) {}
  233. ConcavePolygonShapeBullet::~ConcavePolygonShapeBullet() {
  234. if (meshShape) {
  235. delete meshShape->getMeshInterface();
  236. delete meshShape;
  237. }
  238. faces = PoolVector<Vector3>();
  239. }
  240. void ConcavePolygonShapeBullet::set_data(const Variant &p_data) {
  241. setup(p_data);
  242. }
  243. Variant ConcavePolygonShapeBullet::get_data() const {
  244. return faces;
  245. }
  246. PhysicsServer::ShapeType ConcavePolygonShapeBullet::get_type() const {
  247. return PhysicsServer::SHAPE_CONCAVE_POLYGON;
  248. }
  249. void ConcavePolygonShapeBullet::setup(PoolVector<Vector3> p_faces) {
  250. faces = p_faces;
  251. if (meshShape) {
  252. /// Clear previous created shape
  253. delete meshShape->getMeshInterface();
  254. bulletdelete(meshShape);
  255. }
  256. int src_face_count = faces.size();
  257. if (0 < src_face_count) {
  258. btTriangleMesh *shapeInterface = bulletnew(btTriangleMesh);
  259. // It counts the faces and assert the array contains the correct number of vertices.
  260. ERR_FAIL_COND(src_face_count % 3);
  261. src_face_count /= 3;
  262. PoolVector<Vector3>::Read r = p_faces.read();
  263. const Vector3 *facesr = r.ptr();
  264. btVector3 supVec_0;
  265. btVector3 supVec_1;
  266. btVector3 supVec_2;
  267. for (int i = 0; i < src_face_count; ++i) {
  268. G_TO_B(facesr[i * 3], supVec_0);
  269. G_TO_B(facesr[i * 3 + 1], supVec_1);
  270. G_TO_B(facesr[i * 3 + 2], supVec_2);
  271. shapeInterface->addTriangle(supVec_0, supVec_1, supVec_2);
  272. }
  273. const bool useQuantizedAabbCompression = true;
  274. meshShape = bulletnew(btBvhTriangleMeshShape(shapeInterface, useQuantizedAabbCompression));
  275. } else {
  276. meshShape = NULL;
  277. ERR_PRINT("The faces count are 0, the mesh shape cannot be created");
  278. }
  279. notifyShapeChanged();
  280. }
  281. btCollisionShape *ConcavePolygonShapeBullet::create_bt_shape() {
  282. btCollisionShape *cs = ShapeBullet::create_shape_concave(meshShape);
  283. if (!cs) {
  284. // This is necessary since if 0 faces the creation of concave return NULL
  285. cs = ShapeBullet::create_shape_empty();
  286. }
  287. return prepare(cs);
  288. }
  289. /* Height map shape */
  290. HeightMapShapeBullet::HeightMapShapeBullet()
  291. : ShapeBullet() {}
  292. void HeightMapShapeBullet::set_data(const Variant &p_data) {
  293. ERR_FAIL_COND(p_data.get_type() != Variant::DICTIONARY);
  294. Dictionary d = p_data;
  295. ERR_FAIL_COND(!d.has("width"));
  296. ERR_FAIL_COND(!d.has("depth"));
  297. ERR_FAIL_COND(!d.has("cell_size"));
  298. ERR_FAIL_COND(!d.has("heights"));
  299. int l_width = d["width"];
  300. int l_depth = d["depth"];
  301. real_t l_cell_size = d["cell_size"];
  302. PoolVector<real_t> l_heights = d["heights"];
  303. ERR_FAIL_COND(l_width <= 0);
  304. ERR_FAIL_COND(l_depth <= 0);
  305. ERR_FAIL_COND(l_cell_size <= CMP_EPSILON);
  306. ERR_FAIL_COND(l_heights.size() != (width * depth));
  307. setup(heights, width, depth, cell_size);
  308. }
  309. Variant HeightMapShapeBullet::get_data() const {
  310. ERR_FAIL_V(Variant());
  311. }
  312. PhysicsServer::ShapeType HeightMapShapeBullet::get_type() const {
  313. return PhysicsServer::SHAPE_HEIGHTMAP;
  314. }
  315. void HeightMapShapeBullet::setup(PoolVector<real_t> &p_heights, int p_width, int p_depth, real_t p_cell_size) {
  316. { // Copy
  317. const int heights_size = p_heights.size();
  318. heights.resize(heights_size);
  319. PoolVector<real_t>::Read p_heights_r = p_heights.read();
  320. PoolVector<real_t>::Write heights_w = heights.write();
  321. for (int i = heights_size - 1; 0 <= i; --i) {
  322. heights_w[i] = p_heights_r[i];
  323. }
  324. }
  325. width = p_width;
  326. depth = p_depth;
  327. cell_size = p_cell_size;
  328. notifyShapeChanged();
  329. }
  330. btCollisionShape *HeightMapShapeBullet::create_bt_shape() {
  331. return prepare(ShapeBullet::create_shape_height_field(heights, width, depth, cell_size));
  332. }
  333. /* Ray shape */
  334. RayShapeBullet::RayShapeBullet()
  335. : ShapeBullet(), length(1) {}
  336. void RayShapeBullet::set_data(const Variant &p_data) {
  337. setup(p_data);
  338. }
  339. Variant RayShapeBullet::get_data() const {
  340. return length;
  341. }
  342. PhysicsServer::ShapeType RayShapeBullet::get_type() const {
  343. return PhysicsServer::SHAPE_RAY;
  344. }
  345. void RayShapeBullet::setup(real_t p_length) {
  346. length = p_length;
  347. notifyShapeChanged();
  348. }
  349. btCollisionShape *RayShapeBullet::create_bt_shape() {
  350. return prepare(ShapeBullet::create_shape_ray(length));
  351. }