shape_sw.cpp 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676
  1. /*************************************************************************/
  2. /* shape_sw.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "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. return;
  387. n = p_transform.basis.xform(n);
  388. real_t distance = p_normal.dot(p_transform.origin);
  389. real_t length = Math::abs(p_normal.dot(n));
  390. r_min = distance - length;
  391. r_max = distance + length;
  392. ERR_FAIL_COND(r_max < r_min);
  393. }
  394. Vector3 CapsuleShapeSW::get_support(const Vector3 &p_normal) const {
  395. Vector3 n = p_normal;
  396. real_t h = (n.z > 0) ? height : -height;
  397. n *= radius;
  398. n.z += h * 0.5;
  399. return n;
  400. }
  401. void CapsuleShapeSW::get_supports(const Vector3 &p_normal, int p_max, Vector3 *r_supports, int &r_amount) const {
  402. Vector3 n = p_normal;
  403. real_t d = n.z;
  404. if (Math::abs(d) < _EDGE_IS_VALID_SUPPORT_THRESHOLD) {
  405. // make it flat
  406. n.z = 0.0;
  407. n.normalize();
  408. n *= radius;
  409. r_amount = 2;
  410. r_supports[0] = n;
  411. r_supports[0].z += height * 0.5;
  412. r_supports[1] = n;
  413. r_supports[1].z -= height * 0.5;
  414. } else {
  415. real_t h = (d > 0) ? height : -height;
  416. n *= radius;
  417. n.z += h * 0.5;
  418. r_amount = 1;
  419. *r_supports = n;
  420. }
  421. }
  422. bool CapsuleShapeSW::intersect_segment(const Vector3 &p_begin, const Vector3 &p_end, Vector3 &r_result, Vector3 &r_normal) const {
  423. Vector3 norm = (p_end - p_begin).normalized();
  424. real_t min_d = 1e20;
  425. Vector3 res, n;
  426. bool collision = false;
  427. Vector3 auxres, auxn;
  428. bool collided;
  429. // test against cylinder and spheres :-|
  430. collided = Geometry::segment_intersects_cylinder(p_begin, p_end, height, radius, &auxres, &auxn);
  431. if (collided) {
  432. real_t d = norm.dot(auxres);
  433. if (d < min_d) {
  434. min_d = d;
  435. res = auxres;
  436. n = auxn;
  437. collision = true;
  438. }
  439. }
  440. collided = Geometry::segment_intersects_sphere(p_begin, p_end, Vector3(0, 0, height * 0.5), radius, &auxres, &auxn);
  441. if (collided) {
  442. real_t d = norm.dot(auxres);
  443. if (d < min_d) {
  444. min_d = d;
  445. res = auxres;
  446. n = auxn;
  447. collision = true;
  448. }
  449. }
  450. collided = Geometry::segment_intersects_sphere(p_begin, p_end, Vector3(0, 0, height * -0.5), radius, &auxres, &auxn);
  451. if (collided) {
  452. real_t d = norm.dot(auxres);
  453. if (d < min_d) {
  454. min_d = d;
  455. res = auxres;
  456. n = auxn;
  457. collision = true;
  458. }
  459. }
  460. if (collision) {
  461. r_result = res;
  462. r_normal = n;
  463. }
  464. return collision;
  465. }
  466. bool CapsuleShapeSW::intersect_point(const Vector3 &p_point) const {
  467. if (Math::abs(p_point.z) < height * 0.5) {
  468. return Vector3(p_point.x, p_point.y, 0).length() < radius;
  469. } else {
  470. Vector3 p = p_point;
  471. p.z = Math::abs(p.z) - height * 0.5;
  472. return p.length() < radius;
  473. }
  474. }
  475. Vector3 CapsuleShapeSW::get_closest_point_to(const Vector3 &p_point) const {
  476. Vector3 s[2] = {
  477. Vector3(0, 0, -height * 0.5),
  478. Vector3(0, 0, height * 0.5),
  479. };
  480. Vector3 p = Geometry::get_closest_point_to_segment(p_point, s);
  481. if (p.distance_to(p_point) < radius)
  482. return p_point;
  483. return p + (p_point - p).normalized() * radius;
  484. }
  485. Vector3 CapsuleShapeSW::get_moment_of_inertia(real_t p_mass) const {
  486. // use bad AABB approximation
  487. Vector3 extents = get_aabb().size * 0.5;
  488. return Vector3(
  489. (p_mass / 3.0) * (extents.y * extents.y + extents.z * extents.z),
  490. (p_mass / 3.0) * (extents.x * extents.x + extents.z * extents.z),
  491. (p_mass / 3.0) * (extents.y * extents.y + extents.y * extents.y));
  492. }
  493. void CapsuleShapeSW::_setup(real_t p_height, real_t p_radius) {
  494. height = p_height;
  495. radius = p_radius;
  496. configure(AABB(Vector3(-radius, -radius, -height * 0.5 - radius), Vector3(radius * 2, radius * 2, height + radius * 2.0)));
  497. }
  498. void CapsuleShapeSW::set_data(const Variant &p_data) {
  499. Dictionary d = p_data;
  500. ERR_FAIL_COND(!d.has("radius"));
  501. ERR_FAIL_COND(!d.has("height"));
  502. _setup(d["height"], d["radius"]);
  503. }
  504. Variant CapsuleShapeSW::get_data() const {
  505. Dictionary d;
  506. d["radius"] = radius;
  507. d["height"] = height;
  508. return d;
  509. }
  510. CapsuleShapeSW::CapsuleShapeSW() {
  511. height = radius = 0;
  512. }
  513. /********** CONVEX POLYGON *************/
  514. void ConvexPolygonShapeSW::project_range(const Vector3 &p_normal, const Transform &p_transform, real_t &r_min, real_t &r_max) const {
  515. int vertex_count = mesh.vertices.size();
  516. if (vertex_count == 0)
  517. return;
  518. const Vector3 *vrts = &mesh.vertices[0];
  519. for (int i = 0; i < vertex_count; i++) {
  520. real_t d = p_normal.dot(p_transform.xform(vrts[i]));
  521. if (i == 0 || d > r_max)
  522. r_max = d;
  523. if (i == 0 || d < r_min)
  524. r_min = d;
  525. }
  526. }
  527. Vector3 ConvexPolygonShapeSW::get_support(const Vector3 &p_normal) const {
  528. Vector3 n = p_normal;
  529. int vert_support_idx = -1;
  530. real_t support_max = 0;
  531. int vertex_count = mesh.vertices.size();
  532. if (vertex_count == 0)
  533. return Vector3();
  534. const Vector3 *vrts = &mesh.vertices[0];
  535. for (int i = 0; i < vertex_count; i++) {
  536. real_t d = n.dot(vrts[i]);
  537. if (i == 0 || d > support_max) {
  538. support_max = d;
  539. vert_support_idx = i;
  540. }
  541. }
  542. return vrts[vert_support_idx];
  543. }
  544. void ConvexPolygonShapeSW::get_supports(const Vector3 &p_normal, int p_max, Vector3 *r_supports, int &r_amount) const {
  545. const Geometry::MeshData::Face *faces = mesh.faces.ptr();
  546. int fc = mesh.faces.size();
  547. const Geometry::MeshData::Edge *edges = mesh.edges.ptr();
  548. int ec = mesh.edges.size();
  549. const Vector3 *vertices = mesh.vertices.ptr();
  550. int vc = mesh.vertices.size();
  551. //find vertex first
  552. real_t max = 0;
  553. int vtx = 0;
  554. for (int i = 0; i < vc; i++) {
  555. real_t d = p_normal.dot(vertices[i]);
  556. if (i == 0 || d > max) {
  557. max = d;
  558. vtx = i;
  559. }
  560. }
  561. for (int i = 0; i < fc; i++) {
  562. if (faces[i].plane.normal.dot(p_normal) > _FACE_IS_VALID_SUPPORT_THRESHOLD) {
  563. int ic = faces[i].indices.size();
  564. const int *ind = faces[i].indices.ptr();
  565. bool valid = false;
  566. for (int j = 0; j < ic; j++) {
  567. if (ind[j] == vtx) {
  568. valid = true;
  569. break;
  570. }
  571. }
  572. if (!valid)
  573. continue;
  574. int m = MIN(p_max, ic);
  575. for (int j = 0; j < m; j++) {
  576. r_supports[j] = vertices[ind[j]];
  577. }
  578. r_amount = m;
  579. return;
  580. }
  581. }
  582. for (int i = 0; i < ec; i++) {
  583. real_t dot = (vertices[edges[i].a] - vertices[edges[i].b]).normalized().dot(p_normal);
  584. dot = ABS(dot);
  585. if (dot < _EDGE_IS_VALID_SUPPORT_THRESHOLD && (edges[i].a == vtx || edges[i].b == vtx)) {
  586. r_amount = 2;
  587. r_supports[0] = vertices[edges[i].a];
  588. r_supports[1] = vertices[edges[i].b];
  589. return;
  590. }
  591. }
  592. r_supports[0] = vertices[vtx];
  593. r_amount = 1;
  594. }
  595. bool ConvexPolygonShapeSW::intersect_segment(const Vector3 &p_begin, const Vector3 &p_end, Vector3 &r_result, Vector3 &r_normal) const {
  596. const Geometry::MeshData::Face *faces = mesh.faces.ptr();
  597. int fc = mesh.faces.size();
  598. const Vector3 *vertices = mesh.vertices.ptr();
  599. Vector3 n = p_end - p_begin;
  600. real_t min = 1e20;
  601. bool col = false;
  602. for (int i = 0; i < fc; i++) {
  603. if (faces[i].plane.normal.dot(n) > 0)
  604. continue; //opposing face
  605. int ic = faces[i].indices.size();
  606. const int *ind = faces[i].indices.ptr();
  607. for (int j = 1; j < ic - 1; j++) {
  608. Face3 f(vertices[ind[0]], vertices[ind[j]], vertices[ind[j + 1]]);
  609. Vector3 result;
  610. if (f.intersects_segment(p_begin, p_end, &result)) {
  611. real_t d = n.dot(result);
  612. if (d < min) {
  613. min = d;
  614. r_result = result;
  615. r_normal = faces[i].plane.normal;
  616. col = true;
  617. }
  618. break;
  619. }
  620. }
  621. }
  622. return col;
  623. }
  624. bool ConvexPolygonShapeSW::intersect_point(const Vector3 &p_point) const {
  625. const Geometry::MeshData::Face *faces = mesh.faces.ptr();
  626. int fc = mesh.faces.size();
  627. for (int i = 0; i < fc; i++) {
  628. if (faces[i].plane.distance_to(p_point) >= 0)
  629. return false;
  630. }
  631. return true;
  632. }
  633. Vector3 ConvexPolygonShapeSW::get_closest_point_to(const Vector3 &p_point) const {
  634. const Geometry::MeshData::Face *faces = mesh.faces.ptr();
  635. int fc = mesh.faces.size();
  636. const Vector3 *vertices = mesh.vertices.ptr();
  637. bool all_inside = true;
  638. for (int i = 0; i < fc; i++) {
  639. if (!faces[i].plane.is_point_over(p_point))
  640. continue;
  641. all_inside = false;
  642. bool is_inside = true;
  643. int ic = faces[i].indices.size();
  644. const int *indices = faces[i].indices.ptr();
  645. for (int j = 0; j < ic; j++) {
  646. Vector3 a = vertices[indices[j]];
  647. Vector3 b = vertices[indices[(j + 1) % ic]];
  648. Vector3 n = (a - b).cross(faces[i].plane.normal).normalized();
  649. if (Plane(a, n).is_point_over(p_point)) {
  650. is_inside = false;
  651. break;
  652. }
  653. }
  654. if (is_inside) {
  655. return faces[i].plane.project(p_point);
  656. }
  657. }
  658. if (all_inside) {
  659. return p_point;
  660. }
  661. float min_distance = 1e20;
  662. Vector3 min_point;
  663. //check edges
  664. const Geometry::MeshData::Edge *edges = mesh.edges.ptr();
  665. int ec = mesh.edges.size();
  666. for (int i = 0; i < ec; i++) {
  667. Vector3 s[2] = {
  668. vertices[edges[i].a],
  669. vertices[edges[i].b]
  670. };
  671. Vector3 closest = Geometry::get_closest_point_to_segment(p_point, s);
  672. float d = closest.distance_to(p_point);
  673. if (d < min_distance) {
  674. min_distance = d;
  675. min_point = closest;
  676. }
  677. }
  678. return min_point;
  679. }
  680. Vector3 ConvexPolygonShapeSW::get_moment_of_inertia(real_t p_mass) const {
  681. // use bad AABB approximation
  682. Vector3 extents = get_aabb().size * 0.5;
  683. return Vector3(
  684. (p_mass / 3.0) * (extents.y * extents.y + extents.z * extents.z),
  685. (p_mass / 3.0) * (extents.x * extents.x + extents.z * extents.z),
  686. (p_mass / 3.0) * (extents.y * extents.y + extents.y * extents.y));
  687. }
  688. void ConvexPolygonShapeSW::_setup(const Vector<Vector3> &p_vertices) {
  689. Error err = QuickHull::build(p_vertices, mesh);
  690. if (err != OK)
  691. ERR_PRINT("Failed to build QuickHull");
  692. AABB _aabb;
  693. for (int i = 0; i < mesh.vertices.size(); i++) {
  694. if (i == 0)
  695. _aabb.position = mesh.vertices[i];
  696. else
  697. _aabb.expand_to(mesh.vertices[i]);
  698. }
  699. configure(_aabb);
  700. }
  701. void ConvexPolygonShapeSW::set_data(const Variant &p_data) {
  702. _setup(p_data);
  703. }
  704. Variant ConvexPolygonShapeSW::get_data() const {
  705. return mesh.vertices;
  706. }
  707. ConvexPolygonShapeSW::ConvexPolygonShapeSW() {
  708. }
  709. /********** FACE POLYGON *************/
  710. void FaceShapeSW::project_range(const Vector3 &p_normal, const Transform &p_transform, real_t &r_min, real_t &r_max) const {
  711. for (int i = 0; i < 3; i++) {
  712. Vector3 v = p_transform.xform(vertex[i]);
  713. real_t d = p_normal.dot(v);
  714. if (i == 0 || d > r_max)
  715. r_max = d;
  716. if (i == 0 || d < r_min)
  717. r_min = d;
  718. }
  719. }
  720. Vector3 FaceShapeSW::get_support(const Vector3 &p_normal) const {
  721. int vert_support_idx = -1;
  722. real_t support_max = 0;
  723. for (int i = 0; i < 3; i++) {
  724. real_t d = p_normal.dot(vertex[i]);
  725. if (i == 0 || d > support_max) {
  726. support_max = d;
  727. vert_support_idx = i;
  728. }
  729. }
  730. return vertex[vert_support_idx];
  731. }
  732. void FaceShapeSW::get_supports(const Vector3 &p_normal, int p_max, Vector3 *r_supports, int &r_amount) const {
  733. Vector3 n = p_normal;
  734. /** TEST FACE AS SUPPORT **/
  735. if (normal.dot(n) > _FACE_IS_VALID_SUPPORT_THRESHOLD) {
  736. r_amount = 3;
  737. for (int i = 0; i < 3; i++) {
  738. r_supports[i] = vertex[i];
  739. }
  740. return;
  741. }
  742. /** FIND SUPPORT VERTEX **/
  743. int vert_support_idx = -1;
  744. real_t support_max = 0;
  745. for (int i = 0; i < 3; i++) {
  746. real_t d = n.dot(vertex[i]);
  747. if (i == 0 || d > support_max) {
  748. support_max = d;
  749. vert_support_idx = i;
  750. }
  751. }
  752. /** TEST EDGES AS SUPPORT **/
  753. for (int i = 0; i < 3; i++) {
  754. int nx = (i + 1) % 3;
  755. if (i != vert_support_idx && nx != vert_support_idx)
  756. continue;
  757. // check if edge is valid as a support
  758. real_t dot = (vertex[i] - vertex[nx]).normalized().dot(n);
  759. dot = ABS(dot);
  760. if (dot < _EDGE_IS_VALID_SUPPORT_THRESHOLD) {
  761. r_amount = 2;
  762. r_supports[0] = vertex[i];
  763. r_supports[1] = vertex[nx];
  764. return;
  765. }
  766. }
  767. r_amount = 1;
  768. r_supports[0] = vertex[vert_support_idx];
  769. }
  770. bool FaceShapeSW::intersect_segment(const Vector3 &p_begin, const Vector3 &p_end, Vector3 &r_result, Vector3 &r_normal) const {
  771. bool c = Geometry::segment_intersects_triangle(p_begin, p_end, vertex[0], vertex[1], vertex[2], &r_result);
  772. if (c) {
  773. r_normal = Plane(vertex[0], vertex[1], vertex[2]).normal;
  774. if (r_normal.dot(p_end - p_begin) > 0) {
  775. r_normal = -r_normal;
  776. }
  777. }
  778. return c;
  779. }
  780. bool FaceShapeSW::intersect_point(const Vector3 &p_point) const {
  781. return false; //face is flat
  782. }
  783. Vector3 FaceShapeSW::get_closest_point_to(const Vector3 &p_point) const {
  784. return Face3(vertex[0], vertex[1], vertex[2]).get_closest_point_to(p_point);
  785. }
  786. Vector3 FaceShapeSW::get_moment_of_inertia(real_t p_mass) const {
  787. return Vector3(); // Sorry, but i don't think anyone cares, FaceShape!
  788. }
  789. FaceShapeSW::FaceShapeSW() {
  790. configure(AABB());
  791. }
  792. PoolVector<Vector3> ConcavePolygonShapeSW::get_faces() const {
  793. PoolVector<Vector3> rfaces;
  794. rfaces.resize(faces.size() * 3);
  795. for (int i = 0; i < faces.size(); i++) {
  796. Face f = faces.get(i);
  797. for (int j = 0; j < 3; j++) {
  798. rfaces.set(i * 3 + j, vertices.get(f.indices[j]));
  799. }
  800. }
  801. return rfaces;
  802. }
  803. void ConcavePolygonShapeSW::project_range(const Vector3 &p_normal, const Transform &p_transform, real_t &r_min, real_t &r_max) const {
  804. int count = vertices.size();
  805. if (count == 0) {
  806. r_min = 0;
  807. r_max = 0;
  808. return;
  809. }
  810. PoolVector<Vector3>::Read r = vertices.read();
  811. const Vector3 *vptr = r.ptr();
  812. for (int i = 0; i < count; i++) {
  813. real_t d = p_normal.dot(p_transform.xform(vptr[i]));
  814. if (i == 0 || d > r_max)
  815. r_max = d;
  816. if (i == 0 || d < r_min)
  817. r_min = d;
  818. }
  819. }
  820. Vector3 ConcavePolygonShapeSW::get_support(const Vector3 &p_normal) const {
  821. int count = vertices.size();
  822. if (count == 0)
  823. return Vector3();
  824. PoolVector<Vector3>::Read r = vertices.read();
  825. const Vector3 *vptr = r.ptr();
  826. Vector3 n = p_normal;
  827. int vert_support_idx = -1;
  828. real_t support_max = 0;
  829. for (int i = 0; i < count; i++) {
  830. real_t d = n.dot(vptr[i]);
  831. if (i == 0 || d > support_max) {
  832. support_max = d;
  833. vert_support_idx = i;
  834. }
  835. }
  836. return vptr[vert_support_idx];
  837. }
  838. void ConcavePolygonShapeSW::_cull_segment(int p_idx, _SegmentCullParams *p_params) const {
  839. const BVH *bvh = &p_params->bvh[p_idx];
  840. /*
  841. if (p_params->dir.dot(bvh->aabb.get_support(-p_params->dir))>p_params->min_d)
  842. return; //test against whole AABB, which isn't very costly
  843. */
  844. //printf("addr: %p\n",bvh);
  845. if (!bvh->aabb.intersects_segment(p_params->from, p_params->to)) {
  846. return;
  847. }
  848. if (bvh->face_index >= 0) {
  849. Vector3 res;
  850. Vector3 vertices[3] = {
  851. p_params->vertices[p_params->faces[bvh->face_index].indices[0]],
  852. p_params->vertices[p_params->faces[bvh->face_index].indices[1]],
  853. p_params->vertices[p_params->faces[bvh->face_index].indices[2]]
  854. };
  855. if (Geometry::segment_intersects_triangle(
  856. p_params->from,
  857. p_params->to,
  858. vertices[0],
  859. vertices[1],
  860. vertices[2],
  861. &res)) {
  862. real_t d = p_params->dir.dot(res) - p_params->dir.dot(p_params->from);
  863. //TODO, seems segmen/triangle intersection is broken :(
  864. if (d > 0 && d < p_params->min_d) {
  865. p_params->min_d = d;
  866. p_params->result = res;
  867. p_params->normal = Plane(vertices[0], vertices[1], vertices[2]).normal;
  868. p_params->collisions++;
  869. }
  870. }
  871. } else {
  872. if (bvh->left >= 0)
  873. _cull_segment(bvh->left, p_params);
  874. if (bvh->right >= 0)
  875. _cull_segment(bvh->right, p_params);
  876. }
  877. }
  878. bool ConcavePolygonShapeSW::intersect_segment(const Vector3 &p_begin, const Vector3 &p_end, Vector3 &r_result, Vector3 &r_normal) const {
  879. if (faces.size() == 0)
  880. return false;
  881. // unlock data
  882. PoolVector<Face>::Read fr = faces.read();
  883. PoolVector<Vector3>::Read vr = vertices.read();
  884. PoolVector<BVH>::Read br = bvh.read();
  885. _SegmentCullParams params;
  886. params.from = p_begin;
  887. params.to = p_end;
  888. params.collisions = 0;
  889. params.dir = (p_end - p_begin).normalized();
  890. params.faces = fr.ptr();
  891. params.vertices = vr.ptr();
  892. params.bvh = br.ptr();
  893. params.min_d = 1e20;
  894. // cull
  895. _cull_segment(0, &params);
  896. if (params.collisions > 0) {
  897. r_result = params.result;
  898. r_normal = params.normal;
  899. return true;
  900. } else {
  901. return false;
  902. }
  903. }
  904. bool ConcavePolygonShapeSW::intersect_point(const Vector3 &p_point) const {
  905. return false; //face is flat
  906. }
  907. Vector3 ConcavePolygonShapeSW::get_closest_point_to(const Vector3 &p_point) const {
  908. return Vector3();
  909. }
  910. void ConcavePolygonShapeSW::_cull(int p_idx, _CullParams *p_params) const {
  911. const BVH *bvh = &p_params->bvh[p_idx];
  912. if (!p_params->aabb.intersects(bvh->aabb))
  913. return;
  914. if (bvh->face_index >= 0) {
  915. const Face *f = &p_params->faces[bvh->face_index];
  916. FaceShapeSW *face = p_params->face;
  917. face->normal = f->normal;
  918. face->vertex[0] = p_params->vertices[f->indices[0]];
  919. face->vertex[1] = p_params->vertices[f->indices[1]];
  920. face->vertex[2] = p_params->vertices[f->indices[2]];
  921. p_params->callback(p_params->userdata, face);
  922. } else {
  923. if (bvh->left >= 0) {
  924. _cull(bvh->left, p_params);
  925. }
  926. if (bvh->right >= 0) {
  927. _cull(bvh->right, p_params);
  928. }
  929. }
  930. }
  931. void ConcavePolygonShapeSW::cull(const AABB &p_local_aabb, Callback p_callback, void *p_userdata) const {
  932. // make matrix local to concave
  933. if (faces.size() == 0)
  934. return;
  935. AABB local_aabb = p_local_aabb;
  936. // unlock data
  937. PoolVector<Face>::Read fr = faces.read();
  938. PoolVector<Vector3>::Read vr = vertices.read();
  939. PoolVector<BVH>::Read br = bvh.read();
  940. FaceShapeSW face; // use this to send in the callback
  941. _CullParams params;
  942. params.aabb = local_aabb;
  943. params.face = &face;
  944. params.faces = fr.ptr();
  945. params.vertices = vr.ptr();
  946. params.bvh = br.ptr();
  947. params.callback = p_callback;
  948. params.userdata = p_userdata;
  949. // cull
  950. _cull(0, &params);
  951. }
  952. Vector3 ConcavePolygonShapeSW::get_moment_of_inertia(real_t p_mass) const {
  953. // use bad AABB approximation
  954. Vector3 extents = get_aabb().size * 0.5;
  955. return Vector3(
  956. (p_mass / 3.0) * (extents.y * extents.y + extents.z * extents.z),
  957. (p_mass / 3.0) * (extents.x * extents.x + extents.z * extents.z),
  958. (p_mass / 3.0) * (extents.y * extents.y + extents.y * extents.y));
  959. }
  960. struct _VolumeSW_BVH_Element {
  961. AABB aabb;
  962. Vector3 center;
  963. int face_index;
  964. };
  965. struct _VolumeSW_BVH_CompareX {
  966. _FORCE_INLINE_ bool operator()(const _VolumeSW_BVH_Element &a, const _VolumeSW_BVH_Element &b) const {
  967. return a.center.x < b.center.x;
  968. }
  969. };
  970. struct _VolumeSW_BVH_CompareY {
  971. _FORCE_INLINE_ bool operator()(const _VolumeSW_BVH_Element &a, const _VolumeSW_BVH_Element &b) const {
  972. return a.center.y < b.center.y;
  973. }
  974. };
  975. struct _VolumeSW_BVH_CompareZ {
  976. _FORCE_INLINE_ bool operator()(const _VolumeSW_BVH_Element &a, const _VolumeSW_BVH_Element &b) const {
  977. return a.center.z < b.center.z;
  978. }
  979. };
  980. struct _VolumeSW_BVH {
  981. AABB aabb;
  982. _VolumeSW_BVH *left;
  983. _VolumeSW_BVH *right;
  984. int face_index;
  985. };
  986. _VolumeSW_BVH *_volume_sw_build_bvh(_VolumeSW_BVH_Element *p_elements, int p_size, int &count) {
  987. _VolumeSW_BVH *bvh = memnew(_VolumeSW_BVH);
  988. if (p_size == 1) {
  989. //leaf
  990. bvh->aabb = p_elements[0].aabb;
  991. bvh->left = NULL;
  992. bvh->right = NULL;
  993. bvh->face_index = p_elements->face_index;
  994. count++;
  995. return bvh;
  996. } else {
  997. bvh->face_index = -1;
  998. }
  999. AABB aabb;
  1000. for (int i = 0; i < p_size; i++) {
  1001. if (i == 0)
  1002. aabb = p_elements[i].aabb;
  1003. else
  1004. aabb.merge_with(p_elements[i].aabb);
  1005. }
  1006. bvh->aabb = aabb;
  1007. switch (aabb.get_longest_axis_index()) {
  1008. case 0: {
  1009. SortArray<_VolumeSW_BVH_Element, _VolumeSW_BVH_CompareX> sort_x;
  1010. sort_x.sort(p_elements, p_size);
  1011. } break;
  1012. case 1: {
  1013. SortArray<_VolumeSW_BVH_Element, _VolumeSW_BVH_CompareY> sort_y;
  1014. sort_y.sort(p_elements, p_size);
  1015. } break;
  1016. case 2: {
  1017. SortArray<_VolumeSW_BVH_Element, _VolumeSW_BVH_CompareZ> sort_z;
  1018. sort_z.sort(p_elements, p_size);
  1019. } break;
  1020. }
  1021. int split = p_size / 2;
  1022. bvh->left = _volume_sw_build_bvh(p_elements, split, count);
  1023. bvh->right = _volume_sw_build_bvh(&p_elements[split], p_size - split, count);
  1024. //printf("branch at %p - %i: %i\n",bvh,count,bvh->face_index);
  1025. count++;
  1026. return bvh;
  1027. }
  1028. void ConcavePolygonShapeSW::_fill_bvh(_VolumeSW_BVH *p_bvh_tree, BVH *p_bvh_array, int &p_idx) {
  1029. int idx = p_idx;
  1030. p_bvh_array[idx].aabb = p_bvh_tree->aabb;
  1031. p_bvh_array[idx].face_index = p_bvh_tree->face_index;
  1032. //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);
  1033. if (p_bvh_tree->left) {
  1034. p_bvh_array[idx].left = ++p_idx;
  1035. _fill_bvh(p_bvh_tree->left, p_bvh_array, p_idx);
  1036. } else {
  1037. p_bvh_array[p_idx].left = -1;
  1038. }
  1039. if (p_bvh_tree->right) {
  1040. p_bvh_array[idx].right = ++p_idx;
  1041. _fill_bvh(p_bvh_tree->right, p_bvh_array, p_idx);
  1042. } else {
  1043. p_bvh_array[p_idx].right = -1;
  1044. }
  1045. memdelete(p_bvh_tree);
  1046. }
  1047. void ConcavePolygonShapeSW::_setup(PoolVector<Vector3> p_faces) {
  1048. int src_face_count = p_faces.size();
  1049. if (src_face_count == 0) {
  1050. configure(AABB());
  1051. return;
  1052. }
  1053. ERR_FAIL_COND(src_face_count % 3);
  1054. src_face_count /= 3;
  1055. PoolVector<Vector3>::Read r = p_faces.read();
  1056. const Vector3 *facesr = r.ptr();
  1057. PoolVector<_VolumeSW_BVH_Element> bvh_array;
  1058. bvh_array.resize(src_face_count);
  1059. PoolVector<_VolumeSW_BVH_Element>::Write bvhw = bvh_array.write();
  1060. _VolumeSW_BVH_Element *bvh_arrayw = bvhw.ptr();
  1061. faces.resize(src_face_count);
  1062. PoolVector<Face>::Write w = faces.write();
  1063. Face *facesw = w.ptr();
  1064. vertices.resize(src_face_count * 3);
  1065. PoolVector<Vector3>::Write vw = vertices.write();
  1066. Vector3 *verticesw = vw.ptr();
  1067. AABB _aabb;
  1068. for (int i = 0; i < src_face_count; i++) {
  1069. Face3 face(facesr[i * 3 + 0], facesr[i * 3 + 1], facesr[i * 3 + 2]);
  1070. bvh_arrayw[i].aabb = face.get_aabb();
  1071. bvh_arrayw[i].center = bvh_arrayw[i].aabb.position + bvh_arrayw[i].aabb.size * 0.5;
  1072. bvh_arrayw[i].face_index = i;
  1073. facesw[i].indices[0] = i * 3 + 0;
  1074. facesw[i].indices[1] = i * 3 + 1;
  1075. facesw[i].indices[2] = i * 3 + 2;
  1076. facesw[i].normal = face.get_plane().normal;
  1077. verticesw[i * 3 + 0] = face.vertex[0];
  1078. verticesw[i * 3 + 1] = face.vertex[1];
  1079. verticesw[i * 3 + 2] = face.vertex[2];
  1080. if (i == 0)
  1081. _aabb = bvh_arrayw[i].aabb;
  1082. else
  1083. _aabb.merge_with(bvh_arrayw[i].aabb);
  1084. }
  1085. w = PoolVector<Face>::Write();
  1086. vw = PoolVector<Vector3>::Write();
  1087. int count = 0;
  1088. _VolumeSW_BVH *bvh_tree = _volume_sw_build_bvh(bvh_arrayw, src_face_count, count);
  1089. bvh.resize(count + 1);
  1090. PoolVector<BVH>::Write bvhw2 = bvh.write();
  1091. BVH *bvh_arrayw2 = bvhw2.ptr();
  1092. int idx = 0;
  1093. _fill_bvh(bvh_tree, bvh_arrayw2, idx);
  1094. configure(_aabb); // this type of shape has no margin
  1095. }
  1096. void ConcavePolygonShapeSW::set_data(const Variant &p_data) {
  1097. _setup(p_data);
  1098. }
  1099. Variant ConcavePolygonShapeSW::get_data() const {
  1100. return get_faces();
  1101. }
  1102. ConcavePolygonShapeSW::ConcavePolygonShapeSW() {
  1103. }
  1104. /* HEIGHT MAP SHAPE */
  1105. PoolVector<real_t> HeightMapShapeSW::get_heights() const {
  1106. return heights;
  1107. }
  1108. int HeightMapShapeSW::get_width() const {
  1109. return width;
  1110. }
  1111. int HeightMapShapeSW::get_depth() const {
  1112. return depth;
  1113. }
  1114. real_t HeightMapShapeSW::get_cell_size() const {
  1115. return cell_size;
  1116. }
  1117. void HeightMapShapeSW::project_range(const Vector3 &p_normal, const Transform &p_transform, real_t &r_min, real_t &r_max) const {
  1118. //not very useful, but not very used either
  1119. p_transform.xform(get_aabb()).project_range_in_plane(Plane(p_normal, 0), r_min, r_max);
  1120. }
  1121. Vector3 HeightMapShapeSW::get_support(const Vector3 &p_normal) const {
  1122. //not very useful, but not very used either
  1123. return get_aabb().get_support(p_normal);
  1124. }
  1125. bool HeightMapShapeSW::intersect_segment(const Vector3 &p_begin, const Vector3 &p_end, Vector3 &r_point, Vector3 &r_normal) const {
  1126. return false;
  1127. }
  1128. bool HeightMapShapeSW::intersect_point(const Vector3 &p_point) const {
  1129. return false;
  1130. }
  1131. Vector3 HeightMapShapeSW::get_closest_point_to(const Vector3 &p_point) const {
  1132. return Vector3();
  1133. }
  1134. void HeightMapShapeSW::cull(const AABB &p_local_aabb, Callback p_callback, void *p_userdata) const {
  1135. }
  1136. Vector3 HeightMapShapeSW::get_moment_of_inertia(real_t p_mass) const {
  1137. // use bad AABB approximation
  1138. Vector3 extents = get_aabb().size * 0.5;
  1139. return Vector3(
  1140. (p_mass / 3.0) * (extents.y * extents.y + extents.z * extents.z),
  1141. (p_mass / 3.0) * (extents.x * extents.x + extents.z * extents.z),
  1142. (p_mass / 3.0) * (extents.y * extents.y + extents.y * extents.y));
  1143. }
  1144. void HeightMapShapeSW::_setup(PoolVector<real_t> p_heights, int p_width, int p_depth, real_t p_cell_size) {
  1145. heights = p_heights;
  1146. width = p_width;
  1147. depth = p_depth;
  1148. cell_size = p_cell_size;
  1149. PoolVector<real_t>::Read r = heights.read();
  1150. AABB aabb;
  1151. for (int i = 0; i < depth; i++) {
  1152. for (int j = 0; j < width; j++) {
  1153. real_t h = r[i * width + j];
  1154. Vector3 pos(j * cell_size, h, i * cell_size);
  1155. if (i == 0 || j == 0)
  1156. aabb.position = pos;
  1157. else
  1158. aabb.expand_to(pos);
  1159. }
  1160. }
  1161. configure(aabb);
  1162. }
  1163. void HeightMapShapeSW::set_data(const Variant &p_data) {
  1164. ERR_FAIL_COND(p_data.get_type() != Variant::DICTIONARY);
  1165. Dictionary d = p_data;
  1166. ERR_FAIL_COND(!d.has("width"));
  1167. ERR_FAIL_COND(!d.has("depth"));
  1168. ERR_FAIL_COND(!d.has("cell_size"));
  1169. ERR_FAIL_COND(!d.has("heights"));
  1170. int width = d["width"];
  1171. int depth = d["depth"];
  1172. real_t cell_size = d["cell_size"];
  1173. PoolVector<real_t> heights = d["heights"];
  1174. ERR_FAIL_COND(width <= 0);
  1175. ERR_FAIL_COND(depth <= 0);
  1176. ERR_FAIL_COND(cell_size <= CMP_EPSILON);
  1177. ERR_FAIL_COND(heights.size() != (width * depth));
  1178. _setup(heights, width, depth, cell_size);
  1179. }
  1180. Variant HeightMapShapeSW::get_data() const {
  1181. ERR_FAIL_V(Variant());
  1182. }
  1183. HeightMapShapeSW::HeightMapShapeSW() {
  1184. width = 0;
  1185. depth = 0;
  1186. cell_size = 0;
  1187. }