shape_2d_sw.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083
  1. /*************************************************************************/
  2. /* shape_2d_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_2d_sw.h"
  31. #include "core/math/geometry.h"
  32. #include "core/sort_array.h"
  33. void Shape2DSW::configure(const Rect2 &p_aabb) {
  34. aabb = p_aabb;
  35. configured = true;
  36. for (Map<ShapeOwner2DSW *, int>::Element *E = owners.front(); E; E = E->next()) {
  37. ShapeOwner2DSW *co = (ShapeOwner2DSW *)E->key();
  38. co->_shape_changed();
  39. }
  40. }
  41. Vector2 Shape2DSW::get_support(const Vector2 &p_normal) const {
  42. Vector2 res[2];
  43. int amnt;
  44. get_supports(p_normal, res, amnt);
  45. return res[0];
  46. }
  47. void Shape2DSW::add_owner(ShapeOwner2DSW *p_owner) {
  48. Map<ShapeOwner2DSW *, int>::Element *E = owners.find(p_owner);
  49. if (E) {
  50. E->get()++;
  51. } else {
  52. owners[p_owner] = 1;
  53. }
  54. }
  55. void Shape2DSW::remove_owner(ShapeOwner2DSW *p_owner) {
  56. Map<ShapeOwner2DSW *, int>::Element *E = owners.find(p_owner);
  57. ERR_FAIL_COND(!E);
  58. E->get()--;
  59. if (E->get() == 0) {
  60. owners.erase(E);
  61. }
  62. }
  63. bool Shape2DSW::is_owner(ShapeOwner2DSW *p_owner) const {
  64. return owners.has(p_owner);
  65. }
  66. const Map<ShapeOwner2DSW *, int> &Shape2DSW::get_owners() const {
  67. return owners;
  68. }
  69. Shape2DSW::Shape2DSW() {
  70. custom_bias = 0;
  71. configured = false;
  72. }
  73. Shape2DSW::~Shape2DSW() {
  74. ERR_FAIL_COND(owners.size());
  75. }
  76. /*********************************************************/
  77. /*********************************************************/
  78. /*********************************************************/
  79. void LineShape2DSW::get_supports(const Vector2 &p_normal, Vector2 *r_supports, int &r_amount) const {
  80. r_amount = 0;
  81. }
  82. bool LineShape2DSW::contains_point(const Vector2 &p_point) const {
  83. return normal.dot(p_point) < d;
  84. }
  85. bool LineShape2DSW::intersect_segment(const Vector2 &p_begin, const Vector2 &p_end, Vector2 &r_point, Vector2 &r_normal) const {
  86. Vector2 segment = p_begin - p_end;
  87. real_t den = normal.dot(segment);
  88. //printf("den is %i\n",den);
  89. if (Math::abs(den) <= CMP_EPSILON) {
  90. return false;
  91. }
  92. real_t dist = (normal.dot(p_begin) - d) / den;
  93. //printf("dist is %i\n",dist);
  94. if (dist < -CMP_EPSILON || dist > (1.0 + CMP_EPSILON)) {
  95. return false;
  96. }
  97. r_point = p_begin + segment * -dist;
  98. r_normal = normal;
  99. return true;
  100. }
  101. real_t LineShape2DSW::get_moment_of_inertia(real_t p_mass, const Size2 &p_scale) const {
  102. return 0;
  103. }
  104. void LineShape2DSW::set_data(const Variant &p_data) {
  105. ERR_FAIL_COND(p_data.get_type() != Variant::ARRAY);
  106. Array arr = p_data;
  107. ERR_FAIL_COND(arr.size() != 2);
  108. normal = arr[0];
  109. d = arr[1];
  110. configure(Rect2(Vector2(-1e4, -1e4), Vector2(1e4 * 2, 1e4 * 2)));
  111. }
  112. Variant LineShape2DSW::get_data() const {
  113. Array arr;
  114. arr.resize(2);
  115. arr[0] = normal;
  116. arr[1] = d;
  117. return arr;
  118. }
  119. /*********************************************************/
  120. /*********************************************************/
  121. /*********************************************************/
  122. void RayShape2DSW::get_supports(const Vector2 &p_normal, Vector2 *r_supports, int &r_amount) const {
  123. r_amount = 1;
  124. if (p_normal.y > 0)
  125. *r_supports = Vector2(0, length);
  126. else
  127. *r_supports = Vector2();
  128. }
  129. bool RayShape2DSW::contains_point(const Vector2 &p_point) const {
  130. return false;
  131. }
  132. bool RayShape2DSW::intersect_segment(const Vector2 &p_begin, const Vector2 &p_end, Vector2 &r_point, Vector2 &r_normal) const {
  133. return false; //rays can't be intersected
  134. }
  135. real_t RayShape2DSW::get_moment_of_inertia(real_t p_mass, const Size2 &p_scale) const {
  136. return 0; //rays are mass-less
  137. }
  138. void RayShape2DSW::set_data(const Variant &p_data) {
  139. Dictionary d = p_data;
  140. length = d["length"];
  141. slips_on_slope = d["slips_on_slope"];
  142. configure(Rect2(0, 0, 0.001, length));
  143. }
  144. Variant RayShape2DSW::get_data() const {
  145. Dictionary d;
  146. d["length"] = length;
  147. d["slips_on_slope"] = slips_on_slope;
  148. return d;
  149. }
  150. /*********************************************************/
  151. /*********************************************************/
  152. /*********************************************************/
  153. void SegmentShape2DSW::get_supports(const Vector2 &p_normal, Vector2 *r_supports, int &r_amount) const {
  154. if (Math::abs(p_normal.dot(n)) > _SEGMENT_IS_VALID_SUPPORT_THRESHOLD) {
  155. r_supports[0] = a;
  156. r_supports[1] = b;
  157. r_amount = 2;
  158. return;
  159. }
  160. real_t dp = p_normal.dot(b - a);
  161. if (dp > 0)
  162. *r_supports = b;
  163. else
  164. *r_supports = a;
  165. r_amount = 1;
  166. }
  167. bool SegmentShape2DSW::contains_point(const Vector2 &p_point) const {
  168. return false;
  169. }
  170. bool SegmentShape2DSW::intersect_segment(const Vector2 &p_begin, const Vector2 &p_end, Vector2 &r_point, Vector2 &r_normal) const {
  171. if (!Geometry::segment_intersects_segment_2d(p_begin, p_end, a, b, &r_point))
  172. return false;
  173. if (n.dot(p_begin) > n.dot(a)) {
  174. r_normal = n;
  175. } else {
  176. r_normal = -n;
  177. }
  178. return true;
  179. }
  180. real_t SegmentShape2DSW::get_moment_of_inertia(real_t p_mass, const Size2 &p_scale) const {
  181. return p_mass * ((a * p_scale).distance_squared_to(b * p_scale)) / 12;
  182. }
  183. void SegmentShape2DSW::set_data(const Variant &p_data) {
  184. ERR_FAIL_COND(p_data.get_type() != Variant::RECT2);
  185. Rect2 r = p_data;
  186. a = r.position;
  187. b = r.size;
  188. n = (b - a).tangent();
  189. Rect2 aabb;
  190. aabb.position = a;
  191. aabb.expand_to(b);
  192. if (aabb.size.x == 0)
  193. aabb.size.x = 0.001;
  194. if (aabb.size.y == 0)
  195. aabb.size.y = 0.001;
  196. configure(aabb);
  197. }
  198. Variant SegmentShape2DSW::get_data() const {
  199. Rect2 r;
  200. r.position = a;
  201. r.size = b;
  202. return r;
  203. }
  204. /*********************************************************/
  205. /*********************************************************/
  206. /*********************************************************/
  207. void CircleShape2DSW::get_supports(const Vector2 &p_normal, Vector2 *r_supports, int &r_amount) const {
  208. r_amount = 1;
  209. *r_supports = p_normal * radius;
  210. }
  211. bool CircleShape2DSW::contains_point(const Vector2 &p_point) const {
  212. return p_point.length_squared() < radius * radius;
  213. }
  214. bool CircleShape2DSW::intersect_segment(const Vector2 &p_begin, const Vector2 &p_end, Vector2 &r_point, Vector2 &r_normal) const {
  215. Vector2 line_vec = p_end - p_begin;
  216. real_t a, b, c;
  217. a = line_vec.dot(line_vec);
  218. b = 2 * p_begin.dot(line_vec);
  219. c = p_begin.dot(p_begin) - radius * radius;
  220. real_t sqrtterm = b * b - 4 * a * c;
  221. if (sqrtterm < 0)
  222. return false;
  223. sqrtterm = Math::sqrt(sqrtterm);
  224. real_t res = (-b - sqrtterm) / (2 * a);
  225. if (res < 0 || res > 1 + CMP_EPSILON) {
  226. return false;
  227. }
  228. r_point = p_begin + line_vec * res;
  229. r_normal = r_point.normalized();
  230. return true;
  231. }
  232. real_t CircleShape2DSW::get_moment_of_inertia(real_t p_mass, const Size2 &p_scale) const {
  233. real_t a = radius * p_scale.x;
  234. real_t b = radius * p_scale.y;
  235. return p_mass * (a * a + b * b) / 4;
  236. }
  237. void CircleShape2DSW::set_data(const Variant &p_data) {
  238. ERR_FAIL_COND(!p_data.is_num());
  239. radius = p_data;
  240. configure(Rect2(-radius, -radius, radius * 2, radius * 2));
  241. }
  242. Variant CircleShape2DSW::get_data() const {
  243. return radius;
  244. }
  245. /*********************************************************/
  246. /*********************************************************/
  247. /*********************************************************/
  248. void RectangleShape2DSW::get_supports(const Vector2 &p_normal, Vector2 *r_supports, int &r_amount) const {
  249. for (int i = 0; i < 2; i++) {
  250. Vector2 ag;
  251. ag[i] = 1.0;
  252. real_t dp = ag.dot(p_normal);
  253. if (Math::abs(dp) < _SEGMENT_IS_VALID_SUPPORT_THRESHOLD)
  254. continue;
  255. real_t sgn = dp > 0 ? 1.0 : -1.0;
  256. r_amount = 2;
  257. r_supports[0][i] = half_extents[i] * sgn;
  258. r_supports[0][i ^ 1] = half_extents[i ^ 1];
  259. r_supports[1][i] = half_extents[i] * sgn;
  260. r_supports[1][i ^ 1] = -half_extents[i ^ 1];
  261. return;
  262. }
  263. /* USE POINT */
  264. r_amount = 1;
  265. r_supports[0] = Vector2(
  266. (p_normal.x < 0) ? -half_extents.x : half_extents.x,
  267. (p_normal.y < 0) ? -half_extents.y : half_extents.y);
  268. }
  269. bool RectangleShape2DSW::contains_point(const Vector2 &p_point) const {
  270. float x = p_point.x;
  271. float y = p_point.y;
  272. float edge_x = half_extents.x;
  273. float edge_y = half_extents.y;
  274. return (x >= -edge_x) && (x < edge_x) && (y >= -edge_y) && (y < edge_y);
  275. }
  276. bool RectangleShape2DSW::intersect_segment(const Vector2 &p_begin, const Vector2 &p_end, Vector2 &r_point, Vector2 &r_normal) const {
  277. return get_aabb().intersects_segment(p_begin, p_end, &r_point, &r_normal);
  278. }
  279. real_t RectangleShape2DSW::get_moment_of_inertia(real_t p_mass, const Size2 &p_scale) const {
  280. Vector2 he2 = half_extents * 2 * p_scale;
  281. return p_mass * he2.dot(he2) / 12.0;
  282. }
  283. void RectangleShape2DSW::set_data(const Variant &p_data) {
  284. ERR_FAIL_COND(p_data.get_type() != Variant::VECTOR2);
  285. half_extents = p_data;
  286. configure(Rect2(-half_extents, half_extents * 2.0));
  287. }
  288. Variant RectangleShape2DSW::get_data() const {
  289. return half_extents;
  290. }
  291. /*********************************************************/
  292. /*********************************************************/
  293. /*********************************************************/
  294. void CapsuleShape2DSW::get_supports(const Vector2 &p_normal, Vector2 *r_supports, int &r_amount) const {
  295. Vector2 n = p_normal;
  296. real_t d = n.y;
  297. if (Math::abs(d) < (1.0 - _SEGMENT_IS_VALID_SUPPORT_THRESHOLD)) {
  298. // make it flat
  299. n.y = 0.0;
  300. n.normalize();
  301. n *= radius;
  302. r_amount = 2;
  303. r_supports[0] = n;
  304. r_supports[0].y += height * 0.5;
  305. r_supports[1] = n;
  306. r_supports[1].y -= height * 0.5;
  307. } else {
  308. real_t h = (d > 0) ? height : -height;
  309. n *= radius;
  310. n.y += h * 0.5;
  311. r_amount = 1;
  312. *r_supports = n;
  313. }
  314. }
  315. bool CapsuleShape2DSW::contains_point(const Vector2 &p_point) const {
  316. Vector2 p = p_point;
  317. p.y = Math::abs(p.y);
  318. p.y -= height * 0.5;
  319. if (p.y < 0)
  320. p.y = 0;
  321. return p.length_squared() < radius * radius;
  322. }
  323. bool CapsuleShape2DSW::intersect_segment(const Vector2 &p_begin, const Vector2 &p_end, Vector2 &r_point, Vector2 &r_normal) const {
  324. real_t d = 1e10;
  325. Vector2 n = (p_end - p_begin).normalized();
  326. bool collided = false;
  327. //try spheres
  328. for (int i = 0; i < 2; i++) {
  329. Vector2 begin = p_begin;
  330. Vector2 end = p_end;
  331. real_t ofs = (i == 0) ? -height * 0.5 : height * 0.5;
  332. begin.y += ofs;
  333. end.y += ofs;
  334. Vector2 line_vec = end - begin;
  335. real_t a, b, c;
  336. a = line_vec.dot(line_vec);
  337. b = 2 * begin.dot(line_vec);
  338. c = begin.dot(begin) - radius * radius;
  339. real_t sqrtterm = b * b - 4 * a * c;
  340. if (sqrtterm < 0)
  341. continue;
  342. sqrtterm = Math::sqrt(sqrtterm);
  343. real_t res = (-b - sqrtterm) / (2 * a);
  344. if (res < 0 || res > 1 + CMP_EPSILON) {
  345. continue;
  346. }
  347. Vector2 point = begin + line_vec * res;
  348. Vector2 pointf(point.x, point.y - ofs);
  349. real_t pd = n.dot(pointf);
  350. if (pd < d) {
  351. r_point = pointf;
  352. r_normal = point.normalized();
  353. d = pd;
  354. collided = true;
  355. }
  356. }
  357. Vector2 rpos, rnorm;
  358. if (Rect2(Point2(-radius, -height * 0.5), Size2(radius * 2.0, height)).intersects_segment(p_begin, p_end, &rpos, &rnorm)) {
  359. real_t pd = n.dot(rpos);
  360. if (pd < d) {
  361. r_point = rpos;
  362. r_normal = rnorm;
  363. d = pd;
  364. collided = true;
  365. }
  366. }
  367. //return get_aabb().intersects_segment(p_begin,p_end,&r_point,&r_normal);
  368. return collided; //todo
  369. }
  370. real_t CapsuleShape2DSW::get_moment_of_inertia(real_t p_mass, const Size2 &p_scale) const {
  371. Vector2 he2 = Vector2(radius * 2, height + radius * 2) * p_scale;
  372. return p_mass * he2.dot(he2) / 12.0;
  373. }
  374. void CapsuleShape2DSW::set_data(const Variant &p_data) {
  375. ERR_FAIL_COND(p_data.get_type() != Variant::ARRAY && p_data.get_type() != Variant::VECTOR2);
  376. if (p_data.get_type() == Variant::ARRAY) {
  377. Array arr = p_data;
  378. ERR_FAIL_COND(arr.size() != 2);
  379. height = arr[0];
  380. radius = arr[1];
  381. } else {
  382. Point2 p = p_data;
  383. radius = p.x;
  384. height = p.y;
  385. }
  386. Point2 he(radius, height * 0.5 + radius);
  387. configure(Rect2(-he, he * 2));
  388. }
  389. Variant CapsuleShape2DSW::get_data() const {
  390. return Point2(height, radius);
  391. }
  392. /*********************************************************/
  393. /*********************************************************/
  394. /*********************************************************/
  395. void ConvexPolygonShape2DSW::get_supports(const Vector2 &p_normal, Vector2 *r_supports, int &r_amount) const {
  396. int support_idx = -1;
  397. real_t d = -1e10;
  398. for (int i = 0; i < point_count; i++) {
  399. //test point
  400. real_t ld = p_normal.dot(points[i].pos);
  401. if (ld > d) {
  402. support_idx = i;
  403. d = ld;
  404. }
  405. //test segment
  406. if (points[i].normal.dot(p_normal) > _SEGMENT_IS_VALID_SUPPORT_THRESHOLD) {
  407. r_amount = 2;
  408. r_supports[0] = points[i].pos;
  409. r_supports[1] = points[(i + 1) % point_count].pos;
  410. return;
  411. }
  412. }
  413. ERR_FAIL_COND(support_idx == -1);
  414. r_amount = 1;
  415. r_supports[0] = points[support_idx].pos;
  416. }
  417. bool ConvexPolygonShape2DSW::contains_point(const Vector2 &p_point) const {
  418. bool out = false;
  419. bool in = false;
  420. for (int i = 0; i < point_count; i++) {
  421. real_t d = points[i].normal.dot(p_point) - points[i].normal.dot(points[i].pos);
  422. if (d > 0)
  423. out = true;
  424. else
  425. in = true;
  426. }
  427. return in != out;
  428. }
  429. bool ConvexPolygonShape2DSW::intersect_segment(const Vector2 &p_begin, const Vector2 &p_end, Vector2 &r_point, Vector2 &r_normal) const {
  430. Vector2 n = (p_end - p_begin).normalized();
  431. real_t d = 1e10;
  432. bool inters = false;
  433. for (int i = 0; i < point_count; i++) {
  434. //hmm.. no can do..
  435. /*
  436. if (d.dot(points[i].normal)>=0)
  437. continue;
  438. */
  439. Vector2 res;
  440. if (!Geometry::segment_intersects_segment_2d(p_begin, p_end, points[i].pos, points[(i + 1) % point_count].pos, &res))
  441. continue;
  442. real_t nd = n.dot(res);
  443. if (nd < d) {
  444. d = nd;
  445. r_point = res;
  446. r_normal = points[i].normal;
  447. inters = true;
  448. }
  449. }
  450. if (inters) {
  451. if (n.dot(r_normal) > 0)
  452. r_normal = -r_normal;
  453. }
  454. //return get_aabb().intersects_segment(p_begin,p_end,&r_point,&r_normal);
  455. return inters; //todo
  456. }
  457. real_t ConvexPolygonShape2DSW::get_moment_of_inertia(real_t p_mass, const Size2 &p_scale) const {
  458. Rect2 aabb;
  459. aabb.position = points[0].pos * p_scale;
  460. for (int i = 0; i < point_count; i++) {
  461. aabb.expand_to(points[i].pos * p_scale);
  462. }
  463. return p_mass * aabb.size.dot(aabb.size) / 12.0;
  464. }
  465. void ConvexPolygonShape2DSW::set_data(const Variant &p_data) {
  466. ERR_FAIL_COND(p_data.get_type() != Variant::POOL_VECTOR2_ARRAY && p_data.get_type() != Variant::POOL_REAL_ARRAY);
  467. if (points)
  468. memdelete_arr(points);
  469. points = NULL;
  470. point_count = 0;
  471. if (p_data.get_type() == Variant::POOL_VECTOR2_ARRAY) {
  472. PoolVector<Vector2> arr = p_data;
  473. ERR_FAIL_COND(arr.size() == 0);
  474. point_count = arr.size();
  475. points = memnew_arr(Point, point_count);
  476. PoolVector<Vector2>::Read r = arr.read();
  477. for (int i = 0; i < point_count; i++) {
  478. points[i].pos = r[i];
  479. }
  480. for (int i = 0; i < point_count; i++) {
  481. Vector2 p = points[i].pos;
  482. Vector2 pn = points[(i + 1) % point_count].pos;
  483. points[i].normal = (pn - p).tangent().normalized();
  484. }
  485. } else {
  486. PoolVector<real_t> dvr = p_data;
  487. point_count = dvr.size() / 4;
  488. ERR_FAIL_COND(point_count == 0);
  489. points = memnew_arr(Point, point_count);
  490. PoolVector<real_t>::Read r = dvr.read();
  491. for (int i = 0; i < point_count; i++) {
  492. int idx = i << 2;
  493. points[i].pos.x = r[idx + 0];
  494. points[i].pos.y = r[idx + 1];
  495. points[i].normal.x = r[idx + 2];
  496. points[i].normal.y = r[idx + 3];
  497. }
  498. }
  499. ERR_FAIL_COND(point_count == 0);
  500. Rect2 aabb;
  501. aabb.position = points[0].pos;
  502. for (int i = 1; i < point_count; i++)
  503. aabb.expand_to(points[i].pos);
  504. configure(aabb);
  505. }
  506. Variant ConvexPolygonShape2DSW::get_data() const {
  507. PoolVector<Vector2> dvr;
  508. dvr.resize(point_count);
  509. for (int i = 0; i < point_count; i++) {
  510. dvr.set(i, points[i].pos);
  511. }
  512. return dvr;
  513. }
  514. ConvexPolygonShape2DSW::ConvexPolygonShape2DSW() {
  515. points = NULL;
  516. point_count = 0;
  517. }
  518. ConvexPolygonShape2DSW::~ConvexPolygonShape2DSW() {
  519. if (points)
  520. memdelete_arr(points);
  521. }
  522. //////////////////////////////////////////////////
  523. void ConcavePolygonShape2DSW::get_supports(const Vector2 &p_normal, Vector2 *r_supports, int &r_amount) const {
  524. real_t d = -1e10;
  525. int idx = -1;
  526. for (int i = 0; i < points.size(); i++) {
  527. real_t ld = p_normal.dot(points[i]);
  528. if (ld > d) {
  529. d = ld;
  530. idx = i;
  531. }
  532. }
  533. r_amount = 1;
  534. ERR_FAIL_COND(idx == -1);
  535. *r_supports = points[idx];
  536. }
  537. bool ConcavePolygonShape2DSW::contains_point(const Vector2 &p_point) const {
  538. return false; //sorry
  539. }
  540. bool ConcavePolygonShape2DSW::intersect_segment(const Vector2 &p_begin, const Vector2 &p_end, Vector2 &r_point, Vector2 &r_normal) const {
  541. uint32_t *stack = (uint32_t *)alloca(sizeof(int) * bvh_depth);
  542. enum {
  543. TEST_AABB_BIT = 0,
  544. VISIT_LEFT_BIT = 1,
  545. VISIT_RIGHT_BIT = 2,
  546. VISIT_DONE_BIT = 3,
  547. VISITED_BIT_SHIFT = 29,
  548. NODE_IDX_MASK = (1 << VISITED_BIT_SHIFT) - 1,
  549. VISITED_BIT_MASK = ~NODE_IDX_MASK,
  550. };
  551. Vector2 n = (p_end - p_begin).normalized();
  552. real_t d = 1e10;
  553. bool inters = false;
  554. /*
  555. for(int i=0;i<bvh_depth;i++)
  556. stack[i]=0;
  557. */
  558. int level = 0;
  559. const Segment *segmentptr = &segments[0];
  560. const Vector2 *pointptr = &points[0];
  561. const BVH *bvhptr = &bvh[0];
  562. stack[0] = 0;
  563. while (true) {
  564. uint32_t node = stack[level] & NODE_IDX_MASK;
  565. const BVH &bvh = bvhptr[node];
  566. bool done = false;
  567. switch (stack[level] >> VISITED_BIT_SHIFT) {
  568. case TEST_AABB_BIT: {
  569. bool valid = bvh.aabb.intersects_segment(p_begin, p_end);
  570. if (!valid) {
  571. stack[level] = (VISIT_DONE_BIT << VISITED_BIT_SHIFT) | node;
  572. } else {
  573. if (bvh.left < 0) {
  574. const Segment &s = segmentptr[bvh.right];
  575. Vector2 a = pointptr[s.points[0]];
  576. Vector2 b = pointptr[s.points[1]];
  577. Vector2 res;
  578. if (Geometry::segment_intersects_segment_2d(p_begin, p_end, a, b, &res)) {
  579. real_t nd = n.dot(res);
  580. if (nd < d) {
  581. d = nd;
  582. r_point = res;
  583. r_normal = (b - a).tangent().normalized();
  584. inters = true;
  585. }
  586. }
  587. stack[level] = (VISIT_DONE_BIT << VISITED_BIT_SHIFT) | node;
  588. } else {
  589. stack[level] = (VISIT_LEFT_BIT << VISITED_BIT_SHIFT) | node;
  590. }
  591. }
  592. }
  593. continue;
  594. case VISIT_LEFT_BIT: {
  595. stack[level] = (VISIT_RIGHT_BIT << VISITED_BIT_SHIFT) | node;
  596. stack[level + 1] = bvh.left | TEST_AABB_BIT;
  597. level++;
  598. }
  599. continue;
  600. case VISIT_RIGHT_BIT: {
  601. stack[level] = (VISIT_DONE_BIT << VISITED_BIT_SHIFT) | node;
  602. stack[level + 1] = bvh.right | TEST_AABB_BIT;
  603. level++;
  604. }
  605. continue;
  606. case VISIT_DONE_BIT: {
  607. if (level == 0) {
  608. done = true;
  609. break;
  610. } else
  611. level--;
  612. }
  613. continue;
  614. }
  615. if (done)
  616. break;
  617. }
  618. if (inters) {
  619. if (n.dot(r_normal) > 0)
  620. r_normal = -r_normal;
  621. }
  622. return inters;
  623. }
  624. int ConcavePolygonShape2DSW::_generate_bvh(BVH *p_bvh, int p_len, int p_depth) {
  625. if (p_len == 1) {
  626. bvh_depth = MAX(p_depth, bvh_depth);
  627. bvh.push_back(*p_bvh);
  628. return bvh.size() - 1;
  629. }
  630. //else sort best
  631. Rect2 global_aabb = p_bvh[0].aabb;
  632. for (int i = 1; i < p_len; i++) {
  633. global_aabb = global_aabb.merge(p_bvh[i].aabb);
  634. }
  635. if (global_aabb.size.x > global_aabb.size.y) {
  636. SortArray<BVH, BVH_CompareX> sort;
  637. sort.sort(p_bvh, p_len);
  638. } else {
  639. SortArray<BVH, BVH_CompareY> sort;
  640. sort.sort(p_bvh, p_len);
  641. }
  642. int median = p_len / 2;
  643. BVH node;
  644. node.aabb = global_aabb;
  645. int node_idx = bvh.size();
  646. bvh.push_back(node);
  647. int l = _generate_bvh(p_bvh, median, p_depth + 1);
  648. int r = _generate_bvh(&p_bvh[median], p_len - median, p_depth + 1);
  649. bvh.write[node_idx].left = l;
  650. bvh.write[node_idx].right = r;
  651. return node_idx;
  652. }
  653. void ConcavePolygonShape2DSW::set_data(const Variant &p_data) {
  654. ERR_FAIL_COND(p_data.get_type() != Variant::POOL_VECTOR2_ARRAY && p_data.get_type() != Variant::POOL_REAL_ARRAY);
  655. Rect2 aabb;
  656. if (p_data.get_type() == Variant::POOL_VECTOR2_ARRAY) {
  657. PoolVector<Vector2> p2arr = p_data;
  658. int len = p2arr.size();
  659. ERR_FAIL_COND(len % 2);
  660. segments.clear();
  661. points.clear();
  662. bvh.clear();
  663. bvh_depth = 1;
  664. if (len == 0) {
  665. configure(aabb);
  666. return;
  667. }
  668. PoolVector<Vector2>::Read arr = p2arr.read();
  669. Map<Point2, int> pointmap;
  670. for (int i = 0; i < len; i += 2) {
  671. Point2 p1 = arr[i];
  672. Point2 p2 = arr[i + 1];
  673. int idx_p1, idx_p2;
  674. if (pointmap.has(p1)) {
  675. idx_p1 = pointmap[p1];
  676. } else {
  677. idx_p1 = pointmap.size();
  678. pointmap[p1] = idx_p1;
  679. }
  680. if (pointmap.has(p2)) {
  681. idx_p2 = pointmap[p2];
  682. } else {
  683. idx_p2 = pointmap.size();
  684. pointmap[p2] = idx_p2;
  685. }
  686. Segment s;
  687. s.points[0] = idx_p1;
  688. s.points[1] = idx_p2;
  689. segments.push_back(s);
  690. }
  691. points.resize(pointmap.size());
  692. aabb.position = pointmap.front()->key();
  693. for (Map<Point2, int>::Element *E = pointmap.front(); E; E = E->next()) {
  694. aabb.expand_to(E->key());
  695. points.write[E->get()] = E->key();
  696. }
  697. Vector<BVH> main_vbh;
  698. main_vbh.resize(segments.size());
  699. for (int i = 0; i < main_vbh.size(); i++) {
  700. main_vbh.write[i].aabb.position = points[segments[i].points[0]];
  701. main_vbh.write[i].aabb.expand_to(points[segments[i].points[1]]);
  702. main_vbh.write[i].left = -1;
  703. main_vbh.write[i].right = i;
  704. }
  705. _generate_bvh(main_vbh.ptrw(), main_vbh.size(), 1);
  706. } else {
  707. //dictionary with arrays
  708. }
  709. configure(aabb);
  710. }
  711. Variant ConcavePolygonShape2DSW::get_data() const {
  712. PoolVector<Vector2> rsegments;
  713. int len = segments.size();
  714. rsegments.resize(len * 2);
  715. PoolVector<Vector2>::Write w = rsegments.write();
  716. for (int i = 0; i < len; i++) {
  717. w[(i << 1) + 0] = points[segments[i].points[0]];
  718. w[(i << 1) + 1] = points[segments[i].points[1]];
  719. }
  720. w.release();
  721. return rsegments;
  722. }
  723. void ConcavePolygonShape2DSW::cull(const Rect2 &p_local_aabb, Callback p_callback, void *p_userdata) const {
  724. uint32_t *stack = (uint32_t *)alloca(sizeof(int) * bvh_depth);
  725. enum {
  726. TEST_AABB_BIT = 0,
  727. VISIT_LEFT_BIT = 1,
  728. VISIT_RIGHT_BIT = 2,
  729. VISIT_DONE_BIT = 3,
  730. VISITED_BIT_SHIFT = 29,
  731. NODE_IDX_MASK = (1 << VISITED_BIT_SHIFT) - 1,
  732. VISITED_BIT_MASK = ~NODE_IDX_MASK,
  733. };
  734. /*
  735. for(int i=0;i<bvh_depth;i++)
  736. stack[i]=0;
  737. */
  738. if (segments.size() == 0 || points.size() == 0 || bvh.size() == 0) {
  739. return;
  740. }
  741. int level = 0;
  742. const Segment *segmentptr = &segments[0];
  743. const Vector2 *pointptr = &points[0];
  744. const BVH *bvhptr = &bvh[0];
  745. stack[0] = 0;
  746. while (true) {
  747. uint32_t node = stack[level] & NODE_IDX_MASK;
  748. const BVH &bvh = bvhptr[node];
  749. switch (stack[level] >> VISITED_BIT_SHIFT) {
  750. case TEST_AABB_BIT: {
  751. bool valid = p_local_aabb.intersects(bvh.aabb);
  752. if (!valid) {
  753. stack[level] = (VISIT_DONE_BIT << VISITED_BIT_SHIFT) | node;
  754. } else {
  755. if (bvh.left < 0) {
  756. const Segment &s = segmentptr[bvh.right];
  757. Vector2 a = pointptr[s.points[0]];
  758. Vector2 b = pointptr[s.points[1]];
  759. SegmentShape2DSW ss(a, b, (b - a).tangent().normalized());
  760. p_callback(p_userdata, &ss);
  761. stack[level] = (VISIT_DONE_BIT << VISITED_BIT_SHIFT) | node;
  762. } else {
  763. stack[level] = (VISIT_LEFT_BIT << VISITED_BIT_SHIFT) | node;
  764. }
  765. }
  766. }
  767. continue;
  768. case VISIT_LEFT_BIT: {
  769. stack[level] = (VISIT_RIGHT_BIT << VISITED_BIT_SHIFT) | node;
  770. stack[level + 1] = bvh.left | TEST_AABB_BIT;
  771. level++;
  772. }
  773. continue;
  774. case VISIT_RIGHT_BIT: {
  775. stack[level] = (VISIT_DONE_BIT << VISITED_BIT_SHIFT) | node;
  776. stack[level + 1] = bvh.right | TEST_AABB_BIT;
  777. level++;
  778. }
  779. continue;
  780. case VISIT_DONE_BIT: {
  781. if (level == 0)
  782. return;
  783. else
  784. level--;
  785. }
  786. continue;
  787. }
  788. }
  789. }