bullet_physics_server.cpp 43 KB

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