bullet_physics_server.cpp 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384
  1. /*************************************************************************/
  2. /* bullet_physics_server.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2018 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 "bullet_physics_server.h"
  31. #include "bullet_utilities.h"
  32. #include "class_db.h"
  33. #include "cone_twist_joint_bullet.h"
  34. #include "core/error_macros.h"
  35. #include "core/ustring.h"
  36. #include "generic_6dof_joint_bullet.h"
  37. #include "hinge_joint_bullet.h"
  38. #include "pin_joint_bullet.h"
  39. #include "shape_bullet.h"
  40. #include "slider_joint_bullet.h"
  41. #include <LinearMath/btVector3.h>
  42. #include <assert.h>
  43. /**
  44. @author AndreaCatania
  45. */
  46. #define CreateThenReturnRID(owner, ridData) \
  47. RID rid = owner.make_rid(ridData); \
  48. ridData->set_self(rid); \
  49. ridData->_set_physics_server(this); \
  50. return rid;
  51. // <--------------- Joint creation asserts
  52. /// Assert the body is assigned to a space
  53. #define JointAssertSpace(body, bIndex, ret) \
  54. if (!body->get_space()) { \
  55. ERR_PRINTS("Before create a joint the Body" + String(bIndex) + " must be added to a space!"); \
  56. return ret; \
  57. }
  58. /// Assert the two bodies of joint are in the same space
  59. #define JointAssertSameSpace(bodyA, bodyB, ret) \
  60. if (bodyA->get_space() != bodyB->get_space()) { \
  61. ERR_PRINT("In order to create a joint the Body_A and Body_B must be in the same space!"); \
  62. return RID(); \
  63. }
  64. #define AddJointToSpace(body, joint) \
  65. body->get_space()->add_constraint(joint, joint->is_disabled_collisions_between_bodies());
  66. // <--------------- Joint creation asserts
  67. btEmptyShape *BulletPhysicsServer::emptyShape(ShapeBullet::create_shape_empty());
  68. btEmptyShape *BulletPhysicsServer::get_empty_shape() {
  69. return emptyShape;
  70. }
  71. void BulletPhysicsServer::_bind_methods() {
  72. //ClassDB::bind_method(D_METHOD("DoTest"), &BulletPhysicsServer::DoTest);
  73. }
  74. BulletPhysicsServer::BulletPhysicsServer() :
  75. PhysicsServer(),
  76. active(true),
  77. active_spaces_count(0) {}
  78. BulletPhysicsServer::~BulletPhysicsServer() {}
  79. RID BulletPhysicsServer::shape_create(ShapeType p_shape) {
  80. ShapeBullet *shape = NULL;
  81. switch (p_shape) {
  82. case SHAPE_PLANE: {
  83. shape = bulletnew(PlaneShapeBullet);
  84. } break;
  85. case SHAPE_SPHERE: {
  86. shape = bulletnew(SphereShapeBullet);
  87. } break;
  88. case SHAPE_BOX: {
  89. shape = bulletnew(BoxShapeBullet);
  90. } break;
  91. case SHAPE_CAPSULE: {
  92. shape = bulletnew(CapsuleShapeBullet);
  93. } break;
  94. case SHAPE_CONVEX_POLYGON: {
  95. shape = bulletnew(ConvexPolygonShapeBullet);
  96. } break;
  97. case SHAPE_CONCAVE_POLYGON: {
  98. shape = bulletnew(ConcavePolygonShapeBullet);
  99. } break;
  100. case SHAPE_HEIGHTMAP: {
  101. shape = bulletnew(HeightMapShapeBullet);
  102. } break;
  103. case SHAPE_RAY: {
  104. shape = bulletnew(RayShapeBullet);
  105. } break;
  106. case SHAPE_CUSTOM:
  107. default:
  108. ERR_FAIL_V(RID());
  109. break;
  110. }
  111. CreateThenReturnRID(shape_owner, shape)
  112. }
  113. void BulletPhysicsServer::shape_set_data(RID p_shape, const Variant &p_data) {
  114. ShapeBullet *shape = shape_owner.get(p_shape);
  115. ERR_FAIL_COND(!shape);
  116. shape->set_data(p_data);
  117. }
  118. void BulletPhysicsServer::shape_set_custom_solver_bias(RID p_shape, real_t p_bias) {
  119. //WARN_PRINT("Bias not supported by Bullet physics engine");
  120. }
  121. PhysicsServer::ShapeType BulletPhysicsServer::shape_get_type(RID p_shape) const {
  122. ShapeBullet *shape = shape_owner.get(p_shape);
  123. ERR_FAIL_COND_V(!shape, PhysicsServer::SHAPE_CUSTOM);
  124. return shape->get_type();
  125. }
  126. Variant BulletPhysicsServer::shape_get_data(RID p_shape) const {
  127. ShapeBullet *shape = shape_owner.get(p_shape);
  128. ERR_FAIL_COND_V(!shape, Variant());
  129. return shape->get_data();
  130. }
  131. real_t BulletPhysicsServer::shape_get_custom_solver_bias(RID p_shape) const {
  132. //WARN_PRINT("Bias not supported by Bullet physics engine");
  133. return 0.;
  134. }
  135. RID BulletPhysicsServer::space_create() {
  136. SpaceBullet *space = bulletnew(SpaceBullet(false));
  137. CreateThenReturnRID(space_owner, space);
  138. }
  139. void BulletPhysicsServer::space_set_active(RID p_space, bool p_active) {
  140. SpaceBullet *space = space_owner.get(p_space);
  141. ERR_FAIL_COND(!space);
  142. if (space_is_active(p_space) == p_active) {
  143. return;
  144. }
  145. if (p_active) {
  146. ++active_spaces_count;
  147. active_spaces.push_back(space);
  148. } else {
  149. --active_spaces_count;
  150. active_spaces.erase(space);
  151. }
  152. }
  153. bool BulletPhysicsServer::space_is_active(RID p_space) const {
  154. SpaceBullet *space = space_owner.get(p_space);
  155. ERR_FAIL_COND_V(!space, false);
  156. return -1 != active_spaces.find(space);
  157. }
  158. void BulletPhysicsServer::space_set_param(RID p_space, SpaceParameter p_param, real_t p_value) {
  159. SpaceBullet *space = space_owner.get(p_space);
  160. ERR_FAIL_COND(!space);
  161. space->set_param(p_param, p_value);
  162. }
  163. real_t BulletPhysicsServer::space_get_param(RID p_space, SpaceParameter p_param) const {
  164. SpaceBullet *space = space_owner.get(p_space);
  165. ERR_FAIL_COND_V(!space, 0);
  166. return space->get_param(p_param);
  167. }
  168. PhysicsDirectSpaceState *BulletPhysicsServer::space_get_direct_state(RID p_space) {
  169. SpaceBullet *space = space_owner.get(p_space);
  170. ERR_FAIL_COND_V(!space, NULL);
  171. return space->get_direct_state();
  172. }
  173. void BulletPhysicsServer::space_set_debug_contacts(RID p_space, int p_max_contacts) {
  174. SpaceBullet *space = space_owner.get(p_space);
  175. ERR_FAIL_COND(!space);
  176. space->set_debug_contacts(p_max_contacts);
  177. }
  178. Vector<Vector3> BulletPhysicsServer::space_get_contacts(RID p_space) const {
  179. SpaceBullet *space = space_owner.get(p_space);
  180. ERR_FAIL_COND_V(!space, Vector<Vector3>());
  181. return space->get_debug_contacts();
  182. }
  183. int BulletPhysicsServer::space_get_contact_count(RID p_space) const {
  184. SpaceBullet *space = space_owner.get(p_space);
  185. ERR_FAIL_COND_V(!space, 0);
  186. return space->get_debug_contact_count();
  187. }
  188. RID BulletPhysicsServer::area_create() {
  189. AreaBullet *area = bulletnew(AreaBullet);
  190. area->set_collision_layer(1);
  191. area->set_collision_mask(1);
  192. CreateThenReturnRID(area_owner, area)
  193. }
  194. void BulletPhysicsServer::area_set_space(RID p_area, RID p_space) {
  195. AreaBullet *area = area_owner.get(p_area);
  196. ERR_FAIL_COND(!area);
  197. SpaceBullet *space = NULL;
  198. if (p_space.is_valid()) {
  199. space = space_owner.get(p_space);
  200. ERR_FAIL_COND(!space);
  201. }
  202. area->set_space(space);
  203. }
  204. RID BulletPhysicsServer::area_get_space(RID p_area) const {
  205. AreaBullet *area = area_owner.get(p_area);
  206. return area->get_space()->get_self();
  207. }
  208. void BulletPhysicsServer::area_set_space_override_mode(RID p_area, AreaSpaceOverrideMode p_mode) {
  209. AreaBullet *area = area_owner.get(p_area);
  210. ERR_FAIL_COND(!area)
  211. area->set_spOv_mode(p_mode);
  212. }
  213. PhysicsServer::AreaSpaceOverrideMode BulletPhysicsServer::area_get_space_override_mode(RID p_area) const {
  214. AreaBullet *area = area_owner.get(p_area);
  215. ERR_FAIL_COND_V(!area, PhysicsServer::AREA_SPACE_OVERRIDE_DISABLED);
  216. return area->get_spOv_mode();
  217. }
  218. void BulletPhysicsServer::area_add_shape(RID p_area, RID p_shape, const Transform &p_transform) {
  219. AreaBullet *area = area_owner.get(p_area);
  220. ERR_FAIL_COND(!area);
  221. ShapeBullet *shape = shape_owner.get(p_shape);
  222. ERR_FAIL_COND(!shape);
  223. area->add_shape(shape, p_transform);
  224. }
  225. void BulletPhysicsServer::area_set_shape(RID p_area, int p_shape_idx, RID p_shape) {
  226. AreaBullet *area = area_owner.get(p_area);
  227. ERR_FAIL_COND(!area);
  228. ShapeBullet *shape = shape_owner.get(p_shape);
  229. ERR_FAIL_COND(!shape);
  230. area->set_shape(p_shape_idx, shape);
  231. }
  232. void BulletPhysicsServer::area_set_shape_transform(RID p_area, int p_shape_idx, const Transform &p_transform) {
  233. AreaBullet *area = area_owner.get(p_area);
  234. ERR_FAIL_COND(!area);
  235. area->set_shape_transform(p_shape_idx, p_transform);
  236. }
  237. int BulletPhysicsServer::area_get_shape_count(RID p_area) const {
  238. AreaBullet *area = area_owner.get(p_area);
  239. ERR_FAIL_COND_V(!area, 0);
  240. return area->get_shape_count();
  241. }
  242. RID BulletPhysicsServer::area_get_shape(RID p_area, int p_shape_idx) const {
  243. AreaBullet *area = area_owner.get(p_area);
  244. ERR_FAIL_COND_V(!area, RID());
  245. return area->get_shape(p_shape_idx)->get_self();
  246. }
  247. Transform BulletPhysicsServer::area_get_shape_transform(RID p_area, int p_shape_idx) const {
  248. AreaBullet *area = area_owner.get(p_area);
  249. ERR_FAIL_COND_V(!area, Transform());
  250. return area->get_shape_transform(p_shape_idx);
  251. }
  252. void BulletPhysicsServer::area_remove_shape(RID p_area, int p_shape_idx) {
  253. AreaBullet *area = area_owner.get(p_area);
  254. ERR_FAIL_COND(!area);
  255. return area->remove_shape(p_shape_idx);
  256. }
  257. void BulletPhysicsServer::area_clear_shapes(RID p_area) {
  258. AreaBullet *area = area_owner.get(p_area);
  259. ERR_FAIL_COND(!area);
  260. for (int i = area->get_shape_count(); 0 < i; --i)
  261. area->remove_shape(0);
  262. }
  263. void BulletPhysicsServer::area_set_shape_disabled(RID p_area, int p_shape_idx, bool p_disabled) {
  264. AreaBullet *area = area_owner.get(p_area);
  265. ERR_FAIL_COND(!area);
  266. area->set_shape_disabled(p_shape_idx, p_disabled);
  267. }
  268. void BulletPhysicsServer::area_attach_object_instance_id(RID p_area, ObjectID p_ID) {
  269. if (space_owner.owns(p_area)) {
  270. return;
  271. }
  272. AreaBullet *area = area_owner.get(p_area);
  273. ERR_FAIL_COND(!area);
  274. area->set_instance_id(p_ID);
  275. }
  276. ObjectID BulletPhysicsServer::area_get_object_instance_id(RID p_area) const {
  277. if (space_owner.owns(p_area)) {
  278. return 0;
  279. }
  280. AreaBullet *area = area_owner.get(p_area);
  281. ERR_FAIL_COND_V(!area, ObjectID());
  282. return area->get_instance_id();
  283. }
  284. void BulletPhysicsServer::area_set_param(RID p_area, AreaParameter p_param, const Variant &p_value) {
  285. if (space_owner.owns(p_area)) {
  286. SpaceBullet *space = space_owner.get(p_area);
  287. if (space) {
  288. space->set_param(p_param, p_value);
  289. }
  290. } else {
  291. AreaBullet *area = area_owner.get(p_area);
  292. ERR_FAIL_COND(!area);
  293. area->set_param(p_param, p_value);
  294. }
  295. }
  296. Variant BulletPhysicsServer::area_get_param(RID p_area, AreaParameter p_param) const {
  297. if (space_owner.owns(p_area)) {
  298. SpaceBullet *space = space_owner.get(p_area);
  299. return space->get_param(p_param);
  300. } else {
  301. AreaBullet *area = area_owner.get(p_area);
  302. ERR_FAIL_COND_V(!area, Variant());
  303. return area->get_param(p_param);
  304. }
  305. }
  306. void BulletPhysicsServer::area_set_transform(RID p_area, const Transform &p_transform) {
  307. AreaBullet *area = area_owner.get(p_area);
  308. ERR_FAIL_COND(!area);
  309. area->set_transform(p_transform);
  310. }
  311. Transform BulletPhysicsServer::area_get_transform(RID p_area) const {
  312. AreaBullet *area = area_owner.get(p_area);
  313. ERR_FAIL_COND_V(!area, Transform());
  314. return area->get_transform();
  315. }
  316. void BulletPhysicsServer::area_set_collision_mask(RID p_area, uint32_t p_mask) {
  317. AreaBullet *area = area_owner.get(p_area);
  318. ERR_FAIL_COND(!area);
  319. area->set_collision_mask(p_mask);
  320. }
  321. void BulletPhysicsServer::area_set_collision_layer(RID p_area, uint32_t p_layer) {
  322. AreaBullet *area = area_owner.get(p_area);
  323. ERR_FAIL_COND(!area);
  324. area->set_collision_layer(p_layer);
  325. }
  326. void BulletPhysicsServer::area_set_monitorable(RID p_area, bool p_monitorable) {
  327. AreaBullet *area = area_owner.get(p_area);
  328. ERR_FAIL_COND(!area);
  329. area->set_monitorable(p_monitorable);
  330. }
  331. void BulletPhysicsServer::area_set_monitor_callback(RID p_area, Object *p_receiver, const StringName &p_method) {
  332. AreaBullet *area = area_owner.get(p_area);
  333. ERR_FAIL_COND(!area);
  334. area->set_event_callback(CollisionObjectBullet::TYPE_RIGID_BODY, p_receiver ? p_receiver->get_instance_id() : 0, p_method);
  335. }
  336. void BulletPhysicsServer::area_set_area_monitor_callback(RID p_area, Object *p_receiver, const StringName &p_method) {
  337. AreaBullet *area = area_owner.get(p_area);
  338. ERR_FAIL_COND(!area);
  339. area->set_event_callback(CollisionObjectBullet::TYPE_AREA, p_receiver ? p_receiver->get_instance_id() : 0, p_method);
  340. }
  341. void BulletPhysicsServer::area_set_ray_pickable(RID p_area, bool p_enable) {
  342. AreaBullet *area = area_owner.get(p_area);
  343. ERR_FAIL_COND(!area);
  344. area->set_ray_pickable(p_enable);
  345. }
  346. bool BulletPhysicsServer::area_is_ray_pickable(RID p_area) const {
  347. AreaBullet *area = area_owner.get(p_area);
  348. ERR_FAIL_COND_V(!area, false);
  349. return area->is_ray_pickable();
  350. }
  351. RID BulletPhysicsServer::body_create(BodyMode p_mode, bool p_init_sleeping) {
  352. RigidBodyBullet *body = bulletnew(RigidBodyBullet);
  353. body->set_mode(p_mode);
  354. body->set_collision_layer(1);
  355. body->set_collision_mask(1);
  356. if (p_init_sleeping)
  357. body->set_state(BODY_STATE_SLEEPING, p_init_sleeping);
  358. CreateThenReturnRID(rigid_body_owner, body);
  359. }
  360. void BulletPhysicsServer::body_set_space(RID p_body, RID p_space) {
  361. RigidBodyBullet *body = rigid_body_owner.get(p_body);
  362. ERR_FAIL_COND(!body);
  363. SpaceBullet *space = NULL;
  364. if (p_space.is_valid()) {
  365. space = space_owner.get(p_space);
  366. ERR_FAIL_COND(!space);
  367. }
  368. if (body->get_space() == space)
  369. return; //pointles
  370. body->set_space(space);
  371. }
  372. RID BulletPhysicsServer::body_get_space(RID p_body) const {
  373. RigidBodyBullet *body = rigid_body_owner.get(p_body);
  374. ERR_FAIL_COND_V(!body, RID());
  375. SpaceBullet *space = body->get_space();
  376. if (!space)
  377. return RID();
  378. return space->get_self();
  379. }
  380. void BulletPhysicsServer::body_set_mode(RID p_body, PhysicsServer::BodyMode p_mode) {
  381. RigidBodyBullet *body = rigid_body_owner.get(p_body);
  382. ERR_FAIL_COND(!body);
  383. body->set_mode(p_mode);
  384. }
  385. PhysicsServer::BodyMode BulletPhysicsServer::body_get_mode(RID p_body) const {
  386. RigidBodyBullet *body = rigid_body_owner.get(p_body);
  387. ERR_FAIL_COND_V(!body, BODY_MODE_STATIC);
  388. return body->get_mode();
  389. }
  390. void BulletPhysicsServer::body_add_shape(RID p_body, RID p_shape, const Transform &p_transform) {
  391. RigidBodyBullet *body = rigid_body_owner.get(p_body);
  392. ERR_FAIL_COND(!body);
  393. ShapeBullet *shape = shape_owner.get(p_shape);
  394. ERR_FAIL_COND(!shape);
  395. body->add_shape(shape, p_transform);
  396. }
  397. void BulletPhysicsServer::body_set_shape(RID p_body, int p_shape_idx, RID p_shape) {
  398. RigidBodyBullet *body = rigid_body_owner.get(p_body);
  399. ERR_FAIL_COND(!body);
  400. ShapeBullet *shape = shape_owner.get(p_shape);
  401. ERR_FAIL_COND(!shape);
  402. body->set_shape(p_shape_idx, shape);
  403. }
  404. void BulletPhysicsServer::body_set_shape_transform(RID p_body, int p_shape_idx, const Transform &p_transform) {
  405. RigidBodyBullet *body = rigid_body_owner.get(p_body);
  406. ERR_FAIL_COND(!body);
  407. body->set_shape_transform(p_shape_idx, p_transform);
  408. }
  409. int BulletPhysicsServer::body_get_shape_count(RID p_body) const {
  410. RigidBodyBullet *body = rigid_body_owner.get(p_body);
  411. ERR_FAIL_COND_V(!body, 0);
  412. return body->get_shape_count();
  413. }
  414. RID BulletPhysicsServer::body_get_shape(RID p_body, int p_shape_idx) const {
  415. RigidBodyBullet *body = rigid_body_owner.get(p_body);
  416. ERR_FAIL_COND_V(!body, RID());
  417. ShapeBullet *shape = body->get_shape(p_shape_idx);
  418. ERR_FAIL_COND_V(!shape, RID());
  419. return shape->get_self();
  420. }
  421. Transform BulletPhysicsServer::body_get_shape_transform(RID p_body, int p_shape_idx) const {
  422. RigidBodyBullet *body = rigid_body_owner.get(p_body);
  423. ERR_FAIL_COND_V(!body, Transform());
  424. return body->get_shape_transform(p_shape_idx);
  425. }
  426. void BulletPhysicsServer::body_set_shape_disabled(RID p_body, int p_shape_idx, bool p_disabled) {
  427. RigidBodyBullet *body = rigid_body_owner.get(p_body);
  428. ERR_FAIL_COND(!body);
  429. body->set_shape_disabled(p_shape_idx, p_disabled);
  430. }
  431. void BulletPhysicsServer::body_remove_shape(RID p_body, int p_shape_idx) {
  432. RigidBodyBullet *body = rigid_body_owner.get(p_body);
  433. ERR_FAIL_COND(!body);
  434. body->remove_shape(p_shape_idx);
  435. }
  436. void BulletPhysicsServer::body_clear_shapes(RID p_body) {
  437. RigidBodyBullet *body = rigid_body_owner.get(p_body);
  438. ERR_FAIL_COND(!body);
  439. body->remove_all_shapes();
  440. }
  441. void BulletPhysicsServer::body_attach_object_instance_id(RID p_body, uint32_t p_ID) {
  442. CollisionObjectBullet *body = get_collisin_object(p_body);
  443. if (!body) {
  444. body = soft_body_owner.get(p_body);
  445. }
  446. ERR_FAIL_COND(!body);
  447. body->set_instance_id(p_ID);
  448. }
  449. uint32_t BulletPhysicsServer::body_get_object_instance_id(RID p_body) const {
  450. CollisionObjectBullet *body = get_collisin_object(p_body);
  451. ERR_FAIL_COND_V(!body, 0);
  452. return body->get_instance_id();
  453. }
  454. void BulletPhysicsServer::body_set_enable_continuous_collision_detection(RID p_body, bool p_enable) {
  455. RigidBodyBullet *body = rigid_body_owner.get(p_body);
  456. ERR_FAIL_COND(!body);
  457. body->set_continuous_collision_detection(p_enable);
  458. }
  459. bool BulletPhysicsServer::body_is_continuous_collision_detection_enabled(RID p_body) const {
  460. RigidBodyBullet *body = rigid_body_owner.get(p_body);
  461. ERR_FAIL_COND_V(!body, false);
  462. return body->is_continuous_collision_detection_enabled();
  463. }
  464. void BulletPhysicsServer::body_set_collision_layer(RID p_body, uint32_t p_layer) {
  465. RigidBodyBullet *body = rigid_body_owner.get(p_body);
  466. ERR_FAIL_COND(!body);
  467. body->set_collision_layer(p_layer);
  468. }
  469. uint32_t BulletPhysicsServer::body_get_collision_layer(RID p_body) const {
  470. const RigidBodyBullet *body = rigid_body_owner.get(p_body);
  471. ERR_FAIL_COND_V(!body, 0);
  472. return body->get_collision_layer();
  473. }
  474. void BulletPhysicsServer::body_set_collision_mask(RID p_body, uint32_t p_mask) {
  475. RigidBodyBullet *body = rigid_body_owner.get(p_body);
  476. ERR_FAIL_COND(!body);
  477. body->set_collision_mask(p_mask);
  478. }
  479. uint32_t BulletPhysicsServer::body_get_collision_mask(RID p_body) const {
  480. RigidBodyBullet *body = rigid_body_owner.get(p_body);
  481. ERR_FAIL_COND_V(!body, 0);
  482. return body->get_collision_mask();
  483. }
  484. void BulletPhysicsServer::body_set_user_flags(RID p_body, uint32_t p_flags) {
  485. WARN_PRINT("This function si not currently supported by bullet and Godot");
  486. }
  487. uint32_t BulletPhysicsServer::body_get_user_flags(RID p_body) const {
  488. WARN_PRINT("This function si not currently supported by bullet and Godot");
  489. return 0;
  490. }
  491. void BulletPhysicsServer::body_set_param(RID p_body, BodyParameter p_param, float p_value) {
  492. RigidBodyBullet *body = rigid_body_owner.get(p_body);
  493. ERR_FAIL_COND(!body);
  494. body->set_param(p_param, p_value);
  495. }
  496. float BulletPhysicsServer::body_get_param(RID p_body, BodyParameter p_param) const {
  497. RigidBodyBullet *body = rigid_body_owner.get(p_body);
  498. ERR_FAIL_COND_V(!body, 0);
  499. return body->get_param(p_param);
  500. }
  501. void BulletPhysicsServer::body_set_kinematic_safe_margin(RID p_body, real_t p_margin) {
  502. RigidBodyBullet *body = rigid_body_owner.get(p_body);
  503. ERR_FAIL_COND(!body);
  504. if (body->get_kinematic_utilities()) {
  505. body->get_kinematic_utilities()->setSafeMargin(p_margin);
  506. }
  507. }
  508. real_t BulletPhysicsServer::body_get_kinematic_safe_margin(RID p_body) const {
  509. RigidBodyBullet *body = rigid_body_owner.get(p_body);
  510. ERR_FAIL_COND_V(!body, 0);
  511. if (body->get_kinematic_utilities()) {
  512. return body->get_kinematic_utilities()->safe_margin;
  513. }
  514. return 0;
  515. }
  516. void BulletPhysicsServer::body_set_state(RID p_body, BodyState p_state, const Variant &p_variant) {
  517. RigidBodyBullet *body = rigid_body_owner.get(p_body);
  518. ERR_FAIL_COND(!body);
  519. body->set_state(p_state, p_variant);
  520. }
  521. Variant BulletPhysicsServer::body_get_state(RID p_body, BodyState p_state) const {
  522. RigidBodyBullet *body = rigid_body_owner.get(p_body);
  523. ERR_FAIL_COND_V(!body, Variant());
  524. return body->get_state(p_state);
  525. }
  526. void BulletPhysicsServer::body_set_applied_force(RID p_body, const Vector3 &p_force) {
  527. RigidBodyBullet *body = rigid_body_owner.get(p_body);
  528. ERR_FAIL_COND(!body);
  529. body->set_applied_force(p_force);
  530. }
  531. Vector3 BulletPhysicsServer::body_get_applied_force(RID p_body) const {
  532. RigidBodyBullet *body = rigid_body_owner.get(p_body);
  533. ERR_FAIL_COND_V(!body, Vector3());
  534. return body->get_applied_force();
  535. }
  536. void BulletPhysicsServer::body_set_applied_torque(RID p_body, const Vector3 &p_torque) {
  537. RigidBodyBullet *body = rigid_body_owner.get(p_body);
  538. ERR_FAIL_COND(!body);
  539. body->set_applied_torque(p_torque);
  540. }
  541. Vector3 BulletPhysicsServer::body_get_applied_torque(RID p_body) const {
  542. RigidBodyBullet *body = rigid_body_owner.get(p_body);
  543. ERR_FAIL_COND_V(!body, Vector3());
  544. return body->get_applied_torque();
  545. }
  546. void BulletPhysicsServer::body_apply_impulse(RID p_body, const Vector3 &p_pos, const Vector3 &p_impulse) {
  547. RigidBodyBullet *body = rigid_body_owner.get(p_body);
  548. ERR_FAIL_COND(!body);
  549. body->apply_impulse(p_pos, p_impulse);
  550. }
  551. void BulletPhysicsServer::body_apply_torque_impulse(RID p_body, const Vector3 &p_impulse) {
  552. RigidBodyBullet *body = rigid_body_owner.get(p_body);
  553. ERR_FAIL_COND(!body);
  554. body->apply_torque_impulse(p_impulse);
  555. }
  556. void BulletPhysicsServer::body_set_axis_velocity(RID p_body, const Vector3 &p_axis_velocity) {
  557. RigidBodyBullet *body = rigid_body_owner.get(p_body);
  558. ERR_FAIL_COND(!body);
  559. Vector3 v = body->get_linear_velocity();
  560. Vector3 axis = p_axis_velocity.normalized();
  561. v -= axis * axis.dot(v);
  562. v += p_axis_velocity;
  563. body->set_linear_velocity(v);
  564. }
  565. void BulletPhysicsServer::body_set_axis_lock(RID p_body, BodyAxis p_axis, bool p_lock) {
  566. RigidBodyBullet *body = rigid_body_owner.get(p_body);
  567. ERR_FAIL_COND(!body);
  568. body->set_axis_lock(p_axis, p_lock);
  569. }
  570. bool BulletPhysicsServer::body_is_axis_locked(RID p_body, BodyAxis p_axis) const {
  571. const RigidBodyBullet *body = rigid_body_owner.get(p_body);
  572. ERR_FAIL_COND_V(!body, 0);
  573. return body->is_axis_locked(p_axis);
  574. }
  575. void BulletPhysicsServer::body_add_collision_exception(RID p_body, RID p_body_b) {
  576. RigidBodyBullet *body = rigid_body_owner.get(p_body);
  577. ERR_FAIL_COND(!body);
  578. RigidBodyBullet *other_body = rigid_body_owner.get(p_body_b);
  579. ERR_FAIL_COND(!other_body);
  580. body->add_collision_exception(other_body);
  581. }
  582. void BulletPhysicsServer::body_remove_collision_exception(RID p_body, RID p_body_b) {
  583. RigidBodyBullet *body = rigid_body_owner.get(p_body);
  584. ERR_FAIL_COND(!body);
  585. RigidBodyBullet *other_body = rigid_body_owner.get(p_body_b);
  586. ERR_FAIL_COND(!other_body);
  587. body->remove_collision_exception(other_body);
  588. }
  589. void BulletPhysicsServer::body_get_collision_exceptions(RID p_body, List<RID> *p_exceptions) {
  590. RigidBodyBullet *body = rigid_body_owner.get(p_body);
  591. ERR_FAIL_COND(!body);
  592. for (int i = 0; i < body->get_exceptions().size(); i++) {
  593. p_exceptions->push_back(body->get_exceptions()[i]);
  594. }
  595. }
  596. void BulletPhysicsServer::body_set_max_contacts_reported(RID p_body, int p_contacts) {
  597. RigidBodyBullet *body = rigid_body_owner.get(p_body);
  598. ERR_FAIL_COND(!body);
  599. body->set_max_collisions_detection(p_contacts);
  600. }
  601. int BulletPhysicsServer::body_get_max_contacts_reported(RID p_body) const {
  602. RigidBodyBullet *body = rigid_body_owner.get(p_body);
  603. ERR_FAIL_COND_V(!body, 0);
  604. return body->get_max_collisions_detection();
  605. }
  606. void BulletPhysicsServer::body_set_contacts_reported_depth_threshold(RID p_body, float p_threshold) {
  607. WARN_PRINT("Not supported by bullet and even Godot");
  608. }
  609. float BulletPhysicsServer::body_get_contacts_reported_depth_threshold(RID p_body) const {
  610. WARN_PRINT("Not supported by bullet and even Godot");
  611. return 0.;
  612. }
  613. void BulletPhysicsServer::body_set_omit_force_integration(RID p_body, bool p_omit) {
  614. WARN_PRINT("Not supported by bullet");
  615. }
  616. bool BulletPhysicsServer::body_is_omitting_force_integration(RID p_body) const {
  617. WARN_PRINT("Not supported by bullet");
  618. return false;
  619. }
  620. void BulletPhysicsServer::body_set_force_integration_callback(RID p_body, Object *p_receiver, const StringName &p_method, const Variant &p_udata) {
  621. RigidBodyBullet *body = rigid_body_owner.get(p_body);
  622. ERR_FAIL_COND(!body);
  623. body->set_force_integration_callback(p_receiver ? p_receiver->get_instance_id() : ObjectID(0), p_method, p_udata);
  624. }
  625. void BulletPhysicsServer::body_set_ray_pickable(RID p_body, bool p_enable) {
  626. RigidBodyBullet *body = rigid_body_owner.get(p_body);
  627. ERR_FAIL_COND(!body);
  628. body->set_ray_pickable(p_enable);
  629. }
  630. bool BulletPhysicsServer::body_is_ray_pickable(RID p_body) const {
  631. RigidBodyBullet *body = rigid_body_owner.get(p_body);
  632. ERR_FAIL_COND_V(!body, false);
  633. return body->is_ray_pickable();
  634. }
  635. PhysicsDirectBodyState *BulletPhysicsServer::body_get_direct_state(RID p_body) {
  636. RigidBodyBullet *body = rigid_body_owner.get(p_body);
  637. ERR_FAIL_COND_V(!body, NULL);
  638. return BulletPhysicsDirectBodyState::get_singleton(body);
  639. }
  640. bool BulletPhysicsServer::body_test_motion(RID p_body, const Transform &p_from, const Vector3 &p_motion, bool p_infinite_inertia, MotionResult *r_result) {
  641. RigidBodyBullet *body = rigid_body_owner.get(p_body);
  642. ERR_FAIL_COND_V(!body, false);
  643. ERR_FAIL_COND_V(!body->get_space(), false);
  644. return body->get_space()->test_body_motion(body, p_from, p_motion, p_infinite_inertia, r_result);
  645. }
  646. RID BulletPhysicsServer::soft_body_create(bool p_init_sleeping) {
  647. SoftBodyBullet *body = bulletnew(SoftBodyBullet);
  648. body->set_collision_layer(1);
  649. body->set_collision_mask(1);
  650. if (p_init_sleeping)
  651. body->set_activation_state(false);
  652. CreateThenReturnRID(soft_body_owner, body);
  653. }
  654. void BulletPhysicsServer::soft_body_set_space(RID p_body, RID p_space) {
  655. SoftBodyBullet *body = soft_body_owner.get(p_body);
  656. ERR_FAIL_COND(!body);
  657. SpaceBullet *space = NULL;
  658. if (p_space.is_valid()) {
  659. space = space_owner.get(p_space);
  660. ERR_FAIL_COND(!space);
  661. }
  662. if (body->get_space() == space)
  663. return; //pointles
  664. body->set_space(space);
  665. }
  666. RID BulletPhysicsServer::soft_body_get_space(RID p_body) const {
  667. SoftBodyBullet *body = soft_body_owner.get(p_body);
  668. ERR_FAIL_COND_V(!body, RID());
  669. SpaceBullet *space = body->get_space();
  670. if (!space)
  671. return RID();
  672. return space->get_self();
  673. }
  674. void BulletPhysicsServer::soft_body_set_trimesh_body_shape(RID p_body, PoolVector<int> p_indices, PoolVector<Vector3> p_vertices, int p_triangles_num) {
  675. SoftBodyBullet *body = soft_body_owner.get(p_body);
  676. ERR_FAIL_COND(!body);
  677. body->set_trimesh_body_shape(p_indices, p_vertices, p_triangles_num);
  678. }
  679. void BulletPhysicsServer::soft_body_set_collision_layer(RID p_body, uint32_t p_layer) {
  680. SoftBodyBullet *body = soft_body_owner.get(p_body);
  681. ERR_FAIL_COND(!body);
  682. body->set_collision_layer(p_layer);
  683. }
  684. uint32_t BulletPhysicsServer::soft_body_get_collision_layer(RID p_body) const {
  685. const SoftBodyBullet *body = soft_body_owner.get(p_body);
  686. ERR_FAIL_COND_V(!body, 0);
  687. return body->get_collision_layer();
  688. }
  689. void BulletPhysicsServer::soft_body_set_collision_mask(RID p_body, uint32_t p_mask) {
  690. SoftBodyBullet *body = soft_body_owner.get(p_body);
  691. ERR_FAIL_COND(!body);
  692. body->set_collision_mask(p_mask);
  693. }
  694. uint32_t BulletPhysicsServer::soft_body_get_collision_mask(RID p_body) const {
  695. const SoftBodyBullet *body = soft_body_owner.get(p_body);
  696. ERR_FAIL_COND_V(!body, 0);
  697. return body->get_collision_mask();
  698. }
  699. void BulletPhysicsServer::soft_body_add_collision_exception(RID p_body, RID p_body_b) {
  700. SoftBodyBullet *body = soft_body_owner.get(p_body);
  701. ERR_FAIL_COND(!body);
  702. CollisionObjectBullet *other_body = rigid_body_owner.get(p_body_b);
  703. if (!other_body) {
  704. other_body = soft_body_owner.get(p_body_b);
  705. }
  706. ERR_FAIL_COND(!other_body);
  707. body->add_collision_exception(other_body);
  708. }
  709. void BulletPhysicsServer::soft_body_remove_collision_exception(RID p_body, RID p_body_b) {
  710. SoftBodyBullet *body = soft_body_owner.get(p_body);
  711. ERR_FAIL_COND(!body);
  712. CollisionObjectBullet *other_body = rigid_body_owner.get(p_body_b);
  713. if (!other_body) {
  714. other_body = soft_body_owner.get(p_body_b);
  715. }
  716. ERR_FAIL_COND(!other_body);
  717. body->remove_collision_exception(other_body);
  718. }
  719. void BulletPhysicsServer::soft_body_get_collision_exceptions(RID p_body, List<RID> *p_exceptions) {
  720. SoftBodyBullet *body = soft_body_owner.get(p_body);
  721. ERR_FAIL_COND(!body);
  722. for (int i = 0; i < body->get_exceptions().size(); i++) {
  723. p_exceptions->push_back(body->get_exceptions()[i]);
  724. }
  725. }
  726. void BulletPhysicsServer::soft_body_set_state(RID p_body, BodyState p_state, const Variant &p_variant) {
  727. print_line("TODO MUST BE IMPLEMENTED");
  728. }
  729. Variant BulletPhysicsServer::soft_body_get_state(RID p_body, BodyState p_state) const {
  730. print_line("TODO MUST BE IMPLEMENTED");
  731. return Variant();
  732. }
  733. void BulletPhysicsServer::soft_body_set_transform(RID p_body, const Transform &p_transform) {
  734. SoftBodyBullet *body = soft_body_owner.get(p_body);
  735. ERR_FAIL_COND(!body);
  736. body->set_transform(p_transform);
  737. }
  738. Transform BulletPhysicsServer::soft_body_get_transform(RID p_body) const {
  739. const SoftBodyBullet *body = soft_body_owner.get(p_body);
  740. ERR_FAIL_COND_V(!body, Transform());
  741. return body->get_transform();
  742. }
  743. void BulletPhysicsServer::soft_body_set_ray_pickable(RID p_body, bool p_enable) {
  744. SoftBodyBullet *body = soft_body_owner.get(p_body);
  745. ERR_FAIL_COND(!body);
  746. body->set_ray_pickable(p_enable);
  747. }
  748. bool BulletPhysicsServer::soft_body_is_ray_pickable(RID p_body) const {
  749. SoftBodyBullet *body = soft_body_owner.get(p_body);
  750. ERR_FAIL_COND_V(!body, false);
  751. return body->is_ray_pickable();
  752. }
  753. PhysicsServer::JointType BulletPhysicsServer::joint_get_type(RID p_joint) const {
  754. JointBullet *joint = joint_owner.get(p_joint);
  755. ERR_FAIL_COND_V(!joint, JOINT_PIN);
  756. return joint->get_type();
  757. }
  758. void BulletPhysicsServer::joint_set_solver_priority(RID p_joint, int p_priority) {
  759. //WARN_PRINTS("Joint priority not supported by bullet");
  760. }
  761. int BulletPhysicsServer::joint_get_solver_priority(RID p_joint) const {
  762. //WARN_PRINTS("Joint priority not supported by bullet");
  763. return 0;
  764. }
  765. void BulletPhysicsServer::joint_disable_collisions_between_bodies(RID p_joint, const bool p_disable) {
  766. JointBullet *joint = joint_owner.get(p_joint);
  767. ERR_FAIL_COND(!joint);
  768. joint->disable_collisions_between_bodies(p_disable);
  769. }
  770. bool BulletPhysicsServer::joint_is_disabled_collisions_between_bodies(RID p_joint) const {
  771. JointBullet *joint(joint_owner.get(p_joint));
  772. ERR_FAIL_COND_V(!joint, false);
  773. return joint->is_disabled_collisions_between_bodies();
  774. }
  775. RID BulletPhysicsServer::joint_create_pin(RID p_body_A, const Vector3 &p_local_A, RID p_body_B, const Vector3 &p_local_B) {
  776. RigidBodyBullet *body_A = rigid_body_owner.get(p_body_A);
  777. ERR_FAIL_COND_V(!body_A, RID());
  778. JointAssertSpace(body_A, "A", RID());
  779. RigidBodyBullet *body_B = NULL;
  780. if (p_body_B.is_valid()) {
  781. body_B = rigid_body_owner.get(p_body_B);
  782. JointAssertSpace(body_B, "B", RID());
  783. JointAssertSameSpace(body_A, body_B, RID());
  784. }
  785. ERR_FAIL_COND_V(body_A == body_B, RID());
  786. JointBullet *joint = bulletnew(PinJointBullet(body_A, p_local_A, body_B, p_local_B));
  787. AddJointToSpace(body_A, joint);
  788. CreateThenReturnRID(joint_owner, joint);
  789. }
  790. void BulletPhysicsServer::pin_joint_set_param(RID p_joint, PinJointParam p_param, float p_value) {
  791. JointBullet *joint = joint_owner.get(p_joint);
  792. ERR_FAIL_COND(!joint);
  793. ERR_FAIL_COND(joint->get_type() != JOINT_PIN);
  794. PinJointBullet *pin_joint = static_cast<PinJointBullet *>(joint);
  795. pin_joint->set_param(p_param, p_value);
  796. }
  797. float BulletPhysicsServer::pin_joint_get_param(RID p_joint, PinJointParam p_param) const {
  798. JointBullet *joint = joint_owner.get(p_joint);
  799. ERR_FAIL_COND_V(!joint, 0);
  800. ERR_FAIL_COND_V(joint->get_type() != JOINT_PIN, 0);
  801. PinJointBullet *pin_joint = static_cast<PinJointBullet *>(joint);
  802. return pin_joint->get_param(p_param);
  803. }
  804. void BulletPhysicsServer::pin_joint_set_local_a(RID p_joint, const Vector3 &p_A) {
  805. JointBullet *joint = joint_owner.get(p_joint);
  806. ERR_FAIL_COND(!joint);
  807. ERR_FAIL_COND(joint->get_type() != JOINT_PIN);
  808. PinJointBullet *pin_joint = static_cast<PinJointBullet *>(joint);
  809. pin_joint->setPivotInA(p_A);
  810. }
  811. Vector3 BulletPhysicsServer::pin_joint_get_local_a(RID p_joint) const {
  812. JointBullet *joint = joint_owner.get(p_joint);
  813. ERR_FAIL_COND_V(!joint, Vector3());
  814. ERR_FAIL_COND_V(joint->get_type() != JOINT_PIN, Vector3());
  815. PinJointBullet *pin_joint = static_cast<PinJointBullet *>(joint);
  816. return pin_joint->getPivotInA();
  817. }
  818. void BulletPhysicsServer::pin_joint_set_local_b(RID p_joint, const Vector3 &p_B) {
  819. JointBullet *joint = joint_owner.get(p_joint);
  820. ERR_FAIL_COND(!joint);
  821. ERR_FAIL_COND(joint->get_type() != JOINT_PIN);
  822. PinJointBullet *pin_joint = static_cast<PinJointBullet *>(joint);
  823. pin_joint->setPivotInB(p_B);
  824. }
  825. Vector3 BulletPhysicsServer::pin_joint_get_local_b(RID p_joint) const {
  826. JointBullet *joint = joint_owner.get(p_joint);
  827. ERR_FAIL_COND_V(!joint, Vector3());
  828. ERR_FAIL_COND_V(joint->get_type() != JOINT_PIN, Vector3());
  829. PinJointBullet *pin_joint = static_cast<PinJointBullet *>(joint);
  830. return pin_joint->getPivotInB();
  831. }
  832. RID BulletPhysicsServer::joint_create_hinge(RID p_body_A, const Transform &p_hinge_A, RID p_body_B, const Transform &p_hinge_B) {
  833. RigidBodyBullet *body_A = rigid_body_owner.get(p_body_A);
  834. ERR_FAIL_COND_V(!body_A, RID());
  835. JointAssertSpace(body_A, "A", RID());
  836. RigidBodyBullet *body_B = NULL;
  837. if (p_body_B.is_valid()) {
  838. body_B = rigid_body_owner.get(p_body_B);
  839. JointAssertSpace(body_B, "B", RID());
  840. JointAssertSameSpace(body_A, body_B, RID());
  841. }
  842. ERR_FAIL_COND_V(body_A == body_B, RID());
  843. JointBullet *joint = bulletnew(HingeJointBullet(body_A, body_B, p_hinge_A, p_hinge_B));
  844. AddJointToSpace(body_A, joint);
  845. CreateThenReturnRID(joint_owner, joint);
  846. }
  847. RID BulletPhysicsServer::joint_create_hinge_simple(RID p_body_A, const Vector3 &p_pivot_A, const Vector3 &p_axis_A, RID p_body_B, const Vector3 &p_pivot_B, const Vector3 &p_axis_B) {
  848. RigidBodyBullet *body_A = rigid_body_owner.get(p_body_A);
  849. ERR_FAIL_COND_V(!body_A, RID());
  850. JointAssertSpace(body_A, "A", RID());
  851. RigidBodyBullet *body_B = NULL;
  852. if (p_body_B.is_valid()) {
  853. body_B = rigid_body_owner.get(p_body_B);
  854. JointAssertSpace(body_B, "B", RID());
  855. JointAssertSameSpace(body_A, body_B, RID());
  856. }
  857. ERR_FAIL_COND_V(body_A == body_B, RID());
  858. JointBullet *joint = bulletnew(HingeJointBullet(body_A, body_B, p_pivot_A, p_pivot_B, p_axis_A, p_axis_B));
  859. AddJointToSpace(body_A, joint);
  860. CreateThenReturnRID(joint_owner, joint);
  861. }
  862. void BulletPhysicsServer::hinge_joint_set_param(RID p_joint, HingeJointParam p_param, float p_value) {
  863. JointBullet *joint = joint_owner.get(p_joint);
  864. ERR_FAIL_COND(!joint);
  865. ERR_FAIL_COND(joint->get_type() != JOINT_HINGE);
  866. HingeJointBullet *hinge_joint = static_cast<HingeJointBullet *>(joint);
  867. hinge_joint->set_param(p_param, p_value);
  868. }
  869. float BulletPhysicsServer::hinge_joint_get_param(RID p_joint, HingeJointParam p_param) const {
  870. JointBullet *joint = joint_owner.get(p_joint);
  871. ERR_FAIL_COND_V(!joint, 0);
  872. ERR_FAIL_COND_V(joint->get_type() != JOINT_HINGE, 0);
  873. HingeJointBullet *hinge_joint = static_cast<HingeJointBullet *>(joint);
  874. return hinge_joint->get_param(p_param);
  875. }
  876. void BulletPhysicsServer::hinge_joint_set_flag(RID p_joint, HingeJointFlag p_flag, bool p_value) {
  877. JointBullet *joint = joint_owner.get(p_joint);
  878. ERR_FAIL_COND(!joint);
  879. ERR_FAIL_COND(joint->get_type() != JOINT_HINGE);
  880. HingeJointBullet *hinge_joint = static_cast<HingeJointBullet *>(joint);
  881. hinge_joint->set_flag(p_flag, p_value);
  882. }
  883. bool BulletPhysicsServer::hinge_joint_get_flag(RID p_joint, HingeJointFlag p_flag) const {
  884. JointBullet *joint = joint_owner.get(p_joint);
  885. ERR_FAIL_COND_V(!joint, false);
  886. ERR_FAIL_COND_V(joint->get_type() != JOINT_HINGE, false);
  887. HingeJointBullet *hinge_joint = static_cast<HingeJointBullet *>(joint);
  888. return hinge_joint->get_flag(p_flag);
  889. }
  890. RID BulletPhysicsServer::joint_create_slider(RID p_body_A, const Transform &p_local_frame_A, RID p_body_B, const Transform &p_local_frame_B) {
  891. RigidBodyBullet *body_A = rigid_body_owner.get(p_body_A);
  892. ERR_FAIL_COND_V(!body_A, RID());
  893. JointAssertSpace(body_A, "A", RID());
  894. RigidBodyBullet *body_B = NULL;
  895. if (p_body_B.is_valid()) {
  896. body_B = rigid_body_owner.get(p_body_B);
  897. JointAssertSpace(body_B, "B", RID());
  898. JointAssertSameSpace(body_A, body_B, RID());
  899. }
  900. ERR_FAIL_COND_V(body_A == body_B, RID());
  901. JointBullet *joint = bulletnew(SliderJointBullet(body_A, body_B, p_local_frame_A, p_local_frame_B));
  902. AddJointToSpace(body_A, joint);
  903. CreateThenReturnRID(joint_owner, joint);
  904. }
  905. void BulletPhysicsServer::slider_joint_set_param(RID p_joint, SliderJointParam p_param, float p_value) {
  906. JointBullet *joint = joint_owner.get(p_joint);
  907. ERR_FAIL_COND(!joint);
  908. ERR_FAIL_COND(joint->get_type() != JOINT_SLIDER);
  909. SliderJointBullet *slider_joint = static_cast<SliderJointBullet *>(joint);
  910. slider_joint->set_param(p_param, p_value);
  911. }
  912. float BulletPhysicsServer::slider_joint_get_param(RID p_joint, SliderJointParam p_param) const {
  913. JointBullet *joint = joint_owner.get(p_joint);
  914. ERR_FAIL_COND_V(!joint, 0);
  915. ERR_FAIL_COND_V(joint->get_type() != JOINT_SLIDER, 0);
  916. SliderJointBullet *slider_joint = static_cast<SliderJointBullet *>(joint);
  917. return slider_joint->get_param(p_param);
  918. }
  919. RID BulletPhysicsServer::joint_create_cone_twist(RID p_body_A, const Transform &p_local_frame_A, RID p_body_B, const Transform &p_local_frame_B) {
  920. RigidBodyBullet *body_A = rigid_body_owner.get(p_body_A);
  921. ERR_FAIL_COND_V(!body_A, RID());
  922. JointAssertSpace(body_A, "A", RID());
  923. RigidBodyBullet *body_B = NULL;
  924. if (p_body_B.is_valid()) {
  925. body_B = rigid_body_owner.get(p_body_B);
  926. JointAssertSpace(body_B, "B", RID());
  927. JointAssertSameSpace(body_A, body_B, RID());
  928. }
  929. JointBullet *joint = bulletnew(ConeTwistJointBullet(body_A, body_B, p_local_frame_A, p_local_frame_B));
  930. AddJointToSpace(body_A, joint);
  931. CreateThenReturnRID(joint_owner, joint);
  932. }
  933. void BulletPhysicsServer::cone_twist_joint_set_param(RID p_joint, ConeTwistJointParam p_param, float p_value) {
  934. JointBullet *joint = joint_owner.get(p_joint);
  935. ERR_FAIL_COND(!joint);
  936. ERR_FAIL_COND(joint->get_type() != JOINT_CONE_TWIST);
  937. ConeTwistJointBullet *coneTwist_joint = static_cast<ConeTwistJointBullet *>(joint);
  938. coneTwist_joint->set_param(p_param, p_value);
  939. }
  940. float BulletPhysicsServer::cone_twist_joint_get_param(RID p_joint, ConeTwistJointParam p_param) const {
  941. JointBullet *joint = joint_owner.get(p_joint);
  942. ERR_FAIL_COND_V(!joint, 0.);
  943. ERR_FAIL_COND_V(joint->get_type() != JOINT_CONE_TWIST, 0.);
  944. ConeTwistJointBullet *coneTwist_joint = static_cast<ConeTwistJointBullet *>(joint);
  945. return coneTwist_joint->get_param(p_param);
  946. }
  947. RID BulletPhysicsServer::joint_create_generic_6dof(RID p_body_A, const Transform &p_local_frame_A, RID p_body_B, const Transform &p_local_frame_B) {
  948. RigidBodyBullet *body_A = rigid_body_owner.get(p_body_A);
  949. ERR_FAIL_COND_V(!body_A, RID());
  950. JointAssertSpace(body_A, "A", RID());
  951. RigidBodyBullet *body_B = NULL;
  952. if (p_body_B.is_valid()) {
  953. body_B = rigid_body_owner.get(p_body_B);
  954. JointAssertSpace(body_B, "B", RID());
  955. JointAssertSameSpace(body_A, body_B, RID());
  956. }
  957. ERR_FAIL_COND_V(body_A == body_B, RID());
  958. JointBullet *joint = bulletnew(Generic6DOFJointBullet(body_A, body_B, p_local_frame_A, p_local_frame_B, true));
  959. AddJointToSpace(body_A, joint);
  960. CreateThenReturnRID(joint_owner, joint);
  961. }
  962. void BulletPhysicsServer::generic_6dof_joint_set_param(RID p_joint, Vector3::Axis p_axis, G6DOFJointAxisParam p_param, float p_value) {
  963. JointBullet *joint = joint_owner.get(p_joint);
  964. ERR_FAIL_COND(!joint);
  965. ERR_FAIL_COND(joint->get_type() != JOINT_6DOF);
  966. Generic6DOFJointBullet *generic_6dof_joint = static_cast<Generic6DOFJointBullet *>(joint);
  967. generic_6dof_joint->set_param(p_axis, p_param, p_value);
  968. }
  969. float BulletPhysicsServer::generic_6dof_joint_get_param(RID p_joint, Vector3::Axis p_axis, G6DOFJointAxisParam p_param) {
  970. JointBullet *joint = joint_owner.get(p_joint);
  971. ERR_FAIL_COND_V(!joint, 0);
  972. ERR_FAIL_COND_V(joint->get_type() != JOINT_6DOF, 0);
  973. Generic6DOFJointBullet *generic_6dof_joint = static_cast<Generic6DOFJointBullet *>(joint);
  974. return generic_6dof_joint->get_param(p_axis, p_param);
  975. }
  976. void BulletPhysicsServer::generic_6dof_joint_set_flag(RID p_joint, Vector3::Axis p_axis, G6DOFJointAxisFlag p_flag, bool p_enable) {
  977. JointBullet *joint = joint_owner.get(p_joint);
  978. ERR_FAIL_COND(!joint);
  979. ERR_FAIL_COND(joint->get_type() != JOINT_6DOF);
  980. Generic6DOFJointBullet *generic_6dof_joint = static_cast<Generic6DOFJointBullet *>(joint);
  981. generic_6dof_joint->set_flag(p_axis, p_flag, p_enable);
  982. }
  983. bool BulletPhysicsServer::generic_6dof_joint_get_flag(RID p_joint, Vector3::Axis p_axis, G6DOFJointAxisFlag p_flag) {
  984. JointBullet *joint = joint_owner.get(p_joint);
  985. ERR_FAIL_COND_V(!joint, false);
  986. ERR_FAIL_COND_V(joint->get_type() != JOINT_6DOF, false);
  987. Generic6DOFJointBullet *generic_6dof_joint = static_cast<Generic6DOFJointBullet *>(joint);
  988. return generic_6dof_joint->get_flag(p_axis, p_flag);
  989. }
  990. void BulletPhysicsServer::free(RID p_rid) {
  991. if (shape_owner.owns(p_rid)) {
  992. ShapeBullet *shape = shape_owner.get(p_rid);
  993. // Notify the shape is configured
  994. for (Map<ShapeOwnerBullet *, int>::Element *element = shape->get_owners().front(); element; element = element->next()) {
  995. static_cast<ShapeOwnerBullet *>(element->key())->remove_shape(shape);
  996. }
  997. shape_owner.free(p_rid);
  998. bulletdelete(shape);
  999. } else if (rigid_body_owner.owns(p_rid)) {
  1000. RigidBodyBullet *body = rigid_body_owner.get(p_rid);
  1001. body->set_space(NULL);
  1002. body->remove_all_shapes(true);
  1003. rigid_body_owner.free(p_rid);
  1004. bulletdelete(body);
  1005. } else if (soft_body_owner.owns(p_rid)) {
  1006. SoftBodyBullet *body = soft_body_owner.get(p_rid);
  1007. body->set_space(NULL);
  1008. soft_body_owner.free(p_rid);
  1009. bulletdelete(body);
  1010. } else if (area_owner.owns(p_rid)) {
  1011. AreaBullet *area = area_owner.get(p_rid);
  1012. area->set_space(NULL);
  1013. area->remove_all_shapes(true);
  1014. area_owner.free(p_rid);
  1015. bulletdelete(area);
  1016. } else if (joint_owner.owns(p_rid)) {
  1017. JointBullet *joint = joint_owner.get(p_rid);
  1018. joint->destroy_internal_constraint();
  1019. joint_owner.free(p_rid);
  1020. bulletdelete(joint);
  1021. } else if (space_owner.owns(p_rid)) {
  1022. SpaceBullet *space = space_owner.get(p_rid);
  1023. space->remove_all_collision_objects();
  1024. space_set_active(p_rid, false);
  1025. space_owner.free(p_rid);
  1026. bulletdelete(space);
  1027. } else {
  1028. ERR_EXPLAIN("Invalid ID");
  1029. ERR_FAIL();
  1030. }
  1031. }
  1032. void BulletPhysicsServer::init() {
  1033. BulletPhysicsDirectBodyState::initSingleton();
  1034. }
  1035. void BulletPhysicsServer::step(float p_deltaTime) {
  1036. if (!active)
  1037. return;
  1038. BulletPhysicsDirectBodyState::singleton_setDeltaTime(p_deltaTime);
  1039. for (int i = 0; i < active_spaces_count; ++i) {
  1040. active_spaces[i]->step(p_deltaTime);
  1041. }
  1042. }
  1043. void BulletPhysicsServer::sync() {
  1044. }
  1045. void BulletPhysicsServer::flush_queries() {
  1046. }
  1047. void BulletPhysicsServer::finish() {
  1048. BulletPhysicsDirectBodyState::destroySingleton();
  1049. }
  1050. int BulletPhysicsServer::get_process_info(ProcessInfo p_info) {
  1051. return 0;
  1052. }
  1053. CollisionObjectBullet *BulletPhysicsServer::get_collisin_object(RID p_object) const {
  1054. if (rigid_body_owner.owns(p_object)) {
  1055. return rigid_body_owner.getornull(p_object);
  1056. }
  1057. if (area_owner.owns(p_object)) {
  1058. return area_owner.getornull(p_object);
  1059. }
  1060. if (soft_body_owner.owns(p_object)) {
  1061. return soft_body_owner.getornull(p_object);
  1062. }
  1063. return NULL;
  1064. }
  1065. RigidCollisionObjectBullet *BulletPhysicsServer::get_rigid_collisin_object(RID p_object) const {
  1066. if (rigid_body_owner.owns(p_object)) {
  1067. return rigid_body_owner.getornull(p_object);
  1068. }
  1069. if (area_owner.owns(p_object)) {
  1070. return area_owner.getornull(p_object);
  1071. }
  1072. return NULL;
  1073. }