physics_2d_server_sw.cpp 40 KB

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