bullet_physics_server.cpp 43 KB

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