space_sw.cpp 37 KB

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