godot_space_3d.cpp 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235
  1. /*************************************************************************/
  2. /* godot_space_3d.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 "godot_space_3d.h"
  31. #include "godot_collision_solver_3d.h"
  32. #include "godot_physics_server_3d.h"
  33. #include "core/config/project_settings.h"
  34. #define TEST_MOTION_MIN_CONTACT_DEPTH_FACTOR 0.05
  35. _FORCE_INLINE_ static bool _can_collide_with(GodotCollisionObject3D *p_object, uint32_t p_collision_mask, bool p_collide_with_bodies, bool p_collide_with_areas) {
  36. if (!(p_object->get_collision_layer() & p_collision_mask)) {
  37. return false;
  38. }
  39. if (p_object->get_type() == GodotCollisionObject3D::TYPE_AREA && !p_collide_with_areas) {
  40. return false;
  41. }
  42. if (p_object->get_type() == GodotCollisionObject3D::TYPE_BODY && !p_collide_with_bodies) {
  43. return false;
  44. }
  45. if (p_object->get_type() == GodotCollisionObject3D::TYPE_SOFT_BODY && !p_collide_with_bodies) {
  46. return false;
  47. }
  48. return true;
  49. }
  50. int GodotPhysicsDirectSpaceState3D::intersect_point(const PointParameters &p_parameters, ShapeResult *r_results, int p_result_max) {
  51. ERR_FAIL_COND_V(space->locked, false);
  52. int amount = space->broadphase->cull_point(p_parameters.position, space->intersection_query_results, GodotSpace3D::INTERSECTION_QUERY_MAX, space->intersection_query_subindex_results);
  53. int cc = 0;
  54. //Transform3D ai = p_xform.affine_inverse();
  55. for (int i = 0; i < amount; i++) {
  56. if (cc >= p_result_max) {
  57. break;
  58. }
  59. if (!_can_collide_with(space->intersection_query_results[i], p_parameters.collision_mask, p_parameters.collide_with_bodies, p_parameters.collide_with_areas)) {
  60. continue;
  61. }
  62. //area can't be picked by ray (default)
  63. if (p_parameters.exclude.has(space->intersection_query_results[i]->get_self())) {
  64. continue;
  65. }
  66. const GodotCollisionObject3D *col_obj = space->intersection_query_results[i];
  67. int shape_idx = space->intersection_query_subindex_results[i];
  68. Transform3D inv_xform = col_obj->get_transform() * col_obj->get_shape_transform(shape_idx);
  69. inv_xform.affine_invert();
  70. if (!col_obj->get_shape(shape_idx)->intersect_point(inv_xform.xform(p_parameters.position))) {
  71. continue;
  72. }
  73. r_results[cc].collider_id = col_obj->get_instance_id();
  74. if (r_results[cc].collider_id.is_valid()) {
  75. r_results[cc].collider = ObjectDB::get_instance(r_results[cc].collider_id);
  76. } else {
  77. r_results[cc].collider = nullptr;
  78. }
  79. r_results[cc].rid = col_obj->get_self();
  80. r_results[cc].shape = shape_idx;
  81. cc++;
  82. }
  83. return cc;
  84. }
  85. bool GodotPhysicsDirectSpaceState3D::intersect_ray(const RayParameters &p_parameters, RayResult &r_result) {
  86. ERR_FAIL_COND_V(space->locked, false);
  87. Vector3 begin, end;
  88. Vector3 normal;
  89. begin = p_parameters.from;
  90. end = p_parameters.to;
  91. normal = (end - begin).normalized();
  92. int amount = space->broadphase->cull_segment(begin, end, space->intersection_query_results, GodotSpace3D::INTERSECTION_QUERY_MAX, space->intersection_query_subindex_results);
  93. //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
  94. bool collided = false;
  95. Vector3 res_point, res_normal;
  96. int res_shape;
  97. const GodotCollisionObject3D *res_obj;
  98. real_t min_d = 1e10;
  99. for (int i = 0; i < amount; i++) {
  100. if (!_can_collide_with(space->intersection_query_results[i], p_parameters.collision_mask, p_parameters.collide_with_bodies, p_parameters.collide_with_areas)) {
  101. continue;
  102. }
  103. if (p_parameters.pick_ray && !(space->intersection_query_results[i]->is_ray_pickable())) {
  104. continue;
  105. }
  106. if (p_parameters.exclude.has(space->intersection_query_results[i]->get_self())) {
  107. continue;
  108. }
  109. const GodotCollisionObject3D *col_obj = space->intersection_query_results[i];
  110. int shape_idx = space->intersection_query_subindex_results[i];
  111. Transform3D inv_xform = col_obj->get_shape_inv_transform(shape_idx) * col_obj->get_inv_transform();
  112. Vector3 local_from = inv_xform.xform(begin);
  113. Vector3 local_to = inv_xform.xform(end);
  114. const GodotShape3D *shape = col_obj->get_shape(shape_idx);
  115. Vector3 shape_point, shape_normal;
  116. if (shape->intersect_segment(local_from, local_to, shape_point, shape_normal)) {
  117. Transform3D xform = col_obj->get_transform() * col_obj->get_shape_transform(shape_idx);
  118. shape_point = xform.xform(shape_point);
  119. real_t ld = normal.dot(shape_point);
  120. if (ld < min_d) {
  121. min_d = ld;
  122. res_point = shape_point;
  123. res_normal = inv_xform.basis.xform_inv(shape_normal).normalized();
  124. res_shape = shape_idx;
  125. res_obj = col_obj;
  126. collided = true;
  127. }
  128. }
  129. }
  130. if (!collided) {
  131. return false;
  132. }
  133. r_result.collider_id = res_obj->get_instance_id();
  134. if (r_result.collider_id.is_valid()) {
  135. r_result.collider = ObjectDB::get_instance(r_result.collider_id);
  136. } else {
  137. r_result.collider = nullptr;
  138. }
  139. r_result.normal = res_normal;
  140. r_result.position = res_point;
  141. r_result.rid = res_obj->get_self();
  142. r_result.shape = res_shape;
  143. return true;
  144. }
  145. int GodotPhysicsDirectSpaceState3D::intersect_shape(const ShapeParameters &p_parameters, ShapeResult *r_results, int p_result_max) {
  146. if (p_result_max <= 0) {
  147. return 0;
  148. }
  149. GodotShape3D *shape = GodotPhysicsServer3D::godot_singleton->shape_owner.get_or_null(p_parameters.shape_rid);
  150. ERR_FAIL_COND_V(!shape, 0);
  151. AABB aabb = p_parameters.transform.xform(shape->get_aabb());
  152. int amount = space->broadphase->cull_aabb(aabb, space->intersection_query_results, GodotSpace3D::INTERSECTION_QUERY_MAX, space->intersection_query_subindex_results);
  153. int cc = 0;
  154. //Transform3D ai = p_xform.affine_inverse();
  155. for (int i = 0; i < amount; i++) {
  156. if (cc >= p_result_max) {
  157. break;
  158. }
  159. if (!_can_collide_with(space->intersection_query_results[i], p_parameters.collision_mask, p_parameters.collide_with_bodies, p_parameters.collide_with_areas)) {
  160. continue;
  161. }
  162. //area can't be picked by ray (default)
  163. if (p_parameters.exclude.has(space->intersection_query_results[i]->get_self())) {
  164. continue;
  165. }
  166. const GodotCollisionObject3D *col_obj = space->intersection_query_results[i];
  167. int shape_idx = space->intersection_query_subindex_results[i];
  168. if (!GodotCollisionSolver3D::solve_static(shape, p_parameters.transform, col_obj->get_shape(shape_idx), col_obj->get_transform() * col_obj->get_shape_transform(shape_idx), nullptr, nullptr, nullptr, p_parameters.margin, 0)) {
  169. continue;
  170. }
  171. if (r_results) {
  172. r_results[cc].collider_id = col_obj->get_instance_id();
  173. if (r_results[cc].collider_id.is_valid()) {
  174. r_results[cc].collider = ObjectDB::get_instance(r_results[cc].collider_id);
  175. } else {
  176. r_results[cc].collider = nullptr;
  177. }
  178. r_results[cc].rid = col_obj->get_self();
  179. r_results[cc].shape = shape_idx;
  180. }
  181. cc++;
  182. }
  183. return cc;
  184. }
  185. bool GodotPhysicsDirectSpaceState3D::cast_motion(const ShapeParameters &p_parameters, real_t &p_closest_safe, real_t &p_closest_unsafe, ShapeRestInfo *r_info) {
  186. GodotShape3D *shape = GodotPhysicsServer3D::godot_singleton->shape_owner.get_or_null(p_parameters.shape_rid);
  187. ERR_FAIL_COND_V(!shape, false);
  188. AABB aabb = p_parameters.transform.xform(shape->get_aabb());
  189. aabb = aabb.merge(AABB(aabb.position + p_parameters.motion, aabb.size)); //motion
  190. aabb = aabb.grow(p_parameters.margin);
  191. int amount = space->broadphase->cull_aabb(aabb, space->intersection_query_results, GodotSpace3D::INTERSECTION_QUERY_MAX, space->intersection_query_subindex_results);
  192. real_t best_safe = 1;
  193. real_t best_unsafe = 1;
  194. Transform3D xform_inv = p_parameters.transform.affine_inverse();
  195. GodotMotionShape3D mshape;
  196. mshape.shape = shape;
  197. mshape.motion = xform_inv.basis.xform(p_parameters.motion);
  198. bool best_first = true;
  199. Vector3 motion_normal = p_parameters.motion.normalized();
  200. Vector3 closest_A, closest_B;
  201. for (int i = 0; i < amount; i++) {
  202. if (!_can_collide_with(space->intersection_query_results[i], p_parameters.collision_mask, p_parameters.collide_with_bodies, p_parameters.collide_with_areas)) {
  203. continue;
  204. }
  205. if (p_parameters.exclude.has(space->intersection_query_results[i]->get_self())) {
  206. continue; //ignore excluded
  207. }
  208. const GodotCollisionObject3D *col_obj = space->intersection_query_results[i];
  209. int shape_idx = space->intersection_query_subindex_results[i];
  210. Vector3 point_A, point_B;
  211. Vector3 sep_axis = motion_normal;
  212. Transform3D col_obj_xform = col_obj->get_transform() * col_obj->get_shape_transform(shape_idx);
  213. //test initial overlap, does it collide if going all the way?
  214. if (GodotCollisionSolver3D::solve_distance(&mshape, p_parameters.transform, col_obj->get_shape(shape_idx), col_obj_xform, point_A, point_B, aabb, &sep_axis)) {
  215. continue;
  216. }
  217. //test initial overlap, ignore objects it's inside of.
  218. sep_axis = motion_normal;
  219. if (!GodotCollisionSolver3D::solve_distance(shape, p_parameters.transform, col_obj->get_shape(shape_idx), col_obj_xform, point_A, point_B, aabb, &sep_axis)) {
  220. continue;
  221. }
  222. //just do kinematic solving
  223. real_t low = 0.0;
  224. real_t hi = 1.0;
  225. real_t fraction_coeff = 0.5;
  226. for (int j = 0; j < 8; j++) { //steps should be customizable..
  227. real_t fraction = low + (hi - low) * fraction_coeff;
  228. mshape.motion = xform_inv.basis.xform(p_parameters.motion * fraction);
  229. Vector3 lA, lB;
  230. Vector3 sep = motion_normal; //important optimization for this to work fast enough
  231. bool collided = !GodotCollisionSolver3D::solve_distance(&mshape, p_parameters.transform, col_obj->get_shape(shape_idx), col_obj_xform, lA, lB, aabb, &sep);
  232. if (collided) {
  233. hi = fraction;
  234. if ((j == 0) || (low > 0.0)) { // Did it not collide before?
  235. // When alternating or first iteration, use dichotomy.
  236. fraction_coeff = 0.5;
  237. } else {
  238. // When colliding again, converge faster towards low fraction
  239. // for more accurate results with long motions that collide near the start.
  240. fraction_coeff = 0.25;
  241. }
  242. } else {
  243. point_A = lA;
  244. point_B = lB;
  245. low = fraction;
  246. if ((j == 0) || (hi < 1.0)) { // Did it collide before?
  247. // When alternating or first iteration, use dichotomy.
  248. fraction_coeff = 0.5;
  249. } else {
  250. // When not colliding again, converge faster towards high fraction
  251. // for more accurate results with long motions that collide near the end.
  252. fraction_coeff = 0.75;
  253. }
  254. }
  255. }
  256. if (low < best_safe) {
  257. best_first = true; //force reset
  258. best_safe = low;
  259. best_unsafe = hi;
  260. }
  261. if (r_info && (best_first || (point_A.distance_squared_to(point_B) < closest_A.distance_squared_to(closest_B) && low <= best_safe))) {
  262. closest_A = point_A;
  263. closest_B = point_B;
  264. r_info->collider_id = col_obj->get_instance_id();
  265. r_info->rid = col_obj->get_self();
  266. r_info->shape = shape_idx;
  267. r_info->point = closest_B;
  268. r_info->normal = (closest_A - closest_B).normalized();
  269. best_first = false;
  270. if (col_obj->get_type() == GodotCollisionObject3D::TYPE_BODY) {
  271. const GodotBody3D *body = static_cast<const GodotBody3D *>(col_obj);
  272. Vector3 rel_vec = closest_B - (body->get_transform().origin + body->get_center_of_mass());
  273. r_info->linear_velocity = body->get_linear_velocity() + (body->get_angular_velocity()).cross(rel_vec);
  274. }
  275. }
  276. }
  277. p_closest_safe = best_safe;
  278. p_closest_unsafe = best_unsafe;
  279. return true;
  280. }
  281. bool GodotPhysicsDirectSpaceState3D::collide_shape(const ShapeParameters &p_parameters, Vector3 *r_results, int p_result_max, int &r_result_count) {
  282. if (p_result_max <= 0) {
  283. return false;
  284. }
  285. GodotShape3D *shape = GodotPhysicsServer3D::godot_singleton->shape_owner.get_or_null(p_parameters.shape_rid);
  286. ERR_FAIL_COND_V(!shape, 0);
  287. AABB aabb = p_parameters.transform.xform(shape->get_aabb());
  288. aabb = aabb.grow(p_parameters.margin);
  289. int amount = space->broadphase->cull_aabb(aabb, space->intersection_query_results, GodotSpace3D::INTERSECTION_QUERY_MAX, space->intersection_query_subindex_results);
  290. bool collided = false;
  291. r_result_count = 0;
  292. GodotPhysicsServer3D::CollCbkData cbk;
  293. cbk.max = p_result_max;
  294. cbk.amount = 0;
  295. cbk.ptr = r_results;
  296. GodotCollisionSolver3D::CallbackResult cbkres = GodotPhysicsServer3D::_shape_col_cbk;
  297. GodotPhysicsServer3D::CollCbkData *cbkptr = &cbk;
  298. for (int i = 0; i < amount; i++) {
  299. if (!_can_collide_with(space->intersection_query_results[i], p_parameters.collision_mask, p_parameters.collide_with_bodies, p_parameters.collide_with_areas)) {
  300. continue;
  301. }
  302. const GodotCollisionObject3D *col_obj = space->intersection_query_results[i];
  303. if (p_parameters.exclude.has(col_obj->get_self())) {
  304. continue;
  305. }
  306. int shape_idx = space->intersection_query_subindex_results[i];
  307. if (GodotCollisionSolver3D::solve_static(shape, p_parameters.transform, col_obj->get_shape(shape_idx), col_obj->get_transform() * col_obj->get_shape_transform(shape_idx), cbkres, cbkptr, nullptr, p_parameters.margin)) {
  308. collided = true;
  309. }
  310. }
  311. r_result_count = cbk.amount;
  312. return collided;
  313. }
  314. struct _RestResultData {
  315. const GodotCollisionObject3D *object = nullptr;
  316. int local_shape = 0;
  317. int shape = 0;
  318. Vector3 contact;
  319. Vector3 normal;
  320. real_t len = 0.0;
  321. };
  322. struct _RestCallbackData {
  323. const GodotCollisionObject3D *object = nullptr;
  324. int local_shape = 0;
  325. int shape = 0;
  326. real_t min_allowed_depth = 0.0;
  327. _RestResultData best_result;
  328. int max_results = 0;
  329. int result_count = 0;
  330. _RestResultData *other_results = nullptr;
  331. };
  332. static void _rest_cbk_result(const Vector3 &p_point_A, int p_index_A, const Vector3 &p_point_B, int p_index_B, void *p_userdata) {
  333. _RestCallbackData *rd = (_RestCallbackData *)p_userdata;
  334. Vector3 contact_rel = p_point_B - p_point_A;
  335. real_t len = contact_rel.length();
  336. if (len < rd->min_allowed_depth) {
  337. return;
  338. }
  339. bool is_best_result = (len > rd->best_result.len);
  340. if (rd->other_results && rd->result_count > 0) {
  341. // Consider as new result by default.
  342. int prev_result_count = rd->result_count++;
  343. int result_index = 0;
  344. real_t tested_len = is_best_result ? rd->best_result.len : len;
  345. for (; result_index < prev_result_count - 1; ++result_index) {
  346. if (tested_len > rd->other_results[result_index].len) {
  347. // Re-using a previous result.
  348. rd->result_count--;
  349. break;
  350. }
  351. }
  352. if (result_index < rd->max_results - 1) {
  353. _RestResultData &result = rd->other_results[result_index];
  354. if (is_best_result) {
  355. // Keep the previous best result as separate result.
  356. result = rd->best_result;
  357. } else {
  358. // Keep this result as separate result.
  359. result.len = len;
  360. result.contact = p_point_B;
  361. result.normal = contact_rel / len;
  362. result.object = rd->object;
  363. result.shape = rd->shape;
  364. result.local_shape = rd->local_shape;
  365. }
  366. } else {
  367. // Discarding this result.
  368. rd->result_count--;
  369. }
  370. } else if (is_best_result) {
  371. rd->result_count = 1;
  372. }
  373. if (!is_best_result) {
  374. return;
  375. }
  376. rd->best_result.len = len;
  377. rd->best_result.contact = p_point_B;
  378. rd->best_result.normal = contact_rel / len;
  379. rd->best_result.object = rd->object;
  380. rd->best_result.shape = rd->shape;
  381. rd->best_result.local_shape = rd->local_shape;
  382. }
  383. bool GodotPhysicsDirectSpaceState3D::rest_info(const ShapeParameters &p_parameters, ShapeRestInfo *r_info) {
  384. GodotShape3D *shape = GodotPhysicsServer3D::godot_singleton->shape_owner.get_or_null(p_parameters.shape_rid);
  385. ERR_FAIL_COND_V(!shape, 0);
  386. real_t min_contact_depth = p_parameters.margin * TEST_MOTION_MIN_CONTACT_DEPTH_FACTOR;
  387. AABB aabb = p_parameters.transform.xform(shape->get_aabb());
  388. aabb = aabb.grow(p_parameters.margin);
  389. int amount = space->broadphase->cull_aabb(aabb, space->intersection_query_results, GodotSpace3D::INTERSECTION_QUERY_MAX, space->intersection_query_subindex_results);
  390. _RestCallbackData rcd;
  391. rcd.min_allowed_depth = min_contact_depth;
  392. for (int i = 0; i < amount; i++) {
  393. if (!_can_collide_with(space->intersection_query_results[i], p_parameters.collision_mask, p_parameters.collide_with_bodies, p_parameters.collide_with_areas)) {
  394. continue;
  395. }
  396. const GodotCollisionObject3D *col_obj = space->intersection_query_results[i];
  397. if (p_parameters.exclude.has(col_obj->get_self())) {
  398. continue;
  399. }
  400. int shape_idx = space->intersection_query_subindex_results[i];
  401. rcd.object = col_obj;
  402. rcd.shape = shape_idx;
  403. bool sc = GodotCollisionSolver3D::solve_static(shape, p_parameters.transform, col_obj->get_shape(shape_idx), col_obj->get_transform() * col_obj->get_shape_transform(shape_idx), _rest_cbk_result, &rcd, nullptr, p_parameters.margin);
  404. if (!sc) {
  405. continue;
  406. }
  407. }
  408. if (rcd.best_result.len == 0 || !rcd.best_result.object) {
  409. return false;
  410. }
  411. r_info->collider_id = rcd.best_result.object->get_instance_id();
  412. r_info->shape = rcd.best_result.shape;
  413. r_info->normal = rcd.best_result.normal;
  414. r_info->point = rcd.best_result.contact;
  415. r_info->rid = rcd.best_result.object->get_self();
  416. if (rcd.best_result.object->get_type() == GodotCollisionObject3D::TYPE_BODY) {
  417. const GodotBody3D *body = static_cast<const GodotBody3D *>(rcd.best_result.object);
  418. Vector3 rel_vec = rcd.best_result.contact - (body->get_transform().origin + body->get_center_of_mass());
  419. r_info->linear_velocity = body->get_linear_velocity() + (body->get_angular_velocity()).cross(rel_vec);
  420. } else {
  421. r_info->linear_velocity = Vector3();
  422. }
  423. return true;
  424. }
  425. Vector3 GodotPhysicsDirectSpaceState3D::get_closest_point_to_object_volume(RID p_object, const Vector3 p_point) const {
  426. GodotCollisionObject3D *obj = GodotPhysicsServer3D::godot_singleton->area_owner.get_or_null(p_object);
  427. if (!obj) {
  428. obj = GodotPhysicsServer3D::godot_singleton->body_owner.get_or_null(p_object);
  429. }
  430. ERR_FAIL_COND_V(!obj, Vector3());
  431. ERR_FAIL_COND_V(obj->get_space() != space, Vector3());
  432. real_t min_distance = 1e20;
  433. Vector3 min_point;
  434. bool shapes_found = false;
  435. for (int i = 0; i < obj->get_shape_count(); i++) {
  436. if (obj->is_shape_disabled(i)) {
  437. continue;
  438. }
  439. Transform3D shape_xform = obj->get_transform() * obj->get_shape_transform(i);
  440. GodotShape3D *shape = obj->get_shape(i);
  441. Vector3 point = shape->get_closest_point_to(shape_xform.affine_inverse().xform(p_point));
  442. point = shape_xform.xform(point);
  443. real_t dist = point.distance_to(p_point);
  444. if (dist < min_distance) {
  445. min_distance = dist;
  446. min_point = point;
  447. }
  448. shapes_found = true;
  449. }
  450. if (!shapes_found) {
  451. return obj->get_transform().origin; //no shapes found, use distance to origin.
  452. } else {
  453. return min_point;
  454. }
  455. }
  456. GodotPhysicsDirectSpaceState3D::GodotPhysicsDirectSpaceState3D() {
  457. space = nullptr;
  458. }
  459. ////////////////////////////////////////////////////////////////////////////////////////////////////////////
  460. int GodotSpace3D::_cull_aabb_for_body(GodotBody3D *p_body, const AABB &p_aabb) {
  461. int amount = broadphase->cull_aabb(p_aabb, intersection_query_results, INTERSECTION_QUERY_MAX, intersection_query_subindex_results);
  462. for (int i = 0; i < amount; i++) {
  463. bool keep = true;
  464. if (intersection_query_results[i] == p_body) {
  465. keep = false;
  466. } else if (intersection_query_results[i]->get_type() == GodotCollisionObject3D::TYPE_AREA) {
  467. keep = false;
  468. } else if (intersection_query_results[i]->get_type() == GodotCollisionObject3D::TYPE_SOFT_BODY) {
  469. keep = false;
  470. } else if (!p_body->collides_with(static_cast<GodotBody3D *>(intersection_query_results[i]))) {
  471. keep = false;
  472. } else if (static_cast<GodotBody3D *>(intersection_query_results[i])->has_exception(p_body->get_self()) || p_body->has_exception(intersection_query_results[i]->get_self())) {
  473. keep = false;
  474. }
  475. if (!keep) {
  476. if (i < amount - 1) {
  477. SWAP(intersection_query_results[i], intersection_query_results[amount - 1]);
  478. SWAP(intersection_query_subindex_results[i], intersection_query_subindex_results[amount - 1]);
  479. }
  480. amount--;
  481. i--;
  482. }
  483. }
  484. return amount;
  485. }
  486. bool GodotSpace3D::test_body_motion(GodotBody3D *p_body, const PhysicsServer3D::MotionParameters &p_parameters, PhysicsServer3D::MotionResult *r_result) {
  487. //give me back regular physics engine logic
  488. //this is madness
  489. //and most people using this function will think
  490. //what it does is simpler than using physics
  491. //this took about a week to get right..
  492. //but is it right? who knows at this point..
  493. ERR_FAIL_INDEX_V(p_parameters.max_collisions, PhysicsServer3D::MotionResult::MAX_COLLISIONS, false);
  494. if (r_result) {
  495. *r_result = PhysicsServer3D::MotionResult();
  496. }
  497. AABB body_aabb;
  498. bool shapes_found = false;
  499. for (int i = 0; i < p_body->get_shape_count(); i++) {
  500. if (p_body->is_shape_disabled(i)) {
  501. continue;
  502. }
  503. if (!shapes_found) {
  504. body_aabb = p_body->get_shape_aabb(i);
  505. shapes_found = true;
  506. } else {
  507. body_aabb = body_aabb.merge(p_body->get_shape_aabb(i));
  508. }
  509. }
  510. if (!shapes_found) {
  511. if (r_result) {
  512. r_result->travel = p_parameters.motion;
  513. }
  514. return false;
  515. }
  516. // Undo the currently transform the physics server is aware of and apply the provided one
  517. body_aabb = p_parameters.from.xform(p_body->get_inv_transform().xform(body_aabb));
  518. body_aabb = body_aabb.grow(p_parameters.margin);
  519. real_t min_contact_depth = p_parameters.margin * TEST_MOTION_MIN_CONTACT_DEPTH_FACTOR;
  520. real_t motion_length = p_parameters.motion.length();
  521. Vector3 motion_normal = p_parameters.motion / motion_length;
  522. Transform3D body_transform = p_parameters.from;
  523. bool recovered = false;
  524. {
  525. //STEP 1, FREE BODY IF STUCK
  526. const int max_results = 32;
  527. int recover_attempts = 4;
  528. Vector3 sr[max_results * 2];
  529. do {
  530. GodotPhysicsServer3D::CollCbkData cbk;
  531. cbk.max = max_results;
  532. cbk.amount = 0;
  533. cbk.ptr = sr;
  534. GodotPhysicsServer3D::CollCbkData *cbkptr = &cbk;
  535. GodotCollisionSolver3D::CallbackResult cbkres = GodotPhysicsServer3D::_shape_col_cbk;
  536. bool collided = false;
  537. int amount = _cull_aabb_for_body(p_body, body_aabb);
  538. for (int j = 0; j < p_body->get_shape_count(); j++) {
  539. if (p_body->is_shape_disabled(j)) {
  540. continue;
  541. }
  542. Transform3D body_shape_xform = body_transform * p_body->get_shape_transform(j);
  543. GodotShape3D *body_shape = p_body->get_shape(j);
  544. for (int i = 0; i < amount; i++) {
  545. const GodotCollisionObject3D *col_obj = intersection_query_results[i];
  546. if (p_parameters.exclude_bodies.has(col_obj->get_self())) {
  547. continue;
  548. }
  549. if (p_parameters.exclude_objects.has(col_obj->get_instance_id())) {
  550. continue;
  551. }
  552. int shape_idx = intersection_query_subindex_results[i];
  553. if (GodotCollisionSolver3D::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_parameters.margin)) {
  554. collided = cbk.amount > 0;
  555. }
  556. }
  557. }
  558. if (!collided) {
  559. break;
  560. }
  561. recovered = true;
  562. Vector3 recover_motion;
  563. for (int i = 0; i < cbk.amount; i++) {
  564. Vector3 a = sr[i * 2 + 0];
  565. Vector3 b = sr[i * 2 + 1];
  566. // Compute plane on b towards a.
  567. Vector3 n = (a - b).normalized();
  568. real_t d = n.dot(b);
  569. // Compute depth on recovered motion.
  570. real_t depth = n.dot(a + recover_motion) - d;
  571. if (depth > min_contact_depth + CMP_EPSILON) {
  572. // Only recover if there is penetration.
  573. recover_motion -= n * (depth - min_contact_depth) * 0.4;
  574. }
  575. }
  576. if (recover_motion == Vector3()) {
  577. collided = false;
  578. break;
  579. }
  580. body_transform.origin += recover_motion;
  581. body_aabb.position += recover_motion;
  582. recover_attempts--;
  583. } while (recover_attempts);
  584. }
  585. real_t safe = 1.0;
  586. real_t unsafe = 1.0;
  587. int best_shape = -1;
  588. {
  589. // STEP 2 ATTEMPT MOTION
  590. AABB motion_aabb = body_aabb;
  591. motion_aabb.position += p_parameters.motion;
  592. motion_aabb = motion_aabb.merge(body_aabb);
  593. int amount = _cull_aabb_for_body(p_body, motion_aabb);
  594. for (int j = 0; j < p_body->get_shape_count(); j++) {
  595. if (p_body->is_shape_disabled(j)) {
  596. continue;
  597. }
  598. GodotShape3D *body_shape = p_body->get_shape(j);
  599. // Colliding separation rays allows to properly snap to the ground,
  600. // otherwise it's not needed in regular motion.
  601. if (!p_parameters.collide_separation_ray && (body_shape->get_type() == PhysicsServer3D::SHAPE_SEPARATION_RAY)) {
  602. // When slide on slope is on, separation ray shape acts like a regular shape.
  603. if (!static_cast<GodotSeparationRayShape3D *>(body_shape)->get_slide_on_slope()) {
  604. continue;
  605. }
  606. }
  607. Transform3D body_shape_xform = body_transform * p_body->get_shape_transform(j);
  608. Transform3D body_shape_xform_inv = body_shape_xform.affine_inverse();
  609. GodotMotionShape3D mshape;
  610. mshape.shape = body_shape;
  611. mshape.motion = body_shape_xform_inv.basis.xform(p_parameters.motion);
  612. bool stuck = false;
  613. real_t best_safe = 1;
  614. real_t best_unsafe = 1;
  615. for (int i = 0; i < amount; i++) {
  616. const GodotCollisionObject3D *col_obj = intersection_query_results[i];
  617. if (p_parameters.exclude_bodies.has(col_obj->get_self())) {
  618. continue;
  619. }
  620. if (p_parameters.exclude_objects.has(col_obj->get_instance_id())) {
  621. continue;
  622. }
  623. int shape_idx = intersection_query_subindex_results[i];
  624. //test initial overlap, does it collide if going all the way?
  625. Vector3 point_A, point_B;
  626. Vector3 sep_axis = motion_normal;
  627. Transform3D col_obj_xform = col_obj->get_transform() * col_obj->get_shape_transform(shape_idx);
  628. //test initial overlap, does it collide if going all the way?
  629. if (GodotCollisionSolver3D::solve_distance(&mshape, body_shape_xform, col_obj->get_shape(shape_idx), col_obj_xform, point_A, point_B, motion_aabb, &sep_axis)) {
  630. continue;
  631. }
  632. sep_axis = motion_normal;
  633. if (!GodotCollisionSolver3D::solve_distance(body_shape, body_shape_xform, col_obj->get_shape(shape_idx), col_obj_xform, point_A, point_B, motion_aabb, &sep_axis)) {
  634. stuck = true;
  635. break;
  636. }
  637. //just do kinematic solving
  638. real_t low = 0.0;
  639. real_t hi = 1.0;
  640. real_t fraction_coeff = 0.5;
  641. for (int k = 0; k < 8; k++) { //steps should be customizable..
  642. real_t fraction = low + (hi - low) * fraction_coeff;
  643. mshape.motion = body_shape_xform_inv.basis.xform(p_parameters.motion * fraction);
  644. Vector3 lA, lB;
  645. Vector3 sep = motion_normal; //important optimization for this to work fast enough
  646. bool collided = !GodotCollisionSolver3D::solve_distance(&mshape, body_shape_xform, col_obj->get_shape(shape_idx), col_obj_xform, lA, lB, motion_aabb, &sep);
  647. if (collided) {
  648. hi = fraction;
  649. if ((k == 0) || (low > 0.0)) { // Did it not collide before?
  650. // When alternating or first iteration, use dichotomy.
  651. fraction_coeff = 0.5;
  652. } else {
  653. // When colliding again, converge faster towards low fraction
  654. // for more accurate results with long motions that collide near the start.
  655. fraction_coeff = 0.25;
  656. }
  657. } else {
  658. point_A = lA;
  659. point_B = lB;
  660. low = fraction;
  661. if ((k == 0) || (hi < 1.0)) { // Did it collide before?
  662. // When alternating or first iteration, use dichotomy.
  663. fraction_coeff = 0.5;
  664. } else {
  665. // When not colliding again, converge faster towards high fraction
  666. // for more accurate results with long motions that collide near the end.
  667. fraction_coeff = 0.75;
  668. }
  669. }
  670. }
  671. if (low < best_safe) {
  672. best_safe = low;
  673. best_unsafe = hi;
  674. }
  675. }
  676. if (stuck) {
  677. safe = 0;
  678. unsafe = 0;
  679. best_shape = j; //sadly it's the best
  680. break;
  681. }
  682. if (best_safe == 1.0) {
  683. continue;
  684. }
  685. if (best_safe < safe) {
  686. safe = best_safe;
  687. unsafe = best_unsafe;
  688. best_shape = j;
  689. }
  690. }
  691. }
  692. bool collided = false;
  693. if (recovered || (safe < 1)) {
  694. if (safe >= 1) {
  695. best_shape = -1; //no best shape with cast, reset to -1
  696. }
  697. //it collided, let's get the rest info in unsafe advance
  698. Transform3D ugt = body_transform;
  699. ugt.origin += p_parameters.motion * unsafe;
  700. _RestResultData results[PhysicsServer3D::MotionResult::MAX_COLLISIONS];
  701. _RestCallbackData rcd;
  702. if (p_parameters.max_collisions > 1) {
  703. rcd.max_results = p_parameters.max_collisions;
  704. rcd.other_results = results;
  705. }
  706. // Allowed depth can't be lower than motion length, in order to handle contacts at low speed.
  707. rcd.min_allowed_depth = MIN(motion_length, min_contact_depth);
  708. int from_shape = best_shape != -1 ? best_shape : 0;
  709. int to_shape = best_shape != -1 ? best_shape + 1 : p_body->get_shape_count();
  710. for (int j = from_shape; j < to_shape; j++) {
  711. if (p_body->is_shape_disabled(j)) {
  712. continue;
  713. }
  714. Transform3D body_shape_xform = ugt * p_body->get_shape_transform(j);
  715. GodotShape3D *body_shape = p_body->get_shape(j);
  716. body_aabb.position += p_parameters.motion * unsafe;
  717. int amount = _cull_aabb_for_body(p_body, body_aabb);
  718. for (int i = 0; i < amount; i++) {
  719. const GodotCollisionObject3D *col_obj = intersection_query_results[i];
  720. if (p_parameters.exclude_bodies.has(col_obj->get_self())) {
  721. continue;
  722. }
  723. if (p_parameters.exclude_objects.has(col_obj->get_instance_id())) {
  724. continue;
  725. }
  726. int shape_idx = intersection_query_subindex_results[i];
  727. rcd.object = col_obj;
  728. rcd.shape = shape_idx;
  729. bool sc = GodotCollisionSolver3D::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_parameters.margin);
  730. if (!sc) {
  731. continue;
  732. }
  733. }
  734. }
  735. if (rcd.result_count > 0) {
  736. if (r_result) {
  737. for (int collision_index = 0; collision_index < rcd.result_count; ++collision_index) {
  738. const _RestResultData &result = (collision_index > 0) ? rcd.other_results[collision_index - 1] : rcd.best_result;
  739. PhysicsServer3D::MotionCollision &collision = r_result->collisions[collision_index];
  740. collision.collider = result.object->get_self();
  741. collision.collider_id = result.object->get_instance_id();
  742. collision.collider_shape = result.shape;
  743. collision.local_shape = result.local_shape;
  744. collision.normal = result.normal;
  745. collision.position = result.contact;
  746. collision.depth = result.len;
  747. const GodotBody3D *body = static_cast<const GodotBody3D *>(result.object);
  748. Vector3 rel_vec = result.contact - (body->get_transform().origin + body->get_center_of_mass());
  749. collision.collider_velocity = body->get_linear_velocity() + (body->get_angular_velocity()).cross(rel_vec);
  750. }
  751. r_result->travel = safe * p_parameters.motion;
  752. r_result->remainder = p_parameters.motion - safe * p_parameters.motion;
  753. r_result->travel += (body_transform.get_origin() - p_parameters.from.get_origin());
  754. r_result->collision_safe_fraction = safe;
  755. r_result->collision_unsafe_fraction = unsafe;
  756. r_result->collision_count = rcd.result_count;
  757. }
  758. collided = true;
  759. }
  760. }
  761. if (!collided && r_result) {
  762. r_result->travel = p_parameters.motion;
  763. r_result->remainder = Vector3();
  764. r_result->travel += (body_transform.get_origin() - p_parameters.from.get_origin());
  765. r_result->collision_safe_fraction = 1.0;
  766. r_result->collision_unsafe_fraction = 1.0;
  767. }
  768. return collided;
  769. }
  770. void *GodotSpace3D::_broadphase_pair(GodotCollisionObject3D *A, int p_subindex_A, GodotCollisionObject3D *B, int p_subindex_B, void *p_self) {
  771. if (!A->interacts_with(B)) {
  772. return nullptr;
  773. }
  774. GodotCollisionObject3D::Type type_A = A->get_type();
  775. GodotCollisionObject3D::Type type_B = B->get_type();
  776. if (type_A > type_B) {
  777. SWAP(A, B);
  778. SWAP(p_subindex_A, p_subindex_B);
  779. SWAP(type_A, type_B);
  780. }
  781. GodotSpace3D *self = (GodotSpace3D *)p_self;
  782. self->collision_pairs++;
  783. if (type_A == GodotCollisionObject3D::TYPE_AREA) {
  784. GodotArea3D *area = static_cast<GodotArea3D *>(A);
  785. if (type_B == GodotCollisionObject3D::TYPE_AREA) {
  786. GodotArea3D *area_b = static_cast<GodotArea3D *>(B);
  787. GodotArea2Pair3D *area2_pair = memnew(GodotArea2Pair3D(area_b, p_subindex_B, area, p_subindex_A));
  788. return area2_pair;
  789. } else if (type_B == GodotCollisionObject3D::TYPE_SOFT_BODY) {
  790. GodotSoftBody3D *softbody = static_cast<GodotSoftBody3D *>(B);
  791. GodotAreaSoftBodyPair3D *soft_area_pair = memnew(GodotAreaSoftBodyPair3D(softbody, p_subindex_B, area, p_subindex_A));
  792. return soft_area_pair;
  793. } else {
  794. GodotBody3D *body = static_cast<GodotBody3D *>(B);
  795. GodotAreaPair3D *area_pair = memnew(GodotAreaPair3D(body, p_subindex_B, area, p_subindex_A));
  796. return area_pair;
  797. }
  798. } else if (type_A == GodotCollisionObject3D::TYPE_BODY) {
  799. if (type_B == GodotCollisionObject3D::TYPE_SOFT_BODY) {
  800. GodotBodySoftBodyPair3D *soft_pair = memnew(GodotBodySoftBodyPair3D((GodotBody3D *)A, p_subindex_A, (GodotSoftBody3D *)B));
  801. return soft_pair;
  802. } else {
  803. GodotBodyPair3D *b = memnew(GodotBodyPair3D((GodotBody3D *)A, p_subindex_A, (GodotBody3D *)B, p_subindex_B));
  804. return b;
  805. }
  806. } else {
  807. // Soft Body/Soft Body, not supported.
  808. }
  809. return nullptr;
  810. }
  811. void GodotSpace3D::_broadphase_unpair(GodotCollisionObject3D *A, int p_subindex_A, GodotCollisionObject3D *B, int p_subindex_B, void *p_data, void *p_self) {
  812. if (!p_data) {
  813. return;
  814. }
  815. GodotSpace3D *self = (GodotSpace3D *)p_self;
  816. self->collision_pairs--;
  817. GodotConstraint3D *c = (GodotConstraint3D *)p_data;
  818. memdelete(c);
  819. }
  820. const SelfList<GodotBody3D>::List &GodotSpace3D::get_active_body_list() const {
  821. return active_list;
  822. }
  823. void GodotSpace3D::body_add_to_active_list(SelfList<GodotBody3D> *p_body) {
  824. active_list.add(p_body);
  825. }
  826. void GodotSpace3D::body_remove_from_active_list(SelfList<GodotBody3D> *p_body) {
  827. active_list.remove(p_body);
  828. }
  829. void GodotSpace3D::body_add_to_mass_properties_update_list(SelfList<GodotBody3D> *p_body) {
  830. mass_properties_update_list.add(p_body);
  831. }
  832. void GodotSpace3D::body_remove_from_mass_properties_update_list(SelfList<GodotBody3D> *p_body) {
  833. mass_properties_update_list.remove(p_body);
  834. }
  835. GodotBroadPhase3D *GodotSpace3D::get_broadphase() {
  836. return broadphase;
  837. }
  838. void GodotSpace3D::add_object(GodotCollisionObject3D *p_object) {
  839. ERR_FAIL_COND(objects.has(p_object));
  840. objects.insert(p_object);
  841. }
  842. void GodotSpace3D::remove_object(GodotCollisionObject3D *p_object) {
  843. ERR_FAIL_COND(!objects.has(p_object));
  844. objects.erase(p_object);
  845. }
  846. const Set<GodotCollisionObject3D *> &GodotSpace3D::get_objects() const {
  847. return objects;
  848. }
  849. void GodotSpace3D::body_add_to_state_query_list(SelfList<GodotBody3D> *p_body) {
  850. state_query_list.add(p_body);
  851. }
  852. void GodotSpace3D::body_remove_from_state_query_list(SelfList<GodotBody3D> *p_body) {
  853. state_query_list.remove(p_body);
  854. }
  855. void GodotSpace3D::area_add_to_monitor_query_list(SelfList<GodotArea3D> *p_area) {
  856. monitor_query_list.add(p_area);
  857. }
  858. void GodotSpace3D::area_remove_from_monitor_query_list(SelfList<GodotArea3D> *p_area) {
  859. monitor_query_list.remove(p_area);
  860. }
  861. void GodotSpace3D::area_add_to_moved_list(SelfList<GodotArea3D> *p_area) {
  862. area_moved_list.add(p_area);
  863. }
  864. void GodotSpace3D::area_remove_from_moved_list(SelfList<GodotArea3D> *p_area) {
  865. area_moved_list.remove(p_area);
  866. }
  867. const SelfList<GodotArea3D>::List &GodotSpace3D::get_moved_area_list() const {
  868. return area_moved_list;
  869. }
  870. const SelfList<GodotSoftBody3D>::List &GodotSpace3D::get_active_soft_body_list() const {
  871. return active_soft_body_list;
  872. }
  873. void GodotSpace3D::soft_body_add_to_active_list(SelfList<GodotSoftBody3D> *p_soft_body) {
  874. active_soft_body_list.add(p_soft_body);
  875. }
  876. void GodotSpace3D::soft_body_remove_from_active_list(SelfList<GodotSoftBody3D> *p_soft_body) {
  877. active_soft_body_list.remove(p_soft_body);
  878. }
  879. void GodotSpace3D::call_queries() {
  880. while (state_query_list.first()) {
  881. GodotBody3D *b = state_query_list.first()->self();
  882. state_query_list.remove(state_query_list.first());
  883. b->call_queries();
  884. }
  885. while (monitor_query_list.first()) {
  886. GodotArea3D *a = monitor_query_list.first()->self();
  887. monitor_query_list.remove(monitor_query_list.first());
  888. a->call_queries();
  889. }
  890. }
  891. void GodotSpace3D::setup() {
  892. contact_debug_count = 0;
  893. while (mass_properties_update_list.first()) {
  894. mass_properties_update_list.first()->self()->update_mass_properties();
  895. mass_properties_update_list.remove(mass_properties_update_list.first());
  896. }
  897. }
  898. void GodotSpace3D::update() {
  899. broadphase->update();
  900. }
  901. void GodotSpace3D::set_param(PhysicsServer3D::SpaceParameter p_param, real_t p_value) {
  902. switch (p_param) {
  903. case PhysicsServer3D::SPACE_PARAM_CONTACT_RECYCLE_RADIUS:
  904. contact_recycle_radius = p_value;
  905. break;
  906. case PhysicsServer3D::SPACE_PARAM_CONTACT_MAX_SEPARATION:
  907. contact_max_separation = p_value;
  908. break;
  909. case PhysicsServer3D::SPACE_PARAM_BODY_MAX_ALLOWED_PENETRATION:
  910. contact_max_allowed_penetration = p_value;
  911. break;
  912. case PhysicsServer3D::SPACE_PARAM_BODY_LINEAR_VELOCITY_SLEEP_THRESHOLD:
  913. body_linear_velocity_sleep_threshold = p_value;
  914. break;
  915. case PhysicsServer3D::SPACE_PARAM_BODY_ANGULAR_VELOCITY_SLEEP_THRESHOLD:
  916. body_angular_velocity_sleep_threshold = p_value;
  917. break;
  918. case PhysicsServer3D::SPACE_PARAM_BODY_TIME_TO_SLEEP:
  919. body_time_to_sleep = p_value;
  920. break;
  921. case PhysicsServer3D::SPACE_PARAM_BODY_ANGULAR_VELOCITY_DAMP_RATIO:
  922. body_angular_velocity_damp_ratio = p_value;
  923. break;
  924. case PhysicsServer3D::SPACE_PARAM_CONSTRAINT_DEFAULT_BIAS:
  925. constraint_bias = p_value;
  926. break;
  927. }
  928. }
  929. real_t GodotSpace3D::get_param(PhysicsServer3D::SpaceParameter p_param) const {
  930. switch (p_param) {
  931. case PhysicsServer3D::SPACE_PARAM_CONTACT_RECYCLE_RADIUS:
  932. return contact_recycle_radius;
  933. case PhysicsServer3D::SPACE_PARAM_CONTACT_MAX_SEPARATION:
  934. return contact_max_separation;
  935. case PhysicsServer3D::SPACE_PARAM_BODY_MAX_ALLOWED_PENETRATION:
  936. return contact_max_allowed_penetration;
  937. case PhysicsServer3D::SPACE_PARAM_BODY_LINEAR_VELOCITY_SLEEP_THRESHOLD:
  938. return body_linear_velocity_sleep_threshold;
  939. case PhysicsServer3D::SPACE_PARAM_BODY_ANGULAR_VELOCITY_SLEEP_THRESHOLD:
  940. return body_angular_velocity_sleep_threshold;
  941. case PhysicsServer3D::SPACE_PARAM_BODY_TIME_TO_SLEEP:
  942. return body_time_to_sleep;
  943. case PhysicsServer3D::SPACE_PARAM_BODY_ANGULAR_VELOCITY_DAMP_RATIO:
  944. return body_angular_velocity_damp_ratio;
  945. case PhysicsServer3D::SPACE_PARAM_CONSTRAINT_DEFAULT_BIAS:
  946. return constraint_bias;
  947. }
  948. return 0;
  949. }
  950. void GodotSpace3D::lock() {
  951. locked = true;
  952. }
  953. void GodotSpace3D::unlock() {
  954. locked = false;
  955. }
  956. bool GodotSpace3D::is_locked() const {
  957. return locked;
  958. }
  959. GodotPhysicsDirectSpaceState3D *GodotSpace3D::get_direct_state() {
  960. return direct_access;
  961. }
  962. GodotSpace3D::GodotSpace3D() {
  963. body_linear_velocity_sleep_threshold = GLOBAL_DEF("physics/3d/sleep_threshold_linear", 0.1);
  964. body_angular_velocity_sleep_threshold = GLOBAL_DEF("physics/3d/sleep_threshold_angular", Math::deg2rad(8.0));
  965. body_time_to_sleep = GLOBAL_DEF("physics/3d/time_before_sleep", 0.5);
  966. 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"));
  967. body_angular_velocity_damp_ratio = 10;
  968. broadphase = GodotBroadPhase3D::create_func();
  969. broadphase->set_pair_callback(_broadphase_pair, this);
  970. broadphase->set_unpair_callback(_broadphase_unpair, this);
  971. direct_access = memnew(GodotPhysicsDirectSpaceState3D);
  972. direct_access->space = this;
  973. }
  974. GodotSpace3D::~GodotSpace3D() {
  975. memdelete(broadphase);
  976. memdelete(direct_access);
  977. }