shape_3d_sw.cpp 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711
  1. /*************************************************************************/
  2. /* shape_3d_sw.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "shape_3d_sw.h"
  31. #include "core/math/geometry_3d.h"
  32. #include "core/math/quick_hull.h"
  33. #include "core/templates/sort_array.h"
  34. #define _EDGE_IS_VALID_SUPPORT_THRESHOLD 0.0002
  35. #define _FACE_IS_VALID_SUPPORT_THRESHOLD 0.9998
  36. #define _CYLINDER_EDGE_IS_VALID_SUPPORT_THRESHOLD 0.002
  37. #define _CYLINDER_FACE_IS_VALID_SUPPORT_THRESHOLD 0.999
  38. void Shape3DSW::configure(const AABB &p_aabb) {
  39. aabb = p_aabb;
  40. configured = true;
  41. for (Map<ShapeOwner3DSW *, int>::Element *E = owners.front(); E; E = E->next()) {
  42. ShapeOwner3DSW *co = (ShapeOwner3DSW *)E->key();
  43. co->_shape_changed();
  44. }
  45. }
  46. Vector3 Shape3DSW::get_support(const Vector3 &p_normal) const {
  47. Vector3 res;
  48. int amnt;
  49. FeatureType type;
  50. get_supports(p_normal, 1, &res, amnt, type);
  51. return res;
  52. }
  53. void Shape3DSW::add_owner(ShapeOwner3DSW *p_owner) {
  54. Map<ShapeOwner3DSW *, int>::Element *E = owners.find(p_owner);
  55. if (E) {
  56. E->get()++;
  57. } else {
  58. owners[p_owner] = 1;
  59. }
  60. }
  61. void Shape3DSW::remove_owner(ShapeOwner3DSW *p_owner) {
  62. Map<ShapeOwner3DSW *, int>::Element *E = owners.find(p_owner);
  63. ERR_FAIL_COND(!E);
  64. E->get()--;
  65. if (E->get() == 0) {
  66. owners.erase(E);
  67. }
  68. }
  69. bool Shape3DSW::is_owner(ShapeOwner3DSW *p_owner) const {
  70. return owners.has(p_owner);
  71. }
  72. const Map<ShapeOwner3DSW *, int> &Shape3DSW::get_owners() const {
  73. return owners;
  74. }
  75. Shape3DSW::Shape3DSW() {
  76. custom_bias = 0;
  77. configured = false;
  78. }
  79. Shape3DSW::~Shape3DSW() {
  80. ERR_FAIL_COND(owners.size());
  81. }
  82. Plane PlaneShape3DSW::get_plane() const {
  83. return plane;
  84. }
  85. void PlaneShape3DSW::project_range(const Vector3 &p_normal, const Transform &p_transform, real_t &r_min, real_t &r_max) const {
  86. // gibberish, a plane is infinity
  87. r_min = -1e7;
  88. r_max = 1e7;
  89. }
  90. Vector3 PlaneShape3DSW::get_support(const Vector3 &p_normal) const {
  91. return p_normal * 1e15;
  92. }
  93. bool PlaneShape3DSW::intersect_segment(const Vector3 &p_begin, const Vector3 &p_end, Vector3 &r_result, Vector3 &r_normal) const {
  94. bool inters = plane.intersects_segment(p_begin, p_end, &r_result);
  95. if (inters) {
  96. r_normal = plane.normal;
  97. }
  98. return inters;
  99. }
  100. bool PlaneShape3DSW::intersect_point(const Vector3 &p_point) const {
  101. return plane.distance_to(p_point) < 0;
  102. }
  103. Vector3 PlaneShape3DSW::get_closest_point_to(const Vector3 &p_point) const {
  104. if (plane.is_point_over(p_point)) {
  105. return plane.project(p_point);
  106. } else {
  107. return p_point;
  108. }
  109. }
  110. Vector3 PlaneShape3DSW::get_moment_of_inertia(real_t p_mass) const {
  111. return Vector3(); //wtf
  112. }
  113. void PlaneShape3DSW::_setup(const Plane &p_plane) {
  114. plane = p_plane;
  115. configure(AABB(Vector3(-1e4, -1e4, -1e4), Vector3(1e4 * 2, 1e4 * 2, 1e4 * 2)));
  116. }
  117. void PlaneShape3DSW::set_data(const Variant &p_data) {
  118. _setup(p_data);
  119. }
  120. Variant PlaneShape3DSW::get_data() const {
  121. return plane;
  122. }
  123. PlaneShape3DSW::PlaneShape3DSW() {
  124. }
  125. //
  126. real_t RayShape3DSW::get_length() const {
  127. return length;
  128. }
  129. bool RayShape3DSW::get_slips_on_slope() const {
  130. return slips_on_slope;
  131. }
  132. void RayShape3DSW::project_range(const Vector3 &p_normal, const Transform &p_transform, real_t &r_min, real_t &r_max) const {
  133. // don't think this will be even used
  134. r_min = 0;
  135. r_max = 1;
  136. }
  137. Vector3 RayShape3DSW::get_support(const Vector3 &p_normal) const {
  138. if (p_normal.z > 0) {
  139. return Vector3(0, 0, length);
  140. } else {
  141. return Vector3(0, 0, 0);
  142. }
  143. }
  144. void RayShape3DSW::get_supports(const Vector3 &p_normal, int p_max, Vector3 *r_supports, int &r_amount, FeatureType &r_type) const {
  145. if (Math::abs(p_normal.z) < _EDGE_IS_VALID_SUPPORT_THRESHOLD) {
  146. r_amount = 2;
  147. r_type = FEATURE_EDGE;
  148. r_supports[0] = Vector3(0, 0, 0);
  149. r_supports[1] = Vector3(0, 0, length);
  150. } else if (p_normal.z > 0) {
  151. r_amount = 1;
  152. r_type = FEATURE_POINT;
  153. *r_supports = Vector3(0, 0, length);
  154. } else {
  155. r_amount = 1;
  156. r_type = FEATURE_POINT;
  157. *r_supports = Vector3(0, 0, 0);
  158. }
  159. }
  160. bool RayShape3DSW::intersect_segment(const Vector3 &p_begin, const Vector3 &p_end, Vector3 &r_result, Vector3 &r_normal) const {
  161. return false; //simply not possible
  162. }
  163. bool RayShape3DSW::intersect_point(const Vector3 &p_point) const {
  164. return false; //simply not possible
  165. }
  166. Vector3 RayShape3DSW::get_closest_point_to(const Vector3 &p_point) const {
  167. Vector3 s[2] = {
  168. Vector3(0, 0, 0),
  169. Vector3(0, 0, length)
  170. };
  171. return Geometry3D::get_closest_point_to_segment(p_point, s);
  172. }
  173. Vector3 RayShape3DSW::get_moment_of_inertia(real_t p_mass) const {
  174. return Vector3();
  175. }
  176. void RayShape3DSW::_setup(real_t p_length, bool p_slips_on_slope) {
  177. length = p_length;
  178. slips_on_slope = p_slips_on_slope;
  179. configure(AABB(Vector3(0, 0, 0), Vector3(0.1, 0.1, length)));
  180. }
  181. void RayShape3DSW::set_data(const Variant &p_data) {
  182. Dictionary d = p_data;
  183. _setup(d["length"], d["slips_on_slope"]);
  184. }
  185. Variant RayShape3DSW::get_data() const {
  186. Dictionary d;
  187. d["length"] = length;
  188. d["slips_on_slope"] = slips_on_slope;
  189. return d;
  190. }
  191. RayShape3DSW::RayShape3DSW() {
  192. length = 1;
  193. slips_on_slope = false;
  194. }
  195. /********** SPHERE *************/
  196. real_t SphereShape3DSW::get_radius() const {
  197. return radius;
  198. }
  199. void SphereShape3DSW::project_range(const Vector3 &p_normal, const Transform &p_transform, real_t &r_min, real_t &r_max) const {
  200. real_t d = p_normal.dot(p_transform.origin);
  201. // figure out scale at point
  202. Vector3 local_normal = p_transform.basis.xform_inv(p_normal);
  203. real_t scale = local_normal.length();
  204. r_min = d - (radius)*scale;
  205. r_max = d + (radius)*scale;
  206. }
  207. Vector3 SphereShape3DSW::get_support(const Vector3 &p_normal) const {
  208. return p_normal * radius;
  209. }
  210. void SphereShape3DSW::get_supports(const Vector3 &p_normal, int p_max, Vector3 *r_supports, int &r_amount, FeatureType &r_type) const {
  211. *r_supports = p_normal * radius;
  212. r_amount = 1;
  213. r_type = FEATURE_POINT;
  214. }
  215. bool SphereShape3DSW::intersect_segment(const Vector3 &p_begin, const Vector3 &p_end, Vector3 &r_result, Vector3 &r_normal) const {
  216. return Geometry3D::segment_intersects_sphere(p_begin, p_end, Vector3(), radius, &r_result, &r_normal);
  217. }
  218. bool SphereShape3DSW::intersect_point(const Vector3 &p_point) const {
  219. return p_point.length() < radius;
  220. }
  221. Vector3 SphereShape3DSW::get_closest_point_to(const Vector3 &p_point) const {
  222. Vector3 p = p_point;
  223. real_t l = p.length();
  224. if (l < radius) {
  225. return p_point;
  226. }
  227. return (p / l) * radius;
  228. }
  229. Vector3 SphereShape3DSW::get_moment_of_inertia(real_t p_mass) const {
  230. real_t s = 0.4 * p_mass * radius * radius;
  231. return Vector3(s, s, s);
  232. }
  233. void SphereShape3DSW::_setup(real_t p_radius) {
  234. radius = p_radius;
  235. configure(AABB(Vector3(-radius, -radius, -radius), Vector3(radius * 2.0, radius * 2.0, radius * 2.0)));
  236. }
  237. void SphereShape3DSW::set_data(const Variant &p_data) {
  238. _setup(p_data);
  239. }
  240. Variant SphereShape3DSW::get_data() const {
  241. return radius;
  242. }
  243. SphereShape3DSW::SphereShape3DSW() {
  244. radius = 0;
  245. }
  246. /********** BOX *************/
  247. void BoxShape3DSW::project_range(const Vector3 &p_normal, const Transform &p_transform, real_t &r_min, real_t &r_max) const {
  248. // no matter the angle, the box is mirrored anyway
  249. Vector3 local_normal = p_transform.basis.xform_inv(p_normal);
  250. real_t length = local_normal.abs().dot(half_extents);
  251. real_t distance = p_normal.dot(p_transform.origin);
  252. r_min = distance - length;
  253. r_max = distance + length;
  254. }
  255. Vector3 BoxShape3DSW::get_support(const Vector3 &p_normal) const {
  256. Vector3 point(
  257. (p_normal.x < 0) ? -half_extents.x : half_extents.x,
  258. (p_normal.y < 0) ? -half_extents.y : half_extents.y,
  259. (p_normal.z < 0) ? -half_extents.z : half_extents.z);
  260. return point;
  261. }
  262. void BoxShape3DSW::get_supports(const Vector3 &p_normal, int p_max, Vector3 *r_supports, int &r_amount, FeatureType &r_type) const {
  263. static const int next[3] = { 1, 2, 0 };
  264. static const int next2[3] = { 2, 0, 1 };
  265. for (int i = 0; i < 3; i++) {
  266. Vector3 axis;
  267. axis[i] = 1.0;
  268. real_t dot = p_normal.dot(axis);
  269. if (Math::abs(dot) > _FACE_IS_VALID_SUPPORT_THRESHOLD) {
  270. //Vector3 axis_b;
  271. bool neg = dot < 0;
  272. r_amount = 4;
  273. r_type = FEATURE_FACE;
  274. Vector3 point;
  275. point[i] = half_extents[i];
  276. int i_n = next[i];
  277. int i_n2 = next2[i];
  278. static const real_t sign[4][2] = {
  279. { -1.0, 1.0 },
  280. { 1.0, 1.0 },
  281. { 1.0, -1.0 },
  282. { -1.0, -1.0 },
  283. };
  284. for (int j = 0; j < 4; j++) {
  285. point[i_n] = sign[j][0] * half_extents[i_n];
  286. point[i_n2] = sign[j][1] * half_extents[i_n2];
  287. r_supports[j] = neg ? -point : point;
  288. }
  289. if (neg) {
  290. SWAP(r_supports[1], r_supports[2]);
  291. SWAP(r_supports[0], r_supports[3]);
  292. }
  293. return;
  294. }
  295. r_amount = 0;
  296. }
  297. for (int i = 0; i < 3; i++) {
  298. Vector3 axis;
  299. axis[i] = 1.0;
  300. if (Math::abs(p_normal.dot(axis)) < _EDGE_IS_VALID_SUPPORT_THRESHOLD) {
  301. r_amount = 2;
  302. r_type = FEATURE_EDGE;
  303. int i_n = next[i];
  304. int i_n2 = next2[i];
  305. Vector3 point = half_extents;
  306. if (p_normal[i_n] < 0) {
  307. point[i_n] = -point[i_n];
  308. }
  309. if (p_normal[i_n2] < 0) {
  310. point[i_n2] = -point[i_n2];
  311. }
  312. r_supports[0] = point;
  313. point[i] = -point[i];
  314. r_supports[1] = point;
  315. return;
  316. }
  317. }
  318. /* USE POINT */
  319. Vector3 point(
  320. (p_normal.x < 0) ? -half_extents.x : half_extents.x,
  321. (p_normal.y < 0) ? -half_extents.y : half_extents.y,
  322. (p_normal.z < 0) ? -half_extents.z : half_extents.z);
  323. r_amount = 1;
  324. r_type = FEATURE_POINT;
  325. r_supports[0] = point;
  326. }
  327. bool BoxShape3DSW::intersect_segment(const Vector3 &p_begin, const Vector3 &p_end, Vector3 &r_result, Vector3 &r_normal) const {
  328. AABB aabb(-half_extents, half_extents * 2.0);
  329. return aabb.intersects_segment(p_begin, p_end, &r_result, &r_normal);
  330. }
  331. bool BoxShape3DSW::intersect_point(const Vector3 &p_point) const {
  332. return (Math::abs(p_point.x) < half_extents.x && Math::abs(p_point.y) < half_extents.y && Math::abs(p_point.z) < half_extents.z);
  333. }
  334. Vector3 BoxShape3DSW::get_closest_point_to(const Vector3 &p_point) const {
  335. int outside = 0;
  336. Vector3 min_point;
  337. for (int i = 0; i < 3; i++) {
  338. if (Math::abs(p_point[i]) > half_extents[i]) {
  339. outside++;
  340. if (outside == 1) {
  341. //use plane if only one side matches
  342. Vector3 n;
  343. n[i] = SGN(p_point[i]);
  344. Plane p(n, half_extents[i]);
  345. min_point = p.project(p_point);
  346. }
  347. }
  348. }
  349. if (!outside) {
  350. return p_point; //it's inside, don't do anything else
  351. }
  352. if (outside == 1) { //if only above one plane, this plane clearly wins
  353. return min_point;
  354. }
  355. //check segments
  356. real_t min_distance = 1e20;
  357. Vector3 closest_vertex = half_extents * p_point.sign();
  358. Vector3 s[2] = {
  359. closest_vertex,
  360. closest_vertex
  361. };
  362. for (int i = 0; i < 3; i++) {
  363. s[1] = closest_vertex;
  364. s[1][i] = -s[1][i]; //edge
  365. Vector3 closest_edge = Geometry3D::get_closest_point_to_segment(p_point, s);
  366. real_t d = p_point.distance_to(closest_edge);
  367. if (d < min_distance) {
  368. min_point = closest_edge;
  369. min_distance = d;
  370. }
  371. }
  372. return min_point;
  373. }
  374. Vector3 BoxShape3DSW::get_moment_of_inertia(real_t p_mass) const {
  375. real_t lx = half_extents.x;
  376. real_t ly = half_extents.y;
  377. real_t lz = half_extents.z;
  378. return Vector3((p_mass / 3.0) * (ly * ly + lz * lz), (p_mass / 3.0) * (lx * lx + lz * lz), (p_mass / 3.0) * (lx * lx + ly * ly));
  379. }
  380. void BoxShape3DSW::_setup(const Vector3 &p_half_extents) {
  381. half_extents = p_half_extents.abs();
  382. configure(AABB(-half_extents, half_extents * 2));
  383. }
  384. void BoxShape3DSW::set_data(const Variant &p_data) {
  385. _setup(p_data);
  386. }
  387. Variant BoxShape3DSW::get_data() const {
  388. return half_extents;
  389. }
  390. BoxShape3DSW::BoxShape3DSW() {
  391. }
  392. /********** CAPSULE *************/
  393. void CapsuleShape3DSW::project_range(const Vector3 &p_normal, const Transform &p_transform, real_t &r_min, real_t &r_max) const {
  394. Vector3 n = p_transform.basis.xform_inv(p_normal).normalized();
  395. real_t h = (n.z > 0) ? height : -height;
  396. n *= radius;
  397. n.z += h * 0.5;
  398. r_max = p_normal.dot(p_transform.xform(n));
  399. r_min = p_normal.dot(p_transform.xform(-n));
  400. }
  401. Vector3 CapsuleShape3DSW::get_support(const Vector3 &p_normal) const {
  402. Vector3 n = p_normal;
  403. real_t h = (n.z > 0) ? height : -height;
  404. n *= radius;
  405. n.z += h * 0.5;
  406. return n;
  407. }
  408. void CapsuleShape3DSW::get_supports(const Vector3 &p_normal, int p_max, Vector3 *r_supports, int &r_amount, FeatureType &r_type) const {
  409. Vector3 n = p_normal;
  410. real_t d = n.z;
  411. if (Math::abs(d) < _EDGE_IS_VALID_SUPPORT_THRESHOLD) {
  412. // make it flat
  413. n.z = 0.0;
  414. n.normalize();
  415. n *= radius;
  416. r_amount = 2;
  417. r_type = FEATURE_EDGE;
  418. r_supports[0] = n;
  419. r_supports[0].z += height * 0.5;
  420. r_supports[1] = n;
  421. r_supports[1].z -= height * 0.5;
  422. } else {
  423. real_t h = (d > 0) ? height : -height;
  424. n *= radius;
  425. n.z += h * 0.5;
  426. r_amount = 1;
  427. r_type = FEATURE_POINT;
  428. *r_supports = n;
  429. }
  430. }
  431. bool CapsuleShape3DSW::intersect_segment(const Vector3 &p_begin, const Vector3 &p_end, Vector3 &r_result, Vector3 &r_normal) const {
  432. Vector3 norm = (p_end - p_begin).normalized();
  433. real_t min_d = 1e20;
  434. Vector3 res, n;
  435. bool collision = false;
  436. Vector3 auxres, auxn;
  437. bool collided;
  438. // test against cylinder and spheres :-|
  439. collided = Geometry3D::segment_intersects_cylinder(p_begin, p_end, height, radius, &auxres, &auxn);
  440. if (collided) {
  441. real_t d = norm.dot(auxres);
  442. if (d < min_d) {
  443. min_d = d;
  444. res = auxres;
  445. n = auxn;
  446. collision = true;
  447. }
  448. }
  449. collided = Geometry3D::segment_intersects_sphere(p_begin, p_end, Vector3(0, 0, height * 0.5), radius, &auxres, &auxn);
  450. if (collided) {
  451. real_t d = norm.dot(auxres);
  452. if (d < min_d) {
  453. min_d = d;
  454. res = auxres;
  455. n = auxn;
  456. collision = true;
  457. }
  458. }
  459. collided = Geometry3D::segment_intersects_sphere(p_begin, p_end, Vector3(0, 0, height * -0.5), radius, &auxres, &auxn);
  460. if (collided) {
  461. real_t d = norm.dot(auxres);
  462. if (d < min_d) {
  463. min_d = d;
  464. res = auxres;
  465. n = auxn;
  466. collision = true;
  467. }
  468. }
  469. if (collision) {
  470. r_result = res;
  471. r_normal = n;
  472. }
  473. return collision;
  474. }
  475. bool CapsuleShape3DSW::intersect_point(const Vector3 &p_point) const {
  476. if (Math::abs(p_point.z) < height * 0.5) {
  477. return Vector3(p_point.x, p_point.y, 0).length() < radius;
  478. } else {
  479. Vector3 p = p_point;
  480. p.z = Math::abs(p.z) - height * 0.5;
  481. return p.length() < radius;
  482. }
  483. }
  484. Vector3 CapsuleShape3DSW::get_closest_point_to(const Vector3 &p_point) const {
  485. Vector3 s[2] = {
  486. Vector3(0, 0, -height * 0.5),
  487. Vector3(0, 0, height * 0.5),
  488. };
  489. Vector3 p = Geometry3D::get_closest_point_to_segment(p_point, s);
  490. if (p.distance_to(p_point) < radius) {
  491. return p_point;
  492. }
  493. return p + (p_point - p).normalized() * radius;
  494. }
  495. Vector3 CapsuleShape3DSW::get_moment_of_inertia(real_t p_mass) const {
  496. // use bad AABB approximation
  497. Vector3 extents = get_aabb().size * 0.5;
  498. return Vector3(
  499. (p_mass / 3.0) * (extents.y * extents.y + extents.z * extents.z),
  500. (p_mass / 3.0) * (extents.x * extents.x + extents.z * extents.z),
  501. (p_mass / 3.0) * (extents.y * extents.y + extents.y * extents.y));
  502. }
  503. void CapsuleShape3DSW::_setup(real_t p_height, real_t p_radius) {
  504. height = p_height;
  505. radius = p_radius;
  506. configure(AABB(Vector3(-radius, -radius, -height * 0.5 - radius), Vector3(radius * 2, radius * 2, height + radius * 2.0)));
  507. }
  508. void CapsuleShape3DSW::set_data(const Variant &p_data) {
  509. Dictionary d = p_data;
  510. ERR_FAIL_COND(!d.has("radius"));
  511. ERR_FAIL_COND(!d.has("height"));
  512. _setup(d["height"], d["radius"]);
  513. }
  514. Variant CapsuleShape3DSW::get_data() const {
  515. Dictionary d;
  516. d["radius"] = radius;
  517. d["height"] = height;
  518. return d;
  519. }
  520. CapsuleShape3DSW::CapsuleShape3DSW() {
  521. height = radius = 0;
  522. }
  523. /********** CYLINDER *************/
  524. void CylinderShape3DSW::project_range(const Vector3 &p_normal, const Transform &p_transform, real_t &r_min, real_t &r_max) const {
  525. Vector3 cylinder_axis = p_transform.basis.get_axis(1).normalized();
  526. real_t axis_dot = cylinder_axis.dot(p_normal);
  527. Vector3 local_normal = p_transform.basis.xform_inv(p_normal);
  528. real_t scale = local_normal.length();
  529. real_t scaled_radius = radius * scale;
  530. real_t scaled_height = height * scale;
  531. real_t length;
  532. if (Math::abs(axis_dot) > 1.0) {
  533. length = scaled_height * 0.5;
  534. } else {
  535. length = Math::abs(axis_dot * scaled_height * 0.5) + scaled_radius * Math::sqrt(1.0 - axis_dot * axis_dot);
  536. }
  537. real_t distance = p_normal.dot(p_transform.origin);
  538. r_min = distance - length;
  539. r_max = distance + length;
  540. }
  541. Vector3 CylinderShape3DSW::get_support(const Vector3 &p_normal) const {
  542. Vector3 n = p_normal;
  543. real_t h = (n.y > 0) ? height : -height;
  544. real_t s = Math::sqrt(n.x * n.x + n.z * n.z);
  545. if (Math::is_zero_approx(s)) {
  546. n.x = radius;
  547. n.y = h * 0.5;
  548. n.z = 0.0;
  549. } else {
  550. real_t d = radius / s;
  551. n.x = n.x * d;
  552. n.y = h * 0.5;
  553. n.z = n.z * d;
  554. }
  555. return n;
  556. }
  557. void CylinderShape3DSW::get_supports(const Vector3 &p_normal, int p_max, Vector3 *r_supports, int &r_amount, FeatureType &r_type) const {
  558. real_t d = p_normal.y;
  559. if (Math::abs(d) > _CYLINDER_FACE_IS_VALID_SUPPORT_THRESHOLD) {
  560. real_t h = (d > 0) ? height : -height;
  561. Vector3 n = p_normal;
  562. n.x = 0.0;
  563. n.z = 0.0;
  564. n.y = h * 0.5;
  565. r_amount = 3;
  566. r_type = FEATURE_CIRCLE;
  567. r_supports[0] = n;
  568. r_supports[1] = n;
  569. r_supports[1].x += radius;
  570. r_supports[2] = n;
  571. r_supports[2].z += radius;
  572. } else if (Math::abs(d) < _CYLINDER_EDGE_IS_VALID_SUPPORT_THRESHOLD) {
  573. // make it flat
  574. Vector3 n = p_normal;
  575. n.y = 0.0;
  576. n.normalize();
  577. n *= radius;
  578. r_amount = 2;
  579. r_type = FEATURE_EDGE;
  580. r_supports[0] = n;
  581. r_supports[0].y += height * 0.5;
  582. r_supports[1] = n;
  583. r_supports[1].y -= height * 0.5;
  584. } else {
  585. r_amount = 1;
  586. r_type = FEATURE_POINT;
  587. r_supports[0] = get_support(p_normal);
  588. return;
  589. Vector3 n = p_normal;
  590. real_t h = n.y * Math::sqrt(0.25 * height * height + radius * radius);
  591. if (Math::abs(h) > 1.0) {
  592. // Top or bottom surface.
  593. n.y = (n.y > 0.0) ? height * 0.5 : -height * 0.5;
  594. } else {
  595. // Lateral surface.
  596. n.y = height * 0.5 * h;
  597. }
  598. real_t s = Math::sqrt(n.x * n.x + n.z * n.z);
  599. if (Math::is_zero_approx(s)) {
  600. n.x = 0.0;
  601. n.z = 0.0;
  602. } else {
  603. real_t scaled_radius = radius / s;
  604. n.x = n.x * scaled_radius;
  605. n.z = n.z * scaled_radius;
  606. }
  607. r_supports[0] = n;
  608. }
  609. }
  610. bool CylinderShape3DSW::intersect_segment(const Vector3 &p_begin, const Vector3 &p_end, Vector3 &r_result, Vector3 &r_normal) const {
  611. return Geometry3D::segment_intersects_cylinder(p_begin, p_end, height, radius, &r_result, &r_normal, 1);
  612. }
  613. bool CylinderShape3DSW::intersect_point(const Vector3 &p_point) const {
  614. if (Math::abs(p_point.y) < height * 0.5) {
  615. return Vector3(p_point.x, 0, p_point.z).length() < radius;
  616. }
  617. return false;
  618. }
  619. Vector3 CylinderShape3DSW::get_closest_point_to(const Vector3 &p_point) const {
  620. if (Math::absf(p_point.y) > height * 0.5) {
  621. // Project point to top disk.
  622. real_t dir = p_point.y > 0.0 ? 1.0 : -1.0;
  623. Vector3 circle_pos(0.0, dir * height * 0.5, 0.0);
  624. Plane circle_plane(circle_pos, Vector3(0.0, dir, 0.0));
  625. Vector3 proj_point = circle_plane.project(p_point);
  626. // Clip position.
  627. Vector3 delta_point_1 = proj_point - circle_pos;
  628. real_t dist_point_1 = delta_point_1.length_squared();
  629. if (!Math::is_zero_approx(dist_point_1)) {
  630. dist_point_1 = Math::sqrt(dist_point_1);
  631. proj_point = circle_pos + delta_point_1 * MIN(dist_point_1, radius) / dist_point_1;
  632. }
  633. return proj_point;
  634. } else {
  635. Vector3 s[2] = {
  636. Vector3(0, -height * 0.5, 0),
  637. Vector3(0, height * 0.5, 0),
  638. };
  639. Vector3 p = Geometry3D::get_closest_point_to_segment(p_point, s);
  640. if (p.distance_to(p_point) < radius) {
  641. return p_point;
  642. }
  643. return p + (p_point - p).normalized() * radius;
  644. }
  645. }
  646. Vector3 CylinderShape3DSW::get_moment_of_inertia(real_t p_mass) const {
  647. // use bad AABB approximation
  648. Vector3 extents = get_aabb().size * 0.5;
  649. return Vector3(
  650. (p_mass / 3.0) * (extents.y * extents.y + extents.z * extents.z),
  651. (p_mass / 3.0) * (extents.x * extents.x + extents.z * extents.z),
  652. (p_mass / 3.0) * (extents.y * extents.y + extents.y * extents.y));
  653. }
  654. void CylinderShape3DSW::_setup(real_t p_height, real_t p_radius) {
  655. height = p_height;
  656. radius = p_radius;
  657. configure(AABB(Vector3(-radius, -height * 0.5, -radius), Vector3(radius * 2.0, height, radius * 2.0)));
  658. }
  659. void CylinderShape3DSW::set_data(const Variant &p_data) {
  660. Dictionary d = p_data;
  661. ERR_FAIL_COND(!d.has("radius"));
  662. ERR_FAIL_COND(!d.has("height"));
  663. _setup(d["height"], d["radius"]);
  664. }
  665. Variant CylinderShape3DSW::get_data() const {
  666. Dictionary d;
  667. d["radius"] = radius;
  668. d["height"] = height;
  669. return d;
  670. }
  671. CylinderShape3DSW::CylinderShape3DSW() {
  672. height = radius = 0;
  673. }
  674. /********** CONVEX POLYGON *************/
  675. void ConvexPolygonShape3DSW::project_range(const Vector3 &p_normal, const Transform &p_transform, real_t &r_min, real_t &r_max) const {
  676. int vertex_count = mesh.vertices.size();
  677. if (vertex_count == 0) {
  678. return;
  679. }
  680. const Vector3 *vrts = &mesh.vertices[0];
  681. for (int i = 0; i < vertex_count; i++) {
  682. real_t d = p_normal.dot(p_transform.xform(vrts[i]));
  683. if (i == 0 || d > r_max) {
  684. r_max = d;
  685. }
  686. if (i == 0 || d < r_min) {
  687. r_min = d;
  688. }
  689. }
  690. }
  691. Vector3 ConvexPolygonShape3DSW::get_support(const Vector3 &p_normal) const {
  692. Vector3 n = p_normal;
  693. int vert_support_idx = -1;
  694. real_t support_max = 0;
  695. int vertex_count = mesh.vertices.size();
  696. if (vertex_count == 0) {
  697. return Vector3();
  698. }
  699. const Vector3 *vrts = &mesh.vertices[0];
  700. for (int i = 0; i < vertex_count; i++) {
  701. real_t d = n.dot(vrts[i]);
  702. if (i == 0 || d > support_max) {
  703. support_max = d;
  704. vert_support_idx = i;
  705. }
  706. }
  707. return vrts[vert_support_idx];
  708. }
  709. void ConvexPolygonShape3DSW::get_supports(const Vector3 &p_normal, int p_max, Vector3 *r_supports, int &r_amount, FeatureType &r_type) const {
  710. const Geometry3D::MeshData::Face *faces = mesh.faces.ptr();
  711. int fc = mesh.faces.size();
  712. const Geometry3D::MeshData::Edge *edges = mesh.edges.ptr();
  713. int ec = mesh.edges.size();
  714. const Vector3 *vertices = mesh.vertices.ptr();
  715. int vc = mesh.vertices.size();
  716. //find vertex first
  717. real_t max = 0;
  718. int vtx = 0;
  719. for (int i = 0; i < vc; i++) {
  720. real_t d = p_normal.dot(vertices[i]);
  721. if (i == 0 || d > max) {
  722. max = d;
  723. vtx = i;
  724. }
  725. }
  726. for (int i = 0; i < fc; i++) {
  727. if (faces[i].plane.normal.dot(p_normal) > _FACE_IS_VALID_SUPPORT_THRESHOLD) {
  728. int ic = faces[i].indices.size();
  729. const int *ind = faces[i].indices.ptr();
  730. bool valid = false;
  731. for (int j = 0; j < ic; j++) {
  732. if (ind[j] == vtx) {
  733. valid = true;
  734. break;
  735. }
  736. }
  737. if (!valid) {
  738. continue;
  739. }
  740. int m = MIN(p_max, ic);
  741. for (int j = 0; j < m; j++) {
  742. r_supports[j] = vertices[ind[j]];
  743. }
  744. r_amount = m;
  745. r_type = FEATURE_FACE;
  746. return;
  747. }
  748. }
  749. for (int i = 0; i < ec; i++) {
  750. real_t dot = (vertices[edges[i].a] - vertices[edges[i].b]).normalized().dot(p_normal);
  751. dot = ABS(dot);
  752. if (dot < _EDGE_IS_VALID_SUPPORT_THRESHOLD && (edges[i].a == vtx || edges[i].b == vtx)) {
  753. r_amount = 2;
  754. r_type = FEATURE_EDGE;
  755. r_supports[0] = vertices[edges[i].a];
  756. r_supports[1] = vertices[edges[i].b];
  757. return;
  758. }
  759. }
  760. r_supports[0] = vertices[vtx];
  761. r_amount = 1;
  762. r_type = FEATURE_POINT;
  763. }
  764. bool ConvexPolygonShape3DSW::intersect_segment(const Vector3 &p_begin, const Vector3 &p_end, Vector3 &r_result, Vector3 &r_normal) const {
  765. const Geometry3D::MeshData::Face *faces = mesh.faces.ptr();
  766. int fc = mesh.faces.size();
  767. const Vector3 *vertices = mesh.vertices.ptr();
  768. Vector3 n = p_end - p_begin;
  769. real_t min = 1e20;
  770. bool col = false;
  771. for (int i = 0; i < fc; i++) {
  772. if (faces[i].plane.normal.dot(n) > 0) {
  773. continue; //opposing face
  774. }
  775. int ic = faces[i].indices.size();
  776. const int *ind = faces[i].indices.ptr();
  777. for (int j = 1; j < ic - 1; j++) {
  778. Face3 f(vertices[ind[0]], vertices[ind[j]], vertices[ind[j + 1]]);
  779. Vector3 result;
  780. if (f.intersects_segment(p_begin, p_end, &result)) {
  781. real_t d = n.dot(result);
  782. if (d < min) {
  783. min = d;
  784. r_result = result;
  785. r_normal = faces[i].plane.normal;
  786. col = true;
  787. }
  788. break;
  789. }
  790. }
  791. }
  792. return col;
  793. }
  794. bool ConvexPolygonShape3DSW::intersect_point(const Vector3 &p_point) const {
  795. const Geometry3D::MeshData::Face *faces = mesh.faces.ptr();
  796. int fc = mesh.faces.size();
  797. for (int i = 0; i < fc; i++) {
  798. if (faces[i].plane.distance_to(p_point) >= 0) {
  799. return false;
  800. }
  801. }
  802. return true;
  803. }
  804. Vector3 ConvexPolygonShape3DSW::get_closest_point_to(const Vector3 &p_point) const {
  805. const Geometry3D::MeshData::Face *faces = mesh.faces.ptr();
  806. int fc = mesh.faces.size();
  807. const Vector3 *vertices = mesh.vertices.ptr();
  808. bool all_inside = true;
  809. for (int i = 0; i < fc; i++) {
  810. if (!faces[i].plane.is_point_over(p_point)) {
  811. continue;
  812. }
  813. all_inside = false;
  814. bool is_inside = true;
  815. int ic = faces[i].indices.size();
  816. const int *indices = faces[i].indices.ptr();
  817. for (int j = 0; j < ic; j++) {
  818. Vector3 a = vertices[indices[j]];
  819. Vector3 b = vertices[indices[(j + 1) % ic]];
  820. Vector3 n = (a - b).cross(faces[i].plane.normal).normalized();
  821. if (Plane(a, n).is_point_over(p_point)) {
  822. is_inside = false;
  823. break;
  824. }
  825. }
  826. if (is_inside) {
  827. return faces[i].plane.project(p_point);
  828. }
  829. }
  830. if (all_inside) {
  831. return p_point;
  832. }
  833. real_t min_distance = 1e20;
  834. Vector3 min_point;
  835. //check edges
  836. const Geometry3D::MeshData::Edge *edges = mesh.edges.ptr();
  837. int ec = mesh.edges.size();
  838. for (int i = 0; i < ec; i++) {
  839. Vector3 s[2] = {
  840. vertices[edges[i].a],
  841. vertices[edges[i].b]
  842. };
  843. Vector3 closest = Geometry3D::get_closest_point_to_segment(p_point, s);
  844. real_t d = closest.distance_to(p_point);
  845. if (d < min_distance) {
  846. min_distance = d;
  847. min_point = closest;
  848. }
  849. }
  850. return min_point;
  851. }
  852. Vector3 ConvexPolygonShape3DSW::get_moment_of_inertia(real_t p_mass) const {
  853. // use bad AABB approximation
  854. Vector3 extents = get_aabb().size * 0.5;
  855. return Vector3(
  856. (p_mass / 3.0) * (extents.y * extents.y + extents.z * extents.z),
  857. (p_mass / 3.0) * (extents.x * extents.x + extents.z * extents.z),
  858. (p_mass / 3.0) * (extents.y * extents.y + extents.y * extents.y));
  859. }
  860. void ConvexPolygonShape3DSW::_setup(const Vector<Vector3> &p_vertices) {
  861. Error err = QuickHull::build(p_vertices, mesh);
  862. if (err != OK) {
  863. ERR_PRINT("Failed to build QuickHull");
  864. }
  865. AABB _aabb;
  866. for (int i = 0; i < mesh.vertices.size(); i++) {
  867. if (i == 0) {
  868. _aabb.position = mesh.vertices[i];
  869. } else {
  870. _aabb.expand_to(mesh.vertices[i]);
  871. }
  872. }
  873. configure(_aabb);
  874. }
  875. void ConvexPolygonShape3DSW::set_data(const Variant &p_data) {
  876. _setup(p_data);
  877. }
  878. Variant ConvexPolygonShape3DSW::get_data() const {
  879. return mesh.vertices;
  880. }
  881. ConvexPolygonShape3DSW::ConvexPolygonShape3DSW() {
  882. }
  883. /********** FACE POLYGON *************/
  884. void FaceShape3DSW::project_range(const Vector3 &p_normal, const Transform &p_transform, real_t &r_min, real_t &r_max) const {
  885. for (int i = 0; i < 3; i++) {
  886. Vector3 v = p_transform.xform(vertex[i]);
  887. real_t d = p_normal.dot(v);
  888. if (i == 0 || d > r_max) {
  889. r_max = d;
  890. }
  891. if (i == 0 || d < r_min) {
  892. r_min = d;
  893. }
  894. }
  895. }
  896. Vector3 FaceShape3DSW::get_support(const Vector3 &p_normal) const {
  897. int vert_support_idx = -1;
  898. real_t support_max = 0;
  899. for (int i = 0; i < 3; i++) {
  900. real_t d = p_normal.dot(vertex[i]);
  901. if (i == 0 || d > support_max) {
  902. support_max = d;
  903. vert_support_idx = i;
  904. }
  905. }
  906. return vertex[vert_support_idx];
  907. }
  908. void FaceShape3DSW::get_supports(const Vector3 &p_normal, int p_max, Vector3 *r_supports, int &r_amount, FeatureType &r_type) const {
  909. Vector3 n = p_normal;
  910. /** TEST FACE AS SUPPORT **/
  911. if (normal.dot(n) > _FACE_IS_VALID_SUPPORT_THRESHOLD) {
  912. r_amount = 3;
  913. r_type = FEATURE_FACE;
  914. for (int i = 0; i < 3; i++) {
  915. r_supports[i] = vertex[i];
  916. }
  917. return;
  918. }
  919. /** FIND SUPPORT VERTEX **/
  920. int vert_support_idx = -1;
  921. real_t support_max = 0;
  922. for (int i = 0; i < 3; i++) {
  923. real_t d = n.dot(vertex[i]);
  924. if (i == 0 || d > support_max) {
  925. support_max = d;
  926. vert_support_idx = i;
  927. }
  928. }
  929. /** TEST EDGES AS SUPPORT **/
  930. for (int i = 0; i < 3; i++) {
  931. int nx = (i + 1) % 3;
  932. if (i != vert_support_idx && nx != vert_support_idx) {
  933. continue;
  934. }
  935. // check if edge is valid as a support
  936. real_t dot = (vertex[i] - vertex[nx]).normalized().dot(n);
  937. dot = ABS(dot);
  938. if (dot < _EDGE_IS_VALID_SUPPORT_THRESHOLD) {
  939. r_amount = 2;
  940. r_type = FEATURE_EDGE;
  941. r_supports[0] = vertex[i];
  942. r_supports[1] = vertex[nx];
  943. return;
  944. }
  945. }
  946. r_amount = 1;
  947. r_type = FEATURE_POINT;
  948. r_supports[0] = vertex[vert_support_idx];
  949. }
  950. bool FaceShape3DSW::intersect_segment(const Vector3 &p_begin, const Vector3 &p_end, Vector3 &r_result, Vector3 &r_normal) const {
  951. bool c = Geometry3D::segment_intersects_triangle(p_begin, p_end, vertex[0], vertex[1], vertex[2], &r_result);
  952. if (c) {
  953. r_normal = Plane(vertex[0], vertex[1], vertex[2]).normal;
  954. if (r_normal.dot(p_end - p_begin) > 0) {
  955. r_normal = -r_normal;
  956. }
  957. }
  958. return c;
  959. }
  960. bool FaceShape3DSW::intersect_point(const Vector3 &p_point) const {
  961. return false; //face is flat
  962. }
  963. Vector3 FaceShape3DSW::get_closest_point_to(const Vector3 &p_point) const {
  964. return Face3(vertex[0], vertex[1], vertex[2]).get_closest_point_to(p_point);
  965. }
  966. Vector3 FaceShape3DSW::get_moment_of_inertia(real_t p_mass) const {
  967. return Vector3(); // Sorry, but i don't think anyone cares, FaceShape!
  968. }
  969. FaceShape3DSW::FaceShape3DSW() {
  970. configure(AABB());
  971. }
  972. Vector<Vector3> ConcavePolygonShape3DSW::get_faces() const {
  973. Vector<Vector3> rfaces;
  974. rfaces.resize(faces.size() * 3);
  975. for (int i = 0; i < faces.size(); i++) {
  976. Face f = faces.get(i);
  977. for (int j = 0; j < 3; j++) {
  978. rfaces.set(i * 3 + j, vertices.get(f.indices[j]));
  979. }
  980. }
  981. return rfaces;
  982. }
  983. void ConcavePolygonShape3DSW::project_range(const Vector3 &p_normal, const Transform &p_transform, real_t &r_min, real_t &r_max) const {
  984. int count = vertices.size();
  985. if (count == 0) {
  986. r_min = 0;
  987. r_max = 0;
  988. return;
  989. }
  990. const Vector3 *vptr = vertices.ptr();
  991. for (int i = 0; i < count; i++) {
  992. real_t d = p_normal.dot(p_transform.xform(vptr[i]));
  993. if (i == 0 || d > r_max) {
  994. r_max = d;
  995. }
  996. if (i == 0 || d < r_min) {
  997. r_min = d;
  998. }
  999. }
  1000. }
  1001. Vector3 ConcavePolygonShape3DSW::get_support(const Vector3 &p_normal) const {
  1002. int count = vertices.size();
  1003. if (count == 0) {
  1004. return Vector3();
  1005. }
  1006. const Vector3 *vptr = vertices.ptr();
  1007. Vector3 n = p_normal;
  1008. int vert_support_idx = -1;
  1009. real_t support_max = 0;
  1010. for (int i = 0; i < count; i++) {
  1011. real_t d = n.dot(vptr[i]);
  1012. if (i == 0 || d > support_max) {
  1013. support_max = d;
  1014. vert_support_idx = i;
  1015. }
  1016. }
  1017. return vptr[vert_support_idx];
  1018. }
  1019. void ConcavePolygonShape3DSW::_cull_segment(int p_idx, _SegmentCullParams *p_params) const {
  1020. const BVH *bvh = &p_params->bvh[p_idx];
  1021. /*
  1022. if (p_params->dir.dot(bvh->aabb.get_support(-p_params->dir))>p_params->min_d)
  1023. return; //test against whole AABB, which isn't very costly
  1024. */
  1025. //printf("addr: %p\n",bvh);
  1026. if (!bvh->aabb.intersects_segment(p_params->from, p_params->to)) {
  1027. return;
  1028. }
  1029. if (bvh->face_index >= 0) {
  1030. Vector3 res;
  1031. Vector3 vertices[3] = {
  1032. p_params->vertices[p_params->faces[bvh->face_index].indices[0]],
  1033. p_params->vertices[p_params->faces[bvh->face_index].indices[1]],
  1034. p_params->vertices[p_params->faces[bvh->face_index].indices[2]]
  1035. };
  1036. if (Geometry3D::segment_intersects_triangle(
  1037. p_params->from,
  1038. p_params->to,
  1039. vertices[0],
  1040. vertices[1],
  1041. vertices[2],
  1042. &res)) {
  1043. real_t d = p_params->dir.dot(res) - p_params->dir.dot(p_params->from);
  1044. //TODO, seems segmen/triangle intersection is broken :(
  1045. if (d > 0 && d < p_params->min_d) {
  1046. p_params->min_d = d;
  1047. p_params->result = res;
  1048. p_params->normal = Plane(vertices[0], vertices[1], vertices[2]).normal;
  1049. p_params->collisions++;
  1050. }
  1051. }
  1052. } else {
  1053. if (bvh->left >= 0) {
  1054. _cull_segment(bvh->left, p_params);
  1055. }
  1056. if (bvh->right >= 0) {
  1057. _cull_segment(bvh->right, p_params);
  1058. }
  1059. }
  1060. }
  1061. bool ConcavePolygonShape3DSW::intersect_segment(const Vector3 &p_begin, const Vector3 &p_end, Vector3 &r_result, Vector3 &r_normal) const {
  1062. if (faces.size() == 0) {
  1063. return false;
  1064. }
  1065. // unlock data
  1066. const Face *fr = faces.ptr();
  1067. const Vector3 *vr = vertices.ptr();
  1068. const BVH *br = bvh.ptr();
  1069. _SegmentCullParams params;
  1070. params.from = p_begin;
  1071. params.to = p_end;
  1072. params.collisions = 0;
  1073. params.dir = (p_end - p_begin).normalized();
  1074. params.faces = fr;
  1075. params.vertices = vr;
  1076. params.bvh = br;
  1077. params.min_d = 1e20;
  1078. // cull
  1079. _cull_segment(0, &params);
  1080. if (params.collisions > 0) {
  1081. r_result = params.result;
  1082. r_normal = params.normal;
  1083. return true;
  1084. } else {
  1085. return false;
  1086. }
  1087. }
  1088. bool ConcavePolygonShape3DSW::intersect_point(const Vector3 &p_point) const {
  1089. return false; //face is flat
  1090. }
  1091. Vector3 ConcavePolygonShape3DSW::get_closest_point_to(const Vector3 &p_point) const {
  1092. return Vector3();
  1093. }
  1094. void ConcavePolygonShape3DSW::_cull(int p_idx, _CullParams *p_params) const {
  1095. const BVH *bvh = &p_params->bvh[p_idx];
  1096. if (!p_params->aabb.intersects(bvh->aabb)) {
  1097. return;
  1098. }
  1099. if (bvh->face_index >= 0) {
  1100. const Face *f = &p_params->faces[bvh->face_index];
  1101. FaceShape3DSW *face = p_params->face;
  1102. face->normal = f->normal;
  1103. face->vertex[0] = p_params->vertices[f->indices[0]];
  1104. face->vertex[1] = p_params->vertices[f->indices[1]];
  1105. face->vertex[2] = p_params->vertices[f->indices[2]];
  1106. p_params->callback(p_params->userdata, face);
  1107. } else {
  1108. if (bvh->left >= 0) {
  1109. _cull(bvh->left, p_params);
  1110. }
  1111. if (bvh->right >= 0) {
  1112. _cull(bvh->right, p_params);
  1113. }
  1114. }
  1115. }
  1116. void ConcavePolygonShape3DSW::cull(const AABB &p_local_aabb, Callback p_callback, void *p_userdata) const {
  1117. // make matrix local to concave
  1118. if (faces.size() == 0) {
  1119. return;
  1120. }
  1121. AABB local_aabb = p_local_aabb;
  1122. // unlock data
  1123. const Face *fr = faces.ptr();
  1124. const Vector3 *vr = vertices.ptr();
  1125. const BVH *br = bvh.ptr();
  1126. FaceShape3DSW face; // use this to send in the callback
  1127. _CullParams params;
  1128. params.aabb = local_aabb;
  1129. params.face = &face;
  1130. params.faces = fr;
  1131. params.vertices = vr;
  1132. params.bvh = br;
  1133. params.callback = p_callback;
  1134. params.userdata = p_userdata;
  1135. // cull
  1136. _cull(0, &params);
  1137. }
  1138. Vector3 ConcavePolygonShape3DSW::get_moment_of_inertia(real_t p_mass) const {
  1139. // use bad AABB approximation
  1140. Vector3 extents = get_aabb().size * 0.5;
  1141. return Vector3(
  1142. (p_mass / 3.0) * (extents.y * extents.y + extents.z * extents.z),
  1143. (p_mass / 3.0) * (extents.x * extents.x + extents.z * extents.z),
  1144. (p_mass / 3.0) * (extents.y * extents.y + extents.y * extents.y));
  1145. }
  1146. struct _VolumeSW_BVH_Element {
  1147. AABB aabb;
  1148. Vector3 center;
  1149. int face_index;
  1150. };
  1151. struct _VolumeSW_BVH_CompareX {
  1152. _FORCE_INLINE_ bool operator()(const _VolumeSW_BVH_Element &a, const _VolumeSW_BVH_Element &b) const {
  1153. return a.center.x < b.center.x;
  1154. }
  1155. };
  1156. struct _VolumeSW_BVH_CompareY {
  1157. _FORCE_INLINE_ bool operator()(const _VolumeSW_BVH_Element &a, const _VolumeSW_BVH_Element &b) const {
  1158. return a.center.y < b.center.y;
  1159. }
  1160. };
  1161. struct _VolumeSW_BVH_CompareZ {
  1162. _FORCE_INLINE_ bool operator()(const _VolumeSW_BVH_Element &a, const _VolumeSW_BVH_Element &b) const {
  1163. return a.center.z < b.center.z;
  1164. }
  1165. };
  1166. struct _VolumeSW_BVH {
  1167. AABB aabb;
  1168. _VolumeSW_BVH *left;
  1169. _VolumeSW_BVH *right;
  1170. int face_index;
  1171. };
  1172. _VolumeSW_BVH *_volume_sw_build_bvh(_VolumeSW_BVH_Element *p_elements, int p_size, int &count) {
  1173. _VolumeSW_BVH *bvh = memnew(_VolumeSW_BVH);
  1174. if (p_size == 1) {
  1175. //leaf
  1176. bvh->aabb = p_elements[0].aabb;
  1177. bvh->left = nullptr;
  1178. bvh->right = nullptr;
  1179. bvh->face_index = p_elements->face_index;
  1180. count++;
  1181. return bvh;
  1182. } else {
  1183. bvh->face_index = -1;
  1184. }
  1185. AABB aabb;
  1186. for (int i = 0; i < p_size; i++) {
  1187. if (i == 0) {
  1188. aabb = p_elements[i].aabb;
  1189. } else {
  1190. aabb.merge_with(p_elements[i].aabb);
  1191. }
  1192. }
  1193. bvh->aabb = aabb;
  1194. switch (aabb.get_longest_axis_index()) {
  1195. case 0: {
  1196. SortArray<_VolumeSW_BVH_Element, _VolumeSW_BVH_CompareX> sort_x;
  1197. sort_x.sort(p_elements, p_size);
  1198. } break;
  1199. case 1: {
  1200. SortArray<_VolumeSW_BVH_Element, _VolumeSW_BVH_CompareY> sort_y;
  1201. sort_y.sort(p_elements, p_size);
  1202. } break;
  1203. case 2: {
  1204. SortArray<_VolumeSW_BVH_Element, _VolumeSW_BVH_CompareZ> sort_z;
  1205. sort_z.sort(p_elements, p_size);
  1206. } break;
  1207. }
  1208. int split = p_size / 2;
  1209. bvh->left = _volume_sw_build_bvh(p_elements, split, count);
  1210. bvh->right = _volume_sw_build_bvh(&p_elements[split], p_size - split, count);
  1211. //printf("branch at %p - %i: %i\n",bvh,count,bvh->face_index);
  1212. count++;
  1213. return bvh;
  1214. }
  1215. void ConcavePolygonShape3DSW::_fill_bvh(_VolumeSW_BVH *p_bvh_tree, BVH *p_bvh_array, int &p_idx) {
  1216. int idx = p_idx;
  1217. p_bvh_array[idx].aabb = p_bvh_tree->aabb;
  1218. p_bvh_array[idx].face_index = p_bvh_tree->face_index;
  1219. //printf("%p - %i: %i(%p) -- %p:%p\n",%p_bvh_array[idx],p_idx,p_bvh_array[i]->face_index,&p_bvh_tree->face_index,p_bvh_tree->left,p_bvh_tree->right);
  1220. if (p_bvh_tree->left) {
  1221. p_bvh_array[idx].left = ++p_idx;
  1222. _fill_bvh(p_bvh_tree->left, p_bvh_array, p_idx);
  1223. } else {
  1224. p_bvh_array[p_idx].left = -1;
  1225. }
  1226. if (p_bvh_tree->right) {
  1227. p_bvh_array[idx].right = ++p_idx;
  1228. _fill_bvh(p_bvh_tree->right, p_bvh_array, p_idx);
  1229. } else {
  1230. p_bvh_array[p_idx].right = -1;
  1231. }
  1232. memdelete(p_bvh_tree);
  1233. }
  1234. void ConcavePolygonShape3DSW::_setup(Vector<Vector3> p_faces) {
  1235. int src_face_count = p_faces.size();
  1236. if (src_face_count == 0) {
  1237. configure(AABB());
  1238. return;
  1239. }
  1240. ERR_FAIL_COND(src_face_count % 3);
  1241. src_face_count /= 3;
  1242. const Vector3 *facesr = p_faces.ptr();
  1243. Vector<_VolumeSW_BVH_Element> bvh_array;
  1244. bvh_array.resize(src_face_count);
  1245. _VolumeSW_BVH_Element *bvh_arrayw = bvh_array.ptrw();
  1246. faces.resize(src_face_count);
  1247. Face *facesw = faces.ptrw();
  1248. vertices.resize(src_face_count * 3);
  1249. Vector3 *verticesw = vertices.ptrw();
  1250. AABB _aabb;
  1251. for (int i = 0; i < src_face_count; i++) {
  1252. Face3 face(facesr[i * 3 + 0], facesr[i * 3 + 1], facesr[i * 3 + 2]);
  1253. bvh_arrayw[i].aabb = face.get_aabb();
  1254. bvh_arrayw[i].center = bvh_arrayw[i].aabb.position + bvh_arrayw[i].aabb.size * 0.5;
  1255. bvh_arrayw[i].face_index = i;
  1256. facesw[i].indices[0] = i * 3 + 0;
  1257. facesw[i].indices[1] = i * 3 + 1;
  1258. facesw[i].indices[2] = i * 3 + 2;
  1259. facesw[i].normal = face.get_plane().normal;
  1260. verticesw[i * 3 + 0] = face.vertex[0];
  1261. verticesw[i * 3 + 1] = face.vertex[1];
  1262. verticesw[i * 3 + 2] = face.vertex[2];
  1263. if (i == 0) {
  1264. _aabb = bvh_arrayw[i].aabb;
  1265. } else {
  1266. _aabb.merge_with(bvh_arrayw[i].aabb);
  1267. }
  1268. }
  1269. int count = 0;
  1270. _VolumeSW_BVH *bvh_tree = _volume_sw_build_bvh(bvh_arrayw, src_face_count, count);
  1271. bvh.resize(count + 1);
  1272. BVH *bvh_arrayw2 = bvh.ptrw();
  1273. int idx = 0;
  1274. _fill_bvh(bvh_tree, bvh_arrayw2, idx);
  1275. configure(_aabb); // this type of shape has no margin
  1276. }
  1277. void ConcavePolygonShape3DSW::set_data(const Variant &p_data) {
  1278. _setup(p_data);
  1279. }
  1280. Variant ConcavePolygonShape3DSW::get_data() const {
  1281. return get_faces();
  1282. }
  1283. ConcavePolygonShape3DSW::ConcavePolygonShape3DSW() {
  1284. }
  1285. /* HEIGHT MAP SHAPE */
  1286. Vector<real_t> HeightMapShape3DSW::get_heights() const {
  1287. return heights;
  1288. }
  1289. int HeightMapShape3DSW::get_width() const {
  1290. return width;
  1291. }
  1292. int HeightMapShape3DSW::get_depth() const {
  1293. return depth;
  1294. }
  1295. real_t HeightMapShape3DSW::get_cell_size() const {
  1296. return cell_size;
  1297. }
  1298. void HeightMapShape3DSW::project_range(const Vector3 &p_normal, const Transform &p_transform, real_t &r_min, real_t &r_max) const {
  1299. //not very useful, but not very used either
  1300. p_transform.xform(get_aabb()).project_range_in_plane(Plane(p_normal, 0), r_min, r_max);
  1301. }
  1302. Vector3 HeightMapShape3DSW::get_support(const Vector3 &p_normal) const {
  1303. //not very useful, but not very used either
  1304. return get_aabb().get_support(p_normal);
  1305. }
  1306. bool HeightMapShape3DSW::intersect_segment(const Vector3 &p_begin, const Vector3 &p_end, Vector3 &r_point, Vector3 &r_normal) const {
  1307. return false;
  1308. }
  1309. bool HeightMapShape3DSW::intersect_point(const Vector3 &p_point) const {
  1310. return false;
  1311. }
  1312. Vector3 HeightMapShape3DSW::get_closest_point_to(const Vector3 &p_point) const {
  1313. return Vector3();
  1314. }
  1315. void HeightMapShape3DSW::cull(const AABB &p_local_aabb, Callback p_callback, void *p_userdata) const {
  1316. }
  1317. Vector3 HeightMapShape3DSW::get_moment_of_inertia(real_t p_mass) const {
  1318. // use bad AABB approximation
  1319. Vector3 extents = get_aabb().size * 0.5;
  1320. return Vector3(
  1321. (p_mass / 3.0) * (extents.y * extents.y + extents.z * extents.z),
  1322. (p_mass / 3.0) * (extents.x * extents.x + extents.z * extents.z),
  1323. (p_mass / 3.0) * (extents.y * extents.y + extents.y * extents.y));
  1324. }
  1325. void HeightMapShape3DSW::_setup(Vector<real_t> p_heights, int p_width, int p_depth, real_t p_cell_size) {
  1326. heights = p_heights;
  1327. width = p_width;
  1328. depth = p_depth;
  1329. cell_size = p_cell_size;
  1330. const real_t *r = heights.ptr();
  1331. AABB aabb;
  1332. for (int i = 0; i < depth; i++) {
  1333. for (int j = 0; j < width; j++) {
  1334. real_t h = r[i * width + j];
  1335. Vector3 pos(j * cell_size, h, i * cell_size);
  1336. if (i == 0 || j == 0) {
  1337. aabb.position = pos;
  1338. } else {
  1339. aabb.expand_to(pos);
  1340. }
  1341. }
  1342. }
  1343. configure(aabb);
  1344. }
  1345. void HeightMapShape3DSW::set_data(const Variant &p_data) {
  1346. ERR_FAIL_COND(p_data.get_type() != Variant::DICTIONARY);
  1347. Dictionary d = p_data;
  1348. ERR_FAIL_COND(!d.has("width"));
  1349. ERR_FAIL_COND(!d.has("depth"));
  1350. ERR_FAIL_COND(!d.has("cell_size"));
  1351. ERR_FAIL_COND(!d.has("heights"));
  1352. int width = d["width"];
  1353. int depth = d["depth"];
  1354. real_t cell_size = d["cell_size"];
  1355. Vector<real_t> heights = d["heights"];
  1356. ERR_FAIL_COND(width <= 0);
  1357. ERR_FAIL_COND(depth <= 0);
  1358. ERR_FAIL_COND(cell_size <= CMP_EPSILON);
  1359. ERR_FAIL_COND(heights.size() != (width * depth));
  1360. _setup(heights, width, depth, cell_size);
  1361. }
  1362. Variant HeightMapShape3DSW::get_data() const {
  1363. ERR_FAIL_V(Variant());
  1364. }
  1365. HeightMapShape3DSW::HeightMapShape3DSW() {
  1366. width = 0;
  1367. depth = 0;
  1368. cell_size = 0;
  1369. }