godot_space_2d.cpp 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242
  1. /**************************************************************************/
  2. /* godot_space_2d.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 "godot_space_2d.h"
  31. #include "godot_collision_solver_2d.h"
  32. #include "godot_physics_server_2d.h"
  33. #include "core/config/project_settings.h"
  34. #include "godot_area_pair_2d.h"
  35. #include "godot_body_pair_2d.h"
  36. #define TEST_MOTION_MARGIN_MIN_VALUE 0.0001
  37. #define TEST_MOTION_MIN_CONTACT_DEPTH_FACTOR 0.05
  38. _FORCE_INLINE_ static bool _can_collide_with(GodotCollisionObject2D *p_object, uint32_t p_collision_mask, bool p_collide_with_bodies, bool p_collide_with_areas) {
  39. if (!(p_object->get_collision_layer() & p_collision_mask)) {
  40. return false;
  41. }
  42. if (p_object->get_type() == GodotCollisionObject2D::TYPE_AREA && !p_collide_with_areas) {
  43. return false;
  44. }
  45. if (p_object->get_type() == GodotCollisionObject2D::TYPE_BODY && !p_collide_with_bodies) {
  46. return false;
  47. }
  48. return true;
  49. }
  50. int GodotPhysicsDirectSpaceState2D::intersect_point(const PointParameters &p_parameters, ShapeResult *r_results, int p_result_max) {
  51. if (p_result_max <= 0) {
  52. return 0;
  53. }
  54. Rect2 aabb;
  55. aabb.position = p_parameters.position - Vector2(0.00001, 0.00001);
  56. aabb.size = Vector2(0.00002, 0.00002);
  57. int amount = space->broadphase->cull_aabb(aabb, space->intersection_query_results, GodotSpace2D::INTERSECTION_QUERY_MAX, space->intersection_query_subindex_results);
  58. int cc = 0;
  59. for (int i = 0; i < amount; i++) {
  60. if (!_can_collide_with(space->intersection_query_results[i], p_parameters.collision_mask, p_parameters.collide_with_bodies, p_parameters.collide_with_areas)) {
  61. continue;
  62. }
  63. if (p_parameters.exclude.has(space->intersection_query_results[i]->get_self())) {
  64. continue;
  65. }
  66. const GodotCollisionObject2D *col_obj = space->intersection_query_results[i];
  67. if (p_parameters.pick_point && !col_obj->is_pickable()) {
  68. continue;
  69. }
  70. if (col_obj->get_canvas_instance_id() != p_parameters.canvas_instance_id) {
  71. continue;
  72. }
  73. int shape_idx = space->intersection_query_subindex_results[i];
  74. GodotShape2D *shape = col_obj->get_shape(shape_idx);
  75. Vector2 local_point = (col_obj->get_transform() * col_obj->get_shape_transform(shape_idx)).affine_inverse().xform(p_parameters.position);
  76. if (!shape->contains_point(local_point)) {
  77. continue;
  78. }
  79. if (cc >= p_result_max) {
  80. continue;
  81. }
  82. r_results[cc].collider_id = col_obj->get_instance_id();
  83. if (r_results[cc].collider_id.is_valid()) {
  84. r_results[cc].collider = ObjectDB::get_instance(r_results[cc].collider_id);
  85. }
  86. r_results[cc].rid = col_obj->get_self();
  87. r_results[cc].shape = shape_idx;
  88. cc++;
  89. }
  90. return cc;
  91. }
  92. bool GodotPhysicsDirectSpaceState2D::intersect_ray(const RayParameters &p_parameters, RayResult &r_result) {
  93. ERR_FAIL_COND_V(space->locked, false);
  94. Vector2 begin, end;
  95. Vector2 normal;
  96. begin = p_parameters.from;
  97. end = p_parameters.to;
  98. normal = (end - begin).normalized();
  99. int amount = space->broadphase->cull_segment(begin, end, space->intersection_query_results, GodotSpace2D::INTERSECTION_QUERY_MAX, space->intersection_query_subindex_results);
  100. //todo, create another array that references results, compute AABBs and check closest point to ray origin, sort, and stop evaluating results when beyond first collision
  101. bool collided = false;
  102. Vector2 res_point, res_normal;
  103. int res_shape = -1;
  104. const GodotCollisionObject2D *res_obj = nullptr;
  105. real_t min_d = 1e10;
  106. for (int i = 0; i < amount; i++) {
  107. if (!_can_collide_with(space->intersection_query_results[i], p_parameters.collision_mask, p_parameters.collide_with_bodies, p_parameters.collide_with_areas)) {
  108. continue;
  109. }
  110. if (p_parameters.exclude.has(space->intersection_query_results[i]->get_self())) {
  111. continue;
  112. }
  113. const GodotCollisionObject2D *col_obj = space->intersection_query_results[i];
  114. int shape_idx = space->intersection_query_subindex_results[i];
  115. Transform2D inv_xform = col_obj->get_shape_inv_transform(shape_idx) * col_obj->get_inv_transform();
  116. Vector2 local_from = inv_xform.xform(begin);
  117. Vector2 local_to = inv_xform.xform(end);
  118. const GodotShape2D *shape = col_obj->get_shape(shape_idx);
  119. Vector2 shape_point, shape_normal;
  120. if (shape->contains_point(local_from)) {
  121. if (p_parameters.hit_from_inside) {
  122. // Hit shape at starting point.
  123. min_d = 0;
  124. res_point = begin;
  125. res_normal = Vector2();
  126. res_shape = shape_idx;
  127. res_obj = col_obj;
  128. collided = true;
  129. break;
  130. } else {
  131. // Ignore shape when starting inside.
  132. continue;
  133. }
  134. }
  135. if (shape->intersect_segment(local_from, local_to, shape_point, shape_normal)) {
  136. Transform2D xform = col_obj->get_transform() * col_obj->get_shape_transform(shape_idx);
  137. shape_point = xform.xform(shape_point);
  138. real_t ld = normal.dot(shape_point);
  139. if (ld < min_d) {
  140. min_d = ld;
  141. res_point = shape_point;
  142. res_normal = inv_xform.basis_xform_inv(shape_normal).normalized();
  143. res_shape = shape_idx;
  144. res_obj = col_obj;
  145. collided = true;
  146. }
  147. }
  148. }
  149. if (!collided) {
  150. return false;
  151. }
  152. ERR_FAIL_NULL_V(res_obj, false); // Shouldn't happen but silences warning.
  153. r_result.collider_id = res_obj->get_instance_id();
  154. if (r_result.collider_id.is_valid()) {
  155. r_result.collider = ObjectDB::get_instance(r_result.collider_id);
  156. }
  157. r_result.normal = res_normal;
  158. r_result.position = res_point;
  159. r_result.rid = res_obj->get_self();
  160. r_result.shape = res_shape;
  161. return true;
  162. }
  163. int GodotPhysicsDirectSpaceState2D::intersect_shape(const ShapeParameters &p_parameters, ShapeResult *r_results, int p_result_max) {
  164. if (p_result_max <= 0) {
  165. return 0;
  166. }
  167. GodotShape2D *shape = GodotPhysicsServer2D::godot_singleton->shape_owner.get_or_null(p_parameters.shape_rid);
  168. ERR_FAIL_NULL_V(shape, 0);
  169. Rect2 aabb = p_parameters.transform.xform(shape->get_aabb());
  170. aabb = aabb.merge(Rect2(aabb.position + p_parameters.motion, aabb.size)); //motion
  171. aabb = aabb.grow(p_parameters.margin);
  172. int amount = space->broadphase->cull_aabb(aabb, space->intersection_query_results, GodotSpace2D::INTERSECTION_QUERY_MAX, space->intersection_query_subindex_results);
  173. int cc = 0;
  174. for (int i = 0; i < amount; i++) {
  175. if (cc >= p_result_max) {
  176. break;
  177. }
  178. if (!_can_collide_with(space->intersection_query_results[i], p_parameters.collision_mask, p_parameters.collide_with_bodies, p_parameters.collide_with_areas)) {
  179. continue;
  180. }
  181. if (p_parameters.exclude.has(space->intersection_query_results[i]->get_self())) {
  182. continue;
  183. }
  184. const GodotCollisionObject2D *col_obj = space->intersection_query_results[i];
  185. int shape_idx = space->intersection_query_subindex_results[i];
  186. if (!GodotCollisionSolver2D::solve(shape, p_parameters.transform, p_parameters.motion, col_obj->get_shape(shape_idx), col_obj->get_transform() * col_obj->get_shape_transform(shape_idx), Vector2(), nullptr, nullptr, nullptr, p_parameters.margin)) {
  187. continue;
  188. }
  189. r_results[cc].collider_id = col_obj->get_instance_id();
  190. if (r_results[cc].collider_id.is_valid()) {
  191. r_results[cc].collider = ObjectDB::get_instance(r_results[cc].collider_id);
  192. }
  193. r_results[cc].rid = col_obj->get_self();
  194. r_results[cc].shape = shape_idx;
  195. cc++;
  196. }
  197. return cc;
  198. }
  199. bool GodotPhysicsDirectSpaceState2D::cast_motion(const ShapeParameters &p_parameters, real_t &p_closest_safe, real_t &p_closest_unsafe) {
  200. GodotShape2D *shape = GodotPhysicsServer2D::godot_singleton->shape_owner.get_or_null(p_parameters.shape_rid);
  201. ERR_FAIL_NULL_V(shape, false);
  202. Rect2 aabb = p_parameters.transform.xform(shape->get_aabb());
  203. aabb = aabb.merge(Rect2(aabb.position + p_parameters.motion, aabb.size)); //motion
  204. aabb = aabb.grow(p_parameters.margin);
  205. int amount = space->broadphase->cull_aabb(aabb, space->intersection_query_results, GodotSpace2D::INTERSECTION_QUERY_MAX, space->intersection_query_subindex_results);
  206. real_t best_safe = 1;
  207. real_t best_unsafe = 1;
  208. for (int i = 0; i < amount; i++) {
  209. if (!_can_collide_with(space->intersection_query_results[i], p_parameters.collision_mask, p_parameters.collide_with_bodies, p_parameters.collide_with_areas)) {
  210. continue;
  211. }
  212. if (p_parameters.exclude.has(space->intersection_query_results[i]->get_self())) {
  213. continue; //ignore excluded
  214. }
  215. const GodotCollisionObject2D *col_obj = space->intersection_query_results[i];
  216. int shape_idx = space->intersection_query_subindex_results[i];
  217. Transform2D col_obj_xform = col_obj->get_transform() * col_obj->get_shape_transform(shape_idx);
  218. //test initial overlap, does it collide if going all the way?
  219. if (!GodotCollisionSolver2D::solve(shape, p_parameters.transform, p_parameters.motion, col_obj->get_shape(shape_idx), col_obj_xform, Vector2(), nullptr, nullptr, nullptr, p_parameters.margin)) {
  220. continue;
  221. }
  222. //test initial overlap, ignore objects it's inside of.
  223. if (GodotCollisionSolver2D::solve(shape, p_parameters.transform, Vector2(), col_obj->get_shape(shape_idx), col_obj_xform, Vector2(), nullptr, nullptr, nullptr, p_parameters.margin)) {
  224. continue;
  225. }
  226. Vector2 mnormal = p_parameters.motion.normalized();
  227. //just do kinematic solving
  228. real_t low = 0.0;
  229. real_t hi = 1.0;
  230. real_t fraction_coeff = 0.5;
  231. for (int j = 0; j < 8; j++) { //steps should be customizable..
  232. real_t fraction = low + (hi - low) * fraction_coeff;
  233. Vector2 sep = mnormal; //important optimization for this to work fast enough
  234. bool collided = GodotCollisionSolver2D::solve(shape, p_parameters.transform, p_parameters.motion * fraction, col_obj->get_shape(shape_idx), col_obj_xform, Vector2(), nullptr, nullptr, &sep, p_parameters.margin);
  235. if (collided) {
  236. hi = fraction;
  237. if ((j == 0) || (low > 0.0)) { // Did it not collide before?
  238. // When alternating or first iteration, use dichotomy.
  239. fraction_coeff = 0.5;
  240. } else {
  241. // When colliding again, converge faster towards low fraction
  242. // for more accurate results with long motions that collide near the start.
  243. fraction_coeff = 0.25;
  244. }
  245. } else {
  246. low = fraction;
  247. if ((j == 0) || (hi < 1.0)) { // Did it collide before?
  248. // When alternating or first iteration, use dichotomy.
  249. fraction_coeff = 0.5;
  250. } else {
  251. // When not colliding again, converge faster towards high fraction
  252. // for more accurate results with long motions that collide near the end.
  253. fraction_coeff = 0.75;
  254. }
  255. }
  256. }
  257. if (low < best_safe) {
  258. best_safe = low;
  259. best_unsafe = hi;
  260. }
  261. }
  262. p_closest_safe = best_safe;
  263. p_closest_unsafe = best_unsafe;
  264. return true;
  265. }
  266. bool GodotPhysicsDirectSpaceState2D::collide_shape(const ShapeParameters &p_parameters, Vector2 *r_results, int p_result_max, int &r_result_count) {
  267. if (p_result_max <= 0) {
  268. return false;
  269. }
  270. GodotShape2D *shape = GodotPhysicsServer2D::godot_singleton->shape_owner.get_or_null(p_parameters.shape_rid);
  271. ERR_FAIL_NULL_V(shape, false);
  272. Rect2 aabb = p_parameters.transform.xform(shape->get_aabb());
  273. aabb = aabb.merge(Rect2(aabb.position + p_parameters.motion, aabb.size)); //motion
  274. aabb = aabb.grow(p_parameters.margin);
  275. int amount = space->broadphase->cull_aabb(aabb, space->intersection_query_results, GodotSpace2D::INTERSECTION_QUERY_MAX, space->intersection_query_subindex_results);
  276. bool collided = false;
  277. r_result_count = 0;
  278. GodotPhysicsServer2D::CollCbkData cbk;
  279. cbk.max = p_result_max;
  280. cbk.amount = 0;
  281. cbk.passed = 0;
  282. cbk.ptr = r_results;
  283. GodotCollisionSolver2D::CallbackResult cbkres = GodotPhysicsServer2D::_shape_col_cbk;
  284. GodotPhysicsServer2D::CollCbkData *cbkptr = &cbk;
  285. for (int i = 0; i < amount; i++) {
  286. if (!_can_collide_with(space->intersection_query_results[i], p_parameters.collision_mask, p_parameters.collide_with_bodies, p_parameters.collide_with_areas)) {
  287. continue;
  288. }
  289. const GodotCollisionObject2D *col_obj = space->intersection_query_results[i];
  290. if (p_parameters.exclude.has(col_obj->get_self())) {
  291. continue;
  292. }
  293. int shape_idx = space->intersection_query_subindex_results[i];
  294. cbk.valid_dir = Vector2();
  295. cbk.valid_depth = 0;
  296. if (GodotCollisionSolver2D::solve(shape, p_parameters.transform, p_parameters.motion, col_obj->get_shape(shape_idx), col_obj->get_transform() * col_obj->get_shape_transform(shape_idx), Vector2(), cbkres, cbkptr, nullptr, p_parameters.margin)) {
  297. collided = cbk.amount > 0;
  298. }
  299. }
  300. r_result_count = cbk.amount;
  301. return collided;
  302. }
  303. struct _RestCallbackData2D {
  304. const GodotCollisionObject2D *object = nullptr;
  305. const GodotCollisionObject2D *best_object = nullptr;
  306. int local_shape = 0;
  307. int best_local_shape = 0;
  308. int shape = 0;
  309. int best_shape = 0;
  310. Vector2 best_contact;
  311. Vector2 best_normal;
  312. real_t best_len = 0.0;
  313. Vector2 valid_dir;
  314. real_t valid_depth = 0.0;
  315. real_t min_allowed_depth = 0.0;
  316. };
  317. static void _rest_cbk_result(const Vector2 &p_point_A, const Vector2 &p_point_B, void *p_userdata) {
  318. _RestCallbackData2D *rd = static_cast<_RestCallbackData2D *>(p_userdata);
  319. Vector2 contact_rel = p_point_B - p_point_A;
  320. real_t len = contact_rel.length();
  321. if (len < rd->min_allowed_depth) {
  322. return;
  323. }
  324. if (len <= rd->best_len) {
  325. return;
  326. }
  327. Vector2 normal = contact_rel / len;
  328. if (rd->valid_dir != Vector2()) {
  329. if (len > rd->valid_depth) {
  330. return;
  331. }
  332. if (rd->valid_dir.dot(normal) > -CMP_EPSILON) {
  333. return;
  334. }
  335. }
  336. rd->best_len = len;
  337. rd->best_contact = p_point_B;
  338. rd->best_normal = normal;
  339. rd->best_object = rd->object;
  340. rd->best_shape = rd->shape;
  341. rd->best_local_shape = rd->local_shape;
  342. }
  343. bool GodotPhysicsDirectSpaceState2D::rest_info(const ShapeParameters &p_parameters, ShapeRestInfo *r_info) {
  344. GodotShape2D *shape = GodotPhysicsServer2D::godot_singleton->shape_owner.get_or_null(p_parameters.shape_rid);
  345. ERR_FAIL_NULL_V(shape, false);
  346. real_t margin = MAX(p_parameters.margin, TEST_MOTION_MARGIN_MIN_VALUE);
  347. Rect2 aabb = p_parameters.transform.xform(shape->get_aabb());
  348. aabb = aabb.merge(Rect2(aabb.position + p_parameters.motion, aabb.size)); //motion
  349. aabb = aabb.grow(margin);
  350. int amount = space->broadphase->cull_aabb(aabb, space->intersection_query_results, GodotSpace2D::INTERSECTION_QUERY_MAX, space->intersection_query_subindex_results);
  351. _RestCallbackData2D rcd;
  352. // Allowed depth can't be lower than motion length, in order to handle contacts at low speed.
  353. real_t motion_length = p_parameters.motion.length();
  354. real_t min_contact_depth = margin * TEST_MOTION_MIN_CONTACT_DEPTH_FACTOR;
  355. rcd.min_allowed_depth = MIN(motion_length, min_contact_depth);
  356. for (int i = 0; i < amount; i++) {
  357. if (!_can_collide_with(space->intersection_query_results[i], p_parameters.collision_mask, p_parameters.collide_with_bodies, p_parameters.collide_with_areas)) {
  358. continue;
  359. }
  360. const GodotCollisionObject2D *col_obj = space->intersection_query_results[i];
  361. if (p_parameters.exclude.has(col_obj->get_self())) {
  362. continue;
  363. }
  364. int shape_idx = space->intersection_query_subindex_results[i];
  365. rcd.valid_dir = Vector2();
  366. rcd.object = col_obj;
  367. rcd.shape = shape_idx;
  368. rcd.local_shape = 0;
  369. bool sc = GodotCollisionSolver2D::solve(shape, p_parameters.transform, p_parameters.motion, col_obj->get_shape(shape_idx), col_obj->get_transform() * col_obj->get_shape_transform(shape_idx), Vector2(), _rest_cbk_result, &rcd, nullptr, margin);
  370. if (!sc) {
  371. continue;
  372. }
  373. }
  374. if (rcd.best_len == 0 || !rcd.best_object) {
  375. return false;
  376. }
  377. r_info->collider_id = rcd.best_object->get_instance_id();
  378. r_info->shape = rcd.best_shape;
  379. r_info->normal = rcd.best_normal;
  380. r_info->point = rcd.best_contact;
  381. r_info->rid = rcd.best_object->get_self();
  382. if (rcd.best_object->get_type() == GodotCollisionObject2D::TYPE_BODY) {
  383. const GodotBody2D *body = static_cast<const GodotBody2D *>(rcd.best_object);
  384. Vector2 rel_vec = r_info->point - (body->get_transform().get_origin() + body->get_center_of_mass());
  385. r_info->linear_velocity = Vector2(-body->get_angular_velocity() * rel_vec.y, body->get_angular_velocity() * rel_vec.x) + body->get_linear_velocity();
  386. } else {
  387. r_info->linear_velocity = Vector2();
  388. }
  389. return true;
  390. }
  391. ////////////////////////////////////////////////////////////////////////////////////////////////////////////
  392. int GodotSpace2D::_cull_aabb_for_body(GodotBody2D *p_body, const Rect2 &p_aabb) {
  393. int amount = broadphase->cull_aabb(p_aabb, intersection_query_results, INTERSECTION_QUERY_MAX, intersection_query_subindex_results);
  394. for (int i = 0; i < amount; i++) {
  395. bool keep = true;
  396. if (intersection_query_results[i] == p_body) {
  397. keep = false;
  398. } else if (intersection_query_results[i]->get_type() == GodotCollisionObject2D::TYPE_AREA) {
  399. keep = false;
  400. } else if (!p_body->collides_with(static_cast<GodotBody2D *>(intersection_query_results[i]))) {
  401. keep = false;
  402. } else if (static_cast<GodotBody2D *>(intersection_query_results[i])->has_exception(p_body->get_self()) || p_body->has_exception(intersection_query_results[i]->get_self())) {
  403. keep = false;
  404. }
  405. if (!keep) {
  406. if (i < amount - 1) {
  407. SWAP(intersection_query_results[i], intersection_query_results[amount - 1]);
  408. SWAP(intersection_query_subindex_results[i], intersection_query_subindex_results[amount - 1]);
  409. }
  410. amount--;
  411. i--;
  412. }
  413. }
  414. return amount;
  415. }
  416. bool GodotSpace2D::test_body_motion(GodotBody2D *p_body, const PhysicsServer2D::MotionParameters &p_parameters, PhysicsServer2D::MotionResult *r_result) {
  417. //give me back regular physics engine logic
  418. //this is madness
  419. //and most people using this function will think
  420. //what it does is simpler than using physics
  421. //this took about a week to get right..
  422. //but is it right? who knows at this point..
  423. if (r_result) {
  424. r_result->collider_id = ObjectID();
  425. r_result->collider_shape = 0;
  426. }
  427. Rect2 body_aabb;
  428. bool shapes_found = false;
  429. for (int i = 0; i < p_body->get_shape_count(); i++) {
  430. if (p_body->is_shape_disabled(i)) {
  431. continue;
  432. }
  433. if (!shapes_found) {
  434. body_aabb = p_body->get_shape_aabb(i);
  435. shapes_found = true;
  436. } else {
  437. body_aabb = body_aabb.merge(p_body->get_shape_aabb(i));
  438. }
  439. }
  440. if (!shapes_found) {
  441. if (r_result) {
  442. *r_result = PhysicsServer2D::MotionResult();
  443. r_result->travel = p_parameters.motion;
  444. }
  445. return false;
  446. }
  447. real_t margin = MAX(p_parameters.margin, TEST_MOTION_MARGIN_MIN_VALUE);
  448. // Undo the currently transform the physics server is aware of and apply the provided one
  449. body_aabb = p_parameters.from.xform(p_body->get_inv_transform().xform(body_aabb));
  450. body_aabb = body_aabb.grow(margin);
  451. static const int max_excluded_shape_pairs = 32;
  452. ExcludedShapeSW excluded_shape_pairs[max_excluded_shape_pairs];
  453. int excluded_shape_pair_count = 0;
  454. real_t min_contact_depth = margin * TEST_MOTION_MIN_CONTACT_DEPTH_FACTOR;
  455. real_t motion_length = p_parameters.motion.length();
  456. Vector2 motion_normal = p_parameters.motion / motion_length;
  457. Transform2D body_transform = p_parameters.from;
  458. bool recovered = false;
  459. {
  460. //STEP 1, FREE BODY IF STUCK
  461. const int max_results = 32;
  462. int recover_attempts = 4;
  463. Vector2 sr[max_results * 2];
  464. real_t priorities[max_results];
  465. do {
  466. GodotPhysicsServer2D::CollCbkData cbk;
  467. cbk.max = max_results;
  468. cbk.amount = 0;
  469. cbk.passed = 0;
  470. cbk.ptr = sr;
  471. cbk.invalid_by_dir = 0;
  472. excluded_shape_pair_count = 0; //last step is the one valid
  473. GodotPhysicsServer2D::CollCbkData *cbkptr = &cbk;
  474. GodotCollisionSolver2D::CallbackResult cbkres = GodotPhysicsServer2D::_shape_col_cbk;
  475. int priority_amount = 0;
  476. bool collided = false;
  477. int amount = _cull_aabb_for_body(p_body, body_aabb);
  478. for (int j = 0; j < p_body->get_shape_count(); j++) {
  479. if (p_body->is_shape_disabled(j)) {
  480. continue;
  481. }
  482. GodotShape2D *body_shape = p_body->get_shape(j);
  483. Transform2D body_shape_xform = body_transform * p_body->get_shape_transform(j);
  484. for (int i = 0; i < amount; i++) {
  485. const GodotCollisionObject2D *col_obj = intersection_query_results[i];
  486. if (p_parameters.exclude_bodies.has(col_obj->get_self())) {
  487. continue;
  488. }
  489. if (p_parameters.exclude_objects.has(col_obj->get_instance_id())) {
  490. continue;
  491. }
  492. int shape_idx = intersection_query_subindex_results[i];
  493. Transform2D col_obj_shape_xform = col_obj->get_transform() * col_obj->get_shape_transform(shape_idx);
  494. if (body_shape->allows_one_way_collision() && col_obj->is_shape_set_as_one_way_collision(shape_idx)) {
  495. cbk.valid_dir = col_obj_shape_xform.columns[1].normalized();
  496. real_t owc_margin = col_obj->get_shape_one_way_collision_margin(shape_idx);
  497. cbk.valid_depth = MAX(owc_margin, margin); //user specified, but never less than actual margin or it won't work
  498. cbk.invalid_by_dir = 0;
  499. if (col_obj->get_type() == GodotCollisionObject2D::TYPE_BODY) {
  500. const GodotBody2D *b = static_cast<const GodotBody2D *>(col_obj);
  501. if (b->get_mode() == PhysicsServer2D::BODY_MODE_KINEMATIC || b->get_mode() == PhysicsServer2D::BODY_MODE_RIGID) {
  502. //fix for moving platforms (kinematic and dynamic), margin is increased by how much it moved in the given direction
  503. Vector2 lv = b->get_linear_velocity();
  504. //compute displacement from linear velocity
  505. Vector2 motion = lv * last_step;
  506. real_t motion_len = motion.length();
  507. motion.normalize();
  508. cbk.valid_depth += motion_len * MAX(motion.dot(-cbk.valid_dir), 0.0);
  509. }
  510. }
  511. } else {
  512. cbk.valid_dir = Vector2();
  513. cbk.valid_depth = 0;
  514. cbk.invalid_by_dir = 0;
  515. }
  516. int current_passed = cbk.passed; //save how many points passed collision
  517. bool did_collide = false;
  518. GodotShape2D *against_shape = col_obj->get_shape(shape_idx);
  519. if (GodotCollisionSolver2D::solve(body_shape, body_shape_xform, Vector2(), against_shape, col_obj_shape_xform, Vector2(), cbkres, cbkptr, nullptr, margin)) {
  520. did_collide = cbk.passed > current_passed; //more passed, so collision actually existed
  521. }
  522. while (cbk.amount > priority_amount) {
  523. priorities[priority_amount] = col_obj->get_collision_priority();
  524. priority_amount++;
  525. }
  526. if (!did_collide && cbk.invalid_by_dir > 0) {
  527. //this shape must be excluded
  528. if (excluded_shape_pair_count < max_excluded_shape_pairs) {
  529. ExcludedShapeSW esp;
  530. esp.local_shape = body_shape;
  531. esp.against_object = col_obj;
  532. esp.against_shape_index = shape_idx;
  533. excluded_shape_pairs[excluded_shape_pair_count++] = esp;
  534. }
  535. }
  536. if (did_collide) {
  537. collided = true;
  538. }
  539. }
  540. }
  541. if (!collided) {
  542. break;
  543. }
  544. real_t inv_total_weight = 0.0;
  545. for (int i = 0; i < cbk.amount; i++) {
  546. inv_total_weight += priorities[i];
  547. }
  548. inv_total_weight = Math::is_zero_approx(inv_total_weight) ? 1.0 : (real_t)cbk.amount / inv_total_weight;
  549. recovered = true;
  550. Vector2 recover_motion;
  551. for (int i = 0; i < cbk.amount; i++) {
  552. Vector2 a = sr[i * 2 + 0];
  553. Vector2 b = sr[i * 2 + 1];
  554. // Compute plane on b towards a.
  555. Vector2 n = (a - b).normalized();
  556. real_t d = n.dot(b);
  557. // Compute depth on recovered motion.
  558. real_t depth = n.dot(a + recover_motion) - d;
  559. if (depth > min_contact_depth + CMP_EPSILON) {
  560. // Only recover if there is penetration.
  561. recover_motion -= n * (depth - min_contact_depth) * 0.4 * priorities[i] * inv_total_weight;
  562. }
  563. }
  564. if (recover_motion == Vector2()) {
  565. collided = false;
  566. break;
  567. }
  568. body_transform.columns[2] += recover_motion;
  569. body_aabb.position += recover_motion;
  570. recover_attempts--;
  571. } while (recover_attempts);
  572. }
  573. real_t safe = 1.0;
  574. real_t unsafe = 1.0;
  575. int best_shape = -1;
  576. {
  577. // STEP 2 ATTEMPT MOTION
  578. Rect2 motion_aabb = body_aabb;
  579. motion_aabb.position += p_parameters.motion;
  580. motion_aabb = motion_aabb.merge(body_aabb);
  581. int amount = _cull_aabb_for_body(p_body, motion_aabb);
  582. for (int body_shape_idx = 0; body_shape_idx < p_body->get_shape_count(); body_shape_idx++) {
  583. if (p_body->is_shape_disabled(body_shape_idx)) {
  584. continue;
  585. }
  586. GodotShape2D *body_shape = p_body->get_shape(body_shape_idx);
  587. // Colliding separation rays allows to properly snap to the ground,
  588. // otherwise it's not needed in regular motion.
  589. if (!p_parameters.collide_separation_ray && (body_shape->get_type() == PhysicsServer2D::SHAPE_SEPARATION_RAY)) {
  590. // When slide on slope is on, separation ray shape acts like a regular shape.
  591. if (!static_cast<GodotSeparationRayShape2D *>(body_shape)->get_slide_on_slope()) {
  592. continue;
  593. }
  594. }
  595. Transform2D body_shape_xform = body_transform * p_body->get_shape_transform(body_shape_idx);
  596. bool stuck = false;
  597. real_t best_safe = 1;
  598. real_t best_unsafe = 1;
  599. for (int i = 0; i < amount; i++) {
  600. const GodotCollisionObject2D *col_obj = intersection_query_results[i];
  601. if (p_parameters.exclude_bodies.has(col_obj->get_self())) {
  602. continue;
  603. }
  604. if (p_parameters.exclude_objects.has(col_obj->get_instance_id())) {
  605. continue;
  606. }
  607. int col_shape_idx = intersection_query_subindex_results[i];
  608. GodotShape2D *against_shape = col_obj->get_shape(col_shape_idx);
  609. bool excluded = false;
  610. for (int k = 0; k < excluded_shape_pair_count; k++) {
  611. if (excluded_shape_pairs[k].local_shape == body_shape && excluded_shape_pairs[k].against_object == col_obj && excluded_shape_pairs[k].against_shape_index == col_shape_idx) {
  612. excluded = true;
  613. break;
  614. }
  615. }
  616. if (excluded) {
  617. continue;
  618. }
  619. Transform2D col_obj_shape_xform = col_obj->get_transform() * col_obj->get_shape_transform(col_shape_idx);
  620. //test initial overlap, does it collide if going all the way?
  621. if (!GodotCollisionSolver2D::solve(body_shape, body_shape_xform, p_parameters.motion, against_shape, col_obj_shape_xform, Vector2(), nullptr, nullptr, nullptr, 0)) {
  622. continue;
  623. }
  624. //test initial overlap
  625. if (GodotCollisionSolver2D::solve(body_shape, body_shape_xform, Vector2(), against_shape, col_obj_shape_xform, Vector2(), nullptr, nullptr, nullptr, 0)) {
  626. if (body_shape->allows_one_way_collision() && col_obj->is_shape_set_as_one_way_collision(col_shape_idx)) {
  627. Vector2 direction = col_obj_shape_xform.columns[1].normalized();
  628. if (motion_normal.dot(direction) < 0) {
  629. continue;
  630. }
  631. }
  632. stuck = true;
  633. break;
  634. }
  635. //just do kinematic solving
  636. real_t low = 0.0;
  637. real_t hi = 1.0;
  638. real_t fraction_coeff = 0.5;
  639. for (int k = 0; k < 8; k++) { //steps should be customizable..
  640. real_t fraction = low + (hi - low) * fraction_coeff;
  641. Vector2 sep = motion_normal; //important optimization for this to work fast enough
  642. bool collided = GodotCollisionSolver2D::solve(body_shape, body_shape_xform, p_parameters.motion * fraction, against_shape, col_obj_shape_xform, Vector2(), nullptr, nullptr, &sep, 0);
  643. if (collided) {
  644. hi = fraction;
  645. if ((k == 0) || (low > 0.0)) { // Did it not collide before?
  646. // When alternating or first iteration, use dichotomy.
  647. fraction_coeff = 0.5;
  648. } else {
  649. // When colliding again, converge faster towards low fraction
  650. // for more accurate results with long motions that collide near the start.
  651. fraction_coeff = 0.25;
  652. }
  653. } else {
  654. low = fraction;
  655. if ((k == 0) || (hi < 1.0)) { // Did it collide before?
  656. // When alternating or first iteration, use dichotomy.
  657. fraction_coeff = 0.5;
  658. } else {
  659. // When not colliding again, converge faster towards high fraction
  660. // for more accurate results with long motions that collide near the end.
  661. fraction_coeff = 0.75;
  662. }
  663. }
  664. }
  665. if (body_shape->allows_one_way_collision() && col_obj->is_shape_set_as_one_way_collision(col_shape_idx)) {
  666. Vector2 cd[2];
  667. GodotPhysicsServer2D::CollCbkData cbk;
  668. cbk.max = 1;
  669. cbk.amount = 0;
  670. cbk.passed = 0;
  671. cbk.ptr = cd;
  672. cbk.valid_dir = col_obj_shape_xform.columns[1].normalized();
  673. cbk.valid_depth = 10e20;
  674. Vector2 sep = motion_normal; //important optimization for this to work fast enough
  675. bool collided = GodotCollisionSolver2D::solve(body_shape, body_shape_xform, p_parameters.motion * (hi + contact_max_allowed_penetration), col_obj->get_shape(col_shape_idx), col_obj_shape_xform, Vector2(), GodotPhysicsServer2D::_shape_col_cbk, &cbk, &sep, 0);
  676. if (!collided || cbk.amount == 0) {
  677. continue;
  678. }
  679. }
  680. if (low < best_safe) {
  681. best_safe = low;
  682. best_unsafe = hi;
  683. }
  684. }
  685. if (stuck) {
  686. safe = 0;
  687. unsafe = 0;
  688. best_shape = body_shape_idx; //sadly it's the best
  689. break;
  690. }
  691. if (best_safe == 1.0) {
  692. continue;
  693. }
  694. if (best_safe < safe) {
  695. safe = best_safe;
  696. unsafe = best_unsafe;
  697. best_shape = body_shape_idx;
  698. }
  699. }
  700. }
  701. bool collided = false;
  702. if ((p_parameters.recovery_as_collision && recovered) || (safe < 1)) {
  703. if (safe >= 1) {
  704. best_shape = -1; //no best shape with cast, reset to -1
  705. }
  706. //it collided, let's get the rest info in unsafe advance
  707. Transform2D ugt = body_transform;
  708. ugt.columns[2] += p_parameters.motion * unsafe;
  709. _RestCallbackData2D rcd;
  710. // Allowed depth can't be lower than motion length, in order to handle contacts at low speed.
  711. rcd.min_allowed_depth = MIN(motion_length, min_contact_depth);
  712. body_aabb.position += p_parameters.motion * unsafe;
  713. int amount = _cull_aabb_for_body(p_body, body_aabb);
  714. int from_shape = best_shape != -1 ? best_shape : 0;
  715. int to_shape = best_shape != -1 ? best_shape + 1 : p_body->get_shape_count();
  716. for (int j = from_shape; j < to_shape; j++) {
  717. if (p_body->is_shape_disabled(j)) {
  718. continue;
  719. }
  720. Transform2D body_shape_xform = ugt * p_body->get_shape_transform(j);
  721. GodotShape2D *body_shape = p_body->get_shape(j);
  722. for (int i = 0; i < amount; i++) {
  723. const GodotCollisionObject2D *col_obj = intersection_query_results[i];
  724. if (p_parameters.exclude_bodies.has(col_obj->get_self())) {
  725. continue;
  726. }
  727. if (p_parameters.exclude_objects.has(col_obj->get_instance_id())) {
  728. continue;
  729. }
  730. int shape_idx = intersection_query_subindex_results[i];
  731. GodotShape2D *against_shape = col_obj->get_shape(shape_idx);
  732. bool excluded = false;
  733. for (int k = 0; k < excluded_shape_pair_count; k++) {
  734. if (excluded_shape_pairs[k].local_shape == body_shape && excluded_shape_pairs[k].against_object == col_obj && excluded_shape_pairs[k].against_shape_index == shape_idx) {
  735. excluded = true;
  736. break;
  737. }
  738. }
  739. if (excluded) {
  740. continue;
  741. }
  742. Transform2D col_obj_shape_xform = col_obj->get_transform() * col_obj->get_shape_transform(shape_idx);
  743. if (body_shape->allows_one_way_collision() && col_obj->is_shape_set_as_one_way_collision(shape_idx)) {
  744. rcd.valid_dir = col_obj_shape_xform.columns[1].normalized();
  745. real_t owc_margin = col_obj->get_shape_one_way_collision_margin(shape_idx);
  746. rcd.valid_depth = MAX(owc_margin, margin); //user specified, but never less than actual margin or it won't work
  747. if (col_obj->get_type() == GodotCollisionObject2D::TYPE_BODY) {
  748. const GodotBody2D *b = static_cast<const GodotBody2D *>(col_obj);
  749. if (b->get_mode() == PhysicsServer2D::BODY_MODE_KINEMATIC || b->get_mode() == PhysicsServer2D::BODY_MODE_RIGID) {
  750. //fix for moving platforms (kinematic and dynamic), margin is increased by how much it moved in the given direction
  751. Vector2 lv = b->get_linear_velocity();
  752. //compute displacement from linear velocity
  753. Vector2 motion = lv * last_step;
  754. real_t motion_len = motion.length();
  755. motion.normalize();
  756. rcd.valid_depth += motion_len * MAX(motion.dot(-rcd.valid_dir), 0.0);
  757. }
  758. }
  759. } else {
  760. rcd.valid_dir = Vector2();
  761. rcd.valid_depth = 0;
  762. }
  763. rcd.object = col_obj;
  764. rcd.shape = shape_idx;
  765. rcd.local_shape = j;
  766. bool sc = GodotCollisionSolver2D::solve(body_shape, body_shape_xform, Vector2(), against_shape, col_obj_shape_xform, Vector2(), _rest_cbk_result, &rcd, nullptr, margin);
  767. if (!sc) {
  768. continue;
  769. }
  770. }
  771. }
  772. if (rcd.best_len != 0) {
  773. if (r_result) {
  774. r_result->collider = rcd.best_object->get_self();
  775. r_result->collider_id = rcd.best_object->get_instance_id();
  776. r_result->collider_shape = rcd.best_shape;
  777. r_result->collision_local_shape = rcd.best_local_shape;
  778. r_result->collision_normal = rcd.best_normal;
  779. r_result->collision_point = rcd.best_contact;
  780. r_result->collision_depth = rcd.best_len;
  781. r_result->collision_safe_fraction = safe;
  782. r_result->collision_unsafe_fraction = unsafe;
  783. const GodotBody2D *body = static_cast<const GodotBody2D *>(rcd.best_object);
  784. Vector2 rel_vec = r_result->collision_point - (body->get_transform().get_origin() + body->get_center_of_mass());
  785. r_result->collider_velocity = Vector2(-body->get_angular_velocity() * rel_vec.y, body->get_angular_velocity() * rel_vec.x) + body->get_linear_velocity();
  786. r_result->travel = safe * p_parameters.motion;
  787. r_result->remainder = p_parameters.motion - safe * p_parameters.motion;
  788. r_result->travel += (body_transform.get_origin() - p_parameters.from.get_origin());
  789. }
  790. collided = true;
  791. }
  792. }
  793. if (!collided && r_result) {
  794. r_result->travel = p_parameters.motion;
  795. r_result->remainder = Vector2();
  796. r_result->travel += (body_transform.get_origin() - p_parameters.from.get_origin());
  797. }
  798. return collided;
  799. }
  800. // Assumes a valid collision pair, this should have been checked beforehand in the BVH or octree.
  801. void *GodotSpace2D::_broadphase_pair(GodotCollisionObject2D *A, int p_subindex_A, GodotCollisionObject2D *B, int p_subindex_B, void *p_self) {
  802. GodotCollisionObject2D::Type type_A = A->get_type();
  803. GodotCollisionObject2D::Type type_B = B->get_type();
  804. if (type_A > type_B) {
  805. SWAP(A, B);
  806. SWAP(p_subindex_A, p_subindex_B);
  807. SWAP(type_A, type_B);
  808. }
  809. GodotSpace2D *self = static_cast<GodotSpace2D *>(p_self);
  810. self->collision_pairs++;
  811. if (type_A == GodotCollisionObject2D::TYPE_AREA) {
  812. GodotArea2D *area = static_cast<GodotArea2D *>(A);
  813. if (type_B == GodotCollisionObject2D::TYPE_AREA) {
  814. GodotArea2D *area_b = static_cast<GodotArea2D *>(B);
  815. GodotArea2Pair2D *area2_pair = memnew(GodotArea2Pair2D(area_b, p_subindex_B, area, p_subindex_A));
  816. return area2_pair;
  817. } else {
  818. GodotBody2D *body = static_cast<GodotBody2D *>(B);
  819. GodotAreaPair2D *area_pair = memnew(GodotAreaPair2D(body, p_subindex_B, area, p_subindex_A));
  820. return area_pair;
  821. }
  822. } else {
  823. GodotBodyPair2D *b = memnew(GodotBodyPair2D(static_cast<GodotBody2D *>(A), p_subindex_A, static_cast<GodotBody2D *>(B), p_subindex_B));
  824. return b;
  825. }
  826. }
  827. void GodotSpace2D::_broadphase_unpair(GodotCollisionObject2D *A, int p_subindex_A, GodotCollisionObject2D *B, int p_subindex_B, void *p_data, void *p_self) {
  828. if (!p_data) {
  829. return;
  830. }
  831. GodotSpace2D *self = static_cast<GodotSpace2D *>(p_self);
  832. self->collision_pairs--;
  833. GodotConstraint2D *c = static_cast<GodotConstraint2D *>(p_data);
  834. memdelete(c);
  835. }
  836. const SelfList<GodotBody2D>::List &GodotSpace2D::get_active_body_list() const {
  837. return active_list;
  838. }
  839. void GodotSpace2D::body_add_to_active_list(SelfList<GodotBody2D> *p_body) {
  840. active_list.add(p_body);
  841. }
  842. void GodotSpace2D::body_remove_from_active_list(SelfList<GodotBody2D> *p_body) {
  843. active_list.remove(p_body);
  844. }
  845. void GodotSpace2D::body_add_to_mass_properties_update_list(SelfList<GodotBody2D> *p_body) {
  846. mass_properties_update_list.add(p_body);
  847. }
  848. void GodotSpace2D::body_remove_from_mass_properties_update_list(SelfList<GodotBody2D> *p_body) {
  849. mass_properties_update_list.remove(p_body);
  850. }
  851. GodotBroadPhase2D *GodotSpace2D::get_broadphase() {
  852. return broadphase;
  853. }
  854. void GodotSpace2D::add_object(GodotCollisionObject2D *p_object) {
  855. ERR_FAIL_COND(objects.has(p_object));
  856. objects.insert(p_object);
  857. }
  858. void GodotSpace2D::remove_object(GodotCollisionObject2D *p_object) {
  859. ERR_FAIL_COND(!objects.has(p_object));
  860. objects.erase(p_object);
  861. }
  862. const HashSet<GodotCollisionObject2D *> &GodotSpace2D::get_objects() const {
  863. return objects;
  864. }
  865. void GodotSpace2D::body_add_to_state_query_list(SelfList<GodotBody2D> *p_body) {
  866. state_query_list.add(p_body);
  867. }
  868. void GodotSpace2D::body_remove_from_state_query_list(SelfList<GodotBody2D> *p_body) {
  869. state_query_list.remove(p_body);
  870. }
  871. void GodotSpace2D::area_add_to_monitor_query_list(SelfList<GodotArea2D> *p_area) {
  872. monitor_query_list.add(p_area);
  873. }
  874. void GodotSpace2D::area_remove_from_monitor_query_list(SelfList<GodotArea2D> *p_area) {
  875. monitor_query_list.remove(p_area);
  876. }
  877. void GodotSpace2D::area_add_to_moved_list(SelfList<GodotArea2D> *p_area) {
  878. area_moved_list.add(p_area);
  879. }
  880. void GodotSpace2D::area_remove_from_moved_list(SelfList<GodotArea2D> *p_area) {
  881. area_moved_list.remove(p_area);
  882. }
  883. const SelfList<GodotArea2D>::List &GodotSpace2D::get_moved_area_list() const {
  884. return area_moved_list;
  885. }
  886. void GodotSpace2D::call_queries() {
  887. while (state_query_list.first()) {
  888. GodotBody2D *b = state_query_list.first()->self();
  889. state_query_list.remove(state_query_list.first());
  890. b->call_queries();
  891. }
  892. while (monitor_query_list.first()) {
  893. GodotArea2D *a = monitor_query_list.first()->self();
  894. monitor_query_list.remove(monitor_query_list.first());
  895. a->call_queries();
  896. }
  897. }
  898. void GodotSpace2D::setup() {
  899. contact_debug_count = 0;
  900. while (mass_properties_update_list.first()) {
  901. mass_properties_update_list.first()->self()->update_mass_properties();
  902. mass_properties_update_list.remove(mass_properties_update_list.first());
  903. }
  904. }
  905. void GodotSpace2D::update() {
  906. broadphase->update();
  907. }
  908. void GodotSpace2D::set_param(PhysicsServer2D::SpaceParameter p_param, real_t p_value) {
  909. switch (p_param) {
  910. case PhysicsServer2D::SPACE_PARAM_CONTACT_RECYCLE_RADIUS:
  911. contact_recycle_radius = p_value;
  912. break;
  913. case PhysicsServer2D::SPACE_PARAM_CONTACT_MAX_SEPARATION:
  914. contact_max_separation = p_value;
  915. break;
  916. case PhysicsServer2D::SPACE_PARAM_CONTACT_MAX_ALLOWED_PENETRATION:
  917. contact_max_allowed_penetration = p_value;
  918. break;
  919. case PhysicsServer2D::SPACE_PARAM_CONTACT_DEFAULT_BIAS:
  920. contact_bias = p_value;
  921. break;
  922. case PhysicsServer2D::SPACE_PARAM_BODY_LINEAR_VELOCITY_SLEEP_THRESHOLD:
  923. body_linear_velocity_sleep_threshold = p_value;
  924. break;
  925. case PhysicsServer2D::SPACE_PARAM_BODY_ANGULAR_VELOCITY_SLEEP_THRESHOLD:
  926. body_angular_velocity_sleep_threshold = p_value;
  927. break;
  928. case PhysicsServer2D::SPACE_PARAM_BODY_TIME_TO_SLEEP:
  929. body_time_to_sleep = p_value;
  930. break;
  931. case PhysicsServer2D::SPACE_PARAM_CONSTRAINT_DEFAULT_BIAS:
  932. constraint_bias = p_value;
  933. break;
  934. case PhysicsServer2D::SPACE_PARAM_SOLVER_ITERATIONS:
  935. solver_iterations = p_value;
  936. break;
  937. }
  938. }
  939. real_t GodotSpace2D::get_param(PhysicsServer2D::SpaceParameter p_param) const {
  940. switch (p_param) {
  941. case PhysicsServer2D::SPACE_PARAM_CONTACT_RECYCLE_RADIUS:
  942. return contact_recycle_radius;
  943. case PhysicsServer2D::SPACE_PARAM_CONTACT_MAX_SEPARATION:
  944. return contact_max_separation;
  945. case PhysicsServer2D::SPACE_PARAM_CONTACT_MAX_ALLOWED_PENETRATION:
  946. return contact_max_allowed_penetration;
  947. case PhysicsServer2D::SPACE_PARAM_CONTACT_DEFAULT_BIAS:
  948. return contact_bias;
  949. case PhysicsServer2D::SPACE_PARAM_BODY_LINEAR_VELOCITY_SLEEP_THRESHOLD:
  950. return body_linear_velocity_sleep_threshold;
  951. case PhysicsServer2D::SPACE_PARAM_BODY_ANGULAR_VELOCITY_SLEEP_THRESHOLD:
  952. return body_angular_velocity_sleep_threshold;
  953. case PhysicsServer2D::SPACE_PARAM_BODY_TIME_TO_SLEEP:
  954. return body_time_to_sleep;
  955. case PhysicsServer2D::SPACE_PARAM_CONSTRAINT_DEFAULT_BIAS:
  956. return constraint_bias;
  957. case PhysicsServer2D::SPACE_PARAM_SOLVER_ITERATIONS:
  958. return solver_iterations;
  959. }
  960. return 0;
  961. }
  962. void GodotSpace2D::lock() {
  963. locked = true;
  964. }
  965. void GodotSpace2D::unlock() {
  966. locked = false;
  967. }
  968. bool GodotSpace2D::is_locked() const {
  969. return locked;
  970. }
  971. GodotPhysicsDirectSpaceState2D *GodotSpace2D::get_direct_state() {
  972. return direct_access;
  973. }
  974. GodotSpace2D::GodotSpace2D() {
  975. body_linear_velocity_sleep_threshold = GLOBAL_GET("physics/2d/sleep_threshold_linear");
  976. body_angular_velocity_sleep_threshold = GLOBAL_GET("physics/2d/sleep_threshold_angular");
  977. body_time_to_sleep = GLOBAL_GET("physics/2d/time_before_sleep");
  978. solver_iterations = GLOBAL_GET("physics/2d/solver/solver_iterations");
  979. contact_recycle_radius = GLOBAL_GET("physics/2d/solver/contact_recycle_radius");
  980. contact_max_separation = GLOBAL_GET("physics/2d/solver/contact_max_separation");
  981. contact_max_allowed_penetration = GLOBAL_GET("physics/2d/solver/contact_max_allowed_penetration");
  982. contact_bias = GLOBAL_GET("physics/2d/solver/default_contact_bias");
  983. constraint_bias = GLOBAL_GET("physics/2d/solver/default_constraint_bias");
  984. broadphase = GodotBroadPhase2D::create_func();
  985. broadphase->set_pair_callback(_broadphase_pair, this);
  986. broadphase->set_unpair_callback(_broadphase_unpair, this);
  987. direct_access = memnew(GodotPhysicsDirectSpaceState2D);
  988. direct_access->space = this;
  989. }
  990. GodotSpace2D::~GodotSpace2D() {
  991. memdelete(broadphase);
  992. memdelete(direct_access);
  993. }