space_sw.cpp 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329
  1. /**************************************************************************/
  2. /* space_sw.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "space_sw.h"
  31. #include "collision_solver_sw.h"
  32. #include "core/project_settings.h"
  33. #include "physics_server_sw.h"
  34. #define TEST_MOTION_MARGIN_MIN_VALUE 0.0001
  35. #define TEST_MOTION_MIN_CONTACT_DEPTH_FACTOR 0.05
  36. _FORCE_INLINE_ static bool _can_collide_with(CollisionObjectSW *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() == CollisionObjectSW::TYPE_AREA && !p_collide_with_areas) {
  41. return false;
  42. }
  43. if (p_object->get_type() == CollisionObjectSW::TYPE_BODY && !p_collide_with_bodies) {
  44. return false;
  45. }
  46. return true;
  47. }
  48. int PhysicsDirectSpaceStateSW::intersect_point(const Vector3 &p_point, ShapeResult *r_results, int p_result_max, const Set<RID> &p_exclude, uint32_t p_collision_mask, bool p_collide_with_bodies, bool p_collide_with_areas) {
  49. ERR_FAIL_COND_V(space->locked, false);
  50. int amount = space->broadphase->cull_point(p_point, space->intersection_query_results, SpaceSW::INTERSECTION_QUERY_MAX, space->intersection_query_subindex_results);
  51. int cc = 0;
  52. //Transform ai = p_xform.affine_inverse();
  53. for (int i = 0; i < amount; i++) {
  54. if (cc >= p_result_max) {
  55. break;
  56. }
  57. if (!_can_collide_with(space->intersection_query_results[i], p_collision_mask, p_collide_with_bodies, p_collide_with_areas)) {
  58. continue;
  59. }
  60. //area can't be picked by ray (default)
  61. if (p_exclude.has(space->intersection_query_results[i]->get_self())) {
  62. continue;
  63. }
  64. const CollisionObjectSW *col_obj = space->intersection_query_results[i];
  65. int shape_idx = space->intersection_query_subindex_results[i];
  66. Transform inv_xform = col_obj->get_transform() * col_obj->get_shape_transform(shape_idx);
  67. inv_xform.affine_invert();
  68. if (!col_obj->get_shape(shape_idx)->intersect_point(inv_xform.xform(p_point))) {
  69. continue;
  70. }
  71. r_results[cc].collider_id = col_obj->get_instance_id();
  72. if (r_results[cc].collider_id != 0) {
  73. r_results[cc].collider = ObjectDB::get_instance(r_results[cc].collider_id);
  74. } else {
  75. r_results[cc].collider = nullptr;
  76. }
  77. r_results[cc].rid = col_obj->get_self();
  78. r_results[cc].shape = shape_idx;
  79. cc++;
  80. }
  81. return cc;
  82. }
  83. bool PhysicsDirectSpaceStateSW::intersect_ray(const Vector3 &p_from, const Vector3 &p_to, RayResult &r_result, const Set<RID> &p_exclude, uint32_t p_collision_mask, bool p_collide_with_bodies, bool p_collide_with_areas, bool p_pick_ray) {
  84. ERR_FAIL_COND_V(space->locked, false);
  85. Vector3 begin, end;
  86. Vector3 normal;
  87. begin = p_from;
  88. end = p_to;
  89. normal = (end - begin).normalized();
  90. int amount = space->broadphase->cull_segment(begin, end, space->intersection_query_results, SpaceSW::INTERSECTION_QUERY_MAX, space->intersection_query_subindex_results);
  91. //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
  92. bool collided = false;
  93. Vector3 res_point, res_normal;
  94. int res_shape = 0;
  95. const CollisionObjectSW *res_obj = nullptr;
  96. real_t min_d = 1e10;
  97. for (int i = 0; i < amount; i++) {
  98. if (!_can_collide_with(space->intersection_query_results[i], p_collision_mask, p_collide_with_bodies, p_collide_with_areas)) {
  99. continue;
  100. }
  101. if (p_pick_ray && !(space->intersection_query_results[i]->is_ray_pickable())) {
  102. continue;
  103. }
  104. if (p_exclude.has(space->intersection_query_results[i]->get_self())) {
  105. continue;
  106. }
  107. const CollisionObjectSW *col_obj = space->intersection_query_results[i];
  108. int shape_idx = space->intersection_query_subindex_results[i];
  109. Transform inv_xform = col_obj->get_shape_inv_transform(shape_idx) * col_obj->get_inv_transform();
  110. Vector3 local_from = inv_xform.xform(begin);
  111. Vector3 local_to = inv_xform.xform(end);
  112. const ShapeSW *shape = col_obj->get_shape(shape_idx);
  113. Vector3 shape_point, shape_normal;
  114. if (shape->intersect_segment(local_from, local_to, shape_point, shape_normal)) {
  115. Transform xform = col_obj->get_transform() * col_obj->get_shape_transform(shape_idx);
  116. shape_point = xform.xform(shape_point);
  117. real_t ld = normal.dot(shape_point);
  118. if (ld < min_d) {
  119. min_d = ld;
  120. res_point = shape_point;
  121. res_normal = inv_xform.basis.xform_inv(shape_normal).normalized();
  122. res_shape = shape_idx;
  123. res_obj = col_obj;
  124. collided = true;
  125. }
  126. }
  127. }
  128. if (!collided) {
  129. return false;
  130. }
  131. r_result.collider_id = res_obj->get_instance_id();
  132. if (r_result.collider_id != 0) {
  133. r_result.collider = ObjectDB::get_instance(r_result.collider_id);
  134. } else {
  135. r_result.collider = nullptr;
  136. }
  137. r_result.normal = res_normal;
  138. r_result.position = res_point;
  139. r_result.rid = res_obj->get_self();
  140. r_result.shape = res_shape;
  141. return true;
  142. }
  143. int PhysicsDirectSpaceStateSW::intersect_shape(const RID &p_shape, const Transform &p_xform, real_t p_margin, ShapeResult *r_results, int p_result_max, const Set<RID> &p_exclude, uint32_t p_collision_mask, bool p_collide_with_bodies, bool p_collide_with_areas) {
  144. if (p_result_max <= 0) {
  145. return 0;
  146. }
  147. ShapeSW *shape = static_cast<PhysicsServerSW *>(PhysicsServer::get_singleton())->shape_owner.get(p_shape);
  148. ERR_FAIL_COND_V(!shape, 0);
  149. AABB aabb = p_xform.xform(shape->get_aabb());
  150. int amount = space->broadphase->cull_aabb(aabb, space->intersection_query_results, SpaceSW::INTERSECTION_QUERY_MAX, space->intersection_query_subindex_results);
  151. int cc = 0;
  152. //Transform ai = p_xform.affine_inverse();
  153. for (int i = 0; i < amount; i++) {
  154. if (cc >= p_result_max) {
  155. break;
  156. }
  157. if (!_can_collide_with(space->intersection_query_results[i], p_collision_mask, p_collide_with_bodies, p_collide_with_areas)) {
  158. continue;
  159. }
  160. //area can't be picked by ray (default)
  161. if (p_exclude.has(space->intersection_query_results[i]->get_self())) {
  162. continue;
  163. }
  164. const CollisionObjectSW *col_obj = space->intersection_query_results[i];
  165. int shape_idx = space->intersection_query_subindex_results[i];
  166. if (!CollisionSolverSW::solve_static(shape, p_xform, col_obj->get_shape(shape_idx), col_obj->get_transform() * col_obj->get_shape_transform(shape_idx), nullptr, nullptr, nullptr, p_margin, 0)) {
  167. continue;
  168. }
  169. if (r_results) {
  170. r_results[cc].collider_id = col_obj->get_instance_id();
  171. if (r_results[cc].collider_id != 0) {
  172. r_results[cc].collider = ObjectDB::get_instance(r_results[cc].collider_id);
  173. } else {
  174. r_results[cc].collider = nullptr;
  175. }
  176. r_results[cc].rid = col_obj->get_self();
  177. r_results[cc].shape = shape_idx;
  178. }
  179. cc++;
  180. }
  181. return cc;
  182. }
  183. bool PhysicsDirectSpaceStateSW::cast_motion(const RID &p_shape, const Transform &p_xform, const Vector3 &p_motion, real_t p_margin, real_t &p_closest_safe, real_t &p_closest_unsafe, const Set<RID> &p_exclude, uint32_t p_collision_mask, bool p_collide_with_bodies, bool p_collide_with_areas, ShapeRestInfo *r_info) {
  184. ShapeSW *shape = static_cast<PhysicsServerSW *>(PhysicsServer::get_singleton())->shape_owner.get(p_shape);
  185. ERR_FAIL_COND_V(!shape, false);
  186. AABB aabb = p_xform.xform(shape->get_aabb());
  187. aabb = aabb.merge(AABB(aabb.position + p_motion, aabb.size)); //motion
  188. aabb = aabb.grow(p_margin);
  189. int amount = space->broadphase->cull_aabb(aabb, space->intersection_query_results, SpaceSW::INTERSECTION_QUERY_MAX, space->intersection_query_subindex_results);
  190. real_t best_safe = 1;
  191. real_t best_unsafe = 1;
  192. Transform xform_inv = p_xform.affine_inverse();
  193. MotionShapeSW mshape;
  194. mshape.shape = shape;
  195. mshape.motion = xform_inv.basis.xform(p_motion);
  196. bool best_first = true;
  197. Vector3 motion_normal = p_motion.normalized();
  198. Vector3 closest_A, closest_B;
  199. for (int i = 0; i < amount; i++) {
  200. if (!_can_collide_with(space->intersection_query_results[i], p_collision_mask, p_collide_with_bodies, p_collide_with_areas)) {
  201. continue;
  202. }
  203. if (p_exclude.has(space->intersection_query_results[i]->get_self())) {
  204. continue; //ignore excluded
  205. }
  206. const CollisionObjectSW *col_obj = space->intersection_query_results[i];
  207. int shape_idx = space->intersection_query_subindex_results[i];
  208. Vector3 point_A, point_B;
  209. Vector3 sep_axis = motion_normal;
  210. Transform col_obj_xform = col_obj->get_transform() * col_obj->get_shape_transform(shape_idx);
  211. //test initial overlap, does it collide if going all the way?
  212. if (CollisionSolverSW::solve_distance(&mshape, p_xform, col_obj->get_shape(shape_idx), col_obj_xform, point_A, point_B, aabb, &sep_axis)) {
  213. continue;
  214. }
  215. //test initial overlap, ignore objects it's inside of.
  216. sep_axis = motion_normal;
  217. if (!CollisionSolverSW::solve_distance(shape, p_xform, col_obj->get_shape(shape_idx), col_obj_xform, point_A, point_B, aabb, &sep_axis)) {
  218. continue;
  219. }
  220. //just do kinematic solving
  221. real_t low = 0.0;
  222. real_t hi = 1.0;
  223. real_t fraction_coeff = 0.5;
  224. for (int j = 0; j < 8; j++) { //steps should be customizable..
  225. real_t fraction = low + (hi - low) * fraction_coeff;
  226. mshape.motion = xform_inv.basis.xform(p_motion * fraction);
  227. Vector3 lA, lB;
  228. Vector3 sep = motion_normal; //important optimization for this to work fast enough
  229. bool collided = !CollisionSolverSW::solve_distance(&mshape, p_xform, col_obj->get_shape(shape_idx), col_obj_xform, lA, lB, aabb, &sep);
  230. if (collided) {
  231. hi = fraction;
  232. if ((j == 0) || (low > 0.0)) { // Did it not collide before?
  233. // When alternating or first iteration, use dichotomy.
  234. fraction_coeff = 0.5;
  235. } else {
  236. // When colliding again, converge faster towards low fraction
  237. // for more accurate results with long motions that collide near the start.
  238. fraction_coeff = 0.25;
  239. }
  240. } else {
  241. point_A = lA;
  242. point_B = lB;
  243. low = fraction;
  244. if ((j == 0) || (hi < 1.0)) { // Did it collide before?
  245. // When alternating or first iteration, use dichotomy.
  246. fraction_coeff = 0.5;
  247. } else {
  248. // When not colliding again, converge faster towards high fraction
  249. // for more accurate results with long motions that collide near the end.
  250. fraction_coeff = 0.75;
  251. }
  252. }
  253. }
  254. if (low < best_safe) {
  255. best_first = true; //force reset
  256. best_safe = low;
  257. best_unsafe = hi;
  258. }
  259. if (r_info && (best_first || (point_A.distance_squared_to(point_B) < closest_A.distance_squared_to(closest_B) && low <= best_safe))) {
  260. closest_A = point_A;
  261. closest_B = point_B;
  262. r_info->collider_id = col_obj->get_instance_id();
  263. r_info->rid = col_obj->get_self();
  264. r_info->shape = shape_idx;
  265. r_info->point = closest_B;
  266. r_info->normal = (closest_A - closest_B).normalized();
  267. best_first = false;
  268. if (col_obj->get_type() == CollisionObjectSW::TYPE_BODY) {
  269. const BodySW *body = static_cast<const BodySW *>(col_obj);
  270. Vector3 rel_vec = closest_B - (body->get_transform().origin + body->get_center_of_mass());
  271. r_info->linear_velocity = body->get_linear_velocity() + (body->get_angular_velocity()).cross(rel_vec);
  272. }
  273. }
  274. }
  275. p_closest_safe = best_safe;
  276. p_closest_unsafe = best_unsafe;
  277. return true;
  278. }
  279. bool PhysicsDirectSpaceStateSW::collide_shape(RID p_shape, const Transform &p_shape_xform, real_t p_margin, Vector3 *r_results, int p_result_max, int &r_result_count, const Set<RID> &p_exclude, uint32_t p_collision_mask, bool p_collide_with_bodies, bool p_collide_with_areas) {
  280. if (p_result_max <= 0) {
  281. return false;
  282. }
  283. ShapeSW *shape = static_cast<PhysicsServerSW *>(PhysicsServer::get_singleton())->shape_owner.get(p_shape);
  284. ERR_FAIL_COND_V(!shape, 0);
  285. AABB aabb = p_shape_xform.xform(shape->get_aabb());
  286. aabb = aabb.grow(p_margin);
  287. int amount = space->broadphase->cull_aabb(aabb, space->intersection_query_results, SpaceSW::INTERSECTION_QUERY_MAX, space->intersection_query_subindex_results);
  288. bool collided = false;
  289. r_result_count = 0;
  290. PhysicsServerSW::CollCbkData cbk;
  291. cbk.max = p_result_max;
  292. cbk.amount = 0;
  293. cbk.ptr = r_results;
  294. CollisionSolverSW::CallbackResult cbkres = PhysicsServerSW::_shape_col_cbk;
  295. PhysicsServerSW::CollCbkData *cbkptr = &cbk;
  296. for (int i = 0; i < amount; i++) {
  297. if (!_can_collide_with(space->intersection_query_results[i], p_collision_mask, p_collide_with_bodies, p_collide_with_areas)) {
  298. continue;
  299. }
  300. const CollisionObjectSW *col_obj = space->intersection_query_results[i];
  301. int shape_idx = space->intersection_query_subindex_results[i];
  302. if (p_exclude.has(col_obj->get_self())) {
  303. continue;
  304. }
  305. if (CollisionSolverSW::solve_static(shape, p_shape_xform, col_obj->get_shape(shape_idx), col_obj->get_transform() * col_obj->get_shape_transform(shape_idx), cbkres, cbkptr, nullptr, p_margin)) {
  306. collided = true;
  307. }
  308. }
  309. r_result_count = cbk.amount;
  310. return collided;
  311. }
  312. struct _RestCallbackData {
  313. const CollisionObjectSW *object;
  314. const CollisionObjectSW *best_object;
  315. int local_shape;
  316. int best_local_shape;
  317. int shape;
  318. int best_shape;
  319. Vector3 best_contact;
  320. Vector3 best_normal;
  321. real_t best_len;
  322. real_t min_allowed_depth;
  323. };
  324. static void _rest_cbk_result(const Vector3 &p_point_A, const Vector3 &p_point_B, void *p_userdata) {
  325. _RestCallbackData *rd = (_RestCallbackData *)p_userdata;
  326. Vector3 contact_rel = p_point_B - p_point_A;
  327. real_t len = contact_rel.length();
  328. if (len < rd->min_allowed_depth) {
  329. return;
  330. }
  331. if (len <= rd->best_len) {
  332. return;
  333. }
  334. rd->best_len = len;
  335. rd->best_contact = p_point_B;
  336. rd->best_normal = contact_rel / len;
  337. rd->best_object = rd->object;
  338. rd->best_shape = rd->shape;
  339. rd->best_local_shape = rd->local_shape;
  340. }
  341. bool PhysicsDirectSpaceStateSW::rest_info(RID p_shape, const Transform &p_shape_xform, real_t p_margin, ShapeRestInfo *r_info, const Set<RID> &p_exclude, uint32_t p_collision_mask, bool p_collide_with_bodies, bool p_collide_with_areas) {
  342. ShapeSW *shape = static_cast<PhysicsServerSW *>(PhysicsServer::get_singleton())->shape_owner.get(p_shape);
  343. ERR_FAIL_COND_V(!shape, 0);
  344. real_t margin = MAX(p_margin, TEST_MOTION_MARGIN_MIN_VALUE);
  345. real_t min_contact_depth = margin * TEST_MOTION_MIN_CONTACT_DEPTH_FACTOR;
  346. AABB aabb = p_shape_xform.xform(shape->get_aabb());
  347. aabb = aabb.grow(margin);
  348. int amount = space->broadphase->cull_aabb(aabb, space->intersection_query_results, SpaceSW::INTERSECTION_QUERY_MAX, space->intersection_query_subindex_results);
  349. _RestCallbackData rcd;
  350. rcd.best_len = 0;
  351. rcd.best_object = nullptr;
  352. rcd.best_shape = 0;
  353. rcd.min_allowed_depth = min_contact_depth;
  354. for (int i = 0; i < amount; i++) {
  355. if (!_can_collide_with(space->intersection_query_results[i], p_collision_mask, p_collide_with_bodies, p_collide_with_areas)) {
  356. continue;
  357. }
  358. const CollisionObjectSW *col_obj = space->intersection_query_results[i];
  359. int shape_idx = space->intersection_query_subindex_results[i];
  360. if (p_exclude.has(col_obj->get_self())) {
  361. continue;
  362. }
  363. rcd.object = col_obj;
  364. rcd.shape = shape_idx;
  365. bool sc = CollisionSolverSW::solve_static(shape, p_shape_xform, col_obj->get_shape(shape_idx), col_obj->get_transform() * col_obj->get_shape_transform(shape_idx), _rest_cbk_result, &rcd, nullptr, margin);
  366. if (!sc) {
  367. continue;
  368. }
  369. }
  370. if (rcd.best_len == 0 || !rcd.best_object) {
  371. return false;
  372. }
  373. r_info->collider_id = rcd.best_object->get_instance_id();
  374. r_info->shape = rcd.best_shape;
  375. r_info->normal = rcd.best_normal;
  376. r_info->point = rcd.best_contact;
  377. r_info->rid = rcd.best_object->get_self();
  378. if (rcd.best_object->get_type() == CollisionObjectSW::TYPE_BODY) {
  379. const BodySW *body = static_cast<const BodySW *>(rcd.best_object);
  380. Vector3 rel_vec = rcd.best_contact - (body->get_transform().origin + body->get_center_of_mass());
  381. r_info->linear_velocity = body->get_linear_velocity() + (body->get_angular_velocity()).cross(rel_vec);
  382. } else {
  383. r_info->linear_velocity = Vector3();
  384. }
  385. return true;
  386. }
  387. Vector3 PhysicsDirectSpaceStateSW::get_closest_point_to_object_volume(RID p_object, const Vector3 p_point) const {
  388. CollisionObjectSW *obj = PhysicsServerSW::singleton->area_owner.getornull(p_object);
  389. if (!obj) {
  390. obj = PhysicsServerSW::singleton->body_owner.getornull(p_object);
  391. }
  392. ERR_FAIL_COND_V(!obj, Vector3());
  393. ERR_FAIL_COND_V(obj->get_space() != space, Vector3());
  394. float min_distance = 1e20;
  395. Vector3 min_point;
  396. bool shapes_found = false;
  397. for (int i = 0; i < obj->get_shape_count(); i++) {
  398. if (obj->is_shape_disabled(i)) {
  399. continue;
  400. }
  401. Transform shape_xform = obj->get_transform() * obj->get_shape_transform(i);
  402. ShapeSW *shape = obj->get_shape(i);
  403. Vector3 point = shape->get_closest_point_to(shape_xform.affine_inverse().xform(p_point));
  404. point = shape_xform.xform(point);
  405. float dist = point.distance_to(p_point);
  406. if (dist < min_distance) {
  407. min_distance = dist;
  408. min_point = point;
  409. }
  410. shapes_found = true;
  411. }
  412. if (!shapes_found) {
  413. return obj->get_transform().origin; //no shapes found, use distance to origin.
  414. } else {
  415. return min_point;
  416. }
  417. }
  418. PhysicsDirectSpaceStateSW::PhysicsDirectSpaceStateSW() {
  419. space = nullptr;
  420. }
  421. ////////////////////////////////////////////////////////////////////////////////////////////////////////////
  422. int SpaceSW::_cull_aabb_for_body(BodySW *p_body, const AABB &p_aabb) {
  423. int amount = broadphase->cull_aabb(p_aabb, intersection_query_results, INTERSECTION_QUERY_MAX, intersection_query_subindex_results);
  424. for (int i = 0; i < amount; i++) {
  425. bool keep = true;
  426. if (intersection_query_results[i] == p_body) {
  427. keep = false;
  428. } else if (intersection_query_results[i]->get_type() == CollisionObjectSW::TYPE_AREA) {
  429. keep = false;
  430. } else if ((static_cast<BodySW *>(intersection_query_results[i])->test_collision_mask(p_body)) == 0) {
  431. keep = false;
  432. } else if (static_cast<BodySW *>(intersection_query_results[i])->has_exception(p_body->get_self()) || p_body->has_exception(intersection_query_results[i]->get_self())) {
  433. keep = false;
  434. }
  435. if (!keep) {
  436. if (i < amount - 1) {
  437. SWAP(intersection_query_results[i], intersection_query_results[amount - 1]);
  438. SWAP(intersection_query_subindex_results[i], intersection_query_subindex_results[amount - 1]);
  439. }
  440. amount--;
  441. i--;
  442. }
  443. }
  444. return amount;
  445. }
  446. int SpaceSW::test_body_ray_separation(BodySW *p_body, const Transform &p_transform, bool p_infinite_inertia, Vector3 &r_recover_motion, PhysicsServer::SeparationResult *r_results, int p_result_max, real_t p_margin) {
  447. AABB body_aabb;
  448. bool shapes_found = false;
  449. for (int i = 0; i < p_body->get_shape_count(); i++) {
  450. if (p_body->is_shape_disabled(i)) {
  451. continue;
  452. }
  453. if (!shapes_found) {
  454. body_aabb = p_body->get_shape_aabb(i);
  455. shapes_found = true;
  456. } else {
  457. body_aabb = body_aabb.merge(p_body->get_shape_aabb(i));
  458. }
  459. }
  460. if (!shapes_found) {
  461. return 0;
  462. }
  463. // Undo the currently transform the physics server is aware of and apply the provided one
  464. body_aabb = p_transform.xform(p_body->get_inv_transform().xform(body_aabb));
  465. body_aabb = body_aabb.grow(p_margin);
  466. Transform body_transform = p_transform;
  467. for (int i = 0; i < p_result_max; i++) {
  468. //reset results
  469. r_results[i].collision_depth = -1.0;
  470. }
  471. int rays_found = 0;
  472. {
  473. // raycast AND separate
  474. const int max_results = 32;
  475. int recover_attempts = 4;
  476. Vector3 sr[max_results * 2];
  477. PhysicsServerSW::CollCbkData cbk;
  478. cbk.max = max_results;
  479. PhysicsServerSW::CollCbkData *cbkptr = &cbk;
  480. CollisionSolverSW::CallbackResult cbkres = PhysicsServerSW::_shape_col_cbk;
  481. do {
  482. Vector3 recover_motion;
  483. bool collided = false;
  484. int amount = _cull_aabb_for_body(p_body, body_aabb);
  485. for (int j = 0; j < p_body->get_shape_count(); j++) {
  486. if (p_body->is_shape_disabled(j)) {
  487. continue;
  488. }
  489. ShapeSW *body_shape = p_body->get_shape(j);
  490. if (body_shape->get_type() != PhysicsServer::SHAPE_RAY) {
  491. continue;
  492. }
  493. Transform body_shape_xform = body_transform * p_body->get_shape_transform(j);
  494. for (int i = 0; i < amount; i++) {
  495. const CollisionObjectSW *col_obj = intersection_query_results[i];
  496. int shape_idx = intersection_query_subindex_results[i];
  497. cbk.amount = 0;
  498. cbk.ptr = sr;
  499. if (CollisionObjectSW::TYPE_BODY == col_obj->get_type()) {
  500. const BodySW *b = static_cast<const BodySW *>(col_obj);
  501. if (p_infinite_inertia && PhysicsServer::BODY_MODE_STATIC != b->get_mode() && PhysicsServer::BODY_MODE_KINEMATIC != b->get_mode()) {
  502. continue;
  503. }
  504. }
  505. ShapeSW *against_shape = col_obj->get_shape(shape_idx);
  506. if (CollisionSolverSW::solve_static(body_shape, body_shape_xform, against_shape, col_obj->get_transform() * col_obj->get_shape_transform(shape_idx), cbkres, cbkptr, nullptr, p_margin)) {
  507. if (cbk.amount > 0) {
  508. collided = true;
  509. }
  510. int ray_index = -1; //reuse shape
  511. for (int k = 0; k < rays_found; k++) {
  512. if (r_results[k].collision_local_shape == j) {
  513. ray_index = k;
  514. }
  515. }
  516. if (ray_index == -1 && rays_found < p_result_max) {
  517. ray_index = rays_found;
  518. rays_found++;
  519. }
  520. if (ray_index != -1) {
  521. PhysicsServer::SeparationResult &result = r_results[ray_index];
  522. for (int k = 0; k < cbk.amount; k++) {
  523. Vector3 a = sr[k * 2 + 0];
  524. Vector3 b = sr[k * 2 + 1];
  525. // Compute plane on b towards a.
  526. Vector3 n = (a - b).normalized();
  527. float d = n.dot(b);
  528. // Compute depth on recovered motion.
  529. float depth = n.dot(a + recover_motion) - d;
  530. // Apply recovery without margin.
  531. float separation_depth = depth - p_margin;
  532. if (separation_depth > 0.0) {
  533. // Only recover if there is penetration.
  534. recover_motion -= n * separation_depth;
  535. }
  536. if (depth > result.collision_depth) {
  537. result.collision_depth = depth;
  538. result.collision_point = b;
  539. result.collision_normal = -n;
  540. result.collision_local_shape = j;
  541. result.collider = col_obj->get_self();
  542. result.collider_id = col_obj->get_instance_id();
  543. result.collider_shape = shape_idx;
  544. if (col_obj->get_type() == CollisionObjectSW::TYPE_BODY) {
  545. BodySW *body = (BodySW *)col_obj;
  546. Vector3 rel_vec = b - (body->get_transform().origin + body->get_center_of_mass());
  547. result.collider_velocity = body->get_linear_velocity() + (body->get_angular_velocity()).cross(rel_vec);
  548. }
  549. }
  550. }
  551. }
  552. }
  553. }
  554. }
  555. if (!collided || recover_motion == Vector3()) {
  556. break;
  557. }
  558. body_transform.origin += recover_motion;
  559. body_aabb.position += recover_motion;
  560. recover_attempts--;
  561. } while (recover_attempts);
  562. }
  563. r_recover_motion = body_transform.origin - p_transform.origin;
  564. return rays_found;
  565. }
  566. bool SpaceSW::test_body_motion(BodySW *p_body, const Transform &p_from, const Vector3 &p_motion, bool p_infinite_inertia, real_t p_margin, PhysicsServer::MotionResult *r_result, bool p_exclude_raycast_shapes, const Set<RID> &p_exclude) {
  567. //give me back regular physics engine logic
  568. //this is madness
  569. //and most people using this function will think
  570. //what it does is simpler than using physics
  571. //this took about a week to get right..
  572. //but is it right? who knows at this point..
  573. if (r_result) {
  574. r_result->collider_id = 0;
  575. r_result->collider_shape = 0;
  576. }
  577. AABB body_aabb;
  578. bool shapes_found = false;
  579. for (int i = 0; i < p_body->get_shape_count(); i++) {
  580. if (p_body->is_shape_disabled(i)) {
  581. continue;
  582. }
  583. if (!shapes_found) {
  584. body_aabb = p_body->get_shape_aabb(i);
  585. shapes_found = true;
  586. } else {
  587. body_aabb = body_aabb.merge(p_body->get_shape_aabb(i));
  588. }
  589. }
  590. if (!shapes_found) {
  591. if (r_result) {
  592. *r_result = PhysicsServer::MotionResult();
  593. r_result->motion = p_motion;
  594. }
  595. return false;
  596. }
  597. real_t margin = MAX(p_margin, TEST_MOTION_MARGIN_MIN_VALUE);
  598. // Undo the currently transform the physics server is aware of and apply the provided one
  599. body_aabb = p_from.xform(p_body->get_inv_transform().xform(body_aabb));
  600. body_aabb = body_aabb.grow(margin);
  601. real_t min_contact_depth = margin * TEST_MOTION_MIN_CONTACT_DEPTH_FACTOR;
  602. float motion_length = p_motion.length();
  603. Vector3 motion_normal = p_motion / motion_length;
  604. Transform body_transform = p_from;
  605. bool recovered = false;
  606. {
  607. //STEP 1, FREE BODY IF STUCK
  608. const int max_results = 32;
  609. int recover_attempts = 4;
  610. Vector3 sr[max_results * 2];
  611. do {
  612. PhysicsServerSW::CollCbkData cbk;
  613. cbk.max = max_results;
  614. cbk.amount = 0;
  615. cbk.ptr = sr;
  616. PhysicsServerSW::CollCbkData *cbkptr = &cbk;
  617. CollisionSolverSW::CallbackResult cbkres = PhysicsServerSW::_shape_col_cbk;
  618. bool collided = false;
  619. int amount = _cull_aabb_for_body(p_body, body_aabb);
  620. for (int j = 0; j < p_body->get_shape_count(); j++) {
  621. if (p_body->is_shape_disabled(j)) {
  622. continue;
  623. }
  624. Transform body_shape_xform = body_transform * p_body->get_shape_transform(j);
  625. ShapeSW *body_shape = p_body->get_shape(j);
  626. if (p_exclude_raycast_shapes && body_shape->get_type() == PhysicsServer::SHAPE_RAY) {
  627. continue;
  628. }
  629. for (int i = 0; i < amount; i++) {
  630. const CollisionObjectSW *col_obj = intersection_query_results[i];
  631. if (p_exclude.has(col_obj->get_self())) {
  632. continue;
  633. }
  634. int shape_idx = intersection_query_subindex_results[i];
  635. if (CollisionObjectSW::TYPE_BODY == col_obj->get_type()) {
  636. const BodySW *b = static_cast<const BodySW *>(col_obj);
  637. if (p_infinite_inertia && PhysicsServer::BODY_MODE_STATIC != b->get_mode() && PhysicsServer::BODY_MODE_KINEMATIC != b->get_mode()) {
  638. continue;
  639. }
  640. }
  641. if (CollisionSolverSW::solve_static(body_shape, body_shape_xform, col_obj->get_shape(shape_idx), col_obj->get_transform() * col_obj->get_shape_transform(shape_idx), cbkres, cbkptr, nullptr, margin)) {
  642. collided = cbk.amount > 0;
  643. }
  644. }
  645. }
  646. if (!collided) {
  647. break;
  648. }
  649. recovered = true;
  650. Vector3 recover_motion;
  651. for (int i = 0; i < cbk.amount; i++) {
  652. Vector3 a = sr[i * 2 + 0];
  653. Vector3 b = sr[i * 2 + 1];
  654. // Compute plane on b towards a.
  655. Vector3 n = (a - b).normalized();
  656. float d = n.dot(b);
  657. // Compute depth on recovered motion.
  658. float depth = n.dot(a + recover_motion) - d;
  659. if (depth > min_contact_depth + CMP_EPSILON) {
  660. // Only recover if there is penetration.
  661. recover_motion -= n * (depth - min_contact_depth) * 0.4;
  662. }
  663. }
  664. if (recover_motion == Vector3()) {
  665. collided = false;
  666. break;
  667. }
  668. body_transform.origin += recover_motion;
  669. body_aabb.position += recover_motion;
  670. recover_attempts--;
  671. } while (recover_attempts);
  672. }
  673. real_t safe = 1.0;
  674. real_t unsafe = 1.0;
  675. int best_shape = -1;
  676. {
  677. // STEP 2 ATTEMPT MOTION
  678. AABB motion_aabb = body_aabb;
  679. motion_aabb.position += p_motion;
  680. motion_aabb = motion_aabb.merge(body_aabb);
  681. int amount = _cull_aabb_for_body(p_body, motion_aabb);
  682. for (int j = 0; j < p_body->get_shape_count(); j++) {
  683. if (p_body->is_shape_disabled(j)) {
  684. continue;
  685. }
  686. Transform body_shape_xform = body_transform * p_body->get_shape_transform(j);
  687. ShapeSW *body_shape = p_body->get_shape(j);
  688. if (p_exclude_raycast_shapes && body_shape->get_type() == PhysicsServer::SHAPE_RAY) {
  689. continue;
  690. }
  691. Transform body_shape_xform_inv = body_shape_xform.affine_inverse();
  692. MotionShapeSW mshape;
  693. mshape.shape = body_shape;
  694. mshape.motion = body_shape_xform_inv.basis.xform(p_motion);
  695. bool stuck = false;
  696. real_t best_safe = 1;
  697. real_t best_unsafe = 1;
  698. for (int i = 0; i < amount; i++) {
  699. const CollisionObjectSW *col_obj = intersection_query_results[i];
  700. if (p_exclude.has(col_obj->get_self())) {
  701. continue;
  702. }
  703. int shape_idx = intersection_query_subindex_results[i];
  704. if (CollisionObjectSW::TYPE_BODY == col_obj->get_type()) {
  705. const BodySW *b = static_cast<const BodySW *>(col_obj);
  706. if (p_infinite_inertia && PhysicsServer::BODY_MODE_STATIC != b->get_mode() && PhysicsServer::BODY_MODE_KINEMATIC != b->get_mode()) {
  707. continue;
  708. }
  709. }
  710. //test initial overlap, does it collide if going all the way?
  711. Vector3 point_A, point_B;
  712. Vector3 sep_axis = motion_normal;
  713. Transform col_obj_xform = col_obj->get_transform() * col_obj->get_shape_transform(shape_idx);
  714. //test initial overlap, does it collide if going all the way?
  715. if (CollisionSolverSW::solve_distance(&mshape, body_shape_xform, col_obj->get_shape(shape_idx), col_obj_xform, point_A, point_B, motion_aabb, &sep_axis)) {
  716. continue;
  717. }
  718. sep_axis = motion_normal;
  719. if (!CollisionSolverSW::solve_distance(body_shape, body_shape_xform, col_obj->get_shape(shape_idx), col_obj_xform, point_A, point_B, motion_aabb, &sep_axis)) {
  720. stuck = true;
  721. break;
  722. }
  723. //just do kinematic solving
  724. real_t low = 0.0;
  725. real_t hi = 1.0;
  726. real_t fraction_coeff = 0.5;
  727. for (int k = 0; k < 8; k++) { //steps should be customizable..
  728. real_t fraction = low + (hi - low) * fraction_coeff;
  729. mshape.motion = body_shape_xform_inv.basis.xform(p_motion * fraction);
  730. Vector3 lA, lB;
  731. Vector3 sep = motion_normal; //important optimization for this to work fast enough
  732. bool collided = !CollisionSolverSW::solve_distance(&mshape, body_shape_xform, col_obj->get_shape(shape_idx), col_obj_xform, lA, lB, motion_aabb, &sep);
  733. if (collided) {
  734. hi = fraction;
  735. if ((k == 0) || (low > 0.0)) { // Did it not collide before?
  736. // When alternating or first iteration, use dichotomy.
  737. fraction_coeff = 0.5;
  738. } else {
  739. // When colliding again, converge faster towards low fraction
  740. // for more accurate results with long motions that collide near the start.
  741. fraction_coeff = 0.25;
  742. }
  743. } else {
  744. point_A = lA;
  745. point_B = lB;
  746. low = fraction;
  747. if ((k == 0) || (hi < 1.0)) { // Did it collide before?
  748. // When alternating or first iteration, use dichotomy.
  749. fraction_coeff = 0.5;
  750. } else {
  751. // When not colliding again, converge faster towards high fraction
  752. // for more accurate results with long motions that collide near the end.
  753. fraction_coeff = 0.75;
  754. }
  755. }
  756. }
  757. if (low < best_safe) {
  758. best_safe = low;
  759. best_unsafe = hi;
  760. }
  761. }
  762. if (stuck) {
  763. safe = 0;
  764. unsafe = 0;
  765. best_shape = j; //sadly it's the best
  766. break;
  767. }
  768. if (best_safe == 1.0) {
  769. continue;
  770. }
  771. if (best_safe < safe) {
  772. safe = best_safe;
  773. unsafe = best_unsafe;
  774. best_shape = j;
  775. }
  776. }
  777. }
  778. bool collided = false;
  779. if (recovered || (safe < 1)) {
  780. if (safe >= 1) {
  781. best_shape = -1; //no best shape with cast, reset to -1
  782. }
  783. //it collided, let's get the rest info in unsafe advance
  784. Transform ugt = body_transform;
  785. ugt.origin += p_motion * unsafe;
  786. _RestCallbackData rcd;
  787. rcd.best_len = 0;
  788. rcd.best_object = nullptr;
  789. rcd.best_shape = 0;
  790. // Allowed depth can't be lower than motion length, in order to handle contacts at low speed.
  791. rcd.min_allowed_depth = MIN(motion_length, min_contact_depth);
  792. body_aabb.position += p_motion * unsafe;
  793. int amount = _cull_aabb_for_body(p_body, body_aabb);
  794. int from_shape = best_shape != -1 ? best_shape : 0;
  795. int to_shape = best_shape != -1 ? best_shape + 1 : p_body->get_shape_count();
  796. for (int j = from_shape; j < to_shape; j++) {
  797. if (p_body->is_shape_disabled(j)) {
  798. continue;
  799. }
  800. Transform body_shape_xform = ugt * p_body->get_shape_transform(j);
  801. ShapeSW *body_shape = p_body->get_shape(j);
  802. if (p_exclude_raycast_shapes && body_shape->get_type() == PhysicsServer::SHAPE_RAY) {
  803. continue;
  804. }
  805. for (int i = 0; i < amount; i++) {
  806. const CollisionObjectSW *col_obj = intersection_query_results[i];
  807. if (p_exclude.has(col_obj->get_self())) {
  808. continue;
  809. }
  810. int shape_idx = intersection_query_subindex_results[i];
  811. if (CollisionObjectSW::TYPE_BODY == col_obj->get_type()) {
  812. const BodySW *b = static_cast<const BodySW *>(col_obj);
  813. if (p_infinite_inertia && PhysicsServer::BODY_MODE_STATIC != b->get_mode() && PhysicsServer::BODY_MODE_KINEMATIC != b->get_mode()) {
  814. continue;
  815. }
  816. }
  817. rcd.object = col_obj;
  818. rcd.shape = shape_idx;
  819. rcd.local_shape = j;
  820. bool sc = CollisionSolverSW::solve_static(body_shape, body_shape_xform, col_obj->get_shape(shape_idx), col_obj->get_transform() * col_obj->get_shape_transform(shape_idx), _rest_cbk_result, &rcd, nullptr, margin);
  821. if (!sc) {
  822. continue;
  823. }
  824. }
  825. }
  826. if (rcd.best_len != 0) {
  827. if (r_result) {
  828. r_result->collider = rcd.best_object->get_self();
  829. r_result->collider_id = rcd.best_object->get_instance_id();
  830. r_result->collider_shape = rcd.best_shape;
  831. r_result->collision_local_shape = rcd.best_local_shape;
  832. r_result->collision_normal = rcd.best_normal;
  833. r_result->collision_point = rcd.best_contact;
  834. r_result->collision_depth = rcd.best_len;
  835. r_result->collision_safe_fraction = safe;
  836. r_result->collision_unsafe_fraction = unsafe;
  837. //r_result->collider_metadata = rcd.best_object->get_shape_metadata(rcd.best_shape);
  838. const BodySW *body = static_cast<const BodySW *>(rcd.best_object);
  839. Vector3 rel_vec = rcd.best_contact - (body->get_transform().origin + body->get_center_of_mass());
  840. r_result->collider_velocity = body->get_linear_velocity() + (body->get_angular_velocity()).cross(rel_vec);
  841. r_result->motion = safe * p_motion;
  842. r_result->remainder = p_motion - safe * p_motion;
  843. r_result->motion += (body_transform.get_origin() - p_from.get_origin());
  844. }
  845. collided = true;
  846. }
  847. }
  848. if (!collided && r_result) {
  849. r_result->motion = p_motion;
  850. r_result->remainder = Vector3();
  851. r_result->motion += (body_transform.get_origin() - p_from.get_origin());
  852. }
  853. return collided;
  854. }
  855. // Assumes a valid collision pair, this should have been checked beforehand in the BVH or octree.
  856. void *SpaceSW::_broadphase_pair(CollisionObjectSW *p_object_A, int p_subindex_A, CollisionObjectSW *p_object_B, int p_subindex_B, void *p_pair_data, void *p_self) {
  857. // An existing pair - nothing to do, pair is still valid.
  858. if (p_pair_data) {
  859. return p_pair_data;
  860. }
  861. // New pair
  862. CollisionObjectSW::Type type_A = p_object_A->get_type();
  863. CollisionObjectSW::Type type_B = p_object_B->get_type();
  864. if (type_A > type_B) {
  865. SWAP(p_object_A, p_object_B);
  866. SWAP(p_subindex_A, p_subindex_B);
  867. SWAP(type_A, type_B);
  868. }
  869. SpaceSW *self = (SpaceSW *)p_self;
  870. self->collision_pairs++;
  871. if (type_A == CollisionObjectSW::TYPE_AREA) {
  872. AreaSW *area_a = static_cast<AreaSW *>(p_object_A);
  873. if (type_B == CollisionObjectSW::TYPE_AREA) {
  874. AreaSW *area_b = static_cast<AreaSW *>(p_object_B);
  875. Area2PairSW *area2_pair = memnew(Area2PairSW(area_b, p_subindex_B, area_a, p_subindex_A));
  876. return area2_pair;
  877. } else {
  878. BodySW *body_b = static_cast<BodySW *>(p_object_B);
  879. AreaPairSW *area_pair = memnew(AreaPairSW(body_b, p_subindex_B, area_a, p_subindex_A));
  880. return area_pair;
  881. }
  882. } else {
  883. BodySW *body_a = static_cast<BodySW *>(p_object_A);
  884. BodySW *body_b = static_cast<BodySW *>(p_object_B);
  885. BodyPairSW *body_pair = memnew(BodyPairSW(body_a, p_subindex_A, body_b, p_subindex_B));
  886. return body_pair;
  887. }
  888. return nullptr;
  889. }
  890. void SpaceSW::_broadphase_unpair(CollisionObjectSW *p_object_A, int p_subindex_A, CollisionObjectSW *p_object_B, int p_subindex_B, void *p_pair_data, void *p_self) {
  891. if (!p_pair_data) {
  892. return;
  893. }
  894. SpaceSW *self = (SpaceSW *)p_self;
  895. self->collision_pairs--;
  896. ConstraintSW *c = (ConstraintSW *)p_pair_data;
  897. memdelete(c);
  898. }
  899. const SelfList<BodySW>::List &SpaceSW::get_active_body_list() const {
  900. return active_list;
  901. }
  902. void SpaceSW::body_add_to_active_list(SelfList<BodySW> *p_body) {
  903. active_list.add(p_body);
  904. }
  905. void SpaceSW::body_remove_from_active_list(SelfList<BodySW> *p_body) {
  906. active_list.remove(p_body);
  907. }
  908. void SpaceSW::body_add_to_inertia_update_list(SelfList<BodySW> *p_body) {
  909. inertia_update_list.add(p_body);
  910. }
  911. void SpaceSW::body_remove_from_inertia_update_list(SelfList<BodySW> *p_body) {
  912. inertia_update_list.remove(p_body);
  913. }
  914. BroadPhaseSW *SpaceSW::get_broadphase() {
  915. return broadphase;
  916. }
  917. void SpaceSW::add_object(CollisionObjectSW *p_object) {
  918. ERR_FAIL_COND(objects.has(p_object));
  919. objects.insert(p_object);
  920. }
  921. void SpaceSW::remove_object(CollisionObjectSW *p_object) {
  922. ERR_FAIL_COND(!objects.has(p_object));
  923. objects.erase(p_object);
  924. }
  925. const Set<CollisionObjectSW *> &SpaceSW::get_objects() const {
  926. return objects;
  927. }
  928. void SpaceSW::body_add_to_state_query_list(SelfList<BodySW> *p_body) {
  929. state_query_list.add(p_body);
  930. }
  931. void SpaceSW::body_remove_from_state_query_list(SelfList<BodySW> *p_body) {
  932. state_query_list.remove(p_body);
  933. }
  934. void SpaceSW::area_add_to_monitor_query_list(SelfList<AreaSW> *p_area) {
  935. monitor_query_list.add(p_area);
  936. }
  937. void SpaceSW::area_remove_from_monitor_query_list(SelfList<AreaSW> *p_area) {
  938. monitor_query_list.remove(p_area);
  939. }
  940. void SpaceSW::area_add_to_moved_list(SelfList<AreaSW> *p_area) {
  941. area_moved_list.add(p_area);
  942. }
  943. void SpaceSW::area_remove_from_moved_list(SelfList<AreaSW> *p_area) {
  944. area_moved_list.remove(p_area);
  945. }
  946. const SelfList<AreaSW>::List &SpaceSW::get_moved_area_list() const {
  947. return area_moved_list;
  948. }
  949. void SpaceSW::call_queries() {
  950. while (state_query_list.first()) {
  951. BodySW *b = state_query_list.first()->self();
  952. state_query_list.remove(state_query_list.first());
  953. b->call_queries();
  954. }
  955. while (monitor_query_list.first()) {
  956. AreaSW *a = monitor_query_list.first()->self();
  957. monitor_query_list.remove(monitor_query_list.first());
  958. a->call_queries();
  959. }
  960. }
  961. void SpaceSW::setup() {
  962. contact_debug_count = 0;
  963. while (inertia_update_list.first()) {
  964. inertia_update_list.first()->self()->update_inertias();
  965. inertia_update_list.remove(inertia_update_list.first());
  966. }
  967. }
  968. void SpaceSW::update() {
  969. broadphase->update();
  970. }
  971. void SpaceSW::set_param(PhysicsServer::SpaceParameter p_param, real_t p_value) {
  972. switch (p_param) {
  973. case PhysicsServer::SPACE_PARAM_CONTACT_RECYCLE_RADIUS:
  974. contact_recycle_radius = p_value;
  975. break;
  976. case PhysicsServer::SPACE_PARAM_CONTACT_MAX_SEPARATION:
  977. contact_max_separation = p_value;
  978. break;
  979. case PhysicsServer::SPACE_PARAM_BODY_MAX_ALLOWED_PENETRATION:
  980. contact_max_allowed_penetration = p_value;
  981. break;
  982. case PhysicsServer::SPACE_PARAM_BODY_LINEAR_VELOCITY_SLEEP_THRESHOLD:
  983. body_linear_velocity_sleep_threshold = p_value;
  984. break;
  985. case PhysicsServer::SPACE_PARAM_BODY_ANGULAR_VELOCITY_SLEEP_THRESHOLD:
  986. body_angular_velocity_sleep_threshold = p_value;
  987. break;
  988. case PhysicsServer::SPACE_PARAM_BODY_TIME_TO_SLEEP:
  989. body_time_to_sleep = p_value;
  990. break;
  991. case PhysicsServer::SPACE_PARAM_BODY_ANGULAR_VELOCITY_DAMP_RATIO:
  992. body_angular_velocity_damp_ratio = p_value;
  993. break;
  994. case PhysicsServer::SPACE_PARAM_CONSTRAINT_DEFAULT_BIAS:
  995. constraint_bias = p_value;
  996. break;
  997. }
  998. }
  999. real_t SpaceSW::get_param(PhysicsServer::SpaceParameter p_param) const {
  1000. switch (p_param) {
  1001. case PhysicsServer::SPACE_PARAM_CONTACT_RECYCLE_RADIUS:
  1002. return contact_recycle_radius;
  1003. case PhysicsServer::SPACE_PARAM_CONTACT_MAX_SEPARATION:
  1004. return contact_max_separation;
  1005. case PhysicsServer::SPACE_PARAM_BODY_MAX_ALLOWED_PENETRATION:
  1006. return contact_max_allowed_penetration;
  1007. case PhysicsServer::SPACE_PARAM_BODY_LINEAR_VELOCITY_SLEEP_THRESHOLD:
  1008. return body_linear_velocity_sleep_threshold;
  1009. case PhysicsServer::SPACE_PARAM_BODY_ANGULAR_VELOCITY_SLEEP_THRESHOLD:
  1010. return body_angular_velocity_sleep_threshold;
  1011. case PhysicsServer::SPACE_PARAM_BODY_TIME_TO_SLEEP:
  1012. return body_time_to_sleep;
  1013. case PhysicsServer::SPACE_PARAM_BODY_ANGULAR_VELOCITY_DAMP_RATIO:
  1014. return body_angular_velocity_damp_ratio;
  1015. case PhysicsServer::SPACE_PARAM_CONSTRAINT_DEFAULT_BIAS:
  1016. return constraint_bias;
  1017. }
  1018. return 0;
  1019. }
  1020. void SpaceSW::lock() {
  1021. locked = true;
  1022. }
  1023. void SpaceSW::unlock() {
  1024. locked = false;
  1025. }
  1026. bool SpaceSW::is_locked() const {
  1027. return locked;
  1028. }
  1029. PhysicsDirectSpaceStateSW *SpaceSW::get_direct_state() {
  1030. return direct_access;
  1031. }
  1032. SpaceSW::SpaceSW() {
  1033. collision_pairs = 0;
  1034. active_objects = 0;
  1035. island_count = 0;
  1036. contact_debug_count = 0;
  1037. locked = false;
  1038. contact_recycle_radius = 0.01;
  1039. contact_max_separation = 0.05;
  1040. contact_max_allowed_penetration = 0.01;
  1041. constraint_bias = 0.01;
  1042. body_linear_velocity_sleep_threshold = GLOBAL_DEF("physics/3d/sleep_threshold_linear", 0.1);
  1043. body_angular_velocity_sleep_threshold = GLOBAL_DEF("physics/3d/sleep_threshold_angular", (8.0 / 180.0 * Math_PI));
  1044. body_time_to_sleep = GLOBAL_DEF("physics/3d/time_before_sleep", 0.5);
  1045. ProjectSettings::get_singleton()->set_custom_property_info("physics/3d/time_before_sleep", PropertyInfo(Variant::REAL, "physics/3d/time_before_sleep", PROPERTY_HINT_RANGE, "0,5,0.01,or_greater"));
  1046. body_angular_velocity_damp_ratio = 10;
  1047. broadphase = BroadPhaseSW::create_func();
  1048. broadphase->set_pair_callback(_broadphase_pair, this);
  1049. broadphase->set_unpair_callback(_broadphase_unpair, this);
  1050. area = nullptr;
  1051. direct_access = memnew(PhysicsDirectSpaceStateSW);
  1052. direct_access->space = this;
  1053. for (int i = 0; i < ELAPSED_TIME_MAX; i++) {
  1054. elapsed_time[i] = 0;
  1055. }
  1056. }
  1057. SpaceSW::~SpaceSW() {
  1058. memdelete(broadphase);
  1059. memdelete(direct_access);
  1060. }