space_sw.cpp 42 KB

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