godot_physics_server_2d.cpp 41 KB

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