bullet_physics_server.cpp 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594
  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-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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 "cone_twist_joint_bullet.h"
  33. #include "core/error/error_macros.h"
  34. #include "core/object/class_db.h"
  35. #include "core/string/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_PRINT("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. void BulletPhysicsServer3D::_bind_methods() {
  68. //ClassDB::bind_method(D_METHOD("DoTest"), &BulletPhysicsServer3D::DoTest);
  69. }
  70. BulletPhysicsServer3D::BulletPhysicsServer3D() :
  71. PhysicsServer3D() {}
  72. BulletPhysicsServer3D::~BulletPhysicsServer3D() {}
  73. RID BulletPhysicsServer3D::shape_create(ShapeType p_shape) {
  74. ShapeBullet *shape = nullptr;
  75. switch (p_shape) {
  76. case SHAPE_PLANE: {
  77. shape = bulletnew(PlaneShapeBullet);
  78. } break;
  79. case SHAPE_SPHERE: {
  80. shape = bulletnew(SphereShapeBullet);
  81. } break;
  82. case SHAPE_BOX: {
  83. shape = bulletnew(BoxShapeBullet);
  84. } break;
  85. case SHAPE_CAPSULE: {
  86. shape = bulletnew(CapsuleShapeBullet);
  87. } break;
  88. case SHAPE_CYLINDER: {
  89. shape = bulletnew(CylinderShapeBullet);
  90. } break;
  91. case SHAPE_CONVEX_POLYGON: {
  92. shape = bulletnew(ConvexPolygonShapeBullet);
  93. } break;
  94. case SHAPE_CONCAVE_POLYGON: {
  95. shape = bulletnew(ConcavePolygonShapeBullet);
  96. } break;
  97. case SHAPE_HEIGHTMAP: {
  98. shape = bulletnew(HeightMapShapeBullet);
  99. } break;
  100. case SHAPE_RAY: {
  101. shape = bulletnew(RayShapeBullet);
  102. } break;
  103. case SHAPE_CUSTOM:
  104. default:
  105. ERR_FAIL_V(RID());
  106. break;
  107. }
  108. CreateThenReturnRID(shape_owner, shape)
  109. }
  110. void BulletPhysicsServer3D::shape_set_data(RID p_shape, const Variant &p_data) {
  111. ShapeBullet *shape = shape_owner.getornull(p_shape);
  112. ERR_FAIL_COND(!shape);
  113. shape->set_data(p_data);
  114. }
  115. void BulletPhysicsServer3D::shape_set_custom_solver_bias(RID p_shape, real_t p_bias) {
  116. //WARN_PRINT("Bias not supported by Bullet physics engine");
  117. }
  118. PhysicsServer3D::ShapeType BulletPhysicsServer3D::shape_get_type(RID p_shape) const {
  119. ShapeBullet *shape = shape_owner.getornull(p_shape);
  120. ERR_FAIL_COND_V(!shape, PhysicsServer3D::SHAPE_CUSTOM);
  121. return shape->get_type();
  122. }
  123. Variant BulletPhysicsServer3D::shape_get_data(RID p_shape) const {
  124. ShapeBullet *shape = shape_owner.getornull(p_shape);
  125. ERR_FAIL_COND_V(!shape, Variant());
  126. return shape->get_data();
  127. }
  128. void BulletPhysicsServer3D::shape_set_margin(RID p_shape, real_t p_margin) {
  129. ShapeBullet *shape = shape_owner.getornull(p_shape);
  130. ERR_FAIL_COND(!shape);
  131. shape->set_margin(p_margin);
  132. }
  133. real_t BulletPhysicsServer3D::shape_get_margin(RID p_shape) const {
  134. ShapeBullet *shape = shape_owner.getornull(p_shape);
  135. ERR_FAIL_COND_V(!shape, 0.0);
  136. return shape->get_margin();
  137. }
  138. real_t BulletPhysicsServer3D::shape_get_custom_solver_bias(RID p_shape) const {
  139. //WARN_PRINT("Bias not supported by Bullet physics engine");
  140. return 0.;
  141. }
  142. RID BulletPhysicsServer3D::space_create() {
  143. SpaceBullet *space = bulletnew(SpaceBullet);
  144. CreateThenReturnRID(space_owner, space);
  145. }
  146. void BulletPhysicsServer3D::space_set_active(RID p_space, bool p_active) {
  147. SpaceBullet *space = space_owner.getornull(p_space);
  148. ERR_FAIL_COND(!space);
  149. if (space_is_active(p_space) == p_active) {
  150. return;
  151. }
  152. if (p_active) {
  153. ++active_spaces_count;
  154. active_spaces.push_back(space);
  155. } else {
  156. --active_spaces_count;
  157. active_spaces.erase(space);
  158. }
  159. }
  160. bool BulletPhysicsServer3D::space_is_active(RID p_space) const {
  161. SpaceBullet *space = space_owner.getornull(p_space);
  162. ERR_FAIL_COND_V(!space, false);
  163. return -1 != active_spaces.find(space);
  164. }
  165. void BulletPhysicsServer3D::space_set_param(RID p_space, SpaceParameter p_param, real_t p_value) {
  166. SpaceBullet *space = space_owner.getornull(p_space);
  167. ERR_FAIL_COND(!space);
  168. space->set_param(p_param, p_value);
  169. }
  170. real_t BulletPhysicsServer3D::space_get_param(RID p_space, SpaceParameter p_param) const {
  171. SpaceBullet *space = space_owner.getornull(p_space);
  172. ERR_FAIL_COND_V(!space, 0);
  173. return space->get_param(p_param);
  174. }
  175. PhysicsDirectSpaceState3D *BulletPhysicsServer3D::space_get_direct_state(RID p_space) {
  176. SpaceBullet *space = space_owner.getornull(p_space);
  177. ERR_FAIL_COND_V(!space, nullptr);
  178. return space->get_direct_state();
  179. }
  180. void BulletPhysicsServer3D::space_set_debug_contacts(RID p_space, int p_max_contacts) {
  181. SpaceBullet *space = space_owner.getornull(p_space);
  182. ERR_FAIL_COND(!space);
  183. space->set_debug_contacts(p_max_contacts);
  184. }
  185. Vector<Vector3> BulletPhysicsServer3D::space_get_contacts(RID p_space) const {
  186. SpaceBullet *space = space_owner.getornull(p_space);
  187. ERR_FAIL_COND_V(!space, Vector<Vector3>());
  188. return space->get_debug_contacts();
  189. }
  190. int BulletPhysicsServer3D::space_get_contact_count(RID p_space) const {
  191. SpaceBullet *space = space_owner.getornull(p_space);
  192. ERR_FAIL_COND_V(!space, 0);
  193. return space->get_debug_contact_count();
  194. }
  195. RID BulletPhysicsServer3D::area_create() {
  196. AreaBullet *area = bulletnew(AreaBullet);
  197. area->set_collision_layer(1);
  198. area->set_collision_mask(1);
  199. CreateThenReturnRID(area_owner, area)
  200. }
  201. void BulletPhysicsServer3D::area_set_space(RID p_area, RID p_space) {
  202. AreaBullet *area = area_owner.getornull(p_area);
  203. ERR_FAIL_COND(!area);
  204. SpaceBullet *space = nullptr;
  205. if (p_space.is_valid()) {
  206. space = space_owner.getornull(p_space);
  207. ERR_FAIL_COND(!space);
  208. }
  209. area->set_space(space);
  210. }
  211. RID BulletPhysicsServer3D::area_get_space(RID p_area) const {
  212. AreaBullet *area = area_owner.getornull(p_area);
  213. return area->get_space()->get_self();
  214. }
  215. void BulletPhysicsServer3D::area_set_space_override_mode(RID p_area, AreaSpaceOverrideMode p_mode) {
  216. AreaBullet *area = area_owner.getornull(p_area);
  217. ERR_FAIL_COND(!area);
  218. area->set_spOv_mode(p_mode);
  219. }
  220. PhysicsServer3D::AreaSpaceOverrideMode BulletPhysicsServer3D::area_get_space_override_mode(RID p_area) const {
  221. AreaBullet *area = area_owner.getornull(p_area);
  222. ERR_FAIL_COND_V(!area, PhysicsServer3D::AREA_SPACE_OVERRIDE_DISABLED);
  223. return area->get_spOv_mode();
  224. }
  225. void BulletPhysicsServer3D::area_add_shape(RID p_area, RID p_shape, const Transform &p_transform, bool p_disabled) {
  226. AreaBullet *area = area_owner.getornull(p_area);
  227. ERR_FAIL_COND(!area);
  228. ShapeBullet *shape = shape_owner.getornull(p_shape);
  229. ERR_FAIL_COND(!shape);
  230. area->add_shape(shape, p_transform, p_disabled);
  231. }
  232. void BulletPhysicsServer3D::area_set_shape(RID p_area, int p_shape_idx, RID p_shape) {
  233. AreaBullet *area = area_owner.getornull(p_area);
  234. ERR_FAIL_COND(!area);
  235. ShapeBullet *shape = shape_owner.getornull(p_shape);
  236. ERR_FAIL_COND(!shape);
  237. area->set_shape(p_shape_idx, shape);
  238. }
  239. void BulletPhysicsServer3D::area_set_shape_transform(RID p_area, int p_shape_idx, const Transform &p_transform) {
  240. AreaBullet *area = area_owner.getornull(p_area);
  241. ERR_FAIL_COND(!area);
  242. area->set_shape_transform(p_shape_idx, p_transform);
  243. }
  244. int BulletPhysicsServer3D::area_get_shape_count(RID p_area) const {
  245. AreaBullet *area = area_owner.getornull(p_area);
  246. ERR_FAIL_COND_V(!area, 0);
  247. return area->get_shape_count();
  248. }
  249. RID BulletPhysicsServer3D::area_get_shape(RID p_area, int p_shape_idx) const {
  250. AreaBullet *area = area_owner.getornull(p_area);
  251. ERR_FAIL_COND_V(!area, RID());
  252. return area->get_shape(p_shape_idx)->get_self();
  253. }
  254. Transform BulletPhysicsServer3D::area_get_shape_transform(RID p_area, int p_shape_idx) const {
  255. AreaBullet *area = area_owner.getornull(p_area);
  256. ERR_FAIL_COND_V(!area, Transform());
  257. return area->get_shape_transform(p_shape_idx);
  258. }
  259. void BulletPhysicsServer3D::area_remove_shape(RID p_area, int p_shape_idx) {
  260. AreaBullet *area = area_owner.getornull(p_area);
  261. ERR_FAIL_COND(!area);
  262. return area->remove_shape_full(p_shape_idx);
  263. }
  264. void BulletPhysicsServer3D::area_clear_shapes(RID p_area) {
  265. AreaBullet *area = area_owner.getornull(p_area);
  266. ERR_FAIL_COND(!area);
  267. for (int i = area->get_shape_count(); 0 < i; --i) {
  268. area->remove_shape_full(0);
  269. }
  270. }
  271. void BulletPhysicsServer3D::area_set_shape_disabled(RID p_area, int p_shape_idx, bool p_disabled) {
  272. AreaBullet *area = area_owner.getornull(p_area);
  273. ERR_FAIL_COND(!area);
  274. area->set_shape_disabled(p_shape_idx, p_disabled);
  275. }
  276. void BulletPhysicsServer3D::area_attach_object_instance_id(RID p_area, ObjectID p_id) {
  277. if (space_owner.owns(p_area)) {
  278. return;
  279. }
  280. AreaBullet *area = area_owner.getornull(p_area);
  281. ERR_FAIL_COND(!area);
  282. area->set_instance_id(p_id);
  283. }
  284. ObjectID BulletPhysicsServer3D::area_get_object_instance_id(RID p_area) const {
  285. if (space_owner.owns(p_area)) {
  286. return ObjectID();
  287. }
  288. AreaBullet *area = area_owner.getornull(p_area);
  289. ERR_FAIL_COND_V(!area, ObjectID());
  290. return area->get_instance_id();
  291. }
  292. void BulletPhysicsServer3D::area_set_param(RID p_area, AreaParameter p_param, const Variant &p_value) {
  293. if (space_owner.owns(p_area)) {
  294. SpaceBullet *space = space_owner.getornull(p_area);
  295. if (space) {
  296. space->set_param(p_param, p_value);
  297. }
  298. } else {
  299. AreaBullet *area = area_owner.getornull(p_area);
  300. ERR_FAIL_COND(!area);
  301. area->set_param(p_param, p_value);
  302. }
  303. }
  304. Variant BulletPhysicsServer3D::area_get_param(RID p_area, AreaParameter p_param) const {
  305. if (space_owner.owns(p_area)) {
  306. SpaceBullet *space = space_owner.getornull(p_area);
  307. return space->get_param(p_param);
  308. } else {
  309. AreaBullet *area = area_owner.getornull(p_area);
  310. ERR_FAIL_COND_V(!area, Variant());
  311. return area->get_param(p_param);
  312. }
  313. }
  314. void BulletPhysicsServer3D::area_set_transform(RID p_area, const Transform &p_transform) {
  315. AreaBullet *area = area_owner.getornull(p_area);
  316. ERR_FAIL_COND(!area);
  317. area->set_transform(p_transform);
  318. }
  319. Transform BulletPhysicsServer3D::area_get_transform(RID p_area) const {
  320. AreaBullet *area = area_owner.getornull(p_area);
  321. ERR_FAIL_COND_V(!area, Transform());
  322. return area->get_transform();
  323. }
  324. void BulletPhysicsServer3D::area_set_collision_mask(RID p_area, uint32_t p_mask) {
  325. AreaBullet *area = area_owner.getornull(p_area);
  326. ERR_FAIL_COND(!area);
  327. area->set_collision_mask(p_mask);
  328. }
  329. void BulletPhysicsServer3D::area_set_collision_layer(RID p_area, uint32_t p_layer) {
  330. AreaBullet *area = area_owner.getornull(p_area);
  331. ERR_FAIL_COND(!area);
  332. area->set_collision_layer(p_layer);
  333. }
  334. void BulletPhysicsServer3D::area_set_monitorable(RID p_area, bool p_monitorable) {
  335. AreaBullet *area = area_owner.getornull(p_area);
  336. ERR_FAIL_COND(!area);
  337. area->set_monitorable(p_monitorable);
  338. }
  339. void BulletPhysicsServer3D::area_set_monitor_callback(RID p_area, Object *p_receiver, const StringName &p_method) {
  340. AreaBullet *area = area_owner.getornull(p_area);
  341. ERR_FAIL_COND(!area);
  342. area->set_event_callback(CollisionObjectBullet::TYPE_RIGID_BODY, p_receiver ? p_receiver->get_instance_id() : ObjectID(), p_method);
  343. }
  344. void BulletPhysicsServer3D::area_set_area_monitor_callback(RID p_area, Object *p_receiver, const StringName &p_method) {
  345. AreaBullet *area = area_owner.getornull(p_area);
  346. ERR_FAIL_COND(!area);
  347. area->set_event_callback(CollisionObjectBullet::TYPE_AREA, p_receiver ? p_receiver->get_instance_id() : ObjectID(), p_method);
  348. }
  349. void BulletPhysicsServer3D::area_set_ray_pickable(RID p_area, bool p_enable) {
  350. AreaBullet *area = area_owner.getornull(p_area);
  351. ERR_FAIL_COND(!area);
  352. area->set_ray_pickable(p_enable);
  353. }
  354. bool BulletPhysicsServer3D::area_is_ray_pickable(RID p_area) const {
  355. AreaBullet *area = area_owner.getornull(p_area);
  356. ERR_FAIL_COND_V(!area, false);
  357. return area->is_ray_pickable();
  358. }
  359. RID BulletPhysicsServer3D::body_create(BodyMode p_mode, bool p_init_sleeping) {
  360. RigidBodyBullet *body = bulletnew(RigidBodyBullet);
  361. body->set_mode(p_mode);
  362. body->set_collision_layer(1);
  363. body->set_collision_mask(1);
  364. if (p_init_sleeping) {
  365. body->set_state(BODY_STATE_SLEEPING, p_init_sleeping);
  366. }
  367. CreateThenReturnRID(rigid_body_owner, body);
  368. }
  369. void BulletPhysicsServer3D::body_set_space(RID p_body, RID p_space) {
  370. RigidBodyBullet *body = rigid_body_owner.getornull(p_body);
  371. ERR_FAIL_COND(!body);
  372. SpaceBullet *space = nullptr;
  373. if (p_space.is_valid()) {
  374. space = space_owner.getornull(p_space);
  375. ERR_FAIL_COND(!space);
  376. }
  377. if (body->get_space() == space) {
  378. return; //pointless
  379. }
  380. body->set_space(space);
  381. }
  382. RID BulletPhysicsServer3D::body_get_space(RID p_body) const {
  383. RigidBodyBullet *body = rigid_body_owner.getornull(p_body);
  384. ERR_FAIL_COND_V(!body, RID());
  385. SpaceBullet *space = body->get_space();
  386. if (!space) {
  387. return RID();
  388. }
  389. return space->get_self();
  390. }
  391. void BulletPhysicsServer3D::body_set_mode(RID p_body, PhysicsServer3D::BodyMode p_mode) {
  392. RigidBodyBullet *body = rigid_body_owner.getornull(p_body);
  393. ERR_FAIL_COND(!body);
  394. body->set_mode(p_mode);
  395. }
  396. PhysicsServer3D::BodyMode BulletPhysicsServer3D::body_get_mode(RID p_body) const {
  397. RigidBodyBullet *body = rigid_body_owner.getornull(p_body);
  398. ERR_FAIL_COND_V(!body, BODY_MODE_STATIC);
  399. return body->get_mode();
  400. }
  401. void BulletPhysicsServer3D::body_add_shape(RID p_body, RID p_shape, const Transform &p_transform, bool p_disabled) {
  402. RigidBodyBullet *body = rigid_body_owner.getornull(p_body);
  403. ERR_FAIL_COND(!body);
  404. ShapeBullet *shape = shape_owner.getornull(p_shape);
  405. ERR_FAIL_COND(!shape);
  406. body->add_shape(shape, p_transform, p_disabled);
  407. }
  408. void BulletPhysicsServer3D::body_set_shape(RID p_body, int p_shape_idx, RID p_shape) {
  409. RigidBodyBullet *body = rigid_body_owner.getornull(p_body);
  410. ERR_FAIL_COND(!body);
  411. ShapeBullet *shape = shape_owner.getornull(p_shape);
  412. ERR_FAIL_COND(!shape);
  413. body->set_shape(p_shape_idx, shape);
  414. }
  415. void BulletPhysicsServer3D::body_set_shape_transform(RID p_body, int p_shape_idx, const Transform &p_transform) {
  416. RigidBodyBullet *body = rigid_body_owner.getornull(p_body);
  417. ERR_FAIL_COND(!body);
  418. body->set_shape_transform(p_shape_idx, p_transform);
  419. }
  420. int BulletPhysicsServer3D::body_get_shape_count(RID p_body) const {
  421. RigidBodyBullet *body = rigid_body_owner.getornull(p_body);
  422. ERR_FAIL_COND_V(!body, 0);
  423. return body->get_shape_count();
  424. }
  425. RID BulletPhysicsServer3D::body_get_shape(RID p_body, int p_shape_idx) const {
  426. RigidBodyBullet *body = rigid_body_owner.getornull(p_body);
  427. ERR_FAIL_COND_V(!body, RID());
  428. ShapeBullet *shape = body->get_shape(p_shape_idx);
  429. ERR_FAIL_COND_V(!shape, RID());
  430. return shape->get_self();
  431. }
  432. Transform BulletPhysicsServer3D::body_get_shape_transform(RID p_body, int p_shape_idx) const {
  433. RigidBodyBullet *body = rigid_body_owner.getornull(p_body);
  434. ERR_FAIL_COND_V(!body, Transform());
  435. return body->get_shape_transform(p_shape_idx);
  436. }
  437. void BulletPhysicsServer3D::body_set_shape_disabled(RID p_body, int p_shape_idx, bool p_disabled) {
  438. RigidBodyBullet *body = rigid_body_owner.getornull(p_body);
  439. ERR_FAIL_COND(!body);
  440. body->set_shape_disabled(p_shape_idx, p_disabled);
  441. }
  442. void BulletPhysicsServer3D::body_remove_shape(RID p_body, int p_shape_idx) {
  443. RigidBodyBullet *body = rigid_body_owner.getornull(p_body);
  444. ERR_FAIL_COND(!body);
  445. body->remove_shape_full(p_shape_idx);
  446. }
  447. void BulletPhysicsServer3D::body_clear_shapes(RID p_body) {
  448. RigidBodyBullet *body = rigid_body_owner.getornull(p_body);
  449. ERR_FAIL_COND(!body);
  450. body->remove_all_shapes();
  451. }
  452. void BulletPhysicsServer3D::body_attach_object_instance_id(RID p_body, ObjectID p_id) {
  453. CollisionObjectBullet *body = get_collision_object(p_body);
  454. ERR_FAIL_COND(!body);
  455. body->set_instance_id(p_id);
  456. }
  457. ObjectID BulletPhysicsServer3D::body_get_object_instance_id(RID p_body) const {
  458. CollisionObjectBullet *body = get_collision_object(p_body);
  459. ERR_FAIL_COND_V(!body, ObjectID());
  460. return body->get_instance_id();
  461. }
  462. void BulletPhysicsServer3D::body_set_enable_continuous_collision_detection(RID p_body, bool p_enable) {
  463. RigidBodyBullet *body = rigid_body_owner.getornull(p_body);
  464. ERR_FAIL_COND(!body);
  465. body->set_continuous_collision_detection(p_enable);
  466. }
  467. bool BulletPhysicsServer3D::body_is_continuous_collision_detection_enabled(RID p_body) const {
  468. RigidBodyBullet *body = rigid_body_owner.getornull(p_body);
  469. ERR_FAIL_COND_V(!body, false);
  470. return body->is_continuous_collision_detection_enabled();
  471. }
  472. void BulletPhysicsServer3D::body_set_collision_layer(RID p_body, uint32_t p_layer) {
  473. RigidBodyBullet *body = rigid_body_owner.getornull(p_body);
  474. ERR_FAIL_COND(!body);
  475. body->set_collision_layer(p_layer);
  476. }
  477. uint32_t BulletPhysicsServer3D::body_get_collision_layer(RID p_body) const {
  478. const RigidBodyBullet *body = rigid_body_owner.getornull(p_body);
  479. ERR_FAIL_COND_V(!body, 0);
  480. return body->get_collision_layer();
  481. }
  482. void BulletPhysicsServer3D::body_set_collision_mask(RID p_body, uint32_t p_mask) {
  483. RigidBodyBullet *body = rigid_body_owner.getornull(p_body);
  484. ERR_FAIL_COND(!body);
  485. body->set_collision_mask(p_mask);
  486. }
  487. uint32_t BulletPhysicsServer3D::body_get_collision_mask(RID p_body) const {
  488. RigidBodyBullet *body = rigid_body_owner.getornull(p_body);
  489. ERR_FAIL_COND_V(!body, 0);
  490. return body->get_collision_mask();
  491. }
  492. void BulletPhysicsServer3D::body_set_user_flags(RID p_body, uint32_t p_flags) {
  493. // This function is not currently supported
  494. }
  495. uint32_t BulletPhysicsServer3D::body_get_user_flags(RID p_body) const {
  496. // This function is not currently supported
  497. return 0;
  498. }
  499. void BulletPhysicsServer3D::body_set_param(RID p_body, BodyParameter p_param, real_t p_value) {
  500. RigidBodyBullet *body = rigid_body_owner.getornull(p_body);
  501. ERR_FAIL_COND(!body);
  502. body->set_param(p_param, p_value);
  503. }
  504. real_t BulletPhysicsServer3D::body_get_param(RID p_body, BodyParameter p_param) const {
  505. RigidBodyBullet *body = rigid_body_owner.getornull(p_body);
  506. ERR_FAIL_COND_V(!body, 0);
  507. return body->get_param(p_param);
  508. }
  509. void BulletPhysicsServer3D::body_set_kinematic_safe_margin(RID p_body, real_t p_margin) {
  510. RigidBodyBullet *body = rigid_body_owner.getornull(p_body);
  511. ERR_FAIL_COND(!body);
  512. if (body->get_kinematic_utilities()) {
  513. body->get_kinematic_utilities()->setSafeMargin(p_margin);
  514. }
  515. }
  516. real_t BulletPhysicsServer3D::body_get_kinematic_safe_margin(RID p_body) const {
  517. RigidBodyBullet *body = rigid_body_owner.getornull(p_body);
  518. ERR_FAIL_COND_V(!body, 0);
  519. if (body->get_kinematic_utilities()) {
  520. return body->get_kinematic_utilities()->safe_margin;
  521. }
  522. return 0;
  523. }
  524. void BulletPhysicsServer3D::body_set_state(RID p_body, BodyState p_state, const Variant &p_variant) {
  525. RigidBodyBullet *body = rigid_body_owner.getornull(p_body);
  526. ERR_FAIL_COND(!body);
  527. body->set_state(p_state, p_variant);
  528. }
  529. Variant BulletPhysicsServer3D::body_get_state(RID p_body, BodyState p_state) const {
  530. RigidBodyBullet *body = rigid_body_owner.getornull(p_body);
  531. ERR_FAIL_COND_V(!body, Variant());
  532. return body->get_state(p_state);
  533. }
  534. void BulletPhysicsServer3D::body_set_applied_force(RID p_body, const Vector3 &p_force) {
  535. RigidBodyBullet *body = rigid_body_owner.getornull(p_body);
  536. ERR_FAIL_COND(!body);
  537. body->set_applied_force(p_force);
  538. }
  539. Vector3 BulletPhysicsServer3D::body_get_applied_force(RID p_body) const {
  540. RigidBodyBullet *body = rigid_body_owner.getornull(p_body);
  541. ERR_FAIL_COND_V(!body, Vector3());
  542. return body->get_applied_force();
  543. }
  544. void BulletPhysicsServer3D::body_set_applied_torque(RID p_body, const Vector3 &p_torque) {
  545. RigidBodyBullet *body = rigid_body_owner.getornull(p_body);
  546. ERR_FAIL_COND(!body);
  547. body->set_applied_torque(p_torque);
  548. }
  549. Vector3 BulletPhysicsServer3D::body_get_applied_torque(RID p_body) const {
  550. RigidBodyBullet *body = rigid_body_owner.getornull(p_body);
  551. ERR_FAIL_COND_V(!body, Vector3());
  552. return body->get_applied_torque();
  553. }
  554. void BulletPhysicsServer3D::body_add_central_force(RID p_body, const Vector3 &p_force) {
  555. RigidBodyBullet *body = rigid_body_owner.getornull(p_body);
  556. ERR_FAIL_COND(!body);
  557. body->apply_central_force(p_force);
  558. }
  559. void BulletPhysicsServer3D::body_add_force(RID p_body, const Vector3 &p_force, const Vector3 &p_position) {
  560. RigidBodyBullet *body = rigid_body_owner.getornull(p_body);
  561. ERR_FAIL_COND(!body);
  562. body->apply_force(p_force, p_position);
  563. }
  564. void BulletPhysicsServer3D::body_add_torque(RID p_body, const Vector3 &p_torque) {
  565. RigidBodyBullet *body = rigid_body_owner.getornull(p_body);
  566. ERR_FAIL_COND(!body);
  567. body->apply_torque(p_torque);
  568. }
  569. void BulletPhysicsServer3D::body_apply_central_impulse(RID p_body, const Vector3 &p_impulse) {
  570. RigidBodyBullet *body = rigid_body_owner.getornull(p_body);
  571. ERR_FAIL_COND(!body);
  572. body->apply_central_impulse(p_impulse);
  573. }
  574. void BulletPhysicsServer3D::body_apply_impulse(RID p_body, const Vector3 &p_impulse, const Vector3 &p_position) {
  575. RigidBodyBullet *body = rigid_body_owner.getornull(p_body);
  576. ERR_FAIL_COND(!body);
  577. body->apply_impulse(p_impulse, p_position);
  578. }
  579. void BulletPhysicsServer3D::body_apply_torque_impulse(RID p_body, const Vector3 &p_impulse) {
  580. RigidBodyBullet *body = rigid_body_owner.getornull(p_body);
  581. ERR_FAIL_COND(!body);
  582. body->apply_torque_impulse(p_impulse);
  583. }
  584. void BulletPhysicsServer3D::body_set_axis_velocity(RID p_body, const Vector3 &p_axis_velocity) {
  585. RigidBodyBullet *body = rigid_body_owner.getornull(p_body);
  586. ERR_FAIL_COND(!body);
  587. Vector3 v = body->get_linear_velocity();
  588. Vector3 axis = p_axis_velocity.normalized();
  589. v -= axis * axis.dot(v);
  590. v += p_axis_velocity;
  591. body->set_linear_velocity(v);
  592. }
  593. void BulletPhysicsServer3D::body_set_axis_lock(RID p_body, BodyAxis p_axis, bool p_lock) {
  594. RigidBodyBullet *body = rigid_body_owner.getornull(p_body);
  595. ERR_FAIL_COND(!body);
  596. body->set_axis_lock(p_axis, p_lock);
  597. }
  598. bool BulletPhysicsServer3D::body_is_axis_locked(RID p_body, BodyAxis p_axis) const {
  599. const RigidBodyBullet *body = rigid_body_owner.getornull(p_body);
  600. ERR_FAIL_COND_V(!body, 0);
  601. return body->is_axis_locked(p_axis);
  602. }
  603. void BulletPhysicsServer3D::body_add_collision_exception(RID p_body, RID p_body_b) {
  604. RigidBodyBullet *body = rigid_body_owner.getornull(p_body);
  605. ERR_FAIL_COND(!body);
  606. RigidBodyBullet *other_body = rigid_body_owner.getornull(p_body_b);
  607. ERR_FAIL_COND(!other_body);
  608. body->add_collision_exception(other_body);
  609. }
  610. void BulletPhysicsServer3D::body_remove_collision_exception(RID p_body, RID p_body_b) {
  611. RigidBodyBullet *body = rigid_body_owner.getornull(p_body);
  612. ERR_FAIL_COND(!body);
  613. RigidBodyBullet *other_body = rigid_body_owner.getornull(p_body_b);
  614. ERR_FAIL_COND(!other_body);
  615. body->remove_collision_exception(other_body);
  616. }
  617. void BulletPhysicsServer3D::body_get_collision_exceptions(RID p_body, List<RID> *p_exceptions) {
  618. RigidBodyBullet *body = rigid_body_owner.getornull(p_body);
  619. ERR_FAIL_COND(!body);
  620. for (int i = 0; i < body->get_exceptions().size(); i++) {
  621. p_exceptions->push_back(body->get_exceptions()[i]);
  622. }
  623. }
  624. void BulletPhysicsServer3D::body_set_max_contacts_reported(RID p_body, int p_contacts) {
  625. RigidBodyBullet *body = rigid_body_owner.getornull(p_body);
  626. ERR_FAIL_COND(!body);
  627. body->set_max_collisions_detection(p_contacts);
  628. }
  629. int BulletPhysicsServer3D::body_get_max_contacts_reported(RID p_body) const {
  630. RigidBodyBullet *body = rigid_body_owner.getornull(p_body);
  631. ERR_FAIL_COND_V(!body, 0);
  632. return body->get_max_collisions_detection();
  633. }
  634. void BulletPhysicsServer3D::body_set_contacts_reported_depth_threshold(RID p_body, real_t p_threshold) {
  635. // Not supported by bullet and even Godot
  636. }
  637. real_t BulletPhysicsServer3D::body_get_contacts_reported_depth_threshold(RID p_body) const {
  638. // Not supported by bullet and even Godot
  639. return 0.;
  640. }
  641. void BulletPhysicsServer3D::body_set_omit_force_integration(RID p_body, bool p_omit) {
  642. RigidBodyBullet *body = rigid_body_owner.getornull(p_body);
  643. ERR_FAIL_COND(!body);
  644. body->set_omit_forces_integration(p_omit);
  645. }
  646. bool BulletPhysicsServer3D::body_is_omitting_force_integration(RID p_body) const {
  647. RigidBodyBullet *body = rigid_body_owner.getornull(p_body);
  648. ERR_FAIL_COND_V(!body, false);
  649. return body->get_omit_forces_integration();
  650. }
  651. void BulletPhysicsServer3D::body_set_force_integration_callback(RID p_body, Object *p_receiver, const StringName &p_method, const Variant &p_udata) {
  652. RigidBodyBullet *body = rigid_body_owner.getornull(p_body);
  653. ERR_FAIL_COND(!body);
  654. body->set_force_integration_callback(p_receiver ? p_receiver->get_instance_id() : ObjectID(), p_method, p_udata);
  655. }
  656. void BulletPhysicsServer3D::body_set_ray_pickable(RID p_body, bool p_enable) {
  657. RigidBodyBullet *body = rigid_body_owner.getornull(p_body);
  658. ERR_FAIL_COND(!body);
  659. body->set_ray_pickable(p_enable);
  660. }
  661. bool BulletPhysicsServer3D::body_is_ray_pickable(RID p_body) const {
  662. RigidBodyBullet *body = rigid_body_owner.getornull(p_body);
  663. ERR_FAIL_COND_V(!body, false);
  664. return body->is_ray_pickable();
  665. }
  666. PhysicsDirectBodyState3D *BulletPhysicsServer3D::body_get_direct_state(RID p_body) {
  667. RigidBodyBullet *body = rigid_body_owner.getornull(p_body);
  668. ERR_FAIL_COND_V(!body, nullptr);
  669. return BulletPhysicsDirectBodyState3D::get_singleton(body);
  670. }
  671. bool BulletPhysicsServer3D::body_test_motion(RID p_body, const Transform &p_from, const Vector3 &p_motion, bool p_infinite_inertia, MotionResult *r_result, bool p_exclude_raycast_shapes) {
  672. RigidBodyBullet *body = rigid_body_owner.getornull(p_body);
  673. ERR_FAIL_COND_V(!body, false);
  674. ERR_FAIL_COND_V(!body->get_space(), false);
  675. return body->get_space()->test_body_motion(body, p_from, p_motion, p_infinite_inertia, r_result, p_exclude_raycast_shapes);
  676. }
  677. int BulletPhysicsServer3D::body_test_ray_separation(RID p_body, const Transform &p_transform, bool p_infinite_inertia, Vector3 &r_recover_motion, SeparationResult *r_results, int p_result_max, real_t p_margin) {
  678. RigidBodyBullet *body = rigid_body_owner.getornull(p_body);
  679. ERR_FAIL_COND_V(!body, 0);
  680. ERR_FAIL_COND_V(!body->get_space(), 0);
  681. return body->get_space()->test_ray_separation(body, p_transform, p_infinite_inertia, r_recover_motion, r_results, p_result_max, p_margin);
  682. }
  683. RID BulletPhysicsServer3D::soft_body_create(bool p_init_sleeping) {
  684. SoftBodyBullet *body = bulletnew(SoftBodyBullet);
  685. body->set_collision_layer(1);
  686. body->set_collision_mask(1);
  687. if (p_init_sleeping) {
  688. body->set_activation_state(false);
  689. }
  690. CreateThenReturnRID(soft_body_owner, body);
  691. }
  692. void BulletPhysicsServer3D::soft_body_update_rendering_server(RID p_body, class SoftBodyRenderingServerHandler *p_rendering_server_handler) {
  693. SoftBodyBullet *body = soft_body_owner.getornull(p_body);
  694. ERR_FAIL_COND(!body);
  695. body->update_rendering_server(p_rendering_server_handler);
  696. }
  697. void BulletPhysicsServer3D::soft_body_set_space(RID p_body, RID p_space) {
  698. SoftBodyBullet *body = soft_body_owner.getornull(p_body);
  699. ERR_FAIL_COND(!body);
  700. SpaceBullet *space = nullptr;
  701. if (p_space.is_valid()) {
  702. space = space_owner.getornull(p_space);
  703. ERR_FAIL_COND(!space);
  704. }
  705. if (body->get_space() == space) {
  706. return; //pointless
  707. }
  708. body->set_space(space);
  709. }
  710. RID BulletPhysicsServer3D::soft_body_get_space(RID p_body) const {
  711. SoftBodyBullet *body = soft_body_owner.getornull(p_body);
  712. ERR_FAIL_COND_V(!body, RID());
  713. SpaceBullet *space = body->get_space();
  714. if (!space) {
  715. return RID();
  716. }
  717. return space->get_self();
  718. }
  719. void BulletPhysicsServer3D::soft_body_set_mesh(RID p_body, const REF &p_mesh) {
  720. SoftBodyBullet *body = soft_body_owner.getornull(p_body);
  721. ERR_FAIL_COND(!body);
  722. body->set_soft_mesh(p_mesh);
  723. }
  724. void BulletPhysicsServer3D::soft_body_set_collision_layer(RID p_body, uint32_t p_layer) {
  725. SoftBodyBullet *body = soft_body_owner.getornull(p_body);
  726. ERR_FAIL_COND(!body);
  727. body->set_collision_layer(p_layer);
  728. }
  729. uint32_t BulletPhysicsServer3D::soft_body_get_collision_layer(RID p_body) const {
  730. const SoftBodyBullet *body = soft_body_owner.getornull(p_body);
  731. ERR_FAIL_COND_V(!body, 0);
  732. return body->get_collision_layer();
  733. }
  734. void BulletPhysicsServer3D::soft_body_set_collision_mask(RID p_body, uint32_t p_mask) {
  735. SoftBodyBullet *body = soft_body_owner.getornull(p_body);
  736. ERR_FAIL_COND(!body);
  737. body->set_collision_mask(p_mask);
  738. }
  739. uint32_t BulletPhysicsServer3D::soft_body_get_collision_mask(RID p_body) const {
  740. const SoftBodyBullet *body = soft_body_owner.getornull(p_body);
  741. ERR_FAIL_COND_V(!body, 0);
  742. return body->get_collision_mask();
  743. }
  744. void BulletPhysicsServer3D::soft_body_add_collision_exception(RID p_body, RID p_body_b) {
  745. SoftBodyBullet *body = soft_body_owner.getornull(p_body);
  746. ERR_FAIL_COND(!body);
  747. CollisionObjectBullet *other_body = rigid_body_owner.getornull(p_body_b);
  748. if (!other_body) {
  749. other_body = soft_body_owner.getornull(p_body_b);
  750. }
  751. ERR_FAIL_COND(!other_body);
  752. body->add_collision_exception(other_body);
  753. }
  754. void BulletPhysicsServer3D::soft_body_remove_collision_exception(RID p_body, RID p_body_b) {
  755. SoftBodyBullet *body = soft_body_owner.getornull(p_body);
  756. ERR_FAIL_COND(!body);
  757. CollisionObjectBullet *other_body = rigid_body_owner.getornull(p_body_b);
  758. if (!other_body) {
  759. other_body = soft_body_owner.getornull(p_body_b);
  760. }
  761. ERR_FAIL_COND(!other_body);
  762. body->remove_collision_exception(other_body);
  763. }
  764. void BulletPhysicsServer3D::soft_body_get_collision_exceptions(RID p_body, List<RID> *p_exceptions) {
  765. SoftBodyBullet *body = soft_body_owner.getornull(p_body);
  766. ERR_FAIL_COND(!body);
  767. for (int i = 0; i < body->get_exceptions().size(); i++) {
  768. p_exceptions->push_back(body->get_exceptions()[i]);
  769. }
  770. }
  771. void BulletPhysicsServer3D::soft_body_set_state(RID p_body, BodyState p_state, const Variant &p_variant) {
  772. // FIXME: Must be implemented.
  773. WARN_PRINT("soft_body_state is not implemented yet in Bullet backend.");
  774. }
  775. Variant BulletPhysicsServer3D::soft_body_get_state(RID p_body, BodyState p_state) const {
  776. // FIXME: Must be implemented.
  777. WARN_PRINT("soft_body_state is not implemented yet in Bullet backend.");
  778. return Variant();
  779. }
  780. void BulletPhysicsServer3D::soft_body_set_transform(RID p_body, const Transform &p_transform) {
  781. SoftBodyBullet *body = soft_body_owner.getornull(p_body);
  782. ERR_FAIL_COND(!body);
  783. body->set_soft_transform(p_transform);
  784. }
  785. Vector3 BulletPhysicsServer3D::soft_body_get_vertex_position(RID p_body, int vertex_index) const {
  786. const SoftBodyBullet *body = soft_body_owner.getornull(p_body);
  787. Vector3 pos;
  788. ERR_FAIL_COND_V(!body, pos);
  789. body->get_node_position(vertex_index, pos);
  790. return pos;
  791. }
  792. void BulletPhysicsServer3D::soft_body_set_ray_pickable(RID p_body, bool p_enable) {
  793. SoftBodyBullet *body = soft_body_owner.getornull(p_body);
  794. ERR_FAIL_COND(!body);
  795. body->set_ray_pickable(p_enable);
  796. }
  797. bool BulletPhysicsServer3D::soft_body_is_ray_pickable(RID p_body) const {
  798. SoftBodyBullet *body = soft_body_owner.getornull(p_body);
  799. ERR_FAIL_COND_V(!body, false);
  800. return body->is_ray_pickable();
  801. }
  802. void BulletPhysicsServer3D::soft_body_set_simulation_precision(RID p_body, int p_simulation_precision) {
  803. SoftBodyBullet *body = soft_body_owner.getornull(p_body);
  804. ERR_FAIL_COND(!body);
  805. body->set_simulation_precision(p_simulation_precision);
  806. }
  807. int BulletPhysicsServer3D::soft_body_get_simulation_precision(RID p_body) {
  808. SoftBodyBullet *body = soft_body_owner.getornull(p_body);
  809. ERR_FAIL_COND_V(!body, 0.f);
  810. return body->get_simulation_precision();
  811. }
  812. void BulletPhysicsServer3D::soft_body_set_total_mass(RID p_body, real_t p_total_mass) {
  813. SoftBodyBullet *body = soft_body_owner.getornull(p_body);
  814. ERR_FAIL_COND(!body);
  815. body->set_total_mass(p_total_mass);
  816. }
  817. real_t BulletPhysicsServer3D::soft_body_get_total_mass(RID p_body) {
  818. SoftBodyBullet *body = soft_body_owner.getornull(p_body);
  819. ERR_FAIL_COND_V(!body, 0.f);
  820. return body->get_total_mass();
  821. }
  822. void BulletPhysicsServer3D::soft_body_set_linear_stiffness(RID p_body, real_t p_stiffness) {
  823. SoftBodyBullet *body = soft_body_owner.getornull(p_body);
  824. ERR_FAIL_COND(!body);
  825. body->set_linear_stiffness(p_stiffness);
  826. }
  827. real_t BulletPhysicsServer3D::soft_body_get_linear_stiffness(RID p_body) {
  828. SoftBodyBullet *body = soft_body_owner.getornull(p_body);
  829. ERR_FAIL_COND_V(!body, 0.f);
  830. return body->get_linear_stiffness();
  831. }
  832. void BulletPhysicsServer3D::soft_body_set_angular_stiffness(RID p_body, real_t p_stiffness) {
  833. SoftBodyBullet *body = soft_body_owner.getornull(p_body);
  834. ERR_FAIL_COND(!body);
  835. body->set_angular_stiffness(p_stiffness);
  836. }
  837. real_t BulletPhysicsServer3D::soft_body_get_angular_stiffness(RID p_body) {
  838. SoftBodyBullet *body = soft_body_owner.getornull(p_body);
  839. ERR_FAIL_COND_V(!body, 0.f);
  840. return body->get_angular_stiffness();
  841. }
  842. void BulletPhysicsServer3D::soft_body_set_volume_stiffness(RID p_body, real_t p_stiffness) {
  843. SoftBodyBullet *body = soft_body_owner.getornull(p_body);
  844. ERR_FAIL_COND(!body);
  845. body->set_volume_stiffness(p_stiffness);
  846. }
  847. real_t BulletPhysicsServer3D::soft_body_get_volume_stiffness(RID p_body) {
  848. SoftBodyBullet *body = soft_body_owner.getornull(p_body);
  849. ERR_FAIL_COND_V(!body, 0.f);
  850. return body->get_volume_stiffness();
  851. }
  852. void BulletPhysicsServer3D::soft_body_set_pressure_coefficient(RID p_body, real_t p_pressure_coefficient) {
  853. SoftBodyBullet *body = soft_body_owner.getornull(p_body);
  854. ERR_FAIL_COND(!body);
  855. body->set_pressure_coefficient(p_pressure_coefficient);
  856. }
  857. real_t BulletPhysicsServer3D::soft_body_get_pressure_coefficient(RID p_body) {
  858. SoftBodyBullet *body = soft_body_owner.getornull(p_body);
  859. ERR_FAIL_COND_V(!body, 0.f);
  860. return body->get_pressure_coefficient();
  861. }
  862. void BulletPhysicsServer3D::soft_body_set_pose_matching_coefficient(RID p_body, real_t p_pose_matching_coefficient) {
  863. SoftBodyBullet *body = soft_body_owner.getornull(p_body);
  864. ERR_FAIL_COND(!body);
  865. return body->set_pose_matching_coefficient(p_pose_matching_coefficient);
  866. }
  867. real_t BulletPhysicsServer3D::soft_body_get_pose_matching_coefficient(RID p_body) {
  868. SoftBodyBullet *body = soft_body_owner.getornull(p_body);
  869. ERR_FAIL_COND_V(!body, 0.f);
  870. return body->get_pose_matching_coefficient();
  871. }
  872. void BulletPhysicsServer3D::soft_body_set_damping_coefficient(RID p_body, real_t p_damping_coefficient) {
  873. SoftBodyBullet *body = soft_body_owner.getornull(p_body);
  874. ERR_FAIL_COND(!body);
  875. body->set_damping_coefficient(p_damping_coefficient);
  876. }
  877. real_t BulletPhysicsServer3D::soft_body_get_damping_coefficient(RID p_body) {
  878. SoftBodyBullet *body = soft_body_owner.getornull(p_body);
  879. ERR_FAIL_COND_V(!body, 0.f);
  880. return body->get_damping_coefficient();
  881. }
  882. void BulletPhysicsServer3D::soft_body_set_drag_coefficient(RID p_body, real_t p_drag_coefficient) {
  883. SoftBodyBullet *body = soft_body_owner.getornull(p_body);
  884. ERR_FAIL_COND(!body);
  885. body->set_drag_coefficient(p_drag_coefficient);
  886. }
  887. real_t BulletPhysicsServer3D::soft_body_get_drag_coefficient(RID p_body) {
  888. SoftBodyBullet *body = soft_body_owner.getornull(p_body);
  889. ERR_FAIL_COND_V(!body, 0.f);
  890. return body->get_drag_coefficient();
  891. }
  892. void BulletPhysicsServer3D::soft_body_move_point(RID p_body, int p_point_index, const Vector3 &p_global_position) {
  893. SoftBodyBullet *body = soft_body_owner.getornull(p_body);
  894. ERR_FAIL_COND(!body);
  895. body->set_node_position(p_point_index, p_global_position);
  896. }
  897. Vector3 BulletPhysicsServer3D::soft_body_get_point_global_position(RID p_body, int p_point_index) {
  898. SoftBodyBullet *body = soft_body_owner.getornull(p_body);
  899. ERR_FAIL_COND_V(!body, Vector3(0., 0., 0.));
  900. Vector3 pos;
  901. body->get_node_position(p_point_index, pos);
  902. return pos;
  903. }
  904. Vector3 BulletPhysicsServer3D::soft_body_get_point_offset(RID p_body, int p_point_index) const {
  905. SoftBodyBullet *body = soft_body_owner.getornull(p_body);
  906. ERR_FAIL_COND_V(!body, Vector3());
  907. Vector3 res;
  908. body->get_node_offset(p_point_index, res);
  909. return res;
  910. }
  911. void BulletPhysicsServer3D::soft_body_remove_all_pinned_points(RID p_body) {
  912. SoftBodyBullet *body = soft_body_owner.getornull(p_body);
  913. ERR_FAIL_COND(!body);
  914. body->reset_all_node_mass();
  915. }
  916. void BulletPhysicsServer3D::soft_body_pin_point(RID p_body, int p_point_index, bool p_pin) {
  917. SoftBodyBullet *body = soft_body_owner.getornull(p_body);
  918. ERR_FAIL_COND(!body);
  919. body->set_node_mass(p_point_index, p_pin ? 0 : 1);
  920. }
  921. bool BulletPhysicsServer3D::soft_body_is_point_pinned(RID p_body, int p_point_index) {
  922. SoftBodyBullet *body = soft_body_owner.getornull(p_body);
  923. ERR_FAIL_COND_V(!body, 0.f);
  924. return body->get_node_mass(p_point_index);
  925. }
  926. PhysicsServer3D::JointType BulletPhysicsServer3D::joint_get_type(RID p_joint) const {
  927. JointBullet *joint = joint_owner.getornull(p_joint);
  928. ERR_FAIL_COND_V(!joint, JOINT_PIN);
  929. return joint->get_type();
  930. }
  931. void BulletPhysicsServer3D::joint_set_solver_priority(RID p_joint, int p_priority) {
  932. // Joint priority not supported by bullet
  933. }
  934. int BulletPhysicsServer3D::joint_get_solver_priority(RID p_joint) const {
  935. // Joint priority not supported by bullet
  936. return 0;
  937. }
  938. void BulletPhysicsServer3D::joint_disable_collisions_between_bodies(RID p_joint, const bool p_disable) {
  939. JointBullet *joint = joint_owner.getornull(p_joint);
  940. ERR_FAIL_COND(!joint);
  941. joint->disable_collisions_between_bodies(p_disable);
  942. }
  943. bool BulletPhysicsServer3D::joint_is_disabled_collisions_between_bodies(RID p_joint) const {
  944. JointBullet *joint(joint_owner.getornull(p_joint));
  945. ERR_FAIL_COND_V(!joint, false);
  946. return joint->is_disabled_collisions_between_bodies();
  947. }
  948. RID BulletPhysicsServer3D::joint_create_pin(RID p_body_A, const Vector3 &p_local_A, RID p_body_B, const Vector3 &p_local_B) {
  949. RigidBodyBullet *body_A = rigid_body_owner.getornull(p_body_A);
  950. ERR_FAIL_COND_V(!body_A, RID());
  951. JointAssertSpace(body_A, "A", RID());
  952. RigidBodyBullet *body_B = nullptr;
  953. if (p_body_B.is_valid()) {
  954. body_B = rigid_body_owner.getornull(p_body_B);
  955. JointAssertSpace(body_B, "B", RID());
  956. JointAssertSameSpace(body_A, body_B, RID());
  957. }
  958. ERR_FAIL_COND_V(body_A == body_B, RID());
  959. JointBullet *joint = bulletnew(PinJointBullet(body_A, p_local_A, body_B, p_local_B));
  960. AddJointToSpace(body_A, joint);
  961. CreateThenReturnRID(joint_owner, joint);
  962. }
  963. void BulletPhysicsServer3D::pin_joint_set_param(RID p_joint, PinJointParam p_param, real_t p_value) {
  964. JointBullet *joint = joint_owner.getornull(p_joint);
  965. ERR_FAIL_COND(!joint);
  966. ERR_FAIL_COND(joint->get_type() != JOINT_PIN);
  967. PinJointBullet *pin_joint = static_cast<PinJointBullet *>(joint);
  968. pin_joint->set_param(p_param, p_value);
  969. }
  970. real_t BulletPhysicsServer3D::pin_joint_get_param(RID p_joint, PinJointParam p_param) const {
  971. JointBullet *joint = joint_owner.getornull(p_joint);
  972. ERR_FAIL_COND_V(!joint, 0);
  973. ERR_FAIL_COND_V(joint->get_type() != JOINT_PIN, 0);
  974. PinJointBullet *pin_joint = static_cast<PinJointBullet *>(joint);
  975. return pin_joint->get_param(p_param);
  976. }
  977. void BulletPhysicsServer3D::pin_joint_set_local_a(RID p_joint, const Vector3 &p_A) {
  978. JointBullet *joint = joint_owner.getornull(p_joint);
  979. ERR_FAIL_COND(!joint);
  980. ERR_FAIL_COND(joint->get_type() != JOINT_PIN);
  981. PinJointBullet *pin_joint = static_cast<PinJointBullet *>(joint);
  982. pin_joint->setPivotInA(p_A);
  983. }
  984. Vector3 BulletPhysicsServer3D::pin_joint_get_local_a(RID p_joint) const {
  985. JointBullet *joint = joint_owner.getornull(p_joint);
  986. ERR_FAIL_COND_V(!joint, Vector3());
  987. ERR_FAIL_COND_V(joint->get_type() != JOINT_PIN, Vector3());
  988. PinJointBullet *pin_joint = static_cast<PinJointBullet *>(joint);
  989. return pin_joint->getPivotInA();
  990. }
  991. void BulletPhysicsServer3D::pin_joint_set_local_b(RID p_joint, const Vector3 &p_B) {
  992. JointBullet *joint = joint_owner.getornull(p_joint);
  993. ERR_FAIL_COND(!joint);
  994. ERR_FAIL_COND(joint->get_type() != JOINT_PIN);
  995. PinJointBullet *pin_joint = static_cast<PinJointBullet *>(joint);
  996. pin_joint->setPivotInB(p_B);
  997. }
  998. Vector3 BulletPhysicsServer3D::pin_joint_get_local_b(RID p_joint) const {
  999. JointBullet *joint = joint_owner.getornull(p_joint);
  1000. ERR_FAIL_COND_V(!joint, Vector3());
  1001. ERR_FAIL_COND_V(joint->get_type() != JOINT_PIN, Vector3());
  1002. PinJointBullet *pin_joint = static_cast<PinJointBullet *>(joint);
  1003. return pin_joint->getPivotInB();
  1004. }
  1005. RID BulletPhysicsServer3D::joint_create_hinge(RID p_body_A, const Transform &p_hinge_A, RID p_body_B, const Transform &p_hinge_B) {
  1006. RigidBodyBullet *body_A = rigid_body_owner.getornull(p_body_A);
  1007. ERR_FAIL_COND_V(!body_A, RID());
  1008. JointAssertSpace(body_A, "A", RID());
  1009. RigidBodyBullet *body_B = nullptr;
  1010. if (p_body_B.is_valid()) {
  1011. body_B = rigid_body_owner.getornull(p_body_B);
  1012. JointAssertSpace(body_B, "B", RID());
  1013. JointAssertSameSpace(body_A, body_B, RID());
  1014. }
  1015. ERR_FAIL_COND_V(body_A == body_B, RID());
  1016. JointBullet *joint = bulletnew(HingeJointBullet(body_A, body_B, p_hinge_A, p_hinge_B));
  1017. AddJointToSpace(body_A, joint);
  1018. CreateThenReturnRID(joint_owner, joint);
  1019. }
  1020. RID BulletPhysicsServer3D::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) {
  1021. RigidBodyBullet *body_A = rigid_body_owner.getornull(p_body_A);
  1022. ERR_FAIL_COND_V(!body_A, RID());
  1023. JointAssertSpace(body_A, "A", RID());
  1024. RigidBodyBullet *body_B = nullptr;
  1025. if (p_body_B.is_valid()) {
  1026. body_B = rigid_body_owner.getornull(p_body_B);
  1027. JointAssertSpace(body_B, "B", RID());
  1028. JointAssertSameSpace(body_A, body_B, RID());
  1029. }
  1030. ERR_FAIL_COND_V(body_A == body_B, RID());
  1031. JointBullet *joint = bulletnew(HingeJointBullet(body_A, body_B, p_pivot_A, p_pivot_B, p_axis_A, p_axis_B));
  1032. AddJointToSpace(body_A, joint);
  1033. CreateThenReturnRID(joint_owner, joint);
  1034. }
  1035. void BulletPhysicsServer3D::hinge_joint_set_param(RID p_joint, HingeJointParam p_param, real_t p_value) {
  1036. JointBullet *joint = joint_owner.getornull(p_joint);
  1037. ERR_FAIL_COND(!joint);
  1038. ERR_FAIL_COND(joint->get_type() != JOINT_HINGE);
  1039. HingeJointBullet *hinge_joint = static_cast<HingeJointBullet *>(joint);
  1040. hinge_joint->set_param(p_param, p_value);
  1041. }
  1042. real_t BulletPhysicsServer3D::hinge_joint_get_param(RID p_joint, HingeJointParam p_param) const {
  1043. JointBullet *joint = joint_owner.getornull(p_joint);
  1044. ERR_FAIL_COND_V(!joint, 0);
  1045. ERR_FAIL_COND_V(joint->get_type() != JOINT_HINGE, 0);
  1046. HingeJointBullet *hinge_joint = static_cast<HingeJointBullet *>(joint);
  1047. return hinge_joint->get_param(p_param);
  1048. }
  1049. void BulletPhysicsServer3D::hinge_joint_set_flag(RID p_joint, HingeJointFlag p_flag, bool p_value) {
  1050. JointBullet *joint = joint_owner.getornull(p_joint);
  1051. ERR_FAIL_COND(!joint);
  1052. ERR_FAIL_COND(joint->get_type() != JOINT_HINGE);
  1053. HingeJointBullet *hinge_joint = static_cast<HingeJointBullet *>(joint);
  1054. hinge_joint->set_flag(p_flag, p_value);
  1055. }
  1056. bool BulletPhysicsServer3D::hinge_joint_get_flag(RID p_joint, HingeJointFlag p_flag) const {
  1057. JointBullet *joint = joint_owner.getornull(p_joint);
  1058. ERR_FAIL_COND_V(!joint, false);
  1059. ERR_FAIL_COND_V(joint->get_type() != JOINT_HINGE, false);
  1060. HingeJointBullet *hinge_joint = static_cast<HingeJointBullet *>(joint);
  1061. return hinge_joint->get_flag(p_flag);
  1062. }
  1063. RID BulletPhysicsServer3D::joint_create_slider(RID p_body_A, const Transform &p_local_frame_A, RID p_body_B, const Transform &p_local_frame_B) {
  1064. RigidBodyBullet *body_A = rigid_body_owner.getornull(p_body_A);
  1065. ERR_FAIL_COND_V(!body_A, RID());
  1066. JointAssertSpace(body_A, "A", RID());
  1067. RigidBodyBullet *body_B = nullptr;
  1068. if (p_body_B.is_valid()) {
  1069. body_B = rigid_body_owner.getornull(p_body_B);
  1070. JointAssertSpace(body_B, "B", RID());
  1071. JointAssertSameSpace(body_A, body_B, RID());
  1072. }
  1073. ERR_FAIL_COND_V(body_A == body_B, RID());
  1074. JointBullet *joint = bulletnew(SliderJointBullet(body_A, body_B, p_local_frame_A, p_local_frame_B));
  1075. AddJointToSpace(body_A, joint);
  1076. CreateThenReturnRID(joint_owner, joint);
  1077. }
  1078. void BulletPhysicsServer3D::slider_joint_set_param(RID p_joint, SliderJointParam p_param, real_t p_value) {
  1079. JointBullet *joint = joint_owner.getornull(p_joint);
  1080. ERR_FAIL_COND(!joint);
  1081. ERR_FAIL_COND(joint->get_type() != JOINT_SLIDER);
  1082. SliderJointBullet *slider_joint = static_cast<SliderJointBullet *>(joint);
  1083. slider_joint->set_param(p_param, p_value);
  1084. }
  1085. real_t BulletPhysicsServer3D::slider_joint_get_param(RID p_joint, SliderJointParam p_param) const {
  1086. JointBullet *joint = joint_owner.getornull(p_joint);
  1087. ERR_FAIL_COND_V(!joint, 0);
  1088. ERR_FAIL_COND_V(joint->get_type() != JOINT_SLIDER, 0);
  1089. SliderJointBullet *slider_joint = static_cast<SliderJointBullet *>(joint);
  1090. return slider_joint->get_param(p_param);
  1091. }
  1092. RID BulletPhysicsServer3D::joint_create_cone_twist(RID p_body_A, const Transform &p_local_frame_A, RID p_body_B, const Transform &p_local_frame_B) {
  1093. RigidBodyBullet *body_A = rigid_body_owner.getornull(p_body_A);
  1094. ERR_FAIL_COND_V(!body_A, RID());
  1095. JointAssertSpace(body_A, "A", RID());
  1096. RigidBodyBullet *body_B = nullptr;
  1097. if (p_body_B.is_valid()) {
  1098. body_B = rigid_body_owner.getornull(p_body_B);
  1099. JointAssertSpace(body_B, "B", RID());
  1100. JointAssertSameSpace(body_A, body_B, RID());
  1101. }
  1102. JointBullet *joint = bulletnew(ConeTwistJointBullet(body_A, body_B, p_local_frame_A, p_local_frame_B));
  1103. AddJointToSpace(body_A, joint);
  1104. CreateThenReturnRID(joint_owner, joint);
  1105. }
  1106. void BulletPhysicsServer3D::cone_twist_joint_set_param(RID p_joint, ConeTwistJointParam p_param, real_t p_value) {
  1107. JointBullet *joint = joint_owner.getornull(p_joint);
  1108. ERR_FAIL_COND(!joint);
  1109. ERR_FAIL_COND(joint->get_type() != JOINT_CONE_TWIST);
  1110. ConeTwistJointBullet *coneTwist_joint = static_cast<ConeTwistJointBullet *>(joint);
  1111. coneTwist_joint->set_param(p_param, p_value);
  1112. }
  1113. real_t BulletPhysicsServer3D::cone_twist_joint_get_param(RID p_joint, ConeTwistJointParam p_param) const {
  1114. JointBullet *joint = joint_owner.getornull(p_joint);
  1115. ERR_FAIL_COND_V(!joint, 0.);
  1116. ERR_FAIL_COND_V(joint->get_type() != JOINT_CONE_TWIST, 0.);
  1117. ConeTwistJointBullet *coneTwist_joint = static_cast<ConeTwistJointBullet *>(joint);
  1118. return coneTwist_joint->get_param(p_param);
  1119. }
  1120. RID BulletPhysicsServer3D::joint_create_generic_6dof(RID p_body_A, const Transform &p_local_frame_A, RID p_body_B, const Transform &p_local_frame_B) {
  1121. RigidBodyBullet *body_A = rigid_body_owner.getornull(p_body_A);
  1122. ERR_FAIL_COND_V(!body_A, RID());
  1123. JointAssertSpace(body_A, "A", RID());
  1124. RigidBodyBullet *body_B = nullptr;
  1125. if (p_body_B.is_valid()) {
  1126. body_B = rigid_body_owner.getornull(p_body_B);
  1127. JointAssertSpace(body_B, "B", RID());
  1128. JointAssertSameSpace(body_A, body_B, RID());
  1129. }
  1130. ERR_FAIL_COND_V(body_A == body_B, RID());
  1131. JointBullet *joint = bulletnew(Generic6DOFJointBullet(body_A, body_B, p_local_frame_A, p_local_frame_B));
  1132. AddJointToSpace(body_A, joint);
  1133. CreateThenReturnRID(joint_owner, joint);
  1134. }
  1135. void BulletPhysicsServer3D::generic_6dof_joint_set_param(RID p_joint, Vector3::Axis p_axis, G6DOFJointAxisParam p_param, real_t p_value) {
  1136. JointBullet *joint = joint_owner.getornull(p_joint);
  1137. ERR_FAIL_COND(!joint);
  1138. ERR_FAIL_COND(joint->get_type() != JOINT_6DOF);
  1139. Generic6DOFJointBullet *generic_6dof_joint = static_cast<Generic6DOFJointBullet *>(joint);
  1140. generic_6dof_joint->set_param(p_axis, p_param, p_value);
  1141. }
  1142. real_t BulletPhysicsServer3D::generic_6dof_joint_get_param(RID p_joint, Vector3::Axis p_axis, G6DOFJointAxisParam p_param) {
  1143. JointBullet *joint = joint_owner.getornull(p_joint);
  1144. ERR_FAIL_COND_V(!joint, 0);
  1145. ERR_FAIL_COND_V(joint->get_type() != JOINT_6DOF, 0);
  1146. Generic6DOFJointBullet *generic_6dof_joint = static_cast<Generic6DOFJointBullet *>(joint);
  1147. return generic_6dof_joint->get_param(p_axis, p_param);
  1148. }
  1149. void BulletPhysicsServer3D::generic_6dof_joint_set_flag(RID p_joint, Vector3::Axis p_axis, G6DOFJointAxisFlag p_flag, bool p_enable) {
  1150. JointBullet *joint = joint_owner.getornull(p_joint);
  1151. ERR_FAIL_COND(!joint);
  1152. ERR_FAIL_COND(joint->get_type() != JOINT_6DOF);
  1153. Generic6DOFJointBullet *generic_6dof_joint = static_cast<Generic6DOFJointBullet *>(joint);
  1154. generic_6dof_joint->set_flag(p_axis, p_flag, p_enable);
  1155. }
  1156. bool BulletPhysicsServer3D::generic_6dof_joint_get_flag(RID p_joint, Vector3::Axis p_axis, G6DOFJointAxisFlag p_flag) {
  1157. JointBullet *joint = joint_owner.getornull(p_joint);
  1158. ERR_FAIL_COND_V(!joint, false);
  1159. ERR_FAIL_COND_V(joint->get_type() != JOINT_6DOF, false);
  1160. Generic6DOFJointBullet *generic_6dof_joint = static_cast<Generic6DOFJointBullet *>(joint);
  1161. return generic_6dof_joint->get_flag(p_axis, p_flag);
  1162. }
  1163. void BulletPhysicsServer3D::free(RID p_rid) {
  1164. if (shape_owner.owns(p_rid)) {
  1165. ShapeBullet *shape = shape_owner.getornull(p_rid);
  1166. // Notify the shape is configured
  1167. for (Map<ShapeOwnerBullet *, int>::Element *element = shape->get_owners().front(); element; element = element->next()) {
  1168. static_cast<ShapeOwnerBullet *>(element->key())->remove_shape_full(shape);
  1169. }
  1170. shape_owner.free(p_rid);
  1171. bulletdelete(shape);
  1172. } else if (rigid_body_owner.owns(p_rid)) {
  1173. RigidBodyBullet *body = rigid_body_owner.getornull(p_rid);
  1174. body->set_space(nullptr);
  1175. body->remove_all_shapes(true, true);
  1176. rigid_body_owner.free(p_rid);
  1177. bulletdelete(body);
  1178. } else if (soft_body_owner.owns(p_rid)) {
  1179. SoftBodyBullet *body = soft_body_owner.getornull(p_rid);
  1180. body->set_space(nullptr);
  1181. soft_body_owner.free(p_rid);
  1182. bulletdelete(body);
  1183. } else if (area_owner.owns(p_rid)) {
  1184. AreaBullet *area = area_owner.getornull(p_rid);
  1185. area->set_space(nullptr);
  1186. area->remove_all_shapes(true, true);
  1187. area_owner.free(p_rid);
  1188. bulletdelete(area);
  1189. } else if (joint_owner.owns(p_rid)) {
  1190. JointBullet *joint = joint_owner.getornull(p_rid);
  1191. joint->destroy_internal_constraint();
  1192. joint_owner.free(p_rid);
  1193. bulletdelete(joint);
  1194. } else if (space_owner.owns(p_rid)) {
  1195. SpaceBullet *space = space_owner.getornull(p_rid);
  1196. space->remove_all_collision_objects();
  1197. space_set_active(p_rid, false);
  1198. space_owner.free(p_rid);
  1199. bulletdelete(space);
  1200. } else {
  1201. ERR_FAIL_MSG("Invalid ID.");
  1202. }
  1203. }
  1204. void BulletPhysicsServer3D::init() {
  1205. BulletPhysicsDirectBodyState3D::initSingleton();
  1206. }
  1207. void BulletPhysicsServer3D::step(real_t p_deltaTime) {
  1208. if (!active) {
  1209. return;
  1210. }
  1211. BulletPhysicsDirectBodyState3D::singleton_setDeltaTime(p_deltaTime);
  1212. for (int i = 0; i < active_spaces_count; ++i) {
  1213. active_spaces[i]->step(p_deltaTime);
  1214. }
  1215. }
  1216. void BulletPhysicsServer3D::flush_queries() {
  1217. if (!active) {
  1218. return;
  1219. }
  1220. for (int i = 0; i < active_spaces_count; ++i) {
  1221. active_spaces[i]->flush_queries();
  1222. }
  1223. }
  1224. void BulletPhysicsServer3D::finish() {
  1225. BulletPhysicsDirectBodyState3D::destroySingleton();
  1226. }
  1227. int BulletPhysicsServer3D::get_process_info(ProcessInfo p_info) {
  1228. return 0;
  1229. }
  1230. SpaceBullet *BulletPhysicsServer3D::get_space(RID p_rid) const {
  1231. ERR_FAIL_COND_V_MSG(space_owner.owns(p_rid) == false, nullptr, "The RID is not valid.");
  1232. return space_owner.getornull(p_rid);
  1233. }
  1234. ShapeBullet *BulletPhysicsServer3D::get_shape(RID p_rid) const {
  1235. ERR_FAIL_COND_V_MSG(shape_owner.owns(p_rid) == false, nullptr, "The RID is not valid.");
  1236. return shape_owner.getornull(p_rid);
  1237. }
  1238. CollisionObjectBullet *BulletPhysicsServer3D::get_collision_object(RID p_object) const {
  1239. if (rigid_body_owner.owns(p_object)) {
  1240. return rigid_body_owner.getornull(p_object);
  1241. }
  1242. if (area_owner.owns(p_object)) {
  1243. return area_owner.getornull(p_object);
  1244. }
  1245. if (soft_body_owner.owns(p_object)) {
  1246. return soft_body_owner.getornull(p_object);
  1247. }
  1248. ERR_FAIL_V_MSG(nullptr, "The RID is no valid.");
  1249. }
  1250. RigidCollisionObjectBullet *BulletPhysicsServer3D::get_rigid_collision_object(RID p_object) const {
  1251. if (rigid_body_owner.owns(p_object)) {
  1252. return rigid_body_owner.getornull(p_object);
  1253. }
  1254. if (area_owner.owns(p_object)) {
  1255. return area_owner.getornull(p_object);
  1256. }
  1257. ERR_FAIL_V_MSG(nullptr, "The RID is no valid.");
  1258. }
  1259. JointBullet *BulletPhysicsServer3D::get_joint(RID p_rid) const {
  1260. ERR_FAIL_COND_V_MSG(joint_owner.owns(p_rid) == false, nullptr, "The RID is not valid.");
  1261. return joint_owner.getornull(p_rid);
  1262. }