physics_2d_server_sw.cpp 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112
  1. /*************************************************************************/
  2. /* physics_2d_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_2d_server_sw.h"
  30. #include "broad_phase_2d_basic.h"
  31. #include "broad_phase_2d_hash_grid.h"
  32. #include "collision_solver_2d_sw.h"
  33. RID Physics2DServerSW::shape_create(ShapeType p_shape) {
  34. Shape2DSW *shape=NULL;
  35. switch(p_shape) {
  36. case SHAPE_LINE: {
  37. shape=memnew( LineShape2DSW );
  38. } break;
  39. case SHAPE_RAY: {
  40. shape=memnew( RayShape2DSW );
  41. } break;
  42. case SHAPE_SEGMENT: {
  43. shape=memnew( SegmentShape2DSW);
  44. } break;
  45. case SHAPE_CIRCLE: {
  46. shape=memnew( CircleShape2DSW);
  47. } break;
  48. case SHAPE_RECTANGLE: {
  49. shape=memnew( RectangleShape2DSW);
  50. } break;
  51. case SHAPE_CAPSULE: {
  52. shape=memnew( CapsuleShape2DSW );
  53. } break;
  54. case SHAPE_CONVEX_POLYGON: {
  55. shape=memnew( ConvexPolygonShape2DSW );
  56. } break;
  57. case SHAPE_CONCAVE_POLYGON: {
  58. shape=memnew( ConcavePolygonShape2DSW );
  59. } break;
  60. case SHAPE_CUSTOM: {
  61. ERR_FAIL_V(RID());
  62. } break;
  63. }
  64. RID id = shape_owner.make_rid(shape);
  65. shape->set_self(id);
  66. return id;
  67. };
  68. void Physics2DServerSW::shape_set_data(RID p_shape, const Variant& p_data) {
  69. Shape2DSW *shape = shape_owner.get(p_shape);
  70. ERR_FAIL_COND(!shape);
  71. shape->set_data(p_data);
  72. };
  73. void Physics2DServerSW::shape_set_custom_solver_bias(RID p_shape, real_t p_bias) {
  74. Shape2DSW *shape = shape_owner.get(p_shape);
  75. ERR_FAIL_COND(!shape);
  76. shape->set_custom_bias(p_bias);
  77. }
  78. Physics2DServer::ShapeType Physics2DServerSW::shape_get_type(RID p_shape) const {
  79. const Shape2DSW *shape = shape_owner.get(p_shape);
  80. ERR_FAIL_COND_V(!shape,SHAPE_CUSTOM);
  81. return shape->get_type();
  82. };
  83. Variant Physics2DServerSW::shape_get_data(RID p_shape) const {
  84. const Shape2DSW *shape = shape_owner.get(p_shape);
  85. ERR_FAIL_COND_V(!shape,Variant());
  86. ERR_FAIL_COND_V(!shape->is_configured(),Variant());
  87. return shape->get_data();
  88. };
  89. real_t Physics2DServerSW::shape_get_custom_solver_bias(RID p_shape) const {
  90. const Shape2DSW *shape = shape_owner.get(p_shape);
  91. ERR_FAIL_COND_V(!shape,0);
  92. return shape->get_custom_bias();
  93. }
  94. void Physics2DServerSW::_shape_col_cbk(const Vector2& p_point_A,const Vector2& p_point_B,void *p_userdata) {
  95. CollCbkData *cbk=(CollCbkData *)p_userdata;
  96. if (cbk->amount == cbk->max) {
  97. //find least deep
  98. float min_depth=1e20;
  99. int min_depth_idx=0;
  100. for(int i=0;i<cbk->amount;i++) {
  101. float d = cbk->ptr[i*2+0].distance_squared_to(cbk->ptr[i*2+1]);
  102. if (d<min_depth) {
  103. min_depth=d;
  104. min_depth_idx=i;
  105. }
  106. }
  107. float d = p_point_A.distance_squared_to(p_point_B);
  108. if (d<min_depth)
  109. return;
  110. cbk->ptr[min_depth_idx*2+0]=p_point_A;
  111. cbk->ptr[min_depth_idx*2+1]=p_point_B;
  112. } else {
  113. cbk->ptr[cbk->amount*2+0]=p_point_A;
  114. cbk->ptr[cbk->amount*2+1]=p_point_B;
  115. }
  116. }
  117. bool Physics2DServerSW::shape_collide(RID p_shape_A, const Matrix32& p_xform_A,const Vector2& p_motion_A,RID p_shape_B, const Matrix32& p_xform_B, const Vector2& p_motion_B,Vector2 *r_results,int p_result_max,int &r_result_count) {
  118. Shape2DSW *shape_A = shape_owner.get(p_shape_A);
  119. ERR_FAIL_COND_V(!shape_A,false);
  120. Shape2DSW *shape_B = shape_owner.get(p_shape_B);
  121. ERR_FAIL_COND_V(!shape_B,false);
  122. if (p_result_max==0) {
  123. return CollisionSolver2DSW::solve(shape_A,p_xform_A,p_motion_A,shape_B,p_xform_B,p_motion_B,NULL,NULL);
  124. }
  125. CollCbkData cbk;
  126. cbk.max=p_result_max;
  127. cbk.amount=0;
  128. cbk.ptr=r_results;
  129. bool res= CollisionSolver2DSW::solve(shape_A,p_xform_A,p_motion_A,shape_B,p_xform_B,p_motion_B,_shape_col_cbk,&cbk);
  130. r_result_count=cbk.amount;
  131. return res;
  132. }
  133. RID Physics2DServerSW::space_create() {
  134. Space2DSW *space = memnew( Space2DSW );
  135. RID id = space_owner.make_rid(space);
  136. space->set_self(id);
  137. RID area_id = area_create();
  138. Area2DSW *area = area_owner.get(area_id);
  139. ERR_FAIL_COND_V(!area,RID());
  140. space->set_default_area(area);
  141. area->set_space(space);
  142. area->set_priority(-1);
  143. return id;
  144. };
  145. void Physics2DServerSW::space_set_active(RID p_space,bool p_active) {
  146. Space2DSW *space = space_owner.get(p_space);
  147. ERR_FAIL_COND(!space);
  148. if (p_active)
  149. active_spaces.insert(space);
  150. else
  151. active_spaces.erase(space);
  152. }
  153. bool Physics2DServerSW::space_is_active(RID p_space) const {
  154. const Space2DSW *space = space_owner.get(p_space);
  155. ERR_FAIL_COND_V(!space,false);
  156. return active_spaces.has(space);
  157. }
  158. void Physics2DServerSW::space_set_param(RID p_space,SpaceParameter p_param, real_t p_value) {
  159. Space2DSW *space = space_owner.get(p_space);
  160. ERR_FAIL_COND(!space);
  161. space->set_param(p_param,p_value);
  162. }
  163. real_t Physics2DServerSW::space_get_param(RID p_space,SpaceParameter p_param) const {
  164. const Space2DSW *space = space_owner.get(p_space);
  165. ERR_FAIL_COND_V(!space,0);
  166. return space->get_param(p_param);
  167. }
  168. Physics2DDirectSpaceState* Physics2DServerSW::space_get_direct_state(RID p_space) {
  169. Space2DSW *space = space_owner.get(p_space);
  170. ERR_FAIL_COND_V(!space,NULL);
  171. if (/*doing_sync ||*/ space->is_locked()) {
  172. ERR_EXPLAIN("Space state is inaccesible right now, wait for iteration or fixed process notification.");
  173. ERR_FAIL_V(NULL);
  174. }
  175. return space->get_direct_state();
  176. }
  177. RID Physics2DServerSW::area_create() {
  178. Area2DSW *area = memnew( Area2DSW );
  179. RID rid = area_owner.make_rid(area);
  180. area->set_self(rid);
  181. return rid;
  182. };
  183. void Physics2DServerSW::area_set_space(RID p_area, RID p_space) {
  184. Area2DSW *area = area_owner.get(p_area);
  185. ERR_FAIL_COND(!area);
  186. Space2DSW *space=NULL;
  187. if (p_space.is_valid()) {
  188. space = space_owner.get(p_space);
  189. ERR_FAIL_COND(!space);
  190. }
  191. area->set_space(space);
  192. };
  193. RID Physics2DServerSW::area_get_space(RID p_area) const {
  194. Area2DSW *area = area_owner.get(p_area);
  195. ERR_FAIL_COND_V(!area,RID());
  196. Space2DSW *space = area->get_space();
  197. if (!space)
  198. return RID();
  199. return space->get_self();
  200. };
  201. void Physics2DServerSW::area_set_space_override_mode(RID p_area, AreaSpaceOverrideMode p_mode) {
  202. Area2DSW *area = area_owner.get(p_area);
  203. ERR_FAIL_COND(!area);
  204. area->set_space_override_mode(p_mode);
  205. }
  206. Physics2DServer::AreaSpaceOverrideMode Physics2DServerSW::area_get_space_override_mode(RID p_area) const {
  207. const Area2DSW *area = area_owner.get(p_area);
  208. ERR_FAIL_COND_V(!area,AREA_SPACE_OVERRIDE_DISABLED);
  209. return area->get_space_override_mode();
  210. }
  211. void Physics2DServerSW::area_add_shape(RID p_area, RID p_shape, const Matrix32& p_transform) {
  212. Area2DSW *area = area_owner.get(p_area);
  213. ERR_FAIL_COND(!area);
  214. Shape2DSW *shape = shape_owner.get(p_shape);
  215. ERR_FAIL_COND(!shape);
  216. area->add_shape(shape,p_transform);
  217. }
  218. void Physics2DServerSW::area_set_shape(RID p_area, int p_shape_idx,RID p_shape) {
  219. Area2DSW *area = area_owner.get(p_area);
  220. ERR_FAIL_COND(!area);
  221. Shape2DSW *shape = shape_owner.get(p_shape);
  222. ERR_FAIL_COND(!shape);
  223. ERR_FAIL_COND(!shape->is_configured());
  224. area->set_shape(p_shape_idx,shape);
  225. }
  226. void Physics2DServerSW::area_set_shape_transform(RID p_area, int p_shape_idx, const Matrix32& p_transform) {
  227. Area2DSW *area = area_owner.get(p_area);
  228. ERR_FAIL_COND(!area);
  229. area->set_shape_transform(p_shape_idx,p_transform);
  230. }
  231. int Physics2DServerSW::area_get_shape_count(RID p_area) const {
  232. Area2DSW *area = area_owner.get(p_area);
  233. ERR_FAIL_COND_V(!area,-1);
  234. return area->get_shape_count();
  235. }
  236. RID Physics2DServerSW::area_get_shape(RID p_area, int p_shape_idx) const {
  237. Area2DSW *area = area_owner.get(p_area);
  238. ERR_FAIL_COND_V(!area,RID());
  239. Shape2DSW *shape = area->get_shape(p_shape_idx);
  240. ERR_FAIL_COND_V(!shape,RID());
  241. return shape->get_self();
  242. }
  243. Matrix32 Physics2DServerSW::area_get_shape_transform(RID p_area, int p_shape_idx) const {
  244. Area2DSW *area = area_owner.get(p_area);
  245. ERR_FAIL_COND_V(!area,Matrix32());
  246. return area->get_shape_transform(p_shape_idx);
  247. }
  248. void Physics2DServerSW::area_remove_shape(RID p_area, int p_shape_idx) {
  249. Area2DSW *area = area_owner.get(p_area);
  250. ERR_FAIL_COND(!area);
  251. area->remove_shape(p_shape_idx);
  252. }
  253. void Physics2DServerSW::area_clear_shapes(RID p_area) {
  254. Area2DSW *area = area_owner.get(p_area);
  255. ERR_FAIL_COND(!area);
  256. while(area->get_shape_count())
  257. area->remove_shape(0);
  258. }
  259. void Physics2DServerSW::area_attach_object_instance_ID(RID p_area,ObjectID p_ID) {
  260. if (space_owner.owns(p_area)) {
  261. Space2DSW *space=space_owner.get(p_area);
  262. p_area=space->get_default_area()->get_self();
  263. }
  264. Area2DSW *area = area_owner.get(p_area);
  265. ERR_FAIL_COND(!area);
  266. area->set_instance_id(p_ID);
  267. }
  268. ObjectID Physics2DServerSW::area_get_object_instance_ID(RID p_area) const {
  269. if (space_owner.owns(p_area)) {
  270. Space2DSW *space=space_owner.get(p_area);
  271. p_area=space->get_default_area()->get_self();
  272. }
  273. Area2DSW *area = area_owner.get(p_area);
  274. ERR_FAIL_COND_V(!area,0);
  275. return area->get_instance_id();
  276. }
  277. void Physics2DServerSW::area_set_param(RID p_area,AreaParameter p_param,const Variant& p_value) {
  278. if (space_owner.owns(p_area)) {
  279. Space2DSW *space=space_owner.get(p_area);
  280. p_area=space->get_default_area()->get_self();
  281. }
  282. Area2DSW *area = area_owner.get(p_area);
  283. ERR_FAIL_COND(!area);
  284. area->set_param(p_param,p_value);
  285. };
  286. void Physics2DServerSW::area_set_transform(RID p_area, const Matrix32& p_transform) {
  287. Area2DSW *area = area_owner.get(p_area);
  288. ERR_FAIL_COND(!area);
  289. area->set_transform(p_transform);
  290. };
  291. Variant Physics2DServerSW::area_get_param(RID p_area,AreaParameter p_param) const {
  292. if (space_owner.owns(p_area)) {
  293. Space2DSW *space=space_owner.get(p_area);
  294. p_area=space->get_default_area()->get_self();
  295. }
  296. Area2DSW *area = area_owner.get(p_area);
  297. ERR_FAIL_COND_V(!area,Variant());
  298. return area->get_param(p_param);
  299. };
  300. Matrix32 Physics2DServerSW::area_get_transform(RID p_area) const {
  301. Area2DSW *area = area_owner.get(p_area);
  302. ERR_FAIL_COND_V(!area,Matrix32());
  303. return area->get_transform();
  304. };
  305. void Physics2DServerSW::area_set_monitor_callback(RID p_area,Object *p_receiver,const StringName& p_method) {
  306. Area2DSW *area = area_owner.get(p_area);
  307. ERR_FAIL_COND(!area);
  308. area->set_monitor_callback(p_receiver?p_receiver->get_instance_ID():0,p_method);
  309. }
  310. /* BODY API */
  311. RID Physics2DServerSW::body_create(BodyMode p_mode,bool p_init_sleeping) {
  312. Body2DSW *body = memnew( Body2DSW );
  313. if (p_mode!=BODY_MODE_RIGID)
  314. body->set_mode(p_mode);
  315. if (p_init_sleeping)
  316. body->set_state(BODY_STATE_SLEEPING,p_init_sleeping);
  317. RID rid = body_owner.make_rid(body);
  318. body->set_self(rid);
  319. return rid;
  320. };
  321. void Physics2DServerSW::body_set_space(RID p_body, RID p_space) {
  322. Body2DSW *body = body_owner.get(p_body);
  323. ERR_FAIL_COND(!body);
  324. Space2DSW *space=NULL;
  325. if (p_space.is_valid()) {
  326. space = space_owner.get(p_space);
  327. ERR_FAIL_COND(!space);
  328. }
  329. body->set_space(space);
  330. };
  331. RID Physics2DServerSW::body_get_space(RID p_body) const {
  332. Body2DSW *body = body_owner.get(p_body);
  333. ERR_FAIL_COND_V(!body,RID());
  334. Space2DSW *space = body->get_space();
  335. if (!space)
  336. return RID();
  337. return space->get_self();
  338. };
  339. void Physics2DServerSW::body_set_mode(RID p_body, BodyMode p_mode) {
  340. Body2DSW *body = body_owner.get(p_body);
  341. ERR_FAIL_COND(!body);
  342. body->set_mode(p_mode);
  343. };
  344. Physics2DServer::BodyMode Physics2DServerSW::body_get_mode(RID p_body) const {
  345. Body2DSW *body = body_owner.get(p_body);
  346. ERR_FAIL_COND_V(!body,BODY_MODE_STATIC);
  347. return body->get_mode();
  348. };
  349. void Physics2DServerSW::body_add_shape(RID p_body, RID p_shape, const Matrix32& p_transform) {
  350. Body2DSW *body = body_owner.get(p_body);
  351. ERR_FAIL_COND(!body);
  352. Shape2DSW *shape = shape_owner.get(p_shape);
  353. ERR_FAIL_COND(!shape);
  354. body->add_shape(shape,p_transform);
  355. }
  356. void Physics2DServerSW::body_set_shape(RID p_body, int p_shape_idx,RID p_shape) {
  357. Body2DSW *body = body_owner.get(p_body);
  358. ERR_FAIL_COND(!body);
  359. Shape2DSW *shape = shape_owner.get(p_shape);
  360. ERR_FAIL_COND(!shape);
  361. ERR_FAIL_COND(!shape->is_configured());
  362. body->set_shape(p_shape_idx,shape);
  363. }
  364. void Physics2DServerSW::body_set_shape_transform(RID p_body, int p_shape_idx, const Matrix32& p_transform) {
  365. Body2DSW *body = body_owner.get(p_body);
  366. ERR_FAIL_COND(!body);
  367. body->set_shape_transform(p_shape_idx,p_transform);
  368. }
  369. int Physics2DServerSW::body_get_shape_count(RID p_body) const {
  370. Body2DSW *body = body_owner.get(p_body);
  371. ERR_FAIL_COND_V(!body,-1);
  372. return body->get_shape_count();
  373. }
  374. RID Physics2DServerSW::body_get_shape(RID p_body, int p_shape_idx) const {
  375. Body2DSW *body = body_owner.get(p_body);
  376. ERR_FAIL_COND_V(!body,RID());
  377. Shape2DSW *shape = body->get_shape(p_shape_idx);
  378. ERR_FAIL_COND_V(!shape,RID());
  379. return shape->get_self();
  380. }
  381. Matrix32 Physics2DServerSW::body_get_shape_transform(RID p_body, int p_shape_idx) const {
  382. Body2DSW *body = body_owner.get(p_body);
  383. ERR_FAIL_COND_V(!body,Matrix32());
  384. return body->get_shape_transform(p_shape_idx);
  385. }
  386. void Physics2DServerSW::body_remove_shape(RID p_body, int p_shape_idx) {
  387. Body2DSW *body = body_owner.get(p_body);
  388. ERR_FAIL_COND(!body);
  389. body->remove_shape(p_shape_idx);
  390. }
  391. void Physics2DServerSW::body_clear_shapes(RID p_body) {
  392. Body2DSW *body = body_owner.get(p_body);
  393. ERR_FAIL_COND(!body);
  394. while(body->get_shape_count())
  395. body->remove_shape(0);
  396. }
  397. void Physics2DServerSW::body_set_shape_as_trigger(RID p_body, int p_shape_idx,bool p_enable) {
  398. Body2DSW *body = body_owner.get(p_body);
  399. ERR_FAIL_COND(!body);
  400. ERR_FAIL_INDEX(p_shape_idx,body->get_shape_count());
  401. body->set_shape_as_trigger(p_shape_idx,p_enable);
  402. }
  403. bool Physics2DServerSW::body_is_shape_set_as_trigger(RID p_body, int p_shape_idx) const {
  404. const Body2DSW *body = body_owner.get(p_body);
  405. ERR_FAIL_COND_V(!body,false);
  406. ERR_FAIL_INDEX_V(p_shape_idx,body->get_shape_count(),false);
  407. return body->is_shape_set_as_trigger(p_shape_idx);
  408. }
  409. void Physics2DServerSW::body_set_continuous_collision_detection_mode(RID p_body,CCDMode p_mode) {
  410. Body2DSW *body = body_owner.get(p_body);
  411. ERR_FAIL_COND(!body);
  412. body->set_continuous_collision_detection_mode(p_mode);
  413. }
  414. Physics2DServerSW::CCDMode Physics2DServerSW::body_get_continuous_collision_detection_mode(RID p_body) const{
  415. const Body2DSW *body = body_owner.get(p_body);
  416. ERR_FAIL_COND_V(!body,CCD_MODE_DISABLED);
  417. return body->get_continuous_collision_detection_mode();
  418. }
  419. void Physics2DServerSW::body_attach_object_instance_ID(RID p_body,uint32_t p_ID) {
  420. Body2DSW *body = body_owner.get(p_body);
  421. ERR_FAIL_COND(!body);
  422. body->set_instance_id(p_ID);
  423. };
  424. uint32_t Physics2DServerSW::body_get_object_instance_ID(RID p_body) const {
  425. Body2DSW *body = body_owner.get(p_body);
  426. ERR_FAIL_COND_V(!body,0);
  427. return body->get_instance_id();
  428. };
  429. void Physics2DServerSW::body_set_user_flags(RID p_body, uint32_t p_flags) {
  430. Body2DSW *body = body_owner.get(p_body);
  431. ERR_FAIL_COND(!body);
  432. };
  433. uint32_t Physics2DServerSW::body_get_user_flags(RID p_body, uint32_t p_flags) const {
  434. Body2DSW *body = body_owner.get(p_body);
  435. ERR_FAIL_COND_V(!body,0);
  436. return 0;
  437. };
  438. void Physics2DServerSW::body_set_param(RID p_body, BodyParameter p_param, float p_value) {
  439. Body2DSW *body = body_owner.get(p_body);
  440. ERR_FAIL_COND(!body);
  441. body->set_param(p_param,p_value);
  442. };
  443. float Physics2DServerSW::body_get_param(RID p_body, BodyParameter p_param) const {
  444. Body2DSW *body = body_owner.get(p_body);
  445. ERR_FAIL_COND_V(!body,0);
  446. return body->get_param(p_param);
  447. };
  448. void Physics2DServerSW::body_set_state(RID p_body, BodyState p_state, const Variant& p_variant) {
  449. Body2DSW *body = body_owner.get(p_body);
  450. ERR_FAIL_COND(!body);
  451. body->set_state(p_state,p_variant);
  452. };
  453. Variant Physics2DServerSW::body_get_state(RID p_body, BodyState p_state) const {
  454. Body2DSW *body = body_owner.get(p_body);
  455. ERR_FAIL_COND_V(!body,Variant());
  456. return body->get_state(p_state);
  457. };
  458. void Physics2DServerSW::body_set_applied_force(RID p_body, const Vector2& p_force) {
  459. Body2DSW *body = body_owner.get(p_body);
  460. ERR_FAIL_COND(!body);
  461. body->set_applied_force(p_force);
  462. };
  463. Vector2 Physics2DServerSW::body_get_applied_force(RID p_body) const {
  464. Body2DSW *body = body_owner.get(p_body);
  465. ERR_FAIL_COND_V(!body,Vector2());
  466. return body->get_applied_force();
  467. };
  468. void Physics2DServerSW::body_set_applied_torque(RID p_body, float p_torque) {
  469. Body2DSW *body = body_owner.get(p_body);
  470. ERR_FAIL_COND(!body);
  471. body->set_applied_torque(p_torque);
  472. };
  473. float Physics2DServerSW::body_get_applied_torque(RID p_body) const {
  474. Body2DSW *body = body_owner.get(p_body);
  475. ERR_FAIL_COND_V(!body,0);
  476. return body->get_applied_torque();
  477. };
  478. void Physics2DServerSW::body_apply_impulse(RID p_body, const Vector2& p_pos, const Vector2& p_impulse) {
  479. Body2DSW *body = body_owner.get(p_body);
  480. ERR_FAIL_COND(!body);
  481. body->apply_impulse(p_pos,p_impulse);
  482. };
  483. void Physics2DServerSW::body_set_axis_velocity(RID p_body, const Vector2& p_axis_velocity) {
  484. Body2DSW *body = body_owner.get(p_body);
  485. ERR_FAIL_COND(!body);
  486. Vector2 v = body->get_linear_velocity();
  487. Vector2 axis = p_axis_velocity.normalized();
  488. v-=axis*axis.dot(v);
  489. v+=p_axis_velocity;
  490. body->set_linear_velocity(v);
  491. };
  492. void Physics2DServerSW::body_add_collision_exception(RID p_body, RID p_body_b) {
  493. Body2DSW *body = body_owner.get(p_body);
  494. ERR_FAIL_COND(!body);
  495. body->add_exception(p_body_b);
  496. };
  497. void Physics2DServerSW::body_remove_collision_exception(RID p_body, RID p_body_b) {
  498. Body2DSW *body = body_owner.get(p_body);
  499. ERR_FAIL_COND(!body);
  500. body->remove_exception(p_body);
  501. };
  502. void Physics2DServerSW::body_get_collision_exceptions(RID p_body, List<RID> *p_exceptions) {
  503. Body2DSW *body = body_owner.get(p_body);
  504. ERR_FAIL_COND(!body);
  505. for(int i=0;i<body->get_exceptions().size();i++) {
  506. p_exceptions->push_back(body->get_exceptions()[i]);
  507. }
  508. };
  509. void Physics2DServerSW::body_set_contacts_reported_depth_treshold(RID p_body, float p_treshold) {
  510. Body2DSW *body = body_owner.get(p_body);
  511. ERR_FAIL_COND(!body);
  512. };
  513. float Physics2DServerSW::body_get_contacts_reported_depth_treshold(RID p_body) const {
  514. Body2DSW *body = body_owner.get(p_body);
  515. ERR_FAIL_COND_V(!body,0);
  516. return 0;
  517. };
  518. void Physics2DServerSW::body_set_omit_force_integration(RID p_body,bool p_omit) {
  519. Body2DSW *body = body_owner.get(p_body);
  520. ERR_FAIL_COND(!body);
  521. body->set_omit_force_integration(p_omit);
  522. };
  523. bool Physics2DServerSW::body_is_omitting_force_integration(RID p_body) const {
  524. Body2DSW *body = body_owner.get(p_body);
  525. ERR_FAIL_COND_V(!body,false);
  526. return body->get_omit_force_integration();
  527. };
  528. void Physics2DServerSW::body_set_max_contacts_reported(RID p_body, int p_contacts) {
  529. Body2DSW *body = body_owner.get(p_body);
  530. ERR_FAIL_COND(!body);
  531. body->set_max_contacts_reported(p_contacts);
  532. }
  533. int Physics2DServerSW::body_get_max_contacts_reported(RID p_body) const {
  534. Body2DSW *body = body_owner.get(p_body);
  535. ERR_FAIL_COND_V(!body,-1);
  536. return body->get_max_contacts_reported();
  537. }
  538. void Physics2DServerSW::body_set_force_integration_callback(RID p_body,Object *p_receiver,const StringName& p_method,const Variant& p_udata) {
  539. Body2DSW *body = body_owner.get(p_body);
  540. ERR_FAIL_COND(!body);
  541. body->set_force_integration_callback(p_receiver?p_receiver->get_instance_ID():ObjectID(0),p_method,p_udata);
  542. }
  543. bool Physics2DServerSW::body_collide_shape(RID p_body, int p_body_shape, RID p_shape, const Matrix32& p_shape_xform,const Vector2& p_motion,Vector2 *r_results,int p_result_max,int &r_result_count) {
  544. Body2DSW *body = body_owner.get(p_body);
  545. ERR_FAIL_COND_V(!body,false);
  546. ERR_FAIL_INDEX_V(p_body_shape,body->get_shape_count(),false);
  547. return shape_collide(body->get_shape(p_body_shape)->get_self(),body->get_transform() * body->get_shape_transform(p_body_shape),Vector2(),p_shape,p_shape_xform,p_motion,r_results,p_result_max,r_result_count);
  548. }
  549. /* JOINT API */
  550. void Physics2DServerSW::joint_set_param(RID p_joint, JointParam p_param, real_t p_value) {
  551. Joint2DSW *joint = joint_owner.get(p_joint);
  552. ERR_FAIL_COND(!joint);
  553. switch(p_param) {
  554. case JOINT_PARAM_BIAS: joint->set_bias(p_value); break;
  555. case JOINT_PARAM_MAX_BIAS: joint->set_max_bias(p_value); break;
  556. case JOINT_PARAM_MAX_FORCE: joint->set_max_force(p_value); break;
  557. }
  558. }
  559. real_t Physics2DServerSW::joint_get_param(RID p_joint,JointParam p_param) const {
  560. const Joint2DSW *joint = joint_owner.get(p_joint);
  561. ERR_FAIL_COND_V(!joint,-1);
  562. switch(p_param) {
  563. case JOINT_PARAM_BIAS: return joint->get_bias(); break;
  564. case JOINT_PARAM_MAX_BIAS: return joint->get_max_bias(); break;
  565. case JOINT_PARAM_MAX_FORCE: return joint->get_max_force(); break;
  566. }
  567. return 0;
  568. }
  569. RID Physics2DServerSW::pin_joint_create(const Vector2& p_pos,RID p_body_a,RID p_body_b) {
  570. Body2DSW *A=body_owner.get(p_body_a);
  571. ERR_FAIL_COND_V(!A,RID());
  572. Body2DSW *B=NULL;
  573. if (body_owner.owns(p_body_b)) {
  574. B=body_owner.get(p_body_b);
  575. ERR_FAIL_COND_V(!B,RID());
  576. }
  577. Joint2DSW *joint = memnew( PinJoint2DSW(p_pos,A,B) );
  578. RID self = joint_owner.make_rid(joint);
  579. joint->set_self(self);
  580. return self;
  581. }
  582. RID Physics2DServerSW::groove_joint_create(const Vector2& p_a_groove1,const Vector2& p_a_groove2, const Vector2& p_b_anchor, RID p_body_a,RID p_body_b) {
  583. Body2DSW *A=body_owner.get(p_body_a);
  584. ERR_FAIL_COND_V(!A,RID());
  585. Body2DSW *B=body_owner.get(p_body_b);
  586. ERR_FAIL_COND_V(!B,RID());
  587. Joint2DSW *joint = memnew( GrooveJoint2DSW(p_a_groove1,p_a_groove2,p_b_anchor,A,B) );
  588. RID self = joint_owner.make_rid(joint);
  589. joint->set_self(self);
  590. return self;
  591. }
  592. RID Physics2DServerSW::damped_spring_joint_create(const Vector2& p_anchor_a,const Vector2& p_anchor_b,RID p_body_a,RID p_body_b) {
  593. Body2DSW *A=body_owner.get(p_body_a);
  594. ERR_FAIL_COND_V(!A,RID());
  595. Body2DSW *B=body_owner.get(p_body_b);
  596. ERR_FAIL_COND_V(!B,RID());
  597. Joint2DSW *joint = memnew( DampedSpringJoint2DSW(p_anchor_a,p_anchor_b,A,B) );
  598. RID self = joint_owner.make_rid(joint);
  599. joint->set_self(self);
  600. return self;
  601. }
  602. void Physics2DServerSW::damped_string_joint_set_param(RID p_joint, DampedStringParam p_param, real_t p_value) {
  603. Joint2DSW *j = joint_owner.get(p_joint);
  604. ERR_FAIL_COND(!j);
  605. ERR_FAIL_COND(j->get_type()!=JOINT_DAMPED_SPRING);
  606. DampedSpringJoint2DSW *dsj = static_cast<DampedSpringJoint2DSW*>(j);
  607. dsj->set_param(p_param,p_value);
  608. }
  609. real_t Physics2DServerSW::damped_string_joint_get_param(RID p_joint, DampedStringParam p_param) const {
  610. Joint2DSW *j = joint_owner.get(p_joint);
  611. ERR_FAIL_COND_V(!j,0);
  612. ERR_FAIL_COND_V(j->get_type()!=JOINT_DAMPED_SPRING,0);
  613. DampedSpringJoint2DSW *dsj = static_cast<DampedSpringJoint2DSW*>(j);
  614. return dsj->get_param(p_param);
  615. }
  616. Physics2DServer::JointType Physics2DServerSW::joint_get_type(RID p_joint) const {
  617. Joint2DSW *joint = joint_owner.get(p_joint);
  618. ERR_FAIL_COND_V(!joint,JOINT_PIN);
  619. return joint->get_type();
  620. }
  621. void Physics2DServerSW::free(RID p_rid) {
  622. if (shape_owner.owns(p_rid)) {
  623. Shape2DSW *shape = shape_owner.get(p_rid);
  624. while(shape->get_owners().size()) {
  625. ShapeOwner2DSW *so=shape->get_owners().front()->key();
  626. so->remove_shape(shape);
  627. }
  628. shape_owner.free(p_rid);
  629. memdelete(shape);
  630. } else if (body_owner.owns(p_rid)) {
  631. Body2DSW *body = body_owner.get(p_rid);
  632. // if (body->get_state_query())
  633. // _clear_query(body->get_state_query());
  634. // if (body->get_direct_state_query())
  635. // _clear_query(body->get_direct_state_query());
  636. body->set_space(NULL);
  637. while( body->get_shape_count() ) {
  638. body->remove_shape(0);
  639. }
  640. while (body->get_constraint_map().size()) {
  641. RID self = body->get_constraint_map().front()->key()->get_self();
  642. ERR_FAIL_COND(!self.is_valid());
  643. free(self);
  644. }
  645. body_owner.free(p_rid);
  646. memdelete(body);
  647. } else if (area_owner.owns(p_rid)) {
  648. Area2DSW *area = area_owner.get(p_rid);
  649. // if (area->get_monitor_query())
  650. // _clear_query(area->get_monitor_query());
  651. area->set_space(NULL);
  652. while( area->get_shape_count() ) {
  653. area->remove_shape(0);
  654. }
  655. area_owner.free(p_rid);
  656. memdelete(area);
  657. } else if (space_owner.owns(p_rid)) {
  658. Space2DSW *space = space_owner.get(p_rid);
  659. while(space->get_objects().size()) {
  660. CollisionObject2DSW *co = (CollisionObject2DSW *)space->get_objects().front()->get();
  661. co->set_space(NULL);
  662. }
  663. active_spaces.erase(space);
  664. free(space->get_default_area()->get_self());
  665. space_owner.free(p_rid);
  666. memdelete(space);
  667. } else if (joint_owner.owns(p_rid)) {
  668. Joint2DSW *joint = joint_owner.get(p_rid);
  669. joint_owner.free(p_rid);
  670. memdelete(joint);
  671. } else {
  672. ERR_EXPLAIN("Invalid ID");
  673. ERR_FAIL();
  674. }
  675. };
  676. void Physics2DServerSW::set_active(bool p_active) {
  677. active=p_active;
  678. };
  679. void Physics2DServerSW::init() {
  680. doing_sync=true;
  681. last_step=0.001;
  682. iterations=8;// 8?
  683. stepper = memnew( Step2DSW );
  684. direct_state = memnew( Physics2DDirectBodyStateSW );
  685. };
  686. void Physics2DServerSW::step(float p_step) {
  687. if (!active)
  688. return;
  689. doing_sync=false;
  690. last_step=p_step;
  691. Physics2DDirectBodyStateSW::singleton->step=p_step;
  692. for( Set<const Space2DSW*>::Element *E=active_spaces.front();E;E=E->next()) {
  693. stepper->step((Space2DSW*)E->get(),p_step,iterations);
  694. }
  695. };
  696. void Physics2DServerSW::sync() {
  697. };
  698. void Physics2DServerSW::flush_queries() {
  699. if (!active)
  700. return;
  701. doing_sync=true;
  702. for( Set<const Space2DSW*>::Element *E=active_spaces.front();E;E=E->next()) {
  703. Space2DSW *space=(Space2DSW *)E->get();
  704. space->call_queries();
  705. }
  706. };
  707. void Physics2DServerSW::finish() {
  708. memdelete(stepper);
  709. memdelete(direct_state);
  710. };
  711. Physics2DServerSW::Physics2DServerSW() {
  712. BroadPhase2DSW::create_func=BroadPhase2DHashGrid::_create;
  713. // BroadPhase2DSW::create_func=BroadPhase2DBasic::_create;
  714. active=true;
  715. };
  716. Physics2DServerSW::~Physics2DServerSW() {
  717. };