physics_server_sw.cpp 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069
  1. /*************************************************************************/
  2. /* physics_server_sw.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #include "physics_server_sw.h"
  30. #include "broad_phase_basic.h"
  31. #include "broad_phase_octree.h"
  32. RID PhysicsServerSW::shape_create(ShapeType p_shape) {
  33. ShapeSW *shape=NULL;
  34. switch(p_shape) {
  35. case SHAPE_PLANE: {
  36. shape=memnew( PlaneShapeSW );
  37. } break;
  38. case SHAPE_RAY: {
  39. shape=memnew( RayShapeSW );
  40. } break;
  41. case SHAPE_SPHERE: {
  42. shape=memnew( SphereShapeSW);
  43. } break;
  44. case SHAPE_BOX: {
  45. shape=memnew( BoxShapeSW);
  46. } break;
  47. case SHAPE_CAPSULE: {
  48. shape=memnew( CapsuleShapeSW );
  49. } break;
  50. case SHAPE_CONVEX_POLYGON: {
  51. shape=memnew( ConvexPolygonShapeSW );
  52. } break;
  53. case SHAPE_CONCAVE_POLYGON: {
  54. shape=memnew( ConcavePolygonShapeSW );
  55. } break;
  56. case SHAPE_HEIGHTMAP: {
  57. shape=memnew( HeightMapShapeSW );
  58. } break;
  59. case SHAPE_CUSTOM: {
  60. ERR_FAIL_V(RID());
  61. } break;
  62. }
  63. RID id = shape_owner.make_rid(shape);
  64. shape->set_self(id);
  65. return id;
  66. };
  67. void PhysicsServerSW::shape_set_data(RID p_shape, const Variant& p_data) {
  68. ShapeSW *shape = shape_owner.get(p_shape);
  69. ERR_FAIL_COND(!shape);
  70. shape->set_data(p_data);
  71. };
  72. void PhysicsServerSW::shape_set_custom_solver_bias(RID p_shape, real_t p_bias) {
  73. ShapeSW *shape = shape_owner.get(p_shape);
  74. ERR_FAIL_COND(!shape);
  75. shape->set_custom_bias(p_bias);
  76. }
  77. PhysicsServer::ShapeType PhysicsServerSW::shape_get_type(RID p_shape) const {
  78. const ShapeSW *shape = shape_owner.get(p_shape);
  79. ERR_FAIL_COND_V(!shape,SHAPE_CUSTOM);
  80. return shape->get_type();
  81. };
  82. Variant PhysicsServerSW::shape_get_data(RID p_shape) const {
  83. const ShapeSW *shape = shape_owner.get(p_shape);
  84. ERR_FAIL_COND_V(!shape,Variant());
  85. ERR_FAIL_COND_V(!shape->is_configured(),Variant());
  86. return shape->get_data();
  87. };
  88. real_t PhysicsServerSW::shape_get_custom_solver_bias(RID p_shape) const {
  89. const ShapeSW *shape = shape_owner.get(p_shape);
  90. ERR_FAIL_COND_V(!shape,0);
  91. return shape->get_custom_bias();
  92. }
  93. RID PhysicsServerSW::space_create() {
  94. SpaceSW *space = memnew( SpaceSW );
  95. RID id = space_owner.make_rid(space);
  96. space->set_self(id);
  97. RID area_id = area_create();
  98. AreaSW *area = area_owner.get(area_id);
  99. ERR_FAIL_COND_V(!area,RID());
  100. space->set_default_area(area);
  101. area->set_space(space);
  102. area->set_priority(-1);
  103. return id;
  104. };
  105. void PhysicsServerSW::space_set_active(RID p_space,bool p_active) {
  106. SpaceSW *space = space_owner.get(p_space);
  107. ERR_FAIL_COND(!space);
  108. if (p_active)
  109. active_spaces.insert(space);
  110. else
  111. active_spaces.erase(space);
  112. }
  113. bool PhysicsServerSW::space_is_active(RID p_space) const {
  114. const SpaceSW *space = space_owner.get(p_space);
  115. ERR_FAIL_COND_V(!space,false);
  116. return active_spaces.has(space);
  117. }
  118. void PhysicsServerSW::space_set_param(RID p_space,SpaceParameter p_param, real_t p_value) {
  119. SpaceSW *space = space_owner.get(p_space);
  120. ERR_FAIL_COND(!space);
  121. space->set_param(p_param,p_value);
  122. }
  123. real_t PhysicsServerSW::space_get_param(RID p_space,SpaceParameter p_param) const {
  124. const SpaceSW *space = space_owner.get(p_space);
  125. ERR_FAIL_COND_V(!space,0);
  126. return space->get_param(p_param);
  127. }
  128. PhysicsDirectSpaceState* PhysicsServerSW::space_get_direct_state(RID p_space) {
  129. SpaceSW *space = space_owner.get(p_space);
  130. ERR_FAIL_COND_V(!space,NULL);
  131. if (!doing_sync || space->is_locked()) {
  132. ERR_EXPLAIN("Space state is inaccesible right now, wait for iteration or fixed process notification.");
  133. ERR_FAIL_V(NULL);
  134. }
  135. return space->get_direct_state();
  136. }
  137. RID PhysicsServerSW::area_create() {
  138. AreaSW *area = memnew( AreaSW );
  139. RID rid = area_owner.make_rid(area);
  140. area->set_self(rid);
  141. return rid;
  142. };
  143. void PhysicsServerSW::area_set_space(RID p_area, RID p_space) {
  144. AreaSW *area = area_owner.get(p_area);
  145. ERR_FAIL_COND(!area);
  146. SpaceSW *space=NULL;
  147. if (p_space.is_valid()) {
  148. space = space_owner.get(p_space);
  149. ERR_FAIL_COND(!space);
  150. }
  151. area->set_space(space);
  152. };
  153. RID PhysicsServerSW::area_get_space(RID p_area) const {
  154. AreaSW *area = area_owner.get(p_area);
  155. ERR_FAIL_COND_V(!area,RID());
  156. SpaceSW *space = area->get_space();
  157. if (!space)
  158. return RID();
  159. return space->get_self();
  160. };
  161. void PhysicsServerSW::area_set_space_override_mode(RID p_area, AreaSpaceOverrideMode p_mode) {
  162. AreaSW *area = area_owner.get(p_area);
  163. ERR_FAIL_COND(!area);
  164. area->set_space_override_mode(p_mode);
  165. }
  166. PhysicsServer::AreaSpaceOverrideMode PhysicsServerSW::area_get_space_override_mode(RID p_area) const {
  167. const AreaSW *area = area_owner.get(p_area);
  168. ERR_FAIL_COND_V(!area,AREA_SPACE_OVERRIDE_DISABLED);
  169. return area->get_space_override_mode();
  170. }
  171. void PhysicsServerSW::area_add_shape(RID p_area, RID p_shape, const Transform& p_transform) {
  172. AreaSW *area = area_owner.get(p_area);
  173. ERR_FAIL_COND(!area);
  174. ShapeSW *shape = shape_owner.get(p_shape);
  175. ERR_FAIL_COND(!shape);
  176. area->add_shape(shape,p_transform);
  177. }
  178. void PhysicsServerSW::area_set_shape(RID p_area, int p_shape_idx,RID p_shape) {
  179. AreaSW *area = area_owner.get(p_area);
  180. ERR_FAIL_COND(!area);
  181. ShapeSW *shape = shape_owner.get(p_shape);
  182. ERR_FAIL_COND(!shape);
  183. ERR_FAIL_COND(!shape->is_configured());
  184. area->set_shape(p_shape_idx,shape);
  185. }
  186. void PhysicsServerSW::area_set_shape_transform(RID p_area, int p_shape_idx, const Transform& p_transform) {
  187. AreaSW *area = area_owner.get(p_area);
  188. ERR_FAIL_COND(!area);
  189. area->set_shape_transform(p_shape_idx,p_transform);
  190. }
  191. int PhysicsServerSW::area_get_shape_count(RID p_area) const {
  192. AreaSW *area = area_owner.get(p_area);
  193. ERR_FAIL_COND_V(!area,-1);
  194. return area->get_shape_count();
  195. }
  196. RID PhysicsServerSW::area_get_shape(RID p_area, int p_shape_idx) const {
  197. AreaSW *area = area_owner.get(p_area);
  198. ERR_FAIL_COND_V(!area,RID());
  199. ShapeSW *shape = area->get_shape(p_shape_idx);
  200. ERR_FAIL_COND_V(!shape,RID());
  201. return shape->get_self();
  202. }
  203. Transform PhysicsServerSW::area_get_shape_transform(RID p_area, int p_shape_idx) const {
  204. AreaSW *area = area_owner.get(p_area);
  205. ERR_FAIL_COND_V(!area,Transform());
  206. return area->get_shape_transform(p_shape_idx);
  207. }
  208. void PhysicsServerSW::area_remove_shape(RID p_area, int p_shape_idx) {
  209. AreaSW *area = area_owner.get(p_area);
  210. ERR_FAIL_COND(!area);
  211. area->remove_shape(p_shape_idx);
  212. }
  213. void PhysicsServerSW::area_clear_shapes(RID p_area) {
  214. AreaSW *area = area_owner.get(p_area);
  215. ERR_FAIL_COND(!area);
  216. while(area->get_shape_count())
  217. area->remove_shape(0);
  218. }
  219. void PhysicsServerSW::area_attach_object_instance_ID(RID p_area,ObjectID p_ID) {
  220. if (space_owner.owns(p_area)) {
  221. SpaceSW *space=space_owner.get(p_area);
  222. p_area=space->get_default_area()->get_self();
  223. }
  224. AreaSW *area = area_owner.get(p_area);
  225. ERR_FAIL_COND(!area);
  226. area->set_instance_id(p_ID);
  227. }
  228. ObjectID PhysicsServerSW::area_get_object_instance_ID(RID p_area) const {
  229. if (space_owner.owns(p_area)) {
  230. SpaceSW *space=space_owner.get(p_area);
  231. p_area=space->get_default_area()->get_self();
  232. }
  233. AreaSW *area = area_owner.get(p_area);
  234. ERR_FAIL_COND_V(!area,0);
  235. return area->get_instance_id();
  236. }
  237. void PhysicsServerSW::area_set_param(RID p_area,AreaParameter p_param,const Variant& p_value) {
  238. if (space_owner.owns(p_area)) {
  239. SpaceSW *space=space_owner.get(p_area);
  240. p_area=space->get_default_area()->get_self();
  241. }
  242. AreaSW *area = area_owner.get(p_area);
  243. ERR_FAIL_COND(!area);
  244. area->set_param(p_param,p_value);
  245. };
  246. void PhysicsServerSW::area_set_transform(RID p_area, const Transform& p_transform) {
  247. AreaSW *area = area_owner.get(p_area);
  248. ERR_FAIL_COND(!area);
  249. area->set_transform(p_transform);
  250. };
  251. Variant PhysicsServerSW::area_get_param(RID p_area,AreaParameter p_param) const {
  252. if (space_owner.owns(p_area)) {
  253. SpaceSW *space=space_owner.get(p_area);
  254. p_area=space->get_default_area()->get_self();
  255. }
  256. AreaSW *area = area_owner.get(p_area);
  257. ERR_FAIL_COND_V(!area,Variant());
  258. return area->get_param(p_param);
  259. };
  260. Transform PhysicsServerSW::area_get_transform(RID p_area) const {
  261. AreaSW *area = area_owner.get(p_area);
  262. ERR_FAIL_COND_V(!area,Transform());
  263. return area->get_transform();
  264. };
  265. void PhysicsServerSW::area_set_monitor_callback(RID p_area,Object *p_receiver,const StringName& p_method) {
  266. AreaSW *area = area_owner.get(p_area);
  267. ERR_FAIL_COND(!area);
  268. area->set_monitor_callback(p_receiver?p_receiver->get_instance_ID():0,p_method);
  269. }
  270. /* BODY API */
  271. RID PhysicsServerSW::body_create(BodyMode p_mode,bool p_init_sleeping) {
  272. BodySW *body = memnew( BodySW );
  273. if (p_mode!=BODY_MODE_RIGID)
  274. body->set_mode(p_mode);
  275. if (p_init_sleeping)
  276. body->set_state(BODY_STATE_SLEEPING,p_init_sleeping);
  277. RID rid = body_owner.make_rid(body);
  278. body->set_self(rid);
  279. return rid;
  280. };
  281. void PhysicsServerSW::body_set_space(RID p_body, RID p_space) {
  282. BodySW *body = body_owner.get(p_body);
  283. ERR_FAIL_COND(!body);
  284. SpaceSW *space=NULL;
  285. if (p_space.is_valid()) {
  286. space = space_owner.get(p_space);
  287. ERR_FAIL_COND(!space);
  288. }
  289. if (body->get_space()==space)
  290. return; //pointles
  291. body->set_space(space);
  292. };
  293. RID PhysicsServerSW::body_get_space(RID p_body) const {
  294. BodySW *body = body_owner.get(p_body);
  295. ERR_FAIL_COND_V(!body,RID());
  296. SpaceSW *space = body->get_space();
  297. if (!space)
  298. return RID();
  299. return space->get_self();
  300. };
  301. void PhysicsServerSW::body_set_mode(RID p_body, BodyMode p_mode) {
  302. BodySW *body = body_owner.get(p_body);
  303. ERR_FAIL_COND(!body);
  304. body->set_mode(p_mode);
  305. };
  306. PhysicsServer::BodyMode PhysicsServerSW::body_get_mode(RID p_body, BodyMode p_mode) const {
  307. BodySW *body = body_owner.get(p_body);
  308. ERR_FAIL_COND_V(!body,BODY_MODE_STATIC);
  309. return body->get_mode();
  310. };
  311. void PhysicsServerSW::body_add_shape(RID p_body, RID p_shape, const Transform& p_transform) {
  312. BodySW *body = body_owner.get(p_body);
  313. ERR_FAIL_COND(!body);
  314. ShapeSW *shape = shape_owner.get(p_shape);
  315. ERR_FAIL_COND(!shape);
  316. body->add_shape(shape,p_transform);
  317. }
  318. void PhysicsServerSW::body_set_shape(RID p_body, int p_shape_idx,RID p_shape) {
  319. BodySW *body = body_owner.get(p_body);
  320. ERR_FAIL_COND(!body);
  321. ShapeSW *shape = shape_owner.get(p_shape);
  322. ERR_FAIL_COND(!shape);
  323. ERR_FAIL_COND(!shape->is_configured());
  324. body->set_shape(p_shape_idx,shape);
  325. }
  326. void PhysicsServerSW::body_set_shape_transform(RID p_body, int p_shape_idx, const Transform& p_transform) {
  327. BodySW *body = body_owner.get(p_body);
  328. ERR_FAIL_COND(!body);
  329. body->set_shape_transform(p_shape_idx,p_transform);
  330. }
  331. int PhysicsServerSW::body_get_shape_count(RID p_body) const {
  332. BodySW *body = body_owner.get(p_body);
  333. ERR_FAIL_COND_V(!body,-1);
  334. return body->get_shape_count();
  335. }
  336. RID PhysicsServerSW::body_get_shape(RID p_body, int p_shape_idx) const {
  337. BodySW *body = body_owner.get(p_body);
  338. ERR_FAIL_COND_V(!body,RID());
  339. ShapeSW *shape = body->get_shape(p_shape_idx);
  340. ERR_FAIL_COND_V(!shape,RID());
  341. return shape->get_self();
  342. }
  343. void PhysicsServerSW::body_set_shape_as_trigger(RID p_body, int p_shape_idx,bool p_enable) {
  344. BodySW *body = body_owner.get(p_body);
  345. ERR_FAIL_COND(!body);
  346. }
  347. bool PhysicsServerSW::body_is_shape_set_as_trigger(RID p_body, int p_shape_idx) const{
  348. BodySW *body = body_owner.get(p_body);
  349. ERR_FAIL_COND_V(!body,false);
  350. // todo ?
  351. return false;
  352. }
  353. Transform PhysicsServerSW::body_get_shape_transform(RID p_body, int p_shape_idx) const {
  354. BodySW *body = body_owner.get(p_body);
  355. ERR_FAIL_COND_V(!body,Transform());
  356. return body->get_shape_transform(p_shape_idx);
  357. }
  358. void PhysicsServerSW::body_remove_shape(RID p_body, int p_shape_idx) {
  359. BodySW *body = body_owner.get(p_body);
  360. ERR_FAIL_COND(!body);
  361. body->remove_shape(p_shape_idx);
  362. }
  363. void PhysicsServerSW::body_clear_shapes(RID p_body) {
  364. BodySW *body = body_owner.get(p_body);
  365. ERR_FAIL_COND(!body);
  366. while(body->get_shape_count())
  367. body->remove_shape(0);
  368. }
  369. void PhysicsServerSW::body_set_enable_continuous_collision_detection(RID p_body,bool p_enable) {
  370. BodySW *body = body_owner.get(p_body);
  371. ERR_FAIL_COND(!body);
  372. body->set_continuous_collision_detection(p_enable);
  373. }
  374. bool PhysicsServerSW::body_is_continuous_collision_detection_enabled(RID p_body) const {
  375. BodySW *body = body_owner.get(p_body);
  376. ERR_FAIL_COND_V(!body,false);
  377. return body->is_continuous_collision_detection_enabled();
  378. }
  379. void PhysicsServerSW::body_attach_object_instance_ID(RID p_body,uint32_t p_ID) {
  380. BodySW *body = body_owner.get(p_body);
  381. ERR_FAIL_COND(!body);
  382. body->set_instance_id(p_ID);
  383. };
  384. uint32_t PhysicsServerSW::body_get_object_instance_ID(RID p_body) const {
  385. BodySW *body = body_owner.get(p_body);
  386. ERR_FAIL_COND_V(!body,0);
  387. return body->get_instance_id();
  388. };
  389. void PhysicsServerSW::body_set_user_flags(RID p_body, uint32_t p_flags) {
  390. BodySW *body = body_owner.get(p_body);
  391. ERR_FAIL_COND(!body);
  392. };
  393. uint32_t PhysicsServerSW::body_get_user_flags(RID p_body, uint32_t p_flags) const {
  394. BodySW *body = body_owner.get(p_body);
  395. ERR_FAIL_COND_V(!body,0);
  396. return 0;
  397. };
  398. void PhysicsServerSW::body_set_param(RID p_body, BodyParameter p_param, float p_value) {
  399. BodySW *body = body_owner.get(p_body);
  400. ERR_FAIL_COND(!body);
  401. body->set_param(p_param,p_value);
  402. };
  403. float PhysicsServerSW::body_get_param(RID p_body, BodyParameter p_param) const {
  404. BodySW *body = body_owner.get(p_body);
  405. ERR_FAIL_COND_V(!body,0);
  406. return body->get_param(p_param);
  407. };
  408. void PhysicsServerSW::body_static_simulate_motion(RID p_body,const Transform& p_new_transform) {
  409. BodySW *body = body_owner.get(p_body);
  410. ERR_FAIL_COND(!body);
  411. body->simulate_motion(p_new_transform,last_step);
  412. };
  413. void PhysicsServerSW::body_set_state(RID p_body, BodyState p_state, const Variant& p_variant) {
  414. BodySW *body = body_owner.get(p_body);
  415. ERR_FAIL_COND(!body);
  416. body->set_state(p_state,p_variant);
  417. };
  418. Variant PhysicsServerSW::body_get_state(RID p_body, BodyState p_state) const {
  419. BodySW *body = body_owner.get(p_body);
  420. ERR_FAIL_COND_V(!body,Variant());
  421. return body->get_state(p_state);
  422. };
  423. void PhysicsServerSW::body_set_applied_force(RID p_body, const Vector3& p_force) {
  424. BodySW *body = body_owner.get(p_body);
  425. ERR_FAIL_COND(!body);
  426. body->set_applied_force(p_force);
  427. };
  428. Vector3 PhysicsServerSW::body_get_applied_force(RID p_body) const {
  429. BodySW *body = body_owner.get(p_body);
  430. ERR_FAIL_COND_V(!body,Vector3());
  431. return body->get_applied_force();
  432. };
  433. void PhysicsServerSW::body_set_applied_torque(RID p_body, const Vector3& p_torque) {
  434. BodySW *body = body_owner.get(p_body);
  435. ERR_FAIL_COND(!body);
  436. body->set_applied_torque(p_torque);
  437. };
  438. Vector3 PhysicsServerSW::body_get_applied_torque(RID p_body) const {
  439. BodySW *body = body_owner.get(p_body);
  440. ERR_FAIL_COND_V(!body,Vector3());
  441. return body->get_applied_torque();
  442. };
  443. void PhysicsServerSW::body_apply_impulse(RID p_body, const Vector3& p_pos, const Vector3& p_impulse) {
  444. BodySW *body = body_owner.get(p_body);
  445. ERR_FAIL_COND(!body);
  446. body->apply_impulse(p_pos,p_impulse);
  447. };
  448. void PhysicsServerSW::body_set_axis_velocity(RID p_body, const Vector3& p_axis_velocity) {
  449. BodySW *body = body_owner.get(p_body);
  450. ERR_FAIL_COND(!body);
  451. Vector3 v = body->get_linear_velocity();
  452. Vector3 axis = p_axis_velocity.normalized();
  453. v-=axis*axis.dot(v);
  454. v+=p_axis_velocity;
  455. body->set_linear_velocity(v);
  456. };
  457. void PhysicsServerSW::body_set_axis_lock(RID p_body,BodyAxisLock p_lock) {
  458. BodySW *body = body_owner.get(p_body);
  459. ERR_FAIL_COND(!body);
  460. body->set_axis_lock(p_lock);
  461. }
  462. PhysicsServerSW::BodyAxisLock PhysicsServerSW::body_get_axis_lock(RID p_body) const{
  463. const BodySW *body = body_owner.get(p_body);
  464. ERR_FAIL_COND_V(!body,BODY_AXIS_LOCK_DISABLED);
  465. return body->get_axis_lock();
  466. }
  467. void PhysicsServerSW::body_add_collision_exception(RID p_body, RID p_body_b) {
  468. BodySW *body = body_owner.get(p_body);
  469. ERR_FAIL_COND(!body);
  470. body->add_exception(p_body_b);
  471. };
  472. void PhysicsServerSW::body_remove_collision_exception(RID p_body, RID p_body_b) {
  473. BodySW *body = body_owner.get(p_body);
  474. ERR_FAIL_COND(!body);
  475. body->remove_exception(p_body);
  476. };
  477. void PhysicsServerSW::body_get_collision_exceptions(RID p_body, List<RID> *p_exceptions) {
  478. BodySW *body = body_owner.get(p_body);
  479. ERR_FAIL_COND(!body);
  480. for(int i=0;i<body->get_exceptions().size();i++) {
  481. p_exceptions->push_back(body->get_exceptions()[i]);
  482. }
  483. };
  484. void PhysicsServerSW::body_set_contacts_reported_depth_treshold(RID p_body, float p_treshold) {
  485. BodySW *body = body_owner.get(p_body);
  486. ERR_FAIL_COND(!body);
  487. };
  488. float PhysicsServerSW::body_get_contacts_reported_depth_treshold(RID p_body) const {
  489. BodySW *body = body_owner.get(p_body);
  490. ERR_FAIL_COND_V(!body,0);
  491. return 0;
  492. };
  493. void PhysicsServerSW::body_set_omit_force_integration(RID p_body,bool p_omit) {
  494. BodySW *body = body_owner.get(p_body);
  495. ERR_FAIL_COND(!body);
  496. body->set_omit_force_integration(p_omit);
  497. };
  498. bool PhysicsServerSW::body_is_omitting_force_integration(RID p_body) const {
  499. BodySW *body = body_owner.get(p_body);
  500. ERR_FAIL_COND_V(!body,false);
  501. return body->get_omit_force_integration();
  502. };
  503. void PhysicsServerSW::body_set_max_contacts_reported(RID p_body, int p_contacts) {
  504. BodySW *body = body_owner.get(p_body);
  505. ERR_FAIL_COND(!body);
  506. body->set_max_contacts_reported(p_contacts);
  507. }
  508. int PhysicsServerSW::body_get_max_contacts_reported(RID p_body) const {
  509. BodySW *body = body_owner.get(p_body);
  510. ERR_FAIL_COND_V(!body,-1);
  511. return body->get_max_contacts_reported();
  512. }
  513. void PhysicsServerSW::body_set_force_integration_callback(RID p_body,Object *p_receiver,const StringName& p_method,const Variant& p_udata) {
  514. BodySW *body = body_owner.get(p_body);
  515. ERR_FAIL_COND(!body);
  516. body->set_force_integration_callback(p_receiver?p_receiver->get_instance_ID():ObjectID(0),p_method,p_udata);
  517. }
  518. /* JOINT API */
  519. #if 0
  520. void PhysicsServerSW::joint_set_param(RID p_joint, JointParam p_param, real_t p_value) {
  521. JointSW *joint = joint_owner.get(p_joint);
  522. ERR_FAIL_COND(!joint);
  523. switch(p_param) {
  524. case JOINT_PARAM_BIAS: joint->set_bias(p_value); break;
  525. case JOINT_PARAM_MAX_BIAS: joint->set_max_bias(p_value); break;
  526. case JOINT_PARAM_MAX_FORCE: joint->set_max_force(p_value); break;
  527. }
  528. }
  529. real_t PhysicsServerSW::joint_get_param(RID p_joint,JointParam p_param) const {
  530. const JointSW *joint = joint_owner.get(p_joint);
  531. ERR_FAIL_COND_V(!joint,-1);
  532. switch(p_param) {
  533. case JOINT_PARAM_BIAS: return joint->get_bias(); break;
  534. case JOINT_PARAM_MAX_BIAS: return joint->get_max_bias(); break;
  535. case JOINT_PARAM_MAX_FORCE: return joint->get_max_force(); break;
  536. }
  537. return 0;
  538. }
  539. RID PhysicsServerSW::pin_joint_create(const Vector3& p_pos,RID p_body_a,RID p_body_b) {
  540. BodySW *A=body_owner.get(p_body_a);
  541. ERR_FAIL_COND_V(!A,RID());
  542. BodySW *B=NULL;
  543. if (body_owner.owns(p_body_b)) {
  544. B=body_owner.get(p_body_b);
  545. ERR_FAIL_COND_V(!B,RID());
  546. }
  547. JointSW *joint = memnew( PinJointSW(p_pos,A,B) );
  548. RID self = joint_owner.make_rid(joint);
  549. joint->set_self(self);
  550. return self;
  551. }
  552. RID PhysicsServerSW::groove_joint_create(const Vector3& p_a_groove1,const Vector3& p_a_groove2, const Vector3& p_b_anchor, RID p_body_a,RID p_body_b) {
  553. BodySW *A=body_owner.get(p_body_a);
  554. ERR_FAIL_COND_V(!A,RID());
  555. BodySW *B=body_owner.get(p_body_b);
  556. ERR_FAIL_COND_V(!B,RID());
  557. JointSW *joint = memnew( GrooveJointSW(p_a_groove1,p_a_groove2,p_b_anchor,A,B) );
  558. RID self = joint_owner.make_rid(joint);
  559. joint->set_self(self);
  560. return self;
  561. }
  562. RID PhysicsServerSW::damped_spring_joint_create(const Vector3& p_anchor_a,const Vector3& p_anchor_b,RID p_body_a,RID p_body_b) {
  563. BodySW *A=body_owner.get(p_body_a);
  564. ERR_FAIL_COND_V(!A,RID());
  565. BodySW *B=body_owner.get(p_body_b);
  566. ERR_FAIL_COND_V(!B,RID());
  567. JointSW *joint = memnew( DampedSpringJointSW(p_anchor_a,p_anchor_b,A,B) );
  568. RID self = joint_owner.make_rid(joint);
  569. joint->set_self(self);
  570. return self;
  571. }
  572. void PhysicsServerSW::damped_string_joint_set_param(RID p_joint, DampedStringParam p_param, real_t p_value) {
  573. JointSW *j = joint_owner.get(p_joint);
  574. ERR_FAIL_COND(!j);
  575. ERR_FAIL_COND(j->get_type()!=JOINT_DAMPED_SPRING);
  576. DampedSpringJointSW *dsj = static_cast<DampedSpringJointSW*>(j);
  577. dsj->set_param(p_param,p_value);
  578. }
  579. real_t PhysicsServerSW::damped_string_joint_get_param(RID p_joint, DampedStringParam p_param) const {
  580. JointSW *j = joint_owner.get(p_joint);
  581. ERR_FAIL_COND_V(!j,0);
  582. ERR_FAIL_COND_V(j->get_type()!=JOINT_DAMPED_SPRING,0);
  583. DampedSpringJointSW *dsj = static_cast<DampedSpringJointSW*>(j);
  584. return dsj->get_param(p_param);
  585. }
  586. PhysicsServer::JointType PhysicsServerSW::joint_get_type(RID p_joint) const {
  587. JointSW *joint = joint_owner.get(p_joint);
  588. ERR_FAIL_COND_V(!joint,JOINT_PIN);
  589. return joint->get_type();
  590. }
  591. #endif
  592. void PhysicsServerSW::free(RID p_rid) {
  593. if (shape_owner.owns(p_rid)) {
  594. ShapeSW *shape = shape_owner.get(p_rid);
  595. while(shape->get_owners().size()) {
  596. ShapeOwnerSW *so=shape->get_owners().front()->key();
  597. so->remove_shape(shape);
  598. }
  599. shape_owner.free(p_rid);
  600. memdelete(shape);
  601. } else if (body_owner.owns(p_rid)) {
  602. BodySW *body = body_owner.get(p_rid);
  603. // if (body->get_state_query())
  604. // _clear_query(body->get_state_query());
  605. // if (body->get_direct_state_query())
  606. // _clear_query(body->get_direct_state_query());
  607. body->set_space(NULL);
  608. while( body->get_shape_count() ) {
  609. body->remove_shape(0);
  610. }
  611. while (body->get_constraint_map().size()) {
  612. RID self = body->get_constraint_map().front()->key()->get_self();
  613. ERR_FAIL_COND(!self.is_valid());
  614. free(self);
  615. }
  616. body_owner.free(p_rid);
  617. memdelete(body);
  618. } else if (area_owner.owns(p_rid)) {
  619. AreaSW *area = area_owner.get(p_rid);
  620. // if (area->get_monitor_query())
  621. // _clear_query(area->get_monitor_query());
  622. area->set_space(NULL);
  623. while( area->get_shape_count() ) {
  624. area->remove_shape(0);
  625. }
  626. area_owner.free(p_rid);
  627. memdelete(area);
  628. } else if (space_owner.owns(p_rid)) {
  629. SpaceSW *space = space_owner.get(p_rid);
  630. while(space->get_objects().size()) {
  631. CollisionObjectSW *co = (CollisionObjectSW *)space->get_objects().front()->get();
  632. co->set_space(NULL);
  633. }
  634. active_spaces.erase(space);
  635. free(space->get_default_area()->get_self());
  636. space_owner.free(p_rid);
  637. memdelete(space);
  638. } else if (joint_owner.owns(p_rid)) {
  639. JointSW *joint = joint_owner.get(p_rid);
  640. joint_owner.free(p_rid);
  641. memdelete(joint);
  642. } else {
  643. ERR_EXPLAIN("Invalid ID");
  644. ERR_FAIL();
  645. }
  646. };
  647. void PhysicsServerSW::set_active(bool p_active) {
  648. active=p_active;
  649. };
  650. void PhysicsServerSW::init() {
  651. doing_sync=true;
  652. last_step=0.001;
  653. iterations=8;// 8?
  654. stepper = memnew( StepSW );
  655. direct_state = memnew( PhysicsDirectBodyStateSW );
  656. };
  657. void PhysicsServerSW::step(float p_step) {
  658. if (!active)
  659. return;
  660. doing_sync=false;
  661. last_step=p_step;
  662. PhysicsDirectBodyStateSW::singleton->step=p_step;
  663. for( Set<const SpaceSW*>::Element *E=active_spaces.front();E;E=E->next()) {
  664. stepper->step((SpaceSW*)E->get(),p_step,iterations);
  665. }
  666. };
  667. void PhysicsServerSW::sync() {
  668. };
  669. void PhysicsServerSW::flush_queries() {
  670. if (!active)
  671. return;
  672. doing_sync=true;
  673. for( Set<const SpaceSW*>::Element *E=active_spaces.front();E;E=E->next()) {
  674. SpaceSW *space=(SpaceSW *)E->get();
  675. space->call_queries();
  676. }
  677. };
  678. void PhysicsServerSW::finish() {
  679. memdelete(stepper);
  680. memdelete(direct_state);
  681. };
  682. PhysicsServerSW::PhysicsServerSW() {
  683. BroadPhaseSW::create_func=BroadPhaseOctree::_create;
  684. active=true;
  685. };
  686. PhysicsServerSW::~PhysicsServerSW() {
  687. };