bullet_physics_server.cpp 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389
  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. // This function si not currently supported
  486. }
  487. uint32_t BulletPhysicsServer::body_get_user_flags(RID p_body) const {
  488. // This function si not currently supported
  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. // Not supported by bullet and even Godot
  608. }
  609. float BulletPhysicsServer::body_get_contacts_reported_depth_threshold(RID p_body) const {
  610. // 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. RigidBodyBullet *body = rigid_body_owner.get(p_body);
  615. ERR_FAIL_COND(!body);
  616. body->set_omit_forces_integration(p_omit);
  617. }
  618. bool BulletPhysicsServer::body_is_omitting_force_integration(RID p_body) const {
  619. RigidBodyBullet *body = rigid_body_owner.get(p_body);
  620. ERR_FAIL_COND_V(!body, false);
  621. return body->get_omit_forces_integration();
  622. }
  623. void BulletPhysicsServer::body_set_force_integration_callback(RID p_body, Object *p_receiver, const StringName &p_method, const Variant &p_udata) {
  624. RigidBodyBullet *body = rigid_body_owner.get(p_body);
  625. ERR_FAIL_COND(!body);
  626. body->set_force_integration_callback(p_receiver ? p_receiver->get_instance_id() : ObjectID(0), p_method, p_udata);
  627. }
  628. void BulletPhysicsServer::body_set_ray_pickable(RID p_body, bool p_enable) {
  629. RigidBodyBullet *body = rigid_body_owner.get(p_body);
  630. ERR_FAIL_COND(!body);
  631. body->set_ray_pickable(p_enable);
  632. }
  633. bool BulletPhysicsServer::body_is_ray_pickable(RID p_body) const {
  634. RigidBodyBullet *body = rigid_body_owner.get(p_body);
  635. ERR_FAIL_COND_V(!body, false);
  636. return body->is_ray_pickable();
  637. }
  638. PhysicsDirectBodyState *BulletPhysicsServer::body_get_direct_state(RID p_body) {
  639. RigidBodyBullet *body = rigid_body_owner.get(p_body);
  640. ERR_FAIL_COND_V(!body, NULL);
  641. return BulletPhysicsDirectBodyState::get_singleton(body);
  642. }
  643. bool BulletPhysicsServer::body_test_motion(RID p_body, const Transform &p_from, const Vector3 &p_motion, bool p_infinite_inertia, MotionResult *r_result) {
  644. RigidBodyBullet *body = rigid_body_owner.get(p_body);
  645. ERR_FAIL_COND_V(!body, false);
  646. ERR_FAIL_COND_V(!body->get_space(), false);
  647. return body->get_space()->test_body_motion(body, p_from, p_motion, p_infinite_inertia, r_result);
  648. }
  649. RID BulletPhysicsServer::soft_body_create(bool p_init_sleeping) {
  650. SoftBodyBullet *body = bulletnew(SoftBodyBullet);
  651. body->set_collision_layer(1);
  652. body->set_collision_mask(1);
  653. if (p_init_sleeping)
  654. body->set_activation_state(false);
  655. CreateThenReturnRID(soft_body_owner, body);
  656. }
  657. void BulletPhysicsServer::soft_body_set_space(RID p_body, RID p_space) {
  658. SoftBodyBullet *body = soft_body_owner.get(p_body);
  659. ERR_FAIL_COND(!body);
  660. SpaceBullet *space = NULL;
  661. if (p_space.is_valid()) {
  662. space = space_owner.get(p_space);
  663. ERR_FAIL_COND(!space);
  664. }
  665. if (body->get_space() == space)
  666. return; //pointles
  667. body->set_space(space);
  668. }
  669. RID BulletPhysicsServer::soft_body_get_space(RID p_body) const {
  670. SoftBodyBullet *body = soft_body_owner.get(p_body);
  671. ERR_FAIL_COND_V(!body, RID());
  672. SpaceBullet *space = body->get_space();
  673. if (!space)
  674. return RID();
  675. return space->get_self();
  676. }
  677. void BulletPhysicsServer::soft_body_set_trimesh_body_shape(RID p_body, PoolVector<int> p_indices, PoolVector<Vector3> p_vertices, int p_triangles_num) {
  678. SoftBodyBullet *body = soft_body_owner.get(p_body);
  679. ERR_FAIL_COND(!body);
  680. body->set_trimesh_body_shape(p_indices, p_vertices, p_triangles_num);
  681. }
  682. void BulletPhysicsServer::soft_body_set_collision_layer(RID p_body, uint32_t p_layer) {
  683. SoftBodyBullet *body = soft_body_owner.get(p_body);
  684. ERR_FAIL_COND(!body);
  685. body->set_collision_layer(p_layer);
  686. }
  687. uint32_t BulletPhysicsServer::soft_body_get_collision_layer(RID p_body) const {
  688. const SoftBodyBullet *body = soft_body_owner.get(p_body);
  689. ERR_FAIL_COND_V(!body, 0);
  690. return body->get_collision_layer();
  691. }
  692. void BulletPhysicsServer::soft_body_set_collision_mask(RID p_body, uint32_t p_mask) {
  693. SoftBodyBullet *body = soft_body_owner.get(p_body);
  694. ERR_FAIL_COND(!body);
  695. body->set_collision_mask(p_mask);
  696. }
  697. uint32_t BulletPhysicsServer::soft_body_get_collision_mask(RID p_body) const {
  698. const SoftBodyBullet *body = soft_body_owner.get(p_body);
  699. ERR_FAIL_COND_V(!body, 0);
  700. return body->get_collision_mask();
  701. }
  702. void BulletPhysicsServer::soft_body_add_collision_exception(RID p_body, RID p_body_b) {
  703. SoftBodyBullet *body = soft_body_owner.get(p_body);
  704. ERR_FAIL_COND(!body);
  705. CollisionObjectBullet *other_body = rigid_body_owner.get(p_body_b);
  706. if (!other_body) {
  707. other_body = soft_body_owner.get(p_body_b);
  708. }
  709. ERR_FAIL_COND(!other_body);
  710. body->add_collision_exception(other_body);
  711. }
  712. void BulletPhysicsServer::soft_body_remove_collision_exception(RID p_body, RID p_body_b) {
  713. SoftBodyBullet *body = soft_body_owner.get(p_body);
  714. ERR_FAIL_COND(!body);
  715. CollisionObjectBullet *other_body = rigid_body_owner.get(p_body_b);
  716. if (!other_body) {
  717. other_body = soft_body_owner.get(p_body_b);
  718. }
  719. ERR_FAIL_COND(!other_body);
  720. body->remove_collision_exception(other_body);
  721. }
  722. void BulletPhysicsServer::soft_body_get_collision_exceptions(RID p_body, List<RID> *p_exceptions) {
  723. SoftBodyBullet *body = soft_body_owner.get(p_body);
  724. ERR_FAIL_COND(!body);
  725. for (int i = 0; i < body->get_exceptions().size(); i++) {
  726. p_exceptions->push_back(body->get_exceptions()[i]);
  727. }
  728. }
  729. void BulletPhysicsServer::soft_body_set_state(RID p_body, BodyState p_state, const Variant &p_variant) {
  730. print_line("TODO MUST BE IMPLEMENTED");
  731. }
  732. Variant BulletPhysicsServer::soft_body_get_state(RID p_body, BodyState p_state) const {
  733. print_line("TODO MUST BE IMPLEMENTED");
  734. return Variant();
  735. }
  736. void BulletPhysicsServer::soft_body_set_transform(RID p_body, const Transform &p_transform) {
  737. SoftBodyBullet *body = soft_body_owner.get(p_body);
  738. ERR_FAIL_COND(!body);
  739. body->set_transform(p_transform);
  740. }
  741. Transform BulletPhysicsServer::soft_body_get_transform(RID p_body) const {
  742. const SoftBodyBullet *body = soft_body_owner.get(p_body);
  743. ERR_FAIL_COND_V(!body, Transform());
  744. return body->get_transform();
  745. }
  746. void BulletPhysicsServer::soft_body_set_ray_pickable(RID p_body, bool p_enable) {
  747. SoftBodyBullet *body = soft_body_owner.get(p_body);
  748. ERR_FAIL_COND(!body);
  749. body->set_ray_pickable(p_enable);
  750. }
  751. bool BulletPhysicsServer::soft_body_is_ray_pickable(RID p_body) const {
  752. SoftBodyBullet *body = soft_body_owner.get(p_body);
  753. ERR_FAIL_COND_V(!body, false);
  754. return body->is_ray_pickable();
  755. }
  756. PhysicsServer::JointType BulletPhysicsServer::joint_get_type(RID p_joint) const {
  757. JointBullet *joint = joint_owner.get(p_joint);
  758. ERR_FAIL_COND_V(!joint, JOINT_PIN);
  759. return joint->get_type();
  760. }
  761. void BulletPhysicsServer::joint_set_solver_priority(RID p_joint, int p_priority) {
  762. // Joint priority not supported by bullet
  763. }
  764. int BulletPhysicsServer::joint_get_solver_priority(RID p_joint) const {
  765. // Joint priority not supported by bullet
  766. return 0;
  767. }
  768. void BulletPhysicsServer::joint_disable_collisions_between_bodies(RID p_joint, const bool p_disable) {
  769. JointBullet *joint = joint_owner.get(p_joint);
  770. ERR_FAIL_COND(!joint);
  771. joint->disable_collisions_between_bodies(p_disable);
  772. }
  773. bool BulletPhysicsServer::joint_is_disabled_collisions_between_bodies(RID p_joint) const {
  774. JointBullet *joint(joint_owner.get(p_joint));
  775. ERR_FAIL_COND_V(!joint, false);
  776. return joint->is_disabled_collisions_between_bodies();
  777. }
  778. RID BulletPhysicsServer::joint_create_pin(RID p_body_A, const Vector3 &p_local_A, RID p_body_B, const Vector3 &p_local_B) {
  779. RigidBodyBullet *body_A = rigid_body_owner.get(p_body_A);
  780. ERR_FAIL_COND_V(!body_A, RID());
  781. JointAssertSpace(body_A, "A", RID());
  782. RigidBodyBullet *body_B = NULL;
  783. if (p_body_B.is_valid()) {
  784. body_B = rigid_body_owner.get(p_body_B);
  785. JointAssertSpace(body_B, "B", RID());
  786. JointAssertSameSpace(body_A, body_B, RID());
  787. }
  788. ERR_FAIL_COND_V(body_A == body_B, RID());
  789. JointBullet *joint = bulletnew(PinJointBullet(body_A, p_local_A, body_B, p_local_B));
  790. AddJointToSpace(body_A, joint);
  791. CreateThenReturnRID(joint_owner, joint);
  792. }
  793. void BulletPhysicsServer::pin_joint_set_param(RID p_joint, PinJointParam p_param, float p_value) {
  794. JointBullet *joint = joint_owner.get(p_joint);
  795. ERR_FAIL_COND(!joint);
  796. ERR_FAIL_COND(joint->get_type() != JOINT_PIN);
  797. PinJointBullet *pin_joint = static_cast<PinJointBullet *>(joint);
  798. pin_joint->set_param(p_param, p_value);
  799. }
  800. float BulletPhysicsServer::pin_joint_get_param(RID p_joint, PinJointParam p_param) const {
  801. JointBullet *joint = joint_owner.get(p_joint);
  802. ERR_FAIL_COND_V(!joint, 0);
  803. ERR_FAIL_COND_V(joint->get_type() != JOINT_PIN, 0);
  804. PinJointBullet *pin_joint = static_cast<PinJointBullet *>(joint);
  805. return pin_joint->get_param(p_param);
  806. }
  807. void BulletPhysicsServer::pin_joint_set_local_a(RID p_joint, const Vector3 &p_A) {
  808. JointBullet *joint = joint_owner.get(p_joint);
  809. ERR_FAIL_COND(!joint);
  810. ERR_FAIL_COND(joint->get_type() != JOINT_PIN);
  811. PinJointBullet *pin_joint = static_cast<PinJointBullet *>(joint);
  812. pin_joint->setPivotInA(p_A);
  813. }
  814. Vector3 BulletPhysicsServer::pin_joint_get_local_a(RID p_joint) const {
  815. JointBullet *joint = joint_owner.get(p_joint);
  816. ERR_FAIL_COND_V(!joint, Vector3());
  817. ERR_FAIL_COND_V(joint->get_type() != JOINT_PIN, Vector3());
  818. PinJointBullet *pin_joint = static_cast<PinJointBullet *>(joint);
  819. return pin_joint->getPivotInA();
  820. }
  821. void BulletPhysicsServer::pin_joint_set_local_b(RID p_joint, const Vector3 &p_B) {
  822. JointBullet *joint = joint_owner.get(p_joint);
  823. ERR_FAIL_COND(!joint);
  824. ERR_FAIL_COND(joint->get_type() != JOINT_PIN);
  825. PinJointBullet *pin_joint = static_cast<PinJointBullet *>(joint);
  826. pin_joint->setPivotInB(p_B);
  827. }
  828. Vector3 BulletPhysicsServer::pin_joint_get_local_b(RID p_joint) const {
  829. JointBullet *joint = joint_owner.get(p_joint);
  830. ERR_FAIL_COND_V(!joint, Vector3());
  831. ERR_FAIL_COND_V(joint->get_type() != JOINT_PIN, Vector3());
  832. PinJointBullet *pin_joint = static_cast<PinJointBullet *>(joint);
  833. return pin_joint->getPivotInB();
  834. }
  835. RID BulletPhysicsServer::joint_create_hinge(RID p_body_A, const Transform &p_hinge_A, RID p_body_B, const Transform &p_hinge_B) {
  836. RigidBodyBullet *body_A = rigid_body_owner.get(p_body_A);
  837. ERR_FAIL_COND_V(!body_A, RID());
  838. JointAssertSpace(body_A, "A", RID());
  839. RigidBodyBullet *body_B = NULL;
  840. if (p_body_B.is_valid()) {
  841. body_B = rigid_body_owner.get(p_body_B);
  842. JointAssertSpace(body_B, "B", RID());
  843. JointAssertSameSpace(body_A, body_B, RID());
  844. }
  845. ERR_FAIL_COND_V(body_A == body_B, RID());
  846. JointBullet *joint = bulletnew(HingeJointBullet(body_A, body_B, p_hinge_A, p_hinge_B));
  847. AddJointToSpace(body_A, joint);
  848. CreateThenReturnRID(joint_owner, joint);
  849. }
  850. 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) {
  851. RigidBodyBullet *body_A = rigid_body_owner.get(p_body_A);
  852. ERR_FAIL_COND_V(!body_A, RID());
  853. JointAssertSpace(body_A, "A", RID());
  854. RigidBodyBullet *body_B = NULL;
  855. if (p_body_B.is_valid()) {
  856. body_B = rigid_body_owner.get(p_body_B);
  857. JointAssertSpace(body_B, "B", RID());
  858. JointAssertSameSpace(body_A, body_B, RID());
  859. }
  860. ERR_FAIL_COND_V(body_A == body_B, RID());
  861. JointBullet *joint = bulletnew(HingeJointBullet(body_A, body_B, p_pivot_A, p_pivot_B, p_axis_A, p_axis_B));
  862. AddJointToSpace(body_A, joint);
  863. CreateThenReturnRID(joint_owner, joint);
  864. }
  865. void BulletPhysicsServer::hinge_joint_set_param(RID p_joint, HingeJointParam p_param, float p_value) {
  866. JointBullet *joint = joint_owner.get(p_joint);
  867. ERR_FAIL_COND(!joint);
  868. ERR_FAIL_COND(joint->get_type() != JOINT_HINGE);
  869. HingeJointBullet *hinge_joint = static_cast<HingeJointBullet *>(joint);
  870. hinge_joint->set_param(p_param, p_value);
  871. }
  872. float BulletPhysicsServer::hinge_joint_get_param(RID p_joint, HingeJointParam p_param) const {
  873. JointBullet *joint = joint_owner.get(p_joint);
  874. ERR_FAIL_COND_V(!joint, 0);
  875. ERR_FAIL_COND_V(joint->get_type() != JOINT_HINGE, 0);
  876. HingeJointBullet *hinge_joint = static_cast<HingeJointBullet *>(joint);
  877. return hinge_joint->get_param(p_param);
  878. }
  879. void BulletPhysicsServer::hinge_joint_set_flag(RID p_joint, HingeJointFlag p_flag, bool p_value) {
  880. JointBullet *joint = joint_owner.get(p_joint);
  881. ERR_FAIL_COND(!joint);
  882. ERR_FAIL_COND(joint->get_type() != JOINT_HINGE);
  883. HingeJointBullet *hinge_joint = static_cast<HingeJointBullet *>(joint);
  884. hinge_joint->set_flag(p_flag, p_value);
  885. }
  886. bool BulletPhysicsServer::hinge_joint_get_flag(RID p_joint, HingeJointFlag p_flag) const {
  887. JointBullet *joint = joint_owner.get(p_joint);
  888. ERR_FAIL_COND_V(!joint, false);
  889. ERR_FAIL_COND_V(joint->get_type() != JOINT_HINGE, false);
  890. HingeJointBullet *hinge_joint = static_cast<HingeJointBullet *>(joint);
  891. return hinge_joint->get_flag(p_flag);
  892. }
  893. 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) {
  894. RigidBodyBullet *body_A = rigid_body_owner.get(p_body_A);
  895. ERR_FAIL_COND_V(!body_A, RID());
  896. JointAssertSpace(body_A, "A", RID());
  897. RigidBodyBullet *body_B = NULL;
  898. if (p_body_B.is_valid()) {
  899. body_B = rigid_body_owner.get(p_body_B);
  900. JointAssertSpace(body_B, "B", RID());
  901. JointAssertSameSpace(body_A, body_B, RID());
  902. }
  903. ERR_FAIL_COND_V(body_A == body_B, RID());
  904. JointBullet *joint = bulletnew(SliderJointBullet(body_A, body_B, p_local_frame_A, p_local_frame_B));
  905. AddJointToSpace(body_A, joint);
  906. CreateThenReturnRID(joint_owner, joint);
  907. }
  908. void BulletPhysicsServer::slider_joint_set_param(RID p_joint, SliderJointParam p_param, float p_value) {
  909. JointBullet *joint = joint_owner.get(p_joint);
  910. ERR_FAIL_COND(!joint);
  911. ERR_FAIL_COND(joint->get_type() != JOINT_SLIDER);
  912. SliderJointBullet *slider_joint = static_cast<SliderJointBullet *>(joint);
  913. slider_joint->set_param(p_param, p_value);
  914. }
  915. float BulletPhysicsServer::slider_joint_get_param(RID p_joint, SliderJointParam p_param) const {
  916. JointBullet *joint = joint_owner.get(p_joint);
  917. ERR_FAIL_COND_V(!joint, 0);
  918. ERR_FAIL_COND_V(joint->get_type() != JOINT_SLIDER, 0);
  919. SliderJointBullet *slider_joint = static_cast<SliderJointBullet *>(joint);
  920. return slider_joint->get_param(p_param);
  921. }
  922. 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) {
  923. RigidBodyBullet *body_A = rigid_body_owner.get(p_body_A);
  924. ERR_FAIL_COND_V(!body_A, RID());
  925. JointAssertSpace(body_A, "A", RID());
  926. RigidBodyBullet *body_B = NULL;
  927. if (p_body_B.is_valid()) {
  928. body_B = rigid_body_owner.get(p_body_B);
  929. JointAssertSpace(body_B, "B", RID());
  930. JointAssertSameSpace(body_A, body_B, RID());
  931. }
  932. JointBullet *joint = bulletnew(ConeTwistJointBullet(body_A, body_B, p_local_frame_A, p_local_frame_B));
  933. AddJointToSpace(body_A, joint);
  934. CreateThenReturnRID(joint_owner, joint);
  935. }
  936. void BulletPhysicsServer::cone_twist_joint_set_param(RID p_joint, ConeTwistJointParam p_param, float p_value) {
  937. JointBullet *joint = joint_owner.get(p_joint);
  938. ERR_FAIL_COND(!joint);
  939. ERR_FAIL_COND(joint->get_type() != JOINT_CONE_TWIST);
  940. ConeTwistJointBullet *coneTwist_joint = static_cast<ConeTwistJointBullet *>(joint);
  941. coneTwist_joint->set_param(p_param, p_value);
  942. }
  943. float BulletPhysicsServer::cone_twist_joint_get_param(RID p_joint, ConeTwistJointParam p_param) const {
  944. JointBullet *joint = joint_owner.get(p_joint);
  945. ERR_FAIL_COND_V(!joint, 0.);
  946. ERR_FAIL_COND_V(joint->get_type() != JOINT_CONE_TWIST, 0.);
  947. ConeTwistJointBullet *coneTwist_joint = static_cast<ConeTwistJointBullet *>(joint);
  948. return coneTwist_joint->get_param(p_param);
  949. }
  950. 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) {
  951. RigidBodyBullet *body_A = rigid_body_owner.get(p_body_A);
  952. ERR_FAIL_COND_V(!body_A, RID());
  953. JointAssertSpace(body_A, "A", RID());
  954. RigidBodyBullet *body_B = NULL;
  955. if (p_body_B.is_valid()) {
  956. body_B = rigid_body_owner.get(p_body_B);
  957. JointAssertSpace(body_B, "B", RID());
  958. JointAssertSameSpace(body_A, body_B, RID());
  959. }
  960. ERR_FAIL_COND_V(body_A == body_B, RID());
  961. JointBullet *joint = bulletnew(Generic6DOFJointBullet(body_A, body_B, p_local_frame_A, p_local_frame_B, true));
  962. AddJointToSpace(body_A, joint);
  963. CreateThenReturnRID(joint_owner, joint);
  964. }
  965. void BulletPhysicsServer::generic_6dof_joint_set_param(RID p_joint, Vector3::Axis p_axis, G6DOFJointAxisParam p_param, float p_value) {
  966. JointBullet *joint = joint_owner.get(p_joint);
  967. ERR_FAIL_COND(!joint);
  968. ERR_FAIL_COND(joint->get_type() != JOINT_6DOF);
  969. Generic6DOFJointBullet *generic_6dof_joint = static_cast<Generic6DOFJointBullet *>(joint);
  970. generic_6dof_joint->set_param(p_axis, p_param, p_value);
  971. }
  972. float BulletPhysicsServer::generic_6dof_joint_get_param(RID p_joint, Vector3::Axis p_axis, G6DOFJointAxisParam p_param) {
  973. JointBullet *joint = joint_owner.get(p_joint);
  974. ERR_FAIL_COND_V(!joint, 0);
  975. ERR_FAIL_COND_V(joint->get_type() != JOINT_6DOF, 0);
  976. Generic6DOFJointBullet *generic_6dof_joint = static_cast<Generic6DOFJointBullet *>(joint);
  977. return generic_6dof_joint->get_param(p_axis, p_param);
  978. }
  979. void BulletPhysicsServer::generic_6dof_joint_set_flag(RID p_joint, Vector3::Axis p_axis, G6DOFJointAxisFlag p_flag, bool p_enable) {
  980. JointBullet *joint = joint_owner.get(p_joint);
  981. ERR_FAIL_COND(!joint);
  982. ERR_FAIL_COND(joint->get_type() != JOINT_6DOF);
  983. Generic6DOFJointBullet *generic_6dof_joint = static_cast<Generic6DOFJointBullet *>(joint);
  984. generic_6dof_joint->set_flag(p_axis, p_flag, p_enable);
  985. }
  986. bool BulletPhysicsServer::generic_6dof_joint_get_flag(RID p_joint, Vector3::Axis p_axis, G6DOFJointAxisFlag p_flag) {
  987. JointBullet *joint = joint_owner.get(p_joint);
  988. ERR_FAIL_COND_V(!joint, false);
  989. ERR_FAIL_COND_V(joint->get_type() != JOINT_6DOF, false);
  990. Generic6DOFJointBullet *generic_6dof_joint = static_cast<Generic6DOFJointBullet *>(joint);
  991. return generic_6dof_joint->get_flag(p_axis, p_flag);
  992. }
  993. void BulletPhysicsServer::free(RID p_rid) {
  994. if (shape_owner.owns(p_rid)) {
  995. ShapeBullet *shape = shape_owner.get(p_rid);
  996. // Notify the shape is configured
  997. for (Map<ShapeOwnerBullet *, int>::Element *element = shape->get_owners().front(); element; element = element->next()) {
  998. static_cast<ShapeOwnerBullet *>(element->key())->remove_shape(shape);
  999. }
  1000. shape_owner.free(p_rid);
  1001. bulletdelete(shape);
  1002. } else if (rigid_body_owner.owns(p_rid)) {
  1003. RigidBodyBullet *body = rigid_body_owner.get(p_rid);
  1004. body->set_space(NULL);
  1005. body->remove_all_shapes(true);
  1006. rigid_body_owner.free(p_rid);
  1007. bulletdelete(body);
  1008. } else if (soft_body_owner.owns(p_rid)) {
  1009. SoftBodyBullet *body = soft_body_owner.get(p_rid);
  1010. body->set_space(NULL);
  1011. soft_body_owner.free(p_rid);
  1012. bulletdelete(body);
  1013. } else if (area_owner.owns(p_rid)) {
  1014. AreaBullet *area = area_owner.get(p_rid);
  1015. area->set_space(NULL);
  1016. area->remove_all_shapes(true);
  1017. area_owner.free(p_rid);
  1018. bulletdelete(area);
  1019. } else if (joint_owner.owns(p_rid)) {
  1020. JointBullet *joint = joint_owner.get(p_rid);
  1021. joint->destroy_internal_constraint();
  1022. joint_owner.free(p_rid);
  1023. bulletdelete(joint);
  1024. } else if (space_owner.owns(p_rid)) {
  1025. SpaceBullet *space = space_owner.get(p_rid);
  1026. space->remove_all_collision_objects();
  1027. space_set_active(p_rid, false);
  1028. space_owner.free(p_rid);
  1029. bulletdelete(space);
  1030. } else {
  1031. ERR_EXPLAIN("Invalid ID");
  1032. ERR_FAIL();
  1033. }
  1034. }
  1035. void BulletPhysicsServer::init() {
  1036. BulletPhysicsDirectBodyState::initSingleton();
  1037. }
  1038. void BulletPhysicsServer::step(float p_deltaTime) {
  1039. if (!active)
  1040. return;
  1041. BulletPhysicsDirectBodyState::singleton_setDeltaTime(p_deltaTime);
  1042. for (int i = 0; i < active_spaces_count; ++i) {
  1043. active_spaces[i]->step(p_deltaTime);
  1044. }
  1045. }
  1046. void BulletPhysicsServer::sync() {
  1047. }
  1048. void BulletPhysicsServer::flush_queries() {
  1049. }
  1050. void BulletPhysicsServer::finish() {
  1051. BulletPhysicsDirectBodyState::destroySingleton();
  1052. }
  1053. int BulletPhysicsServer::get_process_info(ProcessInfo p_info) {
  1054. return 0;
  1055. }
  1056. CollisionObjectBullet *BulletPhysicsServer::get_collisin_object(RID p_object) const {
  1057. if (rigid_body_owner.owns(p_object)) {
  1058. return rigid_body_owner.getornull(p_object);
  1059. }
  1060. if (area_owner.owns(p_object)) {
  1061. return area_owner.getornull(p_object);
  1062. }
  1063. if (soft_body_owner.owns(p_object)) {
  1064. return soft_body_owner.getornull(p_object);
  1065. }
  1066. return NULL;
  1067. }
  1068. RigidCollisionObjectBullet *BulletPhysicsServer::get_rigid_collisin_object(RID p_object) const {
  1069. if (rigid_body_owner.owns(p_object)) {
  1070. return rigid_body_owner.getornull(p_object);
  1071. }
  1072. if (area_owner.owns(p_object)) {
  1073. return area_owner.getornull(p_object);
  1074. }
  1075. return NULL;
  1076. }