shape_sw.cpp 37 KB

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