godot_space_2d.cpp 42 KB

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