godot_shape_2d.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986
  1. /**************************************************************************/
  2. /* godot_shape_2d.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "godot_shape_2d.h"
  31. #include "core/math/geometry_2d.h"
  32. #include "core/templates/sort_array.h"
  33. void GodotShape2D::configure(const Rect2 &p_aabb) {
  34. aabb = p_aabb;
  35. configured = true;
  36. for (const KeyValue<GodotShapeOwner2D *, int> &E : owners) {
  37. GodotShapeOwner2D *co = const_cast<GodotShapeOwner2D *>(E.key);
  38. co->_shape_changed();
  39. }
  40. }
  41. Vector2 GodotShape2D::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 GodotShape2D::add_owner(GodotShapeOwner2D *p_owner) {
  48. HashMap<GodotShapeOwner2D *, int>::Iterator E = owners.find(p_owner);
  49. if (E) {
  50. E->value++;
  51. } else {
  52. owners[p_owner] = 1;
  53. }
  54. }
  55. void GodotShape2D::remove_owner(GodotShapeOwner2D *p_owner) {
  56. HashMap<GodotShapeOwner2D *, int>::Iterator E = owners.find(p_owner);
  57. ERR_FAIL_COND(!E);
  58. E->value--;
  59. if (E->value == 0) {
  60. owners.remove(E);
  61. }
  62. }
  63. bool GodotShape2D::is_owner(GodotShapeOwner2D *p_owner) const {
  64. return owners.has(p_owner);
  65. }
  66. const HashMap<GodotShapeOwner2D *, int> &GodotShape2D::get_owners() const {
  67. return owners;
  68. }
  69. GodotShape2D::~GodotShape2D() {
  70. ERR_FAIL_COND(owners.size());
  71. }
  72. /*********************************************************/
  73. /*********************************************************/
  74. /*********************************************************/
  75. void GodotWorldBoundaryShape2D::get_supports(const Vector2 &p_normal, Vector2 *r_supports, int &r_amount) const {
  76. r_amount = 0;
  77. }
  78. bool GodotWorldBoundaryShape2D::contains_point(const Vector2 &p_point) const {
  79. return normal.dot(p_point) < d;
  80. }
  81. bool GodotWorldBoundaryShape2D::intersect_segment(const Vector2 &p_begin, const Vector2 &p_end, Vector2 &r_point, Vector2 &r_normal) const {
  82. Vector2 segment = p_begin - p_end;
  83. real_t den = normal.dot(segment);
  84. //printf("den is %i\n",den);
  85. if (Math::abs(den) <= CMP_EPSILON) {
  86. return false;
  87. }
  88. real_t dist = (normal.dot(p_begin) - d) / den;
  89. //printf("dist is %i\n",dist);
  90. if (dist < -CMP_EPSILON || dist > (1.0 + CMP_EPSILON)) {
  91. return false;
  92. }
  93. r_point = p_begin + segment * -dist;
  94. r_normal = normal;
  95. return true;
  96. }
  97. real_t GodotWorldBoundaryShape2D::get_moment_of_inertia(real_t p_mass, const Size2 &p_scale) const {
  98. return 0;
  99. }
  100. void GodotWorldBoundaryShape2D::set_data(const Variant &p_data) {
  101. ERR_FAIL_COND(p_data.get_type() != Variant::ARRAY);
  102. Array arr = p_data;
  103. ERR_FAIL_COND(arr.size() != 2);
  104. normal = arr[0];
  105. d = arr[1];
  106. configure(Rect2(Vector2(-1e15, -1e15), Vector2(1e15 * 2, 1e15 * 2)));
  107. }
  108. Variant GodotWorldBoundaryShape2D::get_data() const {
  109. Array arr;
  110. arr.resize(2);
  111. arr[0] = normal;
  112. arr[1] = d;
  113. return arr;
  114. }
  115. /*********************************************************/
  116. /*********************************************************/
  117. /*********************************************************/
  118. void GodotSeparationRayShape2D::get_supports(const Vector2 &p_normal, Vector2 *r_supports, int &r_amount) const {
  119. r_amount = 1;
  120. if (p_normal.y > 0) {
  121. *r_supports = Vector2(0, length);
  122. } else {
  123. *r_supports = Vector2();
  124. }
  125. }
  126. bool GodotSeparationRayShape2D::contains_point(const Vector2 &p_point) const {
  127. return false;
  128. }
  129. bool GodotSeparationRayShape2D::intersect_segment(const Vector2 &p_begin, const Vector2 &p_end, Vector2 &r_point, Vector2 &r_normal) const {
  130. return false; //rays can't be intersected
  131. }
  132. real_t GodotSeparationRayShape2D::get_moment_of_inertia(real_t p_mass, const Size2 &p_scale) const {
  133. return 0; //rays are mass-less
  134. }
  135. void GodotSeparationRayShape2D::set_data(const Variant &p_data) {
  136. Dictionary d = p_data;
  137. length = d["length"];
  138. slide_on_slope = d["slide_on_slope"];
  139. configure(Rect2(0, 0, 0.001, length));
  140. }
  141. Variant GodotSeparationRayShape2D::get_data() const {
  142. Dictionary d;
  143. d["length"] = length;
  144. d["slide_on_slope"] = slide_on_slope;
  145. return d;
  146. }
  147. /*********************************************************/
  148. /*********************************************************/
  149. /*********************************************************/
  150. void GodotSegmentShape2D::get_supports(const Vector2 &p_normal, Vector2 *r_supports, int &r_amount) const {
  151. if (Math::abs(p_normal.dot(n)) > segment_is_valid_support_threshold) {
  152. r_supports[0] = a;
  153. r_supports[1] = b;
  154. r_amount = 2;
  155. return;
  156. }
  157. real_t dp = p_normal.dot(b - a);
  158. if (dp > 0) {
  159. *r_supports = b;
  160. } else {
  161. *r_supports = a;
  162. }
  163. r_amount = 1;
  164. }
  165. bool GodotSegmentShape2D::contains_point(const Vector2 &p_point) const {
  166. return false;
  167. }
  168. bool GodotSegmentShape2D::intersect_segment(const Vector2 &p_begin, const Vector2 &p_end, Vector2 &r_point, Vector2 &r_normal) const {
  169. if (!Geometry2D::segment_intersects_segment(p_begin, p_end, a, b, &r_point)) {
  170. return false;
  171. }
  172. if (n.dot(p_begin) > n.dot(a)) {
  173. r_normal = n;
  174. } else {
  175. r_normal = -n;
  176. }
  177. return true;
  178. }
  179. real_t GodotSegmentShape2D::get_moment_of_inertia(real_t p_mass, const Size2 &p_scale) const {
  180. return p_mass * ((a * p_scale).distance_squared_to(b * p_scale)) / 12;
  181. }
  182. void GodotSegmentShape2D::set_data(const Variant &p_data) {
  183. ERR_FAIL_COND(p_data.get_type() != Variant::RECT2);
  184. Rect2 r = p_data;
  185. a = r.position;
  186. b = r.size;
  187. n = (b - a).orthogonal();
  188. Rect2 aabb_new;
  189. aabb_new.position = a;
  190. aabb_new.expand_to(b);
  191. if (aabb_new.size.x == 0) {
  192. aabb_new.size.x = 0.001;
  193. }
  194. if (aabb_new.size.y == 0) {
  195. aabb_new.size.y = 0.001;
  196. }
  197. configure(aabb_new);
  198. }
  199. Variant GodotSegmentShape2D::get_data() const {
  200. Rect2 r;
  201. r.position = a;
  202. r.size = b;
  203. return r;
  204. }
  205. /*********************************************************/
  206. /*********************************************************/
  207. /*********************************************************/
  208. void GodotCircleShape2D::get_supports(const Vector2 &p_normal, Vector2 *r_supports, int &r_amount) const {
  209. r_amount = 1;
  210. *r_supports = p_normal * radius;
  211. }
  212. bool GodotCircleShape2D::contains_point(const Vector2 &p_point) const {
  213. return p_point.length_squared() < radius * radius;
  214. }
  215. bool GodotCircleShape2D::intersect_segment(const Vector2 &p_begin, const Vector2 &p_end, Vector2 &r_point, Vector2 &r_normal) const {
  216. Vector2 line_vec = p_end - p_begin;
  217. real_t a, b, c;
  218. a = line_vec.dot(line_vec);
  219. b = 2 * p_begin.dot(line_vec);
  220. c = p_begin.dot(p_begin) - radius * radius;
  221. real_t sqrtterm = b * b - 4 * a * c;
  222. if (sqrtterm < 0) {
  223. return false;
  224. }
  225. sqrtterm = Math::sqrt(sqrtterm);
  226. real_t res = (-b - sqrtterm) / (2 * a);
  227. if (res < 0 || res > 1 + CMP_EPSILON) {
  228. return false;
  229. }
  230. r_point = p_begin + line_vec * res;
  231. r_normal = r_point.normalized();
  232. return true;
  233. }
  234. real_t GodotCircleShape2D::get_moment_of_inertia(real_t p_mass, const Size2 &p_scale) const {
  235. real_t a = radius * p_scale.x;
  236. real_t b = radius * p_scale.y;
  237. return p_mass * (a * a + b * b) / 4;
  238. }
  239. void GodotCircleShape2D::set_data(const Variant &p_data) {
  240. ERR_FAIL_COND(!p_data.is_num());
  241. radius = p_data;
  242. configure(Rect2(-radius, -radius, radius * 2, radius * 2));
  243. }
  244. Variant GodotCircleShape2D::get_data() const {
  245. return radius;
  246. }
  247. /*********************************************************/
  248. /*********************************************************/
  249. /*********************************************************/
  250. void GodotRectangleShape2D::get_supports(const Vector2 &p_normal, Vector2 *r_supports, int &r_amount) const {
  251. for (int i = 0; i < 2; i++) {
  252. Vector2 ag;
  253. ag[i] = 1.0;
  254. real_t dp = ag.dot(p_normal);
  255. if (Math::abs(dp) <= segment_is_valid_support_threshold) {
  256. continue;
  257. }
  258. real_t sgn = dp > 0 ? 1.0 : -1.0;
  259. r_amount = 2;
  260. r_supports[0][i] = half_extents[i] * sgn;
  261. r_supports[0][i ^ 1] = half_extents[i ^ 1];
  262. r_supports[1][i] = half_extents[i] * sgn;
  263. r_supports[1][i ^ 1] = -half_extents[i ^ 1];
  264. return;
  265. }
  266. /* USE POINT */
  267. r_amount = 1;
  268. r_supports[0] = Vector2(
  269. (p_normal.x < 0) ? -half_extents.x : half_extents.x,
  270. (p_normal.y < 0) ? -half_extents.y : half_extents.y);
  271. }
  272. bool GodotRectangleShape2D::contains_point(const Vector2 &p_point) const {
  273. real_t x = p_point.x;
  274. real_t y = p_point.y;
  275. real_t edge_x = half_extents.x;
  276. real_t edge_y = half_extents.y;
  277. return (x >= -edge_x) && (x < edge_x) && (y >= -edge_y) && (y < edge_y);
  278. }
  279. bool GodotRectangleShape2D::intersect_segment(const Vector2 &p_begin, const Vector2 &p_end, Vector2 &r_point, Vector2 &r_normal) const {
  280. return get_aabb().intersects_segment(p_begin, p_end, &r_point, &r_normal);
  281. }
  282. real_t GodotRectangleShape2D::get_moment_of_inertia(real_t p_mass, const Size2 &p_scale) const {
  283. Vector2 he2 = half_extents * 2 * p_scale;
  284. return p_mass * he2.dot(he2) / 12.0;
  285. }
  286. void GodotRectangleShape2D::set_data(const Variant &p_data) {
  287. ERR_FAIL_COND(p_data.get_type() != Variant::VECTOR2);
  288. half_extents = p_data;
  289. configure(Rect2(-half_extents, half_extents * 2.0));
  290. }
  291. Variant GodotRectangleShape2D::get_data() const {
  292. return half_extents;
  293. }
  294. /*********************************************************/
  295. /*********************************************************/
  296. /*********************************************************/
  297. void GodotCapsuleShape2D::get_supports(const Vector2 &p_normal, Vector2 *r_supports, int &r_amount) const {
  298. Vector2 n = p_normal;
  299. real_t h = height * 0.5 - radius; // half-height of the rectangle part
  300. if (h > 0 && Math::abs(n.x) > segment_is_valid_support_threshold) {
  301. // make it flat
  302. n.y = 0.0;
  303. n.x = SIGN(n.x) * radius;
  304. r_amount = 2;
  305. r_supports[0] = n;
  306. r_supports[0].y += h;
  307. r_supports[1] = n;
  308. r_supports[1].y -= h;
  309. } else {
  310. n *= radius;
  311. n.y += (n.y > 0) ? h : -h;
  312. r_amount = 1;
  313. *r_supports = n;
  314. }
  315. }
  316. bool GodotCapsuleShape2D::contains_point(const Vector2 &p_point) const {
  317. Vector2 p = p_point;
  318. p.y = Math::abs(p.y);
  319. p.y -= height * 0.5 - radius;
  320. if (p.y < 0) {
  321. p.y = 0;
  322. }
  323. return p.length_squared() < radius * radius;
  324. }
  325. bool GodotCapsuleShape2D::intersect_segment(const Vector2 &p_begin, const Vector2 &p_end, Vector2 &r_point, Vector2 &r_normal) const {
  326. real_t d = 1e10;
  327. Vector2 n = (p_end - p_begin).normalized();
  328. bool collided = false;
  329. //try spheres
  330. for (int i = 0; i < 2; i++) {
  331. Vector2 begin = p_begin;
  332. Vector2 end = p_end;
  333. real_t ofs = (i == 0) ? -height * 0.5 + radius : height * 0.5 - radius;
  334. begin.y += ofs;
  335. end.y += ofs;
  336. Vector2 line_vec = end - begin;
  337. real_t a, b, c;
  338. a = line_vec.dot(line_vec);
  339. b = 2 * begin.dot(line_vec);
  340. c = begin.dot(begin) - radius * radius;
  341. real_t sqrtterm = b * b - 4 * a * c;
  342. if (sqrtterm < 0) {
  343. continue;
  344. }
  345. sqrtterm = Math::sqrt(sqrtterm);
  346. real_t res = (-b - sqrtterm) / (2 * a);
  347. if (res < 0 || res > 1 + CMP_EPSILON) {
  348. continue;
  349. }
  350. Vector2 point = begin + line_vec * res;
  351. Vector2 pointf(point.x, point.y - ofs);
  352. real_t pd = n.dot(pointf);
  353. if (pd < d) {
  354. r_point = pointf;
  355. r_normal = point.normalized();
  356. d = pd;
  357. collided = true;
  358. }
  359. }
  360. Vector2 rpos, rnorm;
  361. if (Rect2(Point2(-radius, -height * 0.5 + radius), Size2(radius * 2.0, height - radius * 2)).intersects_segment(p_begin, p_end, &rpos, &rnorm)) {
  362. real_t pd = n.dot(rpos);
  363. if (pd < d) {
  364. r_point = rpos;
  365. r_normal = rnorm;
  366. d = pd;
  367. collided = true;
  368. }
  369. }
  370. //return get_aabb().intersects_segment(p_begin,p_end,&r_point,&r_normal);
  371. return collided; //todo
  372. }
  373. real_t GodotCapsuleShape2D::get_moment_of_inertia(real_t p_mass, const Size2 &p_scale) const {
  374. Vector2 he2 = Vector2(radius * 2, height) * p_scale;
  375. return p_mass * he2.dot(he2) / 12.0;
  376. }
  377. void GodotCapsuleShape2D::set_data(const Variant &p_data) {
  378. ERR_FAIL_COND(p_data.get_type() != Variant::ARRAY && p_data.get_type() != Variant::VECTOR2);
  379. if (p_data.get_type() == Variant::ARRAY) {
  380. Array arr = p_data;
  381. ERR_FAIL_COND(arr.size() != 2);
  382. height = arr[0];
  383. radius = arr[1];
  384. } else {
  385. Point2 p = p_data;
  386. radius = p.x;
  387. height = p.y;
  388. }
  389. Point2 he(radius, height * 0.5);
  390. configure(Rect2(-he, he * 2));
  391. }
  392. Variant GodotCapsuleShape2D::get_data() const {
  393. return Point2(height, radius);
  394. }
  395. /*********************************************************/
  396. /*********************************************************/
  397. /*********************************************************/
  398. void GodotConvexPolygonShape2D::get_supports(const Vector2 &p_normal, Vector2 *r_supports, int &r_amount) const {
  399. int support_idx = -1;
  400. real_t d = -1e10;
  401. r_amount = 0;
  402. for (int i = 0; i < point_count; i++) {
  403. //test point
  404. real_t ld = p_normal.dot(points[i].pos);
  405. if (ld > d) {
  406. support_idx = i;
  407. d = ld;
  408. }
  409. //test segment
  410. if (points[i].normal.dot(p_normal) > segment_is_valid_support_threshold) {
  411. r_amount = 2;
  412. r_supports[0] = points[i].pos;
  413. r_supports[1] = points[(i + 1) % point_count].pos;
  414. return;
  415. }
  416. }
  417. ERR_FAIL_COND_MSG(support_idx == -1, "Convex polygon shape support not found.");
  418. r_amount = 1;
  419. r_supports[0] = points[support_idx].pos;
  420. }
  421. bool GodotConvexPolygonShape2D::contains_point(const Vector2 &p_point) const {
  422. bool out = false;
  423. bool in = false;
  424. for (int i = 0; i < point_count; i++) {
  425. real_t d = points[i].normal.dot(p_point) - points[i].normal.dot(points[i].pos);
  426. if (d > 0) {
  427. out = true;
  428. } else {
  429. in = true;
  430. }
  431. }
  432. return in != out;
  433. }
  434. bool GodotConvexPolygonShape2D::intersect_segment(const Vector2 &p_begin, const Vector2 &p_end, Vector2 &r_point, Vector2 &r_normal) const {
  435. Vector2 n = (p_end - p_begin).normalized();
  436. real_t d = 1e10;
  437. bool inters = false;
  438. for (int i = 0; i < point_count; i++) {
  439. Vector2 res;
  440. if (!Geometry2D::segment_intersects_segment(p_begin, p_end, points[i].pos, points[(i + 1) % point_count].pos, &res)) {
  441. continue;
  442. }
  443. real_t nd = n.dot(res);
  444. if (nd < d) {
  445. d = nd;
  446. r_point = res;
  447. r_normal = points[i].normal;
  448. inters = true;
  449. }
  450. }
  451. return inters;
  452. }
  453. real_t GodotConvexPolygonShape2D::get_moment_of_inertia(real_t p_mass, const Size2 &p_scale) const {
  454. ERR_FAIL_COND_V_MSG(point_count == 0, 0, "Convex polygon shape has no points.");
  455. Rect2 aabb_new;
  456. aabb_new.position = points[0].pos * p_scale;
  457. for (int i = 0; i < point_count; i++) {
  458. aabb_new.expand_to(points[i].pos * p_scale);
  459. }
  460. return p_mass * aabb_new.size.dot(aabb_new.size) / 12.0;
  461. }
  462. void GodotConvexPolygonShape2D::set_data(const Variant &p_data) {
  463. #ifdef REAL_T_IS_DOUBLE
  464. ERR_FAIL_COND(p_data.get_type() != Variant::PACKED_VECTOR2_ARRAY && p_data.get_type() != Variant::PACKED_FLOAT64_ARRAY);
  465. #else
  466. ERR_FAIL_COND(p_data.get_type() != Variant::PACKED_VECTOR2_ARRAY && p_data.get_type() != Variant::PACKED_FLOAT32_ARRAY);
  467. #endif
  468. if (points) {
  469. memdelete_arr(points);
  470. }
  471. points = nullptr;
  472. point_count = 0;
  473. if (p_data.get_type() == Variant::PACKED_VECTOR2_ARRAY) {
  474. Vector<Vector2> arr = p_data;
  475. ERR_FAIL_COND(arr.is_empty());
  476. point_count = arr.size();
  477. points = memnew_arr(Point, point_count);
  478. const Vector2 *r = arr.ptr();
  479. for (int i = 0; i < point_count; i++) {
  480. points[i].pos = r[i];
  481. }
  482. for (int i = 0; i < point_count; i++) {
  483. Vector2 p = points[i].pos;
  484. Vector2 pn = points[(i + 1) % point_count].pos;
  485. points[i].normal = (pn - p).orthogonal().normalized();
  486. }
  487. } else {
  488. Vector<real_t> dvr = p_data;
  489. point_count = dvr.size() / 4;
  490. ERR_FAIL_COND(point_count == 0);
  491. points = memnew_arr(Point, point_count);
  492. const real_t *r = dvr.ptr();
  493. for (int i = 0; i < point_count; i++) {
  494. int idx = i << 2;
  495. points[i].pos.x = r[idx + 0];
  496. points[i].pos.y = r[idx + 1];
  497. points[i].normal.x = r[idx + 2];
  498. points[i].normal.y = r[idx + 3];
  499. }
  500. }
  501. ERR_FAIL_COND(point_count == 0);
  502. Rect2 aabb_new;
  503. aabb_new.position = points[0].pos;
  504. for (int i = 1; i < point_count; i++) {
  505. aabb_new.expand_to(points[i].pos);
  506. }
  507. configure(aabb_new);
  508. }
  509. Variant GodotConvexPolygonShape2D::get_data() const {
  510. Vector<Vector2> dvr;
  511. dvr.resize(point_count);
  512. for (int i = 0; i < point_count; i++) {
  513. dvr.set(i, points[i].pos);
  514. }
  515. return dvr;
  516. }
  517. GodotConvexPolygonShape2D::~GodotConvexPolygonShape2D() {
  518. if (points) {
  519. memdelete_arr(points);
  520. }
  521. }
  522. //////////////////////////////////////////////////
  523. void GodotConcavePolygonShape2D::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 GodotConcavePolygonShape2D::contains_point(const Vector2 &p_point) const {
  538. return false; //sorry
  539. }
  540. bool GodotConcavePolygonShape2D::intersect_segment(const Vector2 &p_begin, const Vector2 &p_end, Vector2 &r_point, Vector2 &r_normal) const {
  541. if (segments.size() == 0 || points.size() == 0) {
  542. return false;
  543. }
  544. uint32_t *stack = (uint32_t *)alloca(sizeof(int) * bvh_depth);
  545. enum {
  546. TEST_AABB_BIT = 0,
  547. VISIT_LEFT_BIT = 1,
  548. VISIT_RIGHT_BIT = 2,
  549. VISIT_DONE_BIT = 3,
  550. VISITED_BIT_SHIFT = 29,
  551. NODE_IDX_MASK = (1 << VISITED_BIT_SHIFT) - 1,
  552. VISITED_BIT_MASK = ~NODE_IDX_MASK,
  553. };
  554. Vector2 n = (p_end - p_begin).normalized();
  555. real_t d = 1e10;
  556. bool inters = false;
  557. /*
  558. for(int i=0;i<bvh_depth;i++)
  559. stack[i]=0;
  560. */
  561. int level = 0;
  562. const Segment *segmentptr = &segments[0];
  563. const Vector2 *pointptr = &points[0];
  564. const BVH *bvhptr = &bvh[0];
  565. stack[0] = 0;
  566. while (true) {
  567. uint32_t node = stack[level] & NODE_IDX_MASK;
  568. const BVH &bvh2 = bvhptr[node];
  569. bool done = false;
  570. switch (stack[level] >> VISITED_BIT_SHIFT) {
  571. case TEST_AABB_BIT: {
  572. bool valid = bvh2.aabb.intersects_segment(p_begin, p_end);
  573. if (!valid) {
  574. stack[level] = (VISIT_DONE_BIT << VISITED_BIT_SHIFT) | node;
  575. } else {
  576. if (bvh2.left < 0) {
  577. const Segment &s = segmentptr[bvh2.right];
  578. Vector2 a = pointptr[s.points[0]];
  579. Vector2 b = pointptr[s.points[1]];
  580. Vector2 res;
  581. if (Geometry2D::segment_intersects_segment(p_begin, p_end, a, b, &res)) {
  582. real_t nd = n.dot(res);
  583. if (nd < d) {
  584. d = nd;
  585. r_point = res;
  586. r_normal = (b - a).orthogonal().normalized();
  587. inters = true;
  588. }
  589. }
  590. stack[level] = (VISIT_DONE_BIT << VISITED_BIT_SHIFT) | node;
  591. } else {
  592. stack[level] = (VISIT_LEFT_BIT << VISITED_BIT_SHIFT) | node;
  593. }
  594. }
  595. }
  596. continue;
  597. case VISIT_LEFT_BIT: {
  598. stack[level] = (VISIT_RIGHT_BIT << VISITED_BIT_SHIFT) | node;
  599. stack[level + 1] = bvh2.left | TEST_AABB_BIT;
  600. level++;
  601. }
  602. continue;
  603. case VISIT_RIGHT_BIT: {
  604. stack[level] = (VISIT_DONE_BIT << VISITED_BIT_SHIFT) | node;
  605. stack[level + 1] = bvh2.right | TEST_AABB_BIT;
  606. level++;
  607. }
  608. continue;
  609. case VISIT_DONE_BIT: {
  610. if (level == 0) {
  611. done = true;
  612. break;
  613. } else {
  614. level--;
  615. }
  616. }
  617. continue;
  618. }
  619. if (done) {
  620. break;
  621. }
  622. }
  623. if (inters) {
  624. if (n.dot(r_normal) > 0) {
  625. r_normal = -r_normal;
  626. }
  627. }
  628. return inters;
  629. }
  630. int GodotConcavePolygonShape2D::_generate_bvh(BVH *p_bvh, int p_len, int p_depth) {
  631. if (p_len == 1) {
  632. bvh_depth = MAX(p_depth, bvh_depth);
  633. bvh.push_back(*p_bvh);
  634. return bvh.size() - 1;
  635. }
  636. //else sort best
  637. Rect2 global_aabb = p_bvh[0].aabb;
  638. for (int i = 1; i < p_len; i++) {
  639. global_aabb = global_aabb.merge(p_bvh[i].aabb);
  640. }
  641. if (global_aabb.size.x > global_aabb.size.y) {
  642. SortArray<BVH, BVH_CompareX> sort;
  643. sort.sort(p_bvh, p_len);
  644. } else {
  645. SortArray<BVH, BVH_CompareY> sort;
  646. sort.sort(p_bvh, p_len);
  647. }
  648. int median = p_len / 2;
  649. BVH node;
  650. node.aabb = global_aabb;
  651. int node_idx = bvh.size();
  652. bvh.push_back(node);
  653. int l = _generate_bvh(p_bvh, median, p_depth + 1);
  654. int r = _generate_bvh(&p_bvh[median], p_len - median, p_depth + 1);
  655. bvh.write[node_idx].left = l;
  656. bvh.write[node_idx].right = r;
  657. return node_idx;
  658. }
  659. void GodotConcavePolygonShape2D::set_data(const Variant &p_data) {
  660. #ifdef REAL_T_IS_DOUBLE
  661. ERR_FAIL_COND(p_data.get_type() != Variant::PACKED_VECTOR2_ARRAY && p_data.get_type() != Variant::PACKED_FLOAT64_ARRAY);
  662. #else
  663. ERR_FAIL_COND(p_data.get_type() != Variant::PACKED_VECTOR2_ARRAY && p_data.get_type() != Variant::PACKED_FLOAT32_ARRAY);
  664. #endif
  665. Rect2 aabb_new;
  666. if (p_data.get_type() == Variant::PACKED_VECTOR2_ARRAY) {
  667. Vector<Vector2> p2arr = p_data;
  668. int len = p2arr.size();
  669. ERR_FAIL_COND(len % 2);
  670. segments.clear();
  671. points.clear();
  672. bvh.clear();
  673. bvh_depth = 1;
  674. if (len == 0) {
  675. configure(aabb_new);
  676. return;
  677. }
  678. const Vector2 *arr = p2arr.ptr();
  679. HashMap<Point2, int> pointmap;
  680. for (int i = 0; i < len; i += 2) {
  681. Point2 p1 = arr[i];
  682. Point2 p2 = arr[i + 1];
  683. int idx_p1, idx_p2;
  684. if (pointmap.has(p1)) {
  685. idx_p1 = pointmap[p1];
  686. } else {
  687. idx_p1 = pointmap.size();
  688. pointmap[p1] = idx_p1;
  689. }
  690. if (pointmap.has(p2)) {
  691. idx_p2 = pointmap[p2];
  692. } else {
  693. idx_p2 = pointmap.size();
  694. pointmap[p2] = idx_p2;
  695. }
  696. Segment s;
  697. s.points[0] = idx_p1;
  698. s.points[1] = idx_p2;
  699. segments.push_back(s);
  700. }
  701. points.resize(pointmap.size());
  702. aabb_new.position = pointmap.begin()->key;
  703. for (const KeyValue<Point2, int> &E : pointmap) {
  704. aabb_new.expand_to(E.key);
  705. points.write[E.value] = E.key;
  706. }
  707. Vector<BVH> main_vbh;
  708. main_vbh.resize(segments.size());
  709. for (int i = 0; i < main_vbh.size(); i++) {
  710. main_vbh.write[i].aabb.position = points[segments[i].points[0]];
  711. main_vbh.write[i].aabb.expand_to(points[segments[i].points[1]]);
  712. main_vbh.write[i].left = -1;
  713. main_vbh.write[i].right = i;
  714. }
  715. _generate_bvh(main_vbh.ptrw(), main_vbh.size(), 1);
  716. } else {
  717. //dictionary with arrays
  718. }
  719. configure(aabb_new);
  720. }
  721. Variant GodotConcavePolygonShape2D::get_data() const {
  722. Vector<Vector2> rsegments;
  723. int len = segments.size();
  724. rsegments.resize(len * 2);
  725. Vector2 *w = rsegments.ptrw();
  726. for (int i = 0; i < len; i++) {
  727. w[(i << 1) + 0] = points[segments[i].points[0]];
  728. w[(i << 1) + 1] = points[segments[i].points[1]];
  729. }
  730. return rsegments;
  731. }
  732. void GodotConcavePolygonShape2D::cull(const Rect2 &p_local_aabb, QueryCallback p_callback, void *p_userdata) const {
  733. uint32_t *stack = (uint32_t *)alloca(sizeof(int) * bvh_depth);
  734. enum {
  735. TEST_AABB_BIT = 0,
  736. VISIT_LEFT_BIT = 1,
  737. VISIT_RIGHT_BIT = 2,
  738. VISIT_DONE_BIT = 3,
  739. VISITED_BIT_SHIFT = 29,
  740. NODE_IDX_MASK = (1 << VISITED_BIT_SHIFT) - 1,
  741. VISITED_BIT_MASK = ~NODE_IDX_MASK,
  742. };
  743. /*
  744. for(int i=0;i<bvh_depth;i++)
  745. stack[i]=0;
  746. */
  747. if (segments.size() == 0 || points.size() == 0 || bvh.size() == 0) {
  748. return;
  749. }
  750. int level = 0;
  751. const Segment *segmentptr = &segments[0];
  752. const Vector2 *pointptr = &points[0];
  753. const BVH *bvhptr = &bvh[0];
  754. stack[0] = 0;
  755. while (true) {
  756. uint32_t node = stack[level] & NODE_IDX_MASK;
  757. const BVH &bvh2 = bvhptr[node];
  758. switch (stack[level] >> VISITED_BIT_SHIFT) {
  759. case TEST_AABB_BIT: {
  760. bool valid = p_local_aabb.intersects(bvh2.aabb);
  761. if (!valid) {
  762. stack[level] = (VISIT_DONE_BIT << VISITED_BIT_SHIFT) | node;
  763. } else {
  764. if (bvh2.left < 0) {
  765. const Segment &s = segmentptr[bvh2.right];
  766. Vector2 a = pointptr[s.points[0]];
  767. Vector2 b = pointptr[s.points[1]];
  768. GodotSegmentShape2D ss(a, b, (b - a).orthogonal().normalized());
  769. if (p_callback(p_userdata, &ss)) {
  770. return;
  771. }
  772. stack[level] = (VISIT_DONE_BIT << VISITED_BIT_SHIFT) | node;
  773. } else {
  774. stack[level] = (VISIT_LEFT_BIT << VISITED_BIT_SHIFT) | node;
  775. }
  776. }
  777. }
  778. continue;
  779. case VISIT_LEFT_BIT: {
  780. stack[level] = (VISIT_RIGHT_BIT << VISITED_BIT_SHIFT) | node;
  781. stack[level + 1] = bvh2.left | TEST_AABB_BIT;
  782. level++;
  783. }
  784. continue;
  785. case VISIT_RIGHT_BIT: {
  786. stack[level] = (VISIT_DONE_BIT << VISITED_BIT_SHIFT) | node;
  787. stack[level + 1] = bvh2.right | TEST_AABB_BIT;
  788. level++;
  789. }
  790. continue;
  791. case VISIT_DONE_BIT: {
  792. if (level == 0) {
  793. return;
  794. } else {
  795. level--;
  796. }
  797. }
  798. continue;
  799. }
  800. }
  801. }