bullet_physics_server.cpp 43 KB

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