godot_space_2d.cpp 40 KB

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