physics_2d_server_sw.cpp 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460
  1. /*************************************************************************/
  2. /* physics_2d_server_sw.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "physics_2d_server_sw.h"
  31. #include "broad_phase_2d_basic.h"
  32. #include "broad_phase_2d_hash_grid.h"
  33. #include "collision_solver_2d_sw.h"
  34. #include "core/os/os.h"
  35. #include "core/project_settings.h"
  36. #include "core/script_language.h"
  37. #define FLUSH_QUERY_CHECK(m_object) \
  38. ERR_FAIL_COND_MSG(m_object->get_space() && flushing_queries, "Can't change this state while flushing queries. Use call_deferred() or set_deferred() to change monitoring state instead.");
  39. RID Physics2DServerSW::_shape_create(ShapeType p_shape) {
  40. Shape2DSW *shape = NULL;
  41. switch (p_shape) {
  42. case SHAPE_LINE: {
  43. shape = memnew(LineShape2DSW);
  44. } break;
  45. case SHAPE_RAY: {
  46. shape = memnew(RayShape2DSW);
  47. } break;
  48. case SHAPE_SEGMENT: {
  49. shape = memnew(SegmentShape2DSW);
  50. } break;
  51. case SHAPE_CIRCLE: {
  52. shape = memnew(CircleShape2DSW);
  53. } break;
  54. case SHAPE_RECTANGLE: {
  55. shape = memnew(RectangleShape2DSW);
  56. } break;
  57. case SHAPE_CAPSULE: {
  58. shape = memnew(CapsuleShape2DSW);
  59. } break;
  60. case SHAPE_CONVEX_POLYGON: {
  61. shape = memnew(ConvexPolygonShape2DSW);
  62. } break;
  63. case SHAPE_CONCAVE_POLYGON: {
  64. shape = memnew(ConcavePolygonShape2DSW);
  65. } break;
  66. case SHAPE_CUSTOM: {
  67. ERR_FAIL_V(RID());
  68. } break;
  69. }
  70. RID id = shape_owner.make_rid(shape);
  71. shape->set_self(id);
  72. return id;
  73. }
  74. RID Physics2DServerSW::line_shape_create() {
  75. return _shape_create(SHAPE_LINE);
  76. }
  77. RID Physics2DServerSW::ray_shape_create() {
  78. return _shape_create(SHAPE_RAY);
  79. }
  80. RID Physics2DServerSW::segment_shape_create() {
  81. return _shape_create(SHAPE_SEGMENT);
  82. }
  83. RID Physics2DServerSW::circle_shape_create() {
  84. return _shape_create(SHAPE_CIRCLE);
  85. }
  86. RID Physics2DServerSW::rectangle_shape_create() {
  87. return _shape_create(SHAPE_RECTANGLE);
  88. }
  89. RID Physics2DServerSW::capsule_shape_create() {
  90. return _shape_create(SHAPE_CAPSULE);
  91. }
  92. RID Physics2DServerSW::convex_polygon_shape_create() {
  93. return _shape_create(SHAPE_CONVEX_POLYGON);
  94. }
  95. RID Physics2DServerSW::concave_polygon_shape_create() {
  96. return _shape_create(SHAPE_CONCAVE_POLYGON);
  97. }
  98. void Physics2DServerSW::shape_set_data(RID p_shape, const Variant &p_data) {
  99. Shape2DSW *shape = shape_owner.get(p_shape);
  100. ERR_FAIL_COND(!shape);
  101. shape->set_data(p_data);
  102. };
  103. void Physics2DServerSW::shape_set_custom_solver_bias(RID p_shape, real_t p_bias) {
  104. Shape2DSW *shape = shape_owner.get(p_shape);
  105. ERR_FAIL_COND(!shape);
  106. shape->set_custom_bias(p_bias);
  107. }
  108. Physics2DServer::ShapeType Physics2DServerSW::shape_get_type(RID p_shape) const {
  109. const Shape2DSW *shape = shape_owner.get(p_shape);
  110. ERR_FAIL_COND_V(!shape, SHAPE_CUSTOM);
  111. return shape->get_type();
  112. };
  113. Variant Physics2DServerSW::shape_get_data(RID p_shape) const {
  114. const Shape2DSW *shape = shape_owner.get(p_shape);
  115. ERR_FAIL_COND_V(!shape, Variant());
  116. ERR_FAIL_COND_V(!shape->is_configured(), Variant());
  117. return shape->get_data();
  118. };
  119. real_t Physics2DServerSW::shape_get_custom_solver_bias(RID p_shape) const {
  120. const Shape2DSW *shape = shape_owner.get(p_shape);
  121. ERR_FAIL_COND_V(!shape, 0);
  122. return shape->get_custom_bias();
  123. }
  124. void Physics2DServerSW::_shape_col_cbk(const Vector2 &p_point_A, const Vector2 &p_point_B, void *p_userdata) {
  125. CollCbkData *cbk = (CollCbkData *)p_userdata;
  126. if (cbk->max == 0)
  127. return;
  128. Vector2 rel_dir = (p_point_A - p_point_B);
  129. real_t rel_length2 = rel_dir.length_squared();
  130. if (cbk->valid_dir != Vector2()) {
  131. if (cbk->valid_depth < 10e20) {
  132. if (rel_length2 > cbk->valid_depth * cbk->valid_depth ||
  133. (rel_length2 > CMP_EPSILON && cbk->valid_dir.dot(rel_dir.normalized()) < CMP_EPSILON)) {
  134. cbk->invalid_by_dir++;
  135. return;
  136. }
  137. } else {
  138. if (rel_length2 > 0 && cbk->valid_dir.dot(rel_dir.normalized()) < CMP_EPSILON) {
  139. return;
  140. }
  141. }
  142. }
  143. if (cbk->amount == cbk->max) {
  144. //find least deep
  145. real_t min_depth = 1e20;
  146. int min_depth_idx = 0;
  147. for (int i = 0; i < cbk->amount; i++) {
  148. real_t d = cbk->ptr[i * 2 + 0].distance_squared_to(cbk->ptr[i * 2 + 1]);
  149. if (d < min_depth) {
  150. min_depth = d;
  151. min_depth_idx = i;
  152. }
  153. }
  154. if (rel_length2 < min_depth)
  155. return;
  156. cbk->ptr[min_depth_idx * 2 + 0] = p_point_A;
  157. cbk->ptr[min_depth_idx * 2 + 1] = p_point_B;
  158. cbk->passed++;
  159. } else {
  160. cbk->ptr[cbk->amount * 2 + 0] = p_point_A;
  161. cbk->ptr[cbk->amount * 2 + 1] = p_point_B;
  162. cbk->amount++;
  163. cbk->passed++;
  164. }
  165. }
  166. bool Physics2DServerSW::shape_collide(RID p_shape_A, const Transform2D &p_xform_A, const Vector2 &p_motion_A, RID p_shape_B, const Transform2D &p_xform_B, const Vector2 &p_motion_B, Vector2 *r_results, int p_result_max, int &r_result_count) {
  167. Shape2DSW *shape_A = shape_owner.get(p_shape_A);
  168. ERR_FAIL_COND_V(!shape_A, false);
  169. Shape2DSW *shape_B = shape_owner.get(p_shape_B);
  170. ERR_FAIL_COND_V(!shape_B, false);
  171. if (p_result_max == 0) {
  172. return CollisionSolver2DSW::solve(shape_A, p_xform_A, p_motion_A, shape_B, p_xform_B, p_motion_B, NULL, NULL);
  173. }
  174. CollCbkData cbk;
  175. cbk.max = p_result_max;
  176. cbk.amount = 0;
  177. cbk.passed = 0;
  178. cbk.ptr = r_results;
  179. bool res = CollisionSolver2DSW::solve(shape_A, p_xform_A, p_motion_A, shape_B, p_xform_B, p_motion_B, _shape_col_cbk, &cbk);
  180. r_result_count = cbk.amount;
  181. return res;
  182. }
  183. RID Physics2DServerSW::space_create() {
  184. Space2DSW *space = memnew(Space2DSW);
  185. RID id = space_owner.make_rid(space);
  186. space->set_self(id);
  187. RID area_id = area_create();
  188. Area2DSW *area = area_owner.get(area_id);
  189. ERR_FAIL_COND_V(!area, RID());
  190. space->set_default_area(area);
  191. area->set_space(space);
  192. area->set_priority(-1);
  193. return id;
  194. };
  195. void Physics2DServerSW::space_set_active(RID p_space, bool p_active) {
  196. Space2DSW *space = space_owner.get(p_space);
  197. ERR_FAIL_COND(!space);
  198. if (p_active)
  199. active_spaces.insert(space);
  200. else
  201. active_spaces.erase(space);
  202. }
  203. bool Physics2DServerSW::space_is_active(RID p_space) const {
  204. const Space2DSW *space = space_owner.get(p_space);
  205. ERR_FAIL_COND_V(!space, false);
  206. return active_spaces.has(space);
  207. }
  208. void Physics2DServerSW::space_set_param(RID p_space, SpaceParameter p_param, real_t p_value) {
  209. Space2DSW *space = space_owner.get(p_space);
  210. ERR_FAIL_COND(!space);
  211. space->set_param(p_param, p_value);
  212. }
  213. real_t Physics2DServerSW::space_get_param(RID p_space, SpaceParameter p_param) const {
  214. const Space2DSW *space = space_owner.get(p_space);
  215. ERR_FAIL_COND_V(!space, 0);
  216. return space->get_param(p_param);
  217. }
  218. void Physics2DServerSW::space_set_debug_contacts(RID p_space, int p_max_contacts) {
  219. Space2DSW *space = space_owner.get(p_space);
  220. ERR_FAIL_COND(!space);
  221. space->set_debug_contacts(p_max_contacts);
  222. }
  223. Vector<Vector2> Physics2DServerSW::space_get_contacts(RID p_space) const {
  224. Space2DSW *space = space_owner.get(p_space);
  225. ERR_FAIL_COND_V(!space, Vector<Vector2>());
  226. return space->get_debug_contacts();
  227. }
  228. int Physics2DServerSW::space_get_contact_count(RID p_space) const {
  229. Space2DSW *space = space_owner.get(p_space);
  230. ERR_FAIL_COND_V(!space, 0);
  231. return space->get_debug_contact_count();
  232. }
  233. Physics2DDirectSpaceState *Physics2DServerSW::space_get_direct_state(RID p_space) {
  234. Space2DSW *space = space_owner.get(p_space);
  235. ERR_FAIL_COND_V(!space, NULL);
  236. ERR_FAIL_COND_V_MSG((using_threads && !doing_sync) || space->is_locked(), NULL, "Space state is inaccessible right now, wait for iteration or physics process notification.");
  237. return space->get_direct_state();
  238. }
  239. RID Physics2DServerSW::area_create() {
  240. Area2DSW *area = memnew(Area2DSW);
  241. RID rid = area_owner.make_rid(area);
  242. area->set_self(rid);
  243. return rid;
  244. };
  245. void Physics2DServerSW::area_set_space(RID p_area, RID p_space) {
  246. Area2DSW *area = area_owner.get(p_area);
  247. ERR_FAIL_COND(!area);
  248. Space2DSW *space = NULL;
  249. if (p_space.is_valid()) {
  250. space = space_owner.get(p_space);
  251. ERR_FAIL_COND(!space);
  252. }
  253. if (area->get_space() == space)
  254. return; //pointless
  255. area->clear_constraints();
  256. area->set_space(space);
  257. };
  258. RID Physics2DServerSW::area_get_space(RID p_area) const {
  259. Area2DSW *area = area_owner.get(p_area);
  260. ERR_FAIL_COND_V(!area, RID());
  261. Space2DSW *space = area->get_space();
  262. if (!space)
  263. return RID();
  264. return space->get_self();
  265. };
  266. void Physics2DServerSW::area_set_space_override_mode(RID p_area, AreaSpaceOverrideMode p_mode) {
  267. Area2DSW *area = area_owner.get(p_area);
  268. ERR_FAIL_COND(!area);
  269. area->set_space_override_mode(p_mode);
  270. }
  271. Physics2DServer::AreaSpaceOverrideMode Physics2DServerSW::area_get_space_override_mode(RID p_area) const {
  272. const Area2DSW *area = area_owner.get(p_area);
  273. ERR_FAIL_COND_V(!area, AREA_SPACE_OVERRIDE_DISABLED);
  274. return area->get_space_override_mode();
  275. }
  276. void Physics2DServerSW::area_add_shape(RID p_area, RID p_shape, const Transform2D &p_transform, bool p_disabled) {
  277. Area2DSW *area = area_owner.get(p_area);
  278. ERR_FAIL_COND(!area);
  279. Shape2DSW *shape = shape_owner.get(p_shape);
  280. ERR_FAIL_COND(!shape);
  281. area->add_shape(shape, p_transform, p_disabled);
  282. }
  283. void Physics2DServerSW::area_set_shape(RID p_area, int p_shape_idx, RID p_shape) {
  284. Area2DSW *area = area_owner.get(p_area);
  285. ERR_FAIL_COND(!area);
  286. Shape2DSW *shape = shape_owner.get(p_shape);
  287. ERR_FAIL_COND(!shape);
  288. ERR_FAIL_COND(!shape->is_configured());
  289. area->set_shape(p_shape_idx, shape);
  290. }
  291. void Physics2DServerSW::area_set_shape_transform(RID p_area, int p_shape_idx, const Transform2D &p_transform) {
  292. Area2DSW *area = area_owner.get(p_area);
  293. ERR_FAIL_COND(!area);
  294. area->set_shape_transform(p_shape_idx, p_transform);
  295. }
  296. void Physics2DServerSW::area_set_shape_disabled(RID p_area, int p_shape, bool p_disabled) {
  297. Area2DSW *area = area_owner.get(p_area);
  298. ERR_FAIL_COND(!area);
  299. ERR_FAIL_INDEX(p_shape, area->get_shape_count());
  300. FLUSH_QUERY_CHECK(area);
  301. area->set_shape_as_disabled(p_shape, p_disabled);
  302. }
  303. int Physics2DServerSW::area_get_shape_count(RID p_area) const {
  304. Area2DSW *area = area_owner.get(p_area);
  305. ERR_FAIL_COND_V(!area, -1);
  306. return area->get_shape_count();
  307. }
  308. RID Physics2DServerSW::area_get_shape(RID p_area, int p_shape_idx) const {
  309. Area2DSW *area = area_owner.get(p_area);
  310. ERR_FAIL_COND_V(!area, RID());
  311. Shape2DSW *shape = area->get_shape(p_shape_idx);
  312. ERR_FAIL_COND_V(!shape, RID());
  313. return shape->get_self();
  314. }
  315. Transform2D Physics2DServerSW::area_get_shape_transform(RID p_area, int p_shape_idx) const {
  316. Area2DSW *area = area_owner.get(p_area);
  317. ERR_FAIL_COND_V(!area, Transform2D());
  318. return area->get_shape_transform(p_shape_idx);
  319. }
  320. void Physics2DServerSW::area_remove_shape(RID p_area, int p_shape_idx) {
  321. Area2DSW *area = area_owner.get(p_area);
  322. ERR_FAIL_COND(!area);
  323. area->remove_shape(p_shape_idx);
  324. }
  325. void Physics2DServerSW::area_clear_shapes(RID p_area) {
  326. Area2DSW *area = area_owner.get(p_area);
  327. ERR_FAIL_COND(!area);
  328. while (area->get_shape_count())
  329. area->remove_shape(0);
  330. }
  331. void Physics2DServerSW::area_attach_object_instance_id(RID p_area, ObjectID p_id) {
  332. if (space_owner.owns(p_area)) {
  333. Space2DSW *space = space_owner.get(p_area);
  334. p_area = space->get_default_area()->get_self();
  335. }
  336. Area2DSW *area = area_owner.get(p_area);
  337. ERR_FAIL_COND(!area);
  338. area->set_instance_id(p_id);
  339. }
  340. ObjectID Physics2DServerSW::area_get_object_instance_id(RID p_area) const {
  341. if (space_owner.owns(p_area)) {
  342. Space2DSW *space = space_owner.get(p_area);
  343. p_area = space->get_default_area()->get_self();
  344. }
  345. Area2DSW *area = area_owner.get(p_area);
  346. ERR_FAIL_COND_V(!area, 0);
  347. return area->get_instance_id();
  348. }
  349. void Physics2DServerSW::area_attach_canvas_instance_id(RID p_area, ObjectID p_id) {
  350. if (space_owner.owns(p_area)) {
  351. Space2DSW *space = space_owner.get(p_area);
  352. p_area = space->get_default_area()->get_self();
  353. }
  354. Area2DSW *area = area_owner.get(p_area);
  355. ERR_FAIL_COND(!area);
  356. area->set_canvas_instance_id(p_id);
  357. }
  358. ObjectID Physics2DServerSW::area_get_canvas_instance_id(RID p_area) const {
  359. if (space_owner.owns(p_area)) {
  360. Space2DSW *space = space_owner.get(p_area);
  361. p_area = space->get_default_area()->get_self();
  362. }
  363. Area2DSW *area = area_owner.get(p_area);
  364. ERR_FAIL_COND_V(!area, 0);
  365. return area->get_canvas_instance_id();
  366. }
  367. void Physics2DServerSW::area_set_param(RID p_area, AreaParameter p_param, const Variant &p_value) {
  368. if (space_owner.owns(p_area)) {
  369. Space2DSW *space = space_owner.get(p_area);
  370. p_area = space->get_default_area()->get_self();
  371. }
  372. Area2DSW *area = area_owner.get(p_area);
  373. ERR_FAIL_COND(!area);
  374. area->set_param(p_param, p_value);
  375. };
  376. void Physics2DServerSW::area_set_transform(RID p_area, const Transform2D &p_transform) {
  377. Area2DSW *area = area_owner.get(p_area);
  378. ERR_FAIL_COND(!area);
  379. area->set_transform(p_transform);
  380. };
  381. Variant Physics2DServerSW::area_get_param(RID p_area, AreaParameter p_param) const {
  382. if (space_owner.owns(p_area)) {
  383. Space2DSW *space = space_owner.get(p_area);
  384. p_area = space->get_default_area()->get_self();
  385. }
  386. Area2DSW *area = area_owner.get(p_area);
  387. ERR_FAIL_COND_V(!area, Variant());
  388. return area->get_param(p_param);
  389. };
  390. Transform2D Physics2DServerSW::area_get_transform(RID p_area) const {
  391. Area2DSW *area = area_owner.get(p_area);
  392. ERR_FAIL_COND_V(!area, Transform2D());
  393. return area->get_transform();
  394. };
  395. void Physics2DServerSW::area_set_pickable(RID p_area, bool p_pickable) {
  396. Area2DSW *area = area_owner.get(p_area);
  397. ERR_FAIL_COND(!area);
  398. area->set_pickable(p_pickable);
  399. }
  400. void Physics2DServerSW::area_set_monitorable(RID p_area, bool p_monitorable) {
  401. Area2DSW *area = area_owner.get(p_area);
  402. ERR_FAIL_COND(!area);
  403. FLUSH_QUERY_CHECK(area);
  404. area->set_monitorable(p_monitorable);
  405. }
  406. void Physics2DServerSW::area_set_collision_mask(RID p_area, uint32_t p_mask) {
  407. Area2DSW *area = area_owner.get(p_area);
  408. ERR_FAIL_COND(!area);
  409. area->set_collision_mask(p_mask);
  410. }
  411. void Physics2DServerSW::area_set_collision_layer(RID p_area, uint32_t p_layer) {
  412. Area2DSW *area = area_owner.get(p_area);
  413. ERR_FAIL_COND(!area);
  414. area->set_collision_layer(p_layer);
  415. }
  416. void Physics2DServerSW::area_set_monitor_callback(RID p_area, Object *p_receiver, const StringName &p_method) {
  417. Area2DSW *area = area_owner.get(p_area);
  418. ERR_FAIL_COND(!area);
  419. area->set_monitor_callback(p_receiver ? p_receiver->get_instance_id() : 0, p_method);
  420. }
  421. void Physics2DServerSW::area_set_area_monitor_callback(RID p_area, Object *p_receiver, const StringName &p_method) {
  422. Area2DSW *area = area_owner.get(p_area);
  423. ERR_FAIL_COND(!area);
  424. area->set_area_monitor_callback(p_receiver ? p_receiver->get_instance_id() : 0, p_method);
  425. }
  426. /* BODY API */
  427. RID Physics2DServerSW::body_create() {
  428. Body2DSW *body = memnew(Body2DSW);
  429. RID rid = body_owner.make_rid(body);
  430. body->set_self(rid);
  431. return rid;
  432. }
  433. void Physics2DServerSW::body_set_space(RID p_body, RID p_space) {
  434. Body2DSW *body = body_owner.get(p_body);
  435. ERR_FAIL_COND(!body);
  436. Space2DSW *space = NULL;
  437. if (p_space.is_valid()) {
  438. space = space_owner.get(p_space);
  439. ERR_FAIL_COND(!space);
  440. }
  441. if (body->get_space() == space)
  442. return; //pointless
  443. body->clear_constraint_map();
  444. body->set_space(space);
  445. };
  446. RID Physics2DServerSW::body_get_space(RID p_body) const {
  447. Body2DSW *body = body_owner.get(p_body);
  448. ERR_FAIL_COND_V(!body, RID());
  449. Space2DSW *space = body->get_space();
  450. if (!space)
  451. return RID();
  452. return space->get_self();
  453. };
  454. void Physics2DServerSW::body_set_mode(RID p_body, BodyMode p_mode) {
  455. Body2DSW *body = body_owner.get(p_body);
  456. ERR_FAIL_COND(!body);
  457. FLUSH_QUERY_CHECK(body);
  458. body->set_mode(p_mode);
  459. };
  460. Physics2DServer::BodyMode Physics2DServerSW::body_get_mode(RID p_body) const {
  461. Body2DSW *body = body_owner.get(p_body);
  462. ERR_FAIL_COND_V(!body, BODY_MODE_STATIC);
  463. return body->get_mode();
  464. };
  465. void Physics2DServerSW::body_add_shape(RID p_body, RID p_shape, const Transform2D &p_transform, bool p_disabled) {
  466. Body2DSW *body = body_owner.get(p_body);
  467. ERR_FAIL_COND(!body);
  468. Shape2DSW *shape = shape_owner.get(p_shape);
  469. ERR_FAIL_COND(!shape);
  470. body->add_shape(shape, p_transform, p_disabled);
  471. }
  472. void Physics2DServerSW::body_set_shape(RID p_body, int p_shape_idx, RID p_shape) {
  473. Body2DSW *body = body_owner.get(p_body);
  474. ERR_FAIL_COND(!body);
  475. Shape2DSW *shape = shape_owner.get(p_shape);
  476. ERR_FAIL_COND(!shape);
  477. ERR_FAIL_COND(!shape->is_configured());
  478. body->set_shape(p_shape_idx, shape);
  479. }
  480. void Physics2DServerSW::body_set_shape_transform(RID p_body, int p_shape_idx, const Transform2D &p_transform) {
  481. Body2DSW *body = body_owner.get(p_body);
  482. ERR_FAIL_COND(!body);
  483. body->set_shape_transform(p_shape_idx, p_transform);
  484. }
  485. void Physics2DServerSW::body_set_shape_metadata(RID p_body, int p_shape_idx, const Variant &p_metadata) {
  486. Body2DSW *body = body_owner.get(p_body);
  487. ERR_FAIL_COND(!body);
  488. body->set_shape_metadata(p_shape_idx, p_metadata);
  489. }
  490. Variant Physics2DServerSW::body_get_shape_metadata(RID p_body, int p_shape_idx) const {
  491. Body2DSW *body = body_owner.get(p_body);
  492. ERR_FAIL_COND_V(!body, Variant());
  493. return body->get_shape_metadata(p_shape_idx);
  494. }
  495. int Physics2DServerSW::body_get_shape_count(RID p_body) const {
  496. Body2DSW *body = body_owner.get(p_body);
  497. ERR_FAIL_COND_V(!body, -1);
  498. return body->get_shape_count();
  499. }
  500. RID Physics2DServerSW::body_get_shape(RID p_body, int p_shape_idx) const {
  501. Body2DSW *body = body_owner.get(p_body);
  502. ERR_FAIL_COND_V(!body, RID());
  503. Shape2DSW *shape = body->get_shape(p_shape_idx);
  504. ERR_FAIL_COND_V(!shape, RID());
  505. return shape->get_self();
  506. }
  507. Transform2D Physics2DServerSW::body_get_shape_transform(RID p_body, int p_shape_idx) const {
  508. Body2DSW *body = body_owner.get(p_body);
  509. ERR_FAIL_COND_V(!body, Transform2D());
  510. return body->get_shape_transform(p_shape_idx);
  511. }
  512. void Physics2DServerSW::body_remove_shape(RID p_body, int p_shape_idx) {
  513. Body2DSW *body = body_owner.get(p_body);
  514. ERR_FAIL_COND(!body);
  515. body->remove_shape(p_shape_idx);
  516. }
  517. void Physics2DServerSW::body_clear_shapes(RID p_body) {
  518. Body2DSW *body = body_owner.get(p_body);
  519. ERR_FAIL_COND(!body);
  520. while (body->get_shape_count())
  521. body->remove_shape(0);
  522. }
  523. void Physics2DServerSW::body_set_shape_disabled(RID p_body, int p_shape_idx, bool p_disabled) {
  524. Body2DSW *body = body_owner.get(p_body);
  525. ERR_FAIL_COND(!body);
  526. ERR_FAIL_INDEX(p_shape_idx, body->get_shape_count());
  527. FLUSH_QUERY_CHECK(body);
  528. body->set_shape_as_disabled(p_shape_idx, p_disabled);
  529. }
  530. void Physics2DServerSW::body_set_shape_as_one_way_collision(RID p_body, int p_shape_idx, bool p_enable, float p_margin) {
  531. Body2DSW *body = body_owner.get(p_body);
  532. ERR_FAIL_COND(!body);
  533. ERR_FAIL_INDEX(p_shape_idx, body->get_shape_count());
  534. FLUSH_QUERY_CHECK(body);
  535. body->set_shape_as_one_way_collision(p_shape_idx, p_enable, p_margin);
  536. }
  537. void Physics2DServerSW::body_set_continuous_collision_detection_mode(RID p_body, CCDMode p_mode) {
  538. Body2DSW *body = body_owner.get(p_body);
  539. ERR_FAIL_COND(!body);
  540. body->set_continuous_collision_detection_mode(p_mode);
  541. }
  542. Physics2DServerSW::CCDMode Physics2DServerSW::body_get_continuous_collision_detection_mode(RID p_body) const {
  543. const Body2DSW *body = body_owner.get(p_body);
  544. ERR_FAIL_COND_V(!body, CCD_MODE_DISABLED);
  545. return body->get_continuous_collision_detection_mode();
  546. }
  547. void Physics2DServerSW::body_attach_object_instance_id(RID p_body, uint32_t p_id) {
  548. Body2DSW *body = body_owner.get(p_body);
  549. ERR_FAIL_COND(!body);
  550. body->set_instance_id(p_id);
  551. };
  552. uint32_t Physics2DServerSW::body_get_object_instance_id(RID p_body) const {
  553. Body2DSW *body = body_owner.get(p_body);
  554. ERR_FAIL_COND_V(!body, 0);
  555. return body->get_instance_id();
  556. };
  557. void Physics2DServerSW::body_attach_canvas_instance_id(RID p_body, uint32_t p_id) {
  558. Body2DSW *body = body_owner.get(p_body);
  559. ERR_FAIL_COND(!body);
  560. body->set_canvas_instance_id(p_id);
  561. };
  562. uint32_t Physics2DServerSW::body_get_canvas_instance_id(RID p_body) const {
  563. Body2DSW *body = body_owner.get(p_body);
  564. ERR_FAIL_COND_V(!body, 0);
  565. return body->get_canvas_instance_id();
  566. };
  567. void Physics2DServerSW::body_set_collision_layer(RID p_body, uint32_t p_layer) {
  568. Body2DSW *body = body_owner.get(p_body);
  569. ERR_FAIL_COND(!body);
  570. body->set_collision_layer(p_layer);
  571. };
  572. uint32_t Physics2DServerSW::body_get_collision_layer(RID p_body) const {
  573. Body2DSW *body = body_owner.get(p_body);
  574. ERR_FAIL_COND_V(!body, 0);
  575. return body->get_collision_layer();
  576. };
  577. void Physics2DServerSW::body_set_collision_mask(RID p_body, uint32_t p_mask) {
  578. Body2DSW *body = body_owner.get(p_body);
  579. ERR_FAIL_COND(!body);
  580. body->set_collision_mask(p_mask);
  581. };
  582. uint32_t Physics2DServerSW::body_get_collision_mask(RID p_body) const {
  583. Body2DSW *body = body_owner.get(p_body);
  584. ERR_FAIL_COND_V(!body, 0);
  585. return body->get_collision_mask();
  586. };
  587. void Physics2DServerSW::body_set_param(RID p_body, BodyParameter p_param, real_t p_value) {
  588. Body2DSW *body = body_owner.get(p_body);
  589. ERR_FAIL_COND(!body);
  590. body->set_param(p_param, p_value);
  591. };
  592. real_t Physics2DServerSW::body_get_param(RID p_body, BodyParameter p_param) const {
  593. Body2DSW *body = body_owner.get(p_body);
  594. ERR_FAIL_COND_V(!body, 0);
  595. return body->get_param(p_param);
  596. };
  597. void Physics2DServerSW::body_set_state(RID p_body, BodyState p_state, const Variant &p_variant) {
  598. Body2DSW *body = body_owner.get(p_body);
  599. ERR_FAIL_COND(!body);
  600. body->set_state(p_state, p_variant);
  601. };
  602. Variant Physics2DServerSW::body_get_state(RID p_body, BodyState p_state) const {
  603. Body2DSW *body = body_owner.get(p_body);
  604. ERR_FAIL_COND_V(!body, Variant());
  605. return body->get_state(p_state);
  606. };
  607. void Physics2DServerSW::body_set_applied_force(RID p_body, const Vector2 &p_force) {
  608. Body2DSW *body = body_owner.get(p_body);
  609. ERR_FAIL_COND(!body);
  610. body->set_applied_force(p_force);
  611. body->wakeup();
  612. };
  613. Vector2 Physics2DServerSW::body_get_applied_force(RID p_body) const {
  614. Body2DSW *body = body_owner.get(p_body);
  615. ERR_FAIL_COND_V(!body, Vector2());
  616. return body->get_applied_force();
  617. };
  618. void Physics2DServerSW::body_set_applied_torque(RID p_body, real_t p_torque) {
  619. Body2DSW *body = body_owner.get(p_body);
  620. ERR_FAIL_COND(!body);
  621. body->set_applied_torque(p_torque);
  622. body->wakeup();
  623. };
  624. real_t Physics2DServerSW::body_get_applied_torque(RID p_body) const {
  625. Body2DSW *body = body_owner.get(p_body);
  626. ERR_FAIL_COND_V(!body, 0);
  627. return body->get_applied_torque();
  628. };
  629. void Physics2DServerSW::body_apply_central_impulse(RID p_body, const Vector2 &p_impulse) {
  630. Body2DSW *body = body_owner.get(p_body);
  631. ERR_FAIL_COND(!body);
  632. body->apply_central_impulse(p_impulse);
  633. body->wakeup();
  634. }
  635. void Physics2DServerSW::body_apply_torque_impulse(RID p_body, real_t p_torque) {
  636. Body2DSW *body = body_owner.get(p_body);
  637. ERR_FAIL_COND(!body);
  638. _update_shapes();
  639. body->apply_torque_impulse(p_torque);
  640. body->wakeup();
  641. }
  642. void Physics2DServerSW::body_apply_impulse(RID p_body, const Vector2 &p_pos, const Vector2 &p_impulse) {
  643. Body2DSW *body = body_owner.get(p_body);
  644. ERR_FAIL_COND(!body);
  645. _update_shapes();
  646. body->apply_impulse(p_pos, p_impulse);
  647. body->wakeup();
  648. };
  649. void Physics2DServerSW::body_add_central_force(RID p_body, const Vector2 &p_force) {
  650. Body2DSW *body = body_owner.get(p_body);
  651. ERR_FAIL_COND(!body);
  652. body->add_central_force(p_force);
  653. body->wakeup();
  654. };
  655. void Physics2DServerSW::body_add_force(RID p_body, const Vector2 &p_offset, const Vector2 &p_force) {
  656. Body2DSW *body = body_owner.get(p_body);
  657. ERR_FAIL_COND(!body);
  658. body->add_force(p_offset, p_force);
  659. body->wakeup();
  660. };
  661. void Physics2DServerSW::body_add_torque(RID p_body, real_t p_torque) {
  662. Body2DSW *body = body_owner.get(p_body);
  663. ERR_FAIL_COND(!body);
  664. body->add_torque(p_torque);
  665. body->wakeup();
  666. };
  667. void Physics2DServerSW::body_set_axis_velocity(RID p_body, const Vector2 &p_axis_velocity) {
  668. Body2DSW *body = body_owner.get(p_body);
  669. ERR_FAIL_COND(!body);
  670. _update_shapes();
  671. Vector2 v = body->get_linear_velocity();
  672. Vector2 axis = p_axis_velocity.normalized();
  673. v -= axis * axis.dot(v);
  674. v += p_axis_velocity;
  675. body->set_linear_velocity(v);
  676. body->wakeup();
  677. };
  678. void Physics2DServerSW::body_add_collision_exception(RID p_body, RID p_body_b) {
  679. Body2DSW *body = body_owner.get(p_body);
  680. ERR_FAIL_COND(!body);
  681. body->add_exception(p_body_b);
  682. body->wakeup();
  683. };
  684. void Physics2DServerSW::body_remove_collision_exception(RID p_body, RID p_body_b) {
  685. Body2DSW *body = body_owner.get(p_body);
  686. ERR_FAIL_COND(!body);
  687. body->remove_exception(p_body_b);
  688. body->wakeup();
  689. };
  690. void Physics2DServerSW::body_get_collision_exceptions(RID p_body, List<RID> *p_exceptions) {
  691. Body2DSW *body = body_owner.get(p_body);
  692. ERR_FAIL_COND(!body);
  693. for (int i = 0; i < body->get_exceptions().size(); i++) {
  694. p_exceptions->push_back(body->get_exceptions()[i]);
  695. }
  696. };
  697. void Physics2DServerSW::body_set_contacts_reported_depth_threshold(RID p_body, real_t p_threshold) {
  698. Body2DSW *body = body_owner.get(p_body);
  699. ERR_FAIL_COND(!body);
  700. };
  701. real_t Physics2DServerSW::body_get_contacts_reported_depth_threshold(RID p_body) const {
  702. Body2DSW *body = body_owner.get(p_body);
  703. ERR_FAIL_COND_V(!body, 0);
  704. return 0;
  705. };
  706. void Physics2DServerSW::body_set_omit_force_integration(RID p_body, bool p_omit) {
  707. Body2DSW *body = body_owner.get(p_body);
  708. ERR_FAIL_COND(!body);
  709. body->set_omit_force_integration(p_omit);
  710. };
  711. bool Physics2DServerSW::body_is_omitting_force_integration(RID p_body) const {
  712. Body2DSW *body = body_owner.get(p_body);
  713. ERR_FAIL_COND_V(!body, false);
  714. return body->get_omit_force_integration();
  715. };
  716. void Physics2DServerSW::body_set_max_contacts_reported(RID p_body, int p_contacts) {
  717. Body2DSW *body = body_owner.get(p_body);
  718. ERR_FAIL_COND(!body);
  719. body->set_max_contacts_reported(p_contacts);
  720. }
  721. int Physics2DServerSW::body_get_max_contacts_reported(RID p_body) const {
  722. Body2DSW *body = body_owner.get(p_body);
  723. ERR_FAIL_COND_V(!body, -1);
  724. return body->get_max_contacts_reported();
  725. }
  726. void Physics2DServerSW::body_set_force_integration_callback(RID p_body, Object *p_receiver, const StringName &p_method, const Variant &p_udata) {
  727. Body2DSW *body = body_owner.get(p_body);
  728. ERR_FAIL_COND(!body);
  729. body->set_force_integration_callback(p_receiver ? p_receiver->get_instance_id() : ObjectID(0), p_method, p_udata);
  730. }
  731. bool Physics2DServerSW::body_collide_shape(RID p_body, int p_body_shape, RID p_shape, const Transform2D &p_shape_xform, const Vector2 &p_motion, Vector2 *r_results, int p_result_max, int &r_result_count) {
  732. Body2DSW *body = body_owner.get(p_body);
  733. ERR_FAIL_COND_V(!body, false);
  734. ERR_FAIL_INDEX_V(p_body_shape, body->get_shape_count(), false);
  735. 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);
  736. }
  737. void Physics2DServerSW::body_set_pickable(RID p_body, bool p_pickable) {
  738. Body2DSW *body = body_owner.get(p_body);
  739. ERR_FAIL_COND(!body);
  740. body->set_pickable(p_pickable);
  741. }
  742. bool Physics2DServerSW::body_test_motion(RID p_body, const Transform2D &p_from, const Vector2 &p_motion, bool p_infinite_inertia, real_t p_margin, MotionResult *r_result, bool p_exclude_raycast_shapes) {
  743. Body2DSW *body = body_owner.get(p_body);
  744. ERR_FAIL_COND_V(!body, false);
  745. ERR_FAIL_COND_V(!body->get_space(), false);
  746. ERR_FAIL_COND_V(body->get_space()->is_locked(), false);
  747. _update_shapes();
  748. return body->get_space()->test_body_motion(body, p_from, p_motion, p_infinite_inertia, p_margin, r_result, p_exclude_raycast_shapes);
  749. }
  750. int Physics2DServerSW::body_test_ray_separation(RID p_body, const Transform2D &p_transform, bool p_infinite_inertia, Vector2 &r_recover_motion, SeparationResult *r_results, int p_result_max, float p_margin) {
  751. Body2DSW *body = body_owner.get(p_body);
  752. ERR_FAIL_COND_V(!body, false);
  753. ERR_FAIL_COND_V(!body->get_space(), false);
  754. ERR_FAIL_COND_V(body->get_space()->is_locked(), false);
  755. return body->get_space()->test_body_ray_separation(body, p_transform, p_infinite_inertia, r_recover_motion, r_results, p_result_max, p_margin);
  756. }
  757. Physics2DDirectBodyState *Physics2DServerSW::body_get_direct_state(RID p_body) {
  758. ERR_FAIL_COND_V_MSG((using_threads && !doing_sync), NULL, "Body state is inaccessible right now, wait for iteration or physics process notification.");
  759. if (!body_owner.owns(p_body))
  760. return NULL;
  761. Body2DSW *body = body_owner.get(p_body);
  762. ERR_FAIL_COND_V(!body, NULL);
  763. ERR_FAIL_COND_V(!body->get_space(), NULL);
  764. ERR_FAIL_COND_V_MSG(body->get_space()->is_locked(), NULL, "Body state is inaccessible right now, wait for iteration or physics process notification.");
  765. direct_state->body = body;
  766. return direct_state;
  767. }
  768. /* JOINT API */
  769. void Physics2DServerSW::joint_set_param(RID p_joint, JointParam p_param, real_t p_value) {
  770. Joint2DSW *joint = joint_owner.get(p_joint);
  771. ERR_FAIL_COND(!joint);
  772. switch (p_param) {
  773. case JOINT_PARAM_BIAS: joint->set_bias(p_value); break;
  774. case JOINT_PARAM_MAX_BIAS: joint->set_max_bias(p_value); break;
  775. case JOINT_PARAM_MAX_FORCE: joint->set_max_force(p_value); break;
  776. }
  777. }
  778. real_t Physics2DServerSW::joint_get_param(RID p_joint, JointParam p_param) const {
  779. const Joint2DSW *joint = joint_owner.get(p_joint);
  780. ERR_FAIL_COND_V(!joint, -1);
  781. switch (p_param) {
  782. case JOINT_PARAM_BIAS: return joint->get_bias(); break;
  783. case JOINT_PARAM_MAX_BIAS: return joint->get_max_bias(); break;
  784. case JOINT_PARAM_MAX_FORCE: return joint->get_max_force(); break;
  785. }
  786. return 0;
  787. }
  788. void Physics2DServerSW::joint_disable_collisions_between_bodies(RID p_joint, const bool p_disable) {
  789. Joint2DSW *joint = joint_owner.get(p_joint);
  790. ERR_FAIL_COND(!joint);
  791. joint->disable_collisions_between_bodies(p_disable);
  792. if (2 == joint->get_body_count()) {
  793. Body2DSW *body_a = *joint->get_body_ptr();
  794. Body2DSW *body_b = *(joint->get_body_ptr() + 1);
  795. if (p_disable) {
  796. body_add_collision_exception(body_a->get_self(), body_b->get_self());
  797. body_add_collision_exception(body_b->get_self(), body_a->get_self());
  798. } else {
  799. body_remove_collision_exception(body_a->get_self(), body_b->get_self());
  800. body_remove_collision_exception(body_b->get_self(), body_a->get_self());
  801. }
  802. }
  803. }
  804. bool Physics2DServerSW::joint_is_disabled_collisions_between_bodies(RID p_joint) const {
  805. const Joint2DSW *joint = joint_owner.get(p_joint);
  806. ERR_FAIL_COND_V(!joint, true);
  807. return joint->is_disabled_collisions_between_bodies();
  808. }
  809. RID Physics2DServerSW::pin_joint_create(const Vector2 &p_pos, RID p_body_a, RID p_body_b) {
  810. Body2DSW *A = body_owner.get(p_body_a);
  811. ERR_FAIL_COND_V(!A, RID());
  812. Body2DSW *B = NULL;
  813. if (body_owner.owns(p_body_b)) {
  814. B = body_owner.get(p_body_b);
  815. ERR_FAIL_COND_V(!B, RID());
  816. }
  817. Joint2DSW *joint = memnew(PinJoint2DSW(p_pos, A, B));
  818. RID self = joint_owner.make_rid(joint);
  819. joint->set_self(self);
  820. return self;
  821. }
  822. 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) {
  823. Body2DSW *A = body_owner.get(p_body_a);
  824. ERR_FAIL_COND_V(!A, RID());
  825. Body2DSW *B = body_owner.get(p_body_b);
  826. ERR_FAIL_COND_V(!B, RID());
  827. Joint2DSW *joint = memnew(GrooveJoint2DSW(p_a_groove1, p_a_groove2, p_b_anchor, A, B));
  828. RID self = joint_owner.make_rid(joint);
  829. joint->set_self(self);
  830. return self;
  831. }
  832. RID Physics2DServerSW::damped_spring_joint_create(const Vector2 &p_anchor_a, const Vector2 &p_anchor_b, RID p_body_a, RID p_body_b) {
  833. Body2DSW *A = body_owner.get(p_body_a);
  834. ERR_FAIL_COND_V(!A, RID());
  835. Body2DSW *B = body_owner.get(p_body_b);
  836. ERR_FAIL_COND_V(!B, RID());
  837. Joint2DSW *joint = memnew(DampedSpringJoint2DSW(p_anchor_a, p_anchor_b, A, B));
  838. RID self = joint_owner.make_rid(joint);
  839. joint->set_self(self);
  840. return self;
  841. }
  842. void Physics2DServerSW::pin_joint_set_param(RID p_joint, PinJointParam p_param, real_t p_value) {
  843. Joint2DSW *j = joint_owner.get(p_joint);
  844. ERR_FAIL_COND(!j);
  845. ERR_FAIL_COND(j->get_type() != JOINT_PIN);
  846. PinJoint2DSW *pin_joint = static_cast<PinJoint2DSW *>(j);
  847. pin_joint->set_param(p_param, p_value);
  848. }
  849. real_t Physics2DServerSW::pin_joint_get_param(RID p_joint, PinJointParam p_param) const {
  850. Joint2DSW *j = joint_owner.get(p_joint);
  851. ERR_FAIL_COND_V(!j, 0);
  852. ERR_FAIL_COND_V(j->get_type() != JOINT_PIN, 0);
  853. PinJoint2DSW *pin_joint = static_cast<PinJoint2DSW *>(j);
  854. return pin_joint->get_param(p_param);
  855. }
  856. void Physics2DServerSW::damped_string_joint_set_param(RID p_joint, DampedStringParam p_param, real_t p_value) {
  857. Joint2DSW *j = joint_owner.get(p_joint);
  858. ERR_FAIL_COND(!j);
  859. ERR_FAIL_COND(j->get_type() != JOINT_DAMPED_SPRING);
  860. DampedSpringJoint2DSW *dsj = static_cast<DampedSpringJoint2DSW *>(j);
  861. dsj->set_param(p_param, p_value);
  862. }
  863. real_t Physics2DServerSW::damped_string_joint_get_param(RID p_joint, DampedStringParam p_param) const {
  864. Joint2DSW *j = joint_owner.get(p_joint);
  865. ERR_FAIL_COND_V(!j, 0);
  866. ERR_FAIL_COND_V(j->get_type() != JOINT_DAMPED_SPRING, 0);
  867. DampedSpringJoint2DSW *dsj = static_cast<DampedSpringJoint2DSW *>(j);
  868. return dsj->get_param(p_param);
  869. }
  870. Physics2DServer::JointType Physics2DServerSW::joint_get_type(RID p_joint) const {
  871. Joint2DSW *joint = joint_owner.get(p_joint);
  872. ERR_FAIL_COND_V(!joint, JOINT_PIN);
  873. return joint->get_type();
  874. }
  875. void Physics2DServerSW::free(RID p_rid) {
  876. _update_shapes(); // just in case
  877. if (shape_owner.owns(p_rid)) {
  878. Shape2DSW *shape = shape_owner.get(p_rid);
  879. while (shape->get_owners().size()) {
  880. ShapeOwner2DSW *so = shape->get_owners().front()->key();
  881. so->remove_shape(shape);
  882. }
  883. shape_owner.free(p_rid);
  884. memdelete(shape);
  885. } else if (body_owner.owns(p_rid)) {
  886. Body2DSW *body = body_owner.get(p_rid);
  887. /*
  888. if (body->get_state_query())
  889. _clear_query(body->get_state_query());
  890. if (body->get_direct_state_query())
  891. _clear_query(body->get_direct_state_query());
  892. */
  893. body_set_space(p_rid, RID());
  894. while (body->get_shape_count()) {
  895. body->remove_shape(0);
  896. }
  897. body_owner.free(p_rid);
  898. memdelete(body);
  899. } else if (area_owner.owns(p_rid)) {
  900. Area2DSW *area = area_owner.get(p_rid);
  901. /*
  902. if (area->get_monitor_query())
  903. _clear_query(area->get_monitor_query());
  904. */
  905. area->set_space(NULL);
  906. while (area->get_shape_count()) {
  907. area->remove_shape(0);
  908. }
  909. area_owner.free(p_rid);
  910. memdelete(area);
  911. } else if (space_owner.owns(p_rid)) {
  912. Space2DSW *space = space_owner.get(p_rid);
  913. while (space->get_objects().size()) {
  914. CollisionObject2DSW *co = (CollisionObject2DSW *)space->get_objects().front()->get();
  915. co->set_space(NULL);
  916. }
  917. active_spaces.erase(space);
  918. free(space->get_default_area()->get_self());
  919. space_owner.free(p_rid);
  920. memdelete(space);
  921. } else if (joint_owner.owns(p_rid)) {
  922. Joint2DSW *joint = joint_owner.get(p_rid);
  923. joint_owner.free(p_rid);
  924. memdelete(joint);
  925. } else {
  926. ERR_FAIL_MSG("Invalid ID.");
  927. }
  928. };
  929. void Physics2DServerSW::set_active(bool p_active) {
  930. active = p_active;
  931. };
  932. void Physics2DServerSW::init() {
  933. doing_sync = false;
  934. last_step = 0.001;
  935. iterations = 8; // 8?
  936. stepper = memnew(Step2DSW);
  937. direct_state = memnew(Physics2DDirectBodyStateSW);
  938. };
  939. void Physics2DServerSW::step(real_t p_step) {
  940. if (!active)
  941. return;
  942. _update_shapes();
  943. last_step = p_step;
  944. Physics2DDirectBodyStateSW::singleton->step = p_step;
  945. island_count = 0;
  946. active_objects = 0;
  947. collision_pairs = 0;
  948. for (Set<const Space2DSW *>::Element *E = active_spaces.front(); E; E = E->next()) {
  949. stepper->step((Space2DSW *)E->get(), p_step, iterations);
  950. island_count += E->get()->get_island_count();
  951. active_objects += E->get()->get_active_objects();
  952. collision_pairs += E->get()->get_collision_pairs();
  953. }
  954. };
  955. void Physics2DServerSW::sync() {
  956. doing_sync = true;
  957. };
  958. void Physics2DServerSW::flush_queries() {
  959. if (!active)
  960. return;
  961. flushing_queries = true;
  962. uint64_t time_beg = OS::get_singleton()->get_ticks_usec();
  963. for (Set<const Space2DSW *>::Element *E = active_spaces.front(); E; E = E->next()) {
  964. Space2DSW *space = (Space2DSW *)E->get();
  965. space->call_queries();
  966. }
  967. flushing_queries = false;
  968. if (ScriptDebugger::get_singleton() && ScriptDebugger::get_singleton()->is_profiling()) {
  969. uint64_t total_time[Space2DSW::ELAPSED_TIME_MAX];
  970. static const char *time_name[Space2DSW::ELAPSED_TIME_MAX] = {
  971. "integrate_forces",
  972. "generate_islands",
  973. "setup_constraints",
  974. "solve_constraints",
  975. "integrate_velocities"
  976. };
  977. for (int i = 0; i < Space2DSW::ELAPSED_TIME_MAX; i++) {
  978. total_time[i] = 0;
  979. }
  980. for (Set<const Space2DSW *>::Element *E = active_spaces.front(); E; E = E->next()) {
  981. for (int i = 0; i < Space2DSW::ELAPSED_TIME_MAX; i++) {
  982. total_time[i] += E->get()->get_elapsed_time(Space2DSW::ElapsedTime(i));
  983. }
  984. }
  985. Array values;
  986. values.resize(Space2DSW::ELAPSED_TIME_MAX * 2);
  987. for (int i = 0; i < Space2DSW::ELAPSED_TIME_MAX; i++) {
  988. values[i * 2 + 0] = time_name[i];
  989. values[i * 2 + 1] = USEC_TO_SEC(total_time[i]);
  990. }
  991. values.push_back("flush_queries");
  992. values.push_back(USEC_TO_SEC(OS::get_singleton()->get_ticks_usec() - time_beg));
  993. ScriptDebugger::get_singleton()->add_profiling_frame_data("physics_2d", values);
  994. }
  995. }
  996. void Physics2DServerSW::end_sync() {
  997. doing_sync = false;
  998. }
  999. void Physics2DServerSW::finish() {
  1000. memdelete(stepper);
  1001. memdelete(direct_state);
  1002. };
  1003. void Physics2DServerSW::_update_shapes() {
  1004. while (pending_shape_update_list.first()) {
  1005. pending_shape_update_list.first()->self()->_shape_changed();
  1006. pending_shape_update_list.remove(pending_shape_update_list.first());
  1007. }
  1008. }
  1009. int Physics2DServerSW::get_process_info(ProcessInfo p_info) {
  1010. switch (p_info) {
  1011. case INFO_ACTIVE_OBJECTS: {
  1012. return active_objects;
  1013. } break;
  1014. case INFO_COLLISION_PAIRS: {
  1015. return collision_pairs;
  1016. } break;
  1017. case INFO_ISLAND_COUNT: {
  1018. return island_count;
  1019. } break;
  1020. }
  1021. return 0;
  1022. }
  1023. Physics2DServerSW *Physics2DServerSW::singletonsw = NULL;
  1024. Physics2DServerSW::Physics2DServerSW() {
  1025. singletonsw = this;
  1026. BroadPhase2DSW::create_func = BroadPhase2DHashGrid::_create;
  1027. //BroadPhase2DSW::create_func=BroadPhase2DBasic::_create;
  1028. active = true;
  1029. island_count = 0;
  1030. active_objects = 0;
  1031. collision_pairs = 0;
  1032. #ifdef NO_THREADS
  1033. using_threads = false;
  1034. #else
  1035. using_threads = int(ProjectSettings::get_singleton()->get("physics/2d/thread_model")) == 2;
  1036. #endif
  1037. flushing_queries = false;
  1038. };
  1039. Physics2DServerSW::~Physics2DServerSW(){
  1040. };