space_3d_sw.cpp 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262
  1. /*************************************************************************/
  2. /* space_3d_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_3d_sw.h"
  31. #include "collision_solver_3d_sw.h"
  32. #include "core/config/project_settings.h"
  33. #include "physics_server_3d_sw.h"
  34. _FORCE_INLINE_ static bool _can_collide_with(CollisionObject3DSW *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() == CollisionObject3DSW::TYPE_AREA && !p_collide_with_areas) {
  39. return false;
  40. }
  41. if (p_object->get_type() == CollisionObject3DSW::TYPE_BODY && !p_collide_with_bodies) {
  42. return false;
  43. }
  44. return true;
  45. }
  46. int PhysicsDirectSpaceState3DSW::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, Space3DSW::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 CollisionObject3DSW *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.is_valid()) {
  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 PhysicsDirectSpaceState3DSW::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, Space3DSW::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 CollisionObject3DSW *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 CollisionObject3DSW *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 Shape3DSW *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.is_valid()) {
  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 PhysicsDirectSpaceState3DSW::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. Shape3DSW *shape = PhysicsServer3DSW::singletonsw->shape_owner.getornull(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, Space3DSW::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 CollisionObject3DSW *col_obj = space->intersection_query_results[i];
  163. int shape_idx = space->intersection_query_subindex_results[i];
  164. if (!CollisionSolver3DSW::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.is_valid()) {
  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 PhysicsDirectSpaceState3DSW::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. Shape3DSW *shape = PhysicsServer3DSW::singletonsw->shape_owner.getornull(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, Space3DSW::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. MotionShape3DSW 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 CollisionObject3DSW *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 (CollisionSolver3DSW::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 (!CollisionSolver3DSW::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 = !CollisionSolver3DSW::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() == CollisionObject3DSW::TYPE_BODY) {
  250. const Body3DSW *body = static_cast<const Body3DSW *>(col_obj);
  251. r_info->linear_velocity = body->get_linear_velocity() + (body->get_angular_velocity()).cross(body->get_transform().origin - closest_B);
  252. }
  253. }
  254. }
  255. p_closest_safe = best_safe;
  256. p_closest_unsafe = best_unsafe;
  257. return true;
  258. }
  259. bool PhysicsDirectSpaceState3DSW::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) {
  260. if (p_result_max <= 0) {
  261. return false;
  262. }
  263. Shape3DSW *shape = PhysicsServer3DSW::singletonsw->shape_owner.getornull(p_shape);
  264. ERR_FAIL_COND_V(!shape, 0);
  265. AABB aabb = p_shape_xform.xform(shape->get_aabb());
  266. aabb = aabb.grow(p_margin);
  267. int amount = space->broadphase->cull_aabb(aabb, space->intersection_query_results, Space3DSW::INTERSECTION_QUERY_MAX, space->intersection_query_subindex_results);
  268. bool collided = false;
  269. r_result_count = 0;
  270. PhysicsServer3DSW::CollCbkData cbk;
  271. cbk.max = p_result_max;
  272. cbk.amount = 0;
  273. cbk.ptr = r_results;
  274. CollisionSolver3DSW::CallbackResult cbkres = PhysicsServer3DSW::_shape_col_cbk;
  275. PhysicsServer3DSW::CollCbkData *cbkptr = &cbk;
  276. for (int i = 0; i < amount; i++) {
  277. if (!_can_collide_with(space->intersection_query_results[i], p_collision_mask, p_collide_with_bodies, p_collide_with_areas)) {
  278. continue;
  279. }
  280. const CollisionObject3DSW *col_obj = space->intersection_query_results[i];
  281. int shape_idx = space->intersection_query_subindex_results[i];
  282. if (p_exclude.has(col_obj->get_self())) {
  283. continue;
  284. }
  285. if (CollisionSolver3DSW::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)) {
  286. collided = true;
  287. }
  288. }
  289. r_result_count = cbk.amount;
  290. return collided;
  291. }
  292. struct _RestCallbackData {
  293. const CollisionObject3DSW *object;
  294. const CollisionObject3DSW *best_object;
  295. int local_shape;
  296. int best_local_shape;
  297. int shape;
  298. int best_shape;
  299. Vector3 best_contact;
  300. Vector3 best_normal;
  301. real_t best_len;
  302. real_t min_allowed_depth;
  303. };
  304. static void _rest_cbk_result(const Vector3 &p_point_A, const Vector3 &p_point_B, void *p_userdata) {
  305. _RestCallbackData *rd = (_RestCallbackData *)p_userdata;
  306. Vector3 contact_rel = p_point_B - p_point_A;
  307. real_t len = contact_rel.length();
  308. if (len < rd->min_allowed_depth) {
  309. return;
  310. }
  311. if (len <= rd->best_len) {
  312. return;
  313. }
  314. rd->best_len = len;
  315. rd->best_contact = p_point_B;
  316. rd->best_normal = contact_rel / len;
  317. rd->best_object = rd->object;
  318. rd->best_shape = rd->shape;
  319. rd->best_local_shape = rd->local_shape;
  320. }
  321. bool PhysicsDirectSpaceState3DSW::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) {
  322. Shape3DSW *shape = PhysicsServer3DSW::singletonsw->shape_owner.getornull(p_shape);
  323. ERR_FAIL_COND_V(!shape, 0);
  324. AABB aabb = p_shape_xform.xform(shape->get_aabb());
  325. aabb = aabb.grow(p_margin);
  326. int amount = space->broadphase->cull_aabb(aabb, space->intersection_query_results, Space3DSW::INTERSECTION_QUERY_MAX, space->intersection_query_subindex_results);
  327. _RestCallbackData rcd;
  328. rcd.best_len = 0;
  329. rcd.best_object = nullptr;
  330. rcd.best_shape = 0;
  331. rcd.min_allowed_depth = space->test_motion_min_contact_depth;
  332. for (int i = 0; i < amount; i++) {
  333. if (!_can_collide_with(space->intersection_query_results[i], p_collision_mask, p_collide_with_bodies, p_collide_with_areas)) {
  334. continue;
  335. }
  336. const CollisionObject3DSW *col_obj = space->intersection_query_results[i];
  337. int shape_idx = space->intersection_query_subindex_results[i];
  338. if (p_exclude.has(col_obj->get_self())) {
  339. continue;
  340. }
  341. rcd.object = col_obj;
  342. rcd.shape = shape_idx;
  343. bool sc = CollisionSolver3DSW::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);
  344. if (!sc) {
  345. continue;
  346. }
  347. }
  348. if (rcd.best_len == 0 || !rcd.best_object) {
  349. return false;
  350. }
  351. r_info->collider_id = rcd.best_object->get_instance_id();
  352. r_info->shape = rcd.best_shape;
  353. r_info->normal = rcd.best_normal;
  354. r_info->point = rcd.best_contact;
  355. r_info->rid = rcd.best_object->get_self();
  356. if (rcd.best_object->get_type() == CollisionObject3DSW::TYPE_BODY) {
  357. const Body3DSW *body = static_cast<const Body3DSW *>(rcd.best_object);
  358. r_info->linear_velocity = body->get_linear_velocity() +
  359. (body->get_angular_velocity()).cross(body->get_transform().origin - rcd.best_contact); // * mPos);
  360. } else {
  361. r_info->linear_velocity = Vector3();
  362. }
  363. return true;
  364. }
  365. Vector3 PhysicsDirectSpaceState3DSW::get_closest_point_to_object_volume(RID p_object, const Vector3 p_point) const {
  366. CollisionObject3DSW *obj = PhysicsServer3DSW::singletonsw->area_owner.getornull(p_object);
  367. if (!obj) {
  368. obj = PhysicsServer3DSW::singletonsw->body_owner.getornull(p_object);
  369. }
  370. ERR_FAIL_COND_V(!obj, Vector3());
  371. ERR_FAIL_COND_V(obj->get_space() != space, Vector3());
  372. real_t min_distance = 1e20;
  373. Vector3 min_point;
  374. bool shapes_found = false;
  375. for (int i = 0; i < obj->get_shape_count(); i++) {
  376. if (obj->is_shape_set_as_disabled(i)) {
  377. continue;
  378. }
  379. Transform shape_xform = obj->get_transform() * obj->get_shape_transform(i);
  380. Shape3DSW *shape = obj->get_shape(i);
  381. Vector3 point = shape->get_closest_point_to(shape_xform.affine_inverse().xform(p_point));
  382. point = shape_xform.xform(point);
  383. real_t dist = point.distance_to(p_point);
  384. if (dist < min_distance) {
  385. min_distance = dist;
  386. min_point = point;
  387. }
  388. shapes_found = true;
  389. }
  390. if (!shapes_found) {
  391. return obj->get_transform().origin; //no shapes found, use distance to origin.
  392. } else {
  393. return min_point;
  394. }
  395. }
  396. PhysicsDirectSpaceState3DSW::PhysicsDirectSpaceState3DSW() {
  397. space = nullptr;
  398. }
  399. ////////////////////////////////////////////////////////////////////////////////////////////////////////////
  400. int Space3DSW::_cull_aabb_for_body(Body3DSW *p_body, const AABB &p_aabb) {
  401. int amount = broadphase->cull_aabb(p_aabb, intersection_query_results, INTERSECTION_QUERY_MAX, intersection_query_subindex_results);
  402. for (int i = 0; i < amount; i++) {
  403. bool keep = true;
  404. if (intersection_query_results[i] == p_body) {
  405. keep = false;
  406. } else if (intersection_query_results[i]->get_type() == CollisionObject3DSW::TYPE_AREA) {
  407. keep = false;
  408. } else if ((static_cast<Body3DSW *>(intersection_query_results[i])->test_collision_mask(p_body)) == 0) {
  409. keep = false;
  410. } else if (static_cast<Body3DSW *>(intersection_query_results[i])->has_exception(p_body->get_self()) || p_body->has_exception(intersection_query_results[i]->get_self())) {
  411. keep = false;
  412. } else if (static_cast<Body3DSW *>(intersection_query_results[i])->is_shape_set_as_disabled(intersection_query_subindex_results[i])) {
  413. keep = false;
  414. }
  415. if (!keep) {
  416. if (i < amount - 1) {
  417. SWAP(intersection_query_results[i], intersection_query_results[amount - 1]);
  418. SWAP(intersection_query_subindex_results[i], intersection_query_subindex_results[amount - 1]);
  419. }
  420. amount--;
  421. i--;
  422. }
  423. }
  424. return amount;
  425. }
  426. int Space3DSW::test_body_ray_separation(Body3DSW *p_body, const Transform &p_transform, bool p_infinite_inertia, Vector3 &r_recover_motion, PhysicsServer3D::SeparationResult *r_results, int p_result_max, real_t p_margin) {
  427. AABB body_aabb;
  428. bool shapes_found = false;
  429. for (int i = 0; i < p_body->get_shape_count(); i++) {
  430. if (p_body->is_shape_set_as_disabled(i)) {
  431. continue;
  432. }
  433. if (!shapes_found) {
  434. body_aabb = p_body->get_shape_aabb(i);
  435. shapes_found = true;
  436. } else {
  437. body_aabb = body_aabb.merge(p_body->get_shape_aabb(i));
  438. }
  439. }
  440. if (!shapes_found) {
  441. return 0;
  442. }
  443. // Undo the currently transform the physics server is aware of and apply the provided one
  444. body_aabb = p_transform.xform(p_body->get_inv_transform().xform(body_aabb));
  445. body_aabb = body_aabb.grow(p_margin);
  446. Transform body_transform = p_transform;
  447. for (int i = 0; i < p_result_max; i++) {
  448. //reset results
  449. r_results[i].collision_depth = 0;
  450. }
  451. int rays_found = 0;
  452. {
  453. // raycast AND separate
  454. const int max_results = 32;
  455. int recover_attempts = 4;
  456. Vector3 sr[max_results * 2];
  457. PhysicsServer3DSW::CollCbkData cbk;
  458. cbk.max = max_results;
  459. PhysicsServer3DSW::CollCbkData *cbkptr = &cbk;
  460. CollisionSolver3DSW::CallbackResult cbkres = PhysicsServer3DSW::_shape_col_cbk;
  461. do {
  462. Vector3 recover_motion;
  463. bool collided = false;
  464. int amount = _cull_aabb_for_body(p_body, body_aabb);
  465. for (int j = 0; j < p_body->get_shape_count(); j++) {
  466. if (p_body->is_shape_set_as_disabled(j)) {
  467. continue;
  468. }
  469. Shape3DSW *body_shape = p_body->get_shape(j);
  470. if (body_shape->get_type() != PhysicsServer3D::SHAPE_RAY) {
  471. continue;
  472. }
  473. Transform body_shape_xform = body_transform * p_body->get_shape_transform(j);
  474. for (int i = 0; i < amount; i++) {
  475. const CollisionObject3DSW *col_obj = intersection_query_results[i];
  476. int shape_idx = intersection_query_subindex_results[i];
  477. cbk.amount = 0;
  478. cbk.ptr = sr;
  479. if (CollisionObject3DSW::TYPE_BODY == col_obj->get_type()) {
  480. const Body3DSW *b = static_cast<const Body3DSW *>(col_obj);
  481. if (p_infinite_inertia && PhysicsServer3D::BODY_MODE_STATIC != b->get_mode() && PhysicsServer3D::BODY_MODE_KINEMATIC != b->get_mode()) {
  482. continue;
  483. }
  484. }
  485. Shape3DSW *against_shape = col_obj->get_shape(shape_idx);
  486. if (CollisionSolver3DSW::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)) {
  487. if (cbk.amount > 0) {
  488. collided = true;
  489. }
  490. int ray_index = -1; //reuse shape
  491. for (int k = 0; k < rays_found; k++) {
  492. if (r_results[k].collision_local_shape == j) {
  493. ray_index = k;
  494. }
  495. }
  496. if (ray_index == -1 && rays_found < p_result_max) {
  497. ray_index = rays_found;
  498. rays_found++;
  499. }
  500. if (ray_index != -1) {
  501. PhysicsServer3D::SeparationResult &result = r_results[ray_index];
  502. for (int k = 0; k < cbk.amount; k++) {
  503. Vector3 a = sr[k * 2 + 0];
  504. Vector3 b = sr[k * 2 + 1];
  505. recover_motion += (b - a) / cbk.amount;
  506. real_t depth = a.distance_to(b);
  507. if (depth > result.collision_depth) {
  508. result.collision_depth = depth;
  509. result.collision_point = b;
  510. result.collision_normal = (b - a).normalized();
  511. result.collision_local_shape = j;
  512. result.collider = col_obj->get_self();
  513. result.collider_id = col_obj->get_instance_id();
  514. result.collider_shape = shape_idx;
  515. //result.collider_metadata = col_obj->get_shape_metadata(shape_idx);
  516. if (col_obj->get_type() == CollisionObject3DSW::TYPE_BODY) {
  517. Body3DSW *body = (Body3DSW *)col_obj;
  518. Vector3 rel_vec = b - body->get_transform().get_origin();
  519. //result.collider_velocity = Vector3(-body->get_angular_velocity() * rel_vec.y, body->get_angular_velocity() * rel_vec.x) + body->get_linear_velocity();
  520. result.collider_velocity = body->get_linear_velocity() + (body->get_angular_velocity()).cross(body->get_transform().origin - rel_vec); // * mPos);
  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 Space3DSW::test_body_motion(Body3DSW *p_body, const Transform &p_from, const Vector3 &p_motion, bool p_infinite_inertia, real_t p_margin, PhysicsServer3D::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 = ObjectID();
  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 = PhysicsServer3D::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. real_t 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. PhysicsServer3DSW::CollCbkData cbk;
  591. cbk.max = max_results;
  592. cbk.amount = 0;
  593. cbk.ptr = sr;
  594. PhysicsServer3DSW::CollCbkData *cbkptr = &cbk;
  595. CollisionSolver3DSW::CallbackResult cbkres = PhysicsServer3DSW::_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. Shape3DSW *body_shape = p_body->get_shape(j);
  604. if (p_exclude_raycast_shapes && body_shape->get_type() == PhysicsServer3D::SHAPE_RAY) {
  605. continue;
  606. }
  607. for (int i = 0; i < amount; i++) {
  608. const CollisionObject3DSW *col_obj = intersection_query_results[i];
  609. int shape_idx = intersection_query_subindex_results[i];
  610. if (CollisionSolver3DSW::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. real_t d = n.dot(b);
  625. // Compute depth on recovered motion.
  626. real_t 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. Shape3DSW *body_shape = p_body->get_shape(j);
  657. if (p_exclude_raycast_shapes && body_shape->get_type() == PhysicsServer3D::SHAPE_RAY) {
  658. continue;
  659. }
  660. Transform body_shape_xform_inv = body_shape_xform.affine_inverse();
  661. MotionShape3DSW 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 CollisionObject3DSW *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 (CollisionSolver3DSW::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 (!CollisionSolver3DSW::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 = !CollisionSolver3DSW::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. Shape3DSW *body_shape = p_body->get_shape(j);
  743. if (p_exclude_raycast_shapes && body_shape->get_type() == PhysicsServer3D::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 CollisionObject3DSW *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. bool sc = CollisionSolver3DSW::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);
  754. if (!sc) {
  755. continue;
  756. }
  757. }
  758. }
  759. if (rcd.best_len != 0) {
  760. if (r_result) {
  761. r_result->collider = rcd.best_object->get_self();
  762. r_result->collider_id = rcd.best_object->get_instance_id();
  763. r_result->collider_shape = rcd.best_shape;
  764. r_result->collision_local_shape = rcd.best_local_shape;
  765. r_result->collision_normal = rcd.best_normal;
  766. r_result->collision_point = rcd.best_contact;
  767. //r_result->collider_metadata = rcd.best_object->get_shape_metadata(rcd.best_shape);
  768. const Body3DSW *body = static_cast<const Body3DSW *>(rcd.best_object);
  769. //Vector3 rel_vec = r_result->collision_point - body->get_transform().get_origin();
  770. // r_result->collider_velocity = Vector3(-body->get_angular_velocity() * rel_vec.y, body->get_angular_velocity() * rel_vec.x) + body->get_linear_velocity();
  771. r_result->collider_velocity = body->get_linear_velocity() + (body->get_angular_velocity()).cross(body->get_transform().origin - rcd.best_contact); // * mPos);
  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 *Space3DSW::_broadphase_pair(CollisionObject3DSW *A, int p_subindex_A, CollisionObject3DSW *B, int p_subindex_B, void *p_self) {
  787. if (!A->test_collision_mask(B)) {
  788. return nullptr;
  789. }
  790. CollisionObject3DSW::Type type_A = A->get_type();
  791. CollisionObject3DSW::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. Space3DSW *self = (Space3DSW *)p_self;
  798. self->collision_pairs++;
  799. if (type_A == CollisionObject3DSW::TYPE_AREA) {
  800. Area3DSW *area = static_cast<Area3DSW *>(A);
  801. if (type_B == CollisionObject3DSW::TYPE_AREA) {
  802. Area3DSW *area_b = static_cast<Area3DSW *>(B);
  803. Area2Pair3DSW *area2_pair = memnew(Area2Pair3DSW(area_b, p_subindex_B, area, p_subindex_A));
  804. return area2_pair;
  805. } else {
  806. Body3DSW *body = static_cast<Body3DSW *>(B);
  807. AreaPair3DSW *area_pair = memnew(AreaPair3DSW(body, p_subindex_B, area, p_subindex_A));
  808. return area_pair;
  809. }
  810. } else {
  811. BodyPair3DSW *b = memnew(BodyPair3DSW((Body3DSW *)A, p_subindex_A, (Body3DSW *)B, p_subindex_B));
  812. return b;
  813. }
  814. return nullptr;
  815. }
  816. void Space3DSW::_broadphase_unpair(CollisionObject3DSW *A, int p_subindex_A, CollisionObject3DSW *B, int p_subindex_B, void *p_data, void *p_self) {
  817. if (!p_data) {
  818. return;
  819. }
  820. Space3DSW *self = (Space3DSW *)p_self;
  821. self->collision_pairs--;
  822. Constraint3DSW *c = (Constraint3DSW *)p_data;
  823. memdelete(c);
  824. }
  825. const SelfList<Body3DSW>::List &Space3DSW::get_active_body_list() const {
  826. return active_list;
  827. }
  828. void Space3DSW::body_add_to_active_list(SelfList<Body3DSW> *p_body) {
  829. active_list.add(p_body);
  830. }
  831. void Space3DSW::body_remove_from_active_list(SelfList<Body3DSW> *p_body) {
  832. active_list.remove(p_body);
  833. }
  834. void Space3DSW::body_add_to_inertia_update_list(SelfList<Body3DSW> *p_body) {
  835. inertia_update_list.add(p_body);
  836. }
  837. void Space3DSW::body_remove_from_inertia_update_list(SelfList<Body3DSW> *p_body) {
  838. inertia_update_list.remove(p_body);
  839. }
  840. BroadPhase3DSW *Space3DSW::get_broadphase() {
  841. return broadphase;
  842. }
  843. void Space3DSW::add_object(CollisionObject3DSW *p_object) {
  844. ERR_FAIL_COND(objects.has(p_object));
  845. objects.insert(p_object);
  846. }
  847. void Space3DSW::remove_object(CollisionObject3DSW *p_object) {
  848. ERR_FAIL_COND(!objects.has(p_object));
  849. objects.erase(p_object);
  850. }
  851. const Set<CollisionObject3DSW *> &Space3DSW::get_objects() const {
  852. return objects;
  853. }
  854. void Space3DSW::body_add_to_state_query_list(SelfList<Body3DSW> *p_body) {
  855. state_query_list.add(p_body);
  856. }
  857. void Space3DSW::body_remove_from_state_query_list(SelfList<Body3DSW> *p_body) {
  858. state_query_list.remove(p_body);
  859. }
  860. void Space3DSW::area_add_to_monitor_query_list(SelfList<Area3DSW> *p_area) {
  861. monitor_query_list.add(p_area);
  862. }
  863. void Space3DSW::area_remove_from_monitor_query_list(SelfList<Area3DSW> *p_area) {
  864. monitor_query_list.remove(p_area);
  865. }
  866. void Space3DSW::area_add_to_moved_list(SelfList<Area3DSW> *p_area) {
  867. area_moved_list.add(p_area);
  868. }
  869. void Space3DSW::area_remove_from_moved_list(SelfList<Area3DSW> *p_area) {
  870. area_moved_list.remove(p_area);
  871. }
  872. const SelfList<Area3DSW>::List &Space3DSW::get_moved_area_list() const {
  873. return area_moved_list;
  874. }
  875. void Space3DSW::call_queries() {
  876. while (state_query_list.first()) {
  877. Body3DSW *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. Area3DSW *a = monitor_query_list.first()->self();
  883. monitor_query_list.remove(monitor_query_list.first());
  884. a->call_queries();
  885. }
  886. }
  887. void Space3DSW::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 Space3DSW::update() {
  895. broadphase->update();
  896. }
  897. void Space3DSW::set_param(PhysicsServer3D::SpaceParameter p_param, real_t p_value) {
  898. switch (p_param) {
  899. case PhysicsServer3D::SPACE_PARAM_CONTACT_RECYCLE_RADIUS:
  900. contact_recycle_radius = p_value;
  901. break;
  902. case PhysicsServer3D::SPACE_PARAM_CONTACT_MAX_SEPARATION:
  903. contact_max_separation = p_value;
  904. break;
  905. case PhysicsServer3D::SPACE_PARAM_BODY_MAX_ALLOWED_PENETRATION:
  906. contact_max_allowed_penetration = p_value;
  907. break;
  908. case PhysicsServer3D::SPACE_PARAM_BODY_LINEAR_VELOCITY_SLEEP_THRESHOLD:
  909. body_linear_velocity_sleep_threshold = p_value;
  910. break;
  911. case PhysicsServer3D::SPACE_PARAM_BODY_ANGULAR_VELOCITY_SLEEP_THRESHOLD:
  912. body_angular_velocity_sleep_threshold = p_value;
  913. break;
  914. case PhysicsServer3D::SPACE_PARAM_BODY_TIME_TO_SLEEP:
  915. body_time_to_sleep = p_value;
  916. break;
  917. case PhysicsServer3D::SPACE_PARAM_BODY_ANGULAR_VELOCITY_DAMP_RATIO:
  918. body_angular_velocity_damp_ratio = p_value;
  919. break;
  920. case PhysicsServer3D::SPACE_PARAM_CONSTRAINT_DEFAULT_BIAS:
  921. constraint_bias = p_value;
  922. break;
  923. case PhysicsServer3D::SPACE_PARAM_TEST_MOTION_MIN_CONTACT_DEPTH:
  924. test_motion_min_contact_depth = p_value;
  925. break;
  926. }
  927. }
  928. real_t Space3DSW::get_param(PhysicsServer3D::SpaceParameter p_param) const {
  929. switch (p_param) {
  930. case PhysicsServer3D::SPACE_PARAM_CONTACT_RECYCLE_RADIUS:
  931. return contact_recycle_radius;
  932. case PhysicsServer3D::SPACE_PARAM_CONTACT_MAX_SEPARATION:
  933. return contact_max_separation;
  934. case PhysicsServer3D::SPACE_PARAM_BODY_MAX_ALLOWED_PENETRATION:
  935. return contact_max_allowed_penetration;
  936. case PhysicsServer3D::SPACE_PARAM_BODY_LINEAR_VELOCITY_SLEEP_THRESHOLD:
  937. return body_linear_velocity_sleep_threshold;
  938. case PhysicsServer3D::SPACE_PARAM_BODY_ANGULAR_VELOCITY_SLEEP_THRESHOLD:
  939. return body_angular_velocity_sleep_threshold;
  940. case PhysicsServer3D::SPACE_PARAM_BODY_TIME_TO_SLEEP:
  941. return body_time_to_sleep;
  942. case PhysicsServer3D::SPACE_PARAM_BODY_ANGULAR_VELOCITY_DAMP_RATIO:
  943. return body_angular_velocity_damp_ratio;
  944. case PhysicsServer3D::SPACE_PARAM_CONSTRAINT_DEFAULT_BIAS:
  945. return constraint_bias;
  946. case PhysicsServer3D::SPACE_PARAM_TEST_MOTION_MIN_CONTACT_DEPTH:
  947. return test_motion_min_contact_depth;
  948. }
  949. return 0;
  950. }
  951. void Space3DSW::lock() {
  952. locked = true;
  953. }
  954. void Space3DSW::unlock() {
  955. locked = false;
  956. }
  957. bool Space3DSW::is_locked() const {
  958. return locked;
  959. }
  960. PhysicsDirectSpaceState3DSW *Space3DSW::get_direct_state() {
  961. return direct_access;
  962. }
  963. Space3DSW::Space3DSW() {
  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", Math::deg2rad(8.0));
  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::FLOAT, "physics/3d/time_before_sleep", PROPERTY_HINT_RANGE, "0,5,0.01,or_greater"));
  978. body_angular_velocity_damp_ratio = 10;
  979. broadphase = BroadPhase3DSW::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(PhysicsDirectSpaceState3DSW);
  984. direct_access->space = this;
  985. for (int i = 0; i < ELAPSED_TIME_MAX; i++) {
  986. elapsed_time[i] = 0;
  987. }
  988. }
  989. Space3DSW::~Space3DSW() {
  990. memdelete(broadphase);
  991. memdelete(direct_access);
  992. }