space_sw.cpp 39 KB

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