godot_shape_2d.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987
  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(-1e4, -1e4), Vector2(1e4 * 2, 1e4 * 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.normalize();
  304. n *= radius;
  305. r_amount = 2;
  306. r_supports[0] = n;
  307. r_supports[0].y += h;
  308. r_supports[1] = n;
  309. r_supports[1].y -= h;
  310. } else {
  311. n *= radius;
  312. n.y += (n.y > 0) ? h : -h;
  313. r_amount = 1;
  314. *r_supports = n;
  315. }
  316. }
  317. bool GodotCapsuleShape2D::contains_point(const Vector2 &p_point) const {
  318. Vector2 p = p_point;
  319. p.y = Math::abs(p.y);
  320. p.y -= height * 0.5 - radius;
  321. if (p.y < 0) {
  322. p.y = 0;
  323. }
  324. return p.length_squared() < radius * radius;
  325. }
  326. bool GodotCapsuleShape2D::intersect_segment(const Vector2 &p_begin, const Vector2 &p_end, Vector2 &r_point, Vector2 &r_normal) const {
  327. real_t d = 1e10;
  328. Vector2 n = (p_end - p_begin).normalized();
  329. bool collided = false;
  330. //try spheres
  331. for (int i = 0; i < 2; i++) {
  332. Vector2 begin = p_begin;
  333. Vector2 end = p_end;
  334. real_t ofs = (i == 0) ? -height * 0.5 + radius : height * 0.5 - radius;
  335. begin.y += ofs;
  336. end.y += ofs;
  337. Vector2 line_vec = end - begin;
  338. real_t a, b, c;
  339. a = line_vec.dot(line_vec);
  340. b = 2 * begin.dot(line_vec);
  341. c = begin.dot(begin) - radius * radius;
  342. real_t sqrtterm = b * b - 4 * a * c;
  343. if (sqrtterm < 0) {
  344. continue;
  345. }
  346. sqrtterm = Math::sqrt(sqrtterm);
  347. real_t res = (-b - sqrtterm) / (2 * a);
  348. if (res < 0 || res > 1 + CMP_EPSILON) {
  349. continue;
  350. }
  351. Vector2 point = begin + line_vec * res;
  352. Vector2 pointf(point.x, point.y - ofs);
  353. real_t pd = n.dot(pointf);
  354. if (pd < d) {
  355. r_point = pointf;
  356. r_normal = point.normalized();
  357. d = pd;
  358. collided = true;
  359. }
  360. }
  361. Vector2 rpos, rnorm;
  362. if (Rect2(Point2(-radius, -height * 0.5 + radius), Size2(radius * 2.0, height - radius * 2)).intersects_segment(p_begin, p_end, &rpos, &rnorm)) {
  363. real_t pd = n.dot(rpos);
  364. if (pd < d) {
  365. r_point = rpos;
  366. r_normal = rnorm;
  367. d = pd;
  368. collided = true;
  369. }
  370. }
  371. //return get_aabb().intersects_segment(p_begin,p_end,&r_point,&r_normal);
  372. return collided; //todo
  373. }
  374. real_t GodotCapsuleShape2D::get_moment_of_inertia(real_t p_mass, const Size2 &p_scale) const {
  375. Vector2 he2 = Vector2(radius * 2, height) * p_scale;
  376. return p_mass * he2.dot(he2) / 12.0;
  377. }
  378. void GodotCapsuleShape2D::set_data(const Variant &p_data) {
  379. ERR_FAIL_COND(p_data.get_type() != Variant::ARRAY && p_data.get_type() != Variant::VECTOR2);
  380. if (p_data.get_type() == Variant::ARRAY) {
  381. Array arr = p_data;
  382. ERR_FAIL_COND(arr.size() != 2);
  383. height = arr[0];
  384. radius = arr[1];
  385. } else {
  386. Point2 p = p_data;
  387. radius = p.x;
  388. height = p.y;
  389. }
  390. Point2 he(radius, height * 0.5);
  391. configure(Rect2(-he, he * 2));
  392. }
  393. Variant GodotCapsuleShape2D::get_data() const {
  394. return Point2(height, radius);
  395. }
  396. /*********************************************************/
  397. /*********************************************************/
  398. /*********************************************************/
  399. void GodotConvexPolygonShape2D::get_supports(const Vector2 &p_normal, Vector2 *r_supports, int &r_amount) const {
  400. int support_idx = -1;
  401. real_t d = -1e10;
  402. r_amount = 0;
  403. for (int i = 0; i < point_count; i++) {
  404. //test point
  405. real_t ld = p_normal.dot(points[i].pos);
  406. if (ld > d) {
  407. support_idx = i;
  408. d = ld;
  409. }
  410. //test segment
  411. if (points[i].normal.dot(p_normal) > segment_is_valid_support_threshold) {
  412. r_amount = 2;
  413. r_supports[0] = points[i].pos;
  414. r_supports[1] = points[(i + 1) % point_count].pos;
  415. return;
  416. }
  417. }
  418. ERR_FAIL_COND_MSG(support_idx == -1, "Convex polygon shape support not found.");
  419. r_amount = 1;
  420. r_supports[0] = points[support_idx].pos;
  421. }
  422. bool GodotConvexPolygonShape2D::contains_point(const Vector2 &p_point) const {
  423. bool out = false;
  424. bool in = false;
  425. for (int i = 0; i < point_count; i++) {
  426. real_t d = points[i].normal.dot(p_point) - points[i].normal.dot(points[i].pos);
  427. if (d > 0) {
  428. out = true;
  429. } else {
  430. in = true;
  431. }
  432. }
  433. return in != out;
  434. }
  435. bool GodotConvexPolygonShape2D::intersect_segment(const Vector2 &p_begin, const Vector2 &p_end, Vector2 &r_point, Vector2 &r_normal) const {
  436. Vector2 n = (p_end - p_begin).normalized();
  437. real_t d = 1e10;
  438. bool inters = false;
  439. for (int i = 0; i < point_count; i++) {
  440. Vector2 res;
  441. if (!Geometry2D::segment_intersects_segment(p_begin, p_end, points[i].pos, points[(i + 1) % point_count].pos, &res)) {
  442. continue;
  443. }
  444. real_t nd = n.dot(res);
  445. if (nd < d) {
  446. d = nd;
  447. r_point = res;
  448. r_normal = points[i].normal;
  449. inters = true;
  450. }
  451. }
  452. return inters;
  453. }
  454. real_t GodotConvexPolygonShape2D::get_moment_of_inertia(real_t p_mass, const Size2 &p_scale) const {
  455. ERR_FAIL_COND_V_MSG(point_count == 0, 0, "Convex polygon shape has no points.");
  456. Rect2 aabb_new;
  457. aabb_new.position = points[0].pos * p_scale;
  458. for (int i = 0; i < point_count; i++) {
  459. aabb_new.expand_to(points[i].pos * p_scale);
  460. }
  461. return p_mass * aabb_new.size.dot(aabb_new.size) / 12.0;
  462. }
  463. void GodotConvexPolygonShape2D::set_data(const Variant &p_data) {
  464. #ifdef REAL_T_IS_DOUBLE
  465. ERR_FAIL_COND(p_data.get_type() != Variant::PACKED_VECTOR2_ARRAY && p_data.get_type() != Variant::PACKED_FLOAT64_ARRAY);
  466. #else
  467. ERR_FAIL_COND(p_data.get_type() != Variant::PACKED_VECTOR2_ARRAY && p_data.get_type() != Variant::PACKED_FLOAT32_ARRAY);
  468. #endif
  469. if (points) {
  470. memdelete_arr(points);
  471. }
  472. points = nullptr;
  473. point_count = 0;
  474. if (p_data.get_type() == Variant::PACKED_VECTOR2_ARRAY) {
  475. Vector<Vector2> arr = p_data;
  476. ERR_FAIL_COND(arr.size() == 0);
  477. point_count = arr.size();
  478. points = memnew_arr(Point, point_count);
  479. const Vector2 *r = arr.ptr();
  480. for (int i = 0; i < point_count; i++) {
  481. points[i].pos = r[i];
  482. }
  483. for (int i = 0; i < point_count; i++) {
  484. Vector2 p = points[i].pos;
  485. Vector2 pn = points[(i + 1) % point_count].pos;
  486. points[i].normal = (pn - p).orthogonal().normalized();
  487. }
  488. } else {
  489. Vector<real_t> dvr = p_data;
  490. point_count = dvr.size() / 4;
  491. ERR_FAIL_COND(point_count == 0);
  492. points = memnew_arr(Point, point_count);
  493. const real_t *r = dvr.ptr();
  494. for (int i = 0; i < point_count; i++) {
  495. int idx = i << 2;
  496. points[i].pos.x = r[idx + 0];
  497. points[i].pos.y = r[idx + 1];
  498. points[i].normal.x = r[idx + 2];
  499. points[i].normal.y = r[idx + 3];
  500. }
  501. }
  502. ERR_FAIL_COND(point_count == 0);
  503. Rect2 aabb_new;
  504. aabb_new.position = points[0].pos;
  505. for (int i = 1; i < point_count; i++) {
  506. aabb_new.expand_to(points[i].pos);
  507. }
  508. configure(aabb_new);
  509. }
  510. Variant GodotConvexPolygonShape2D::get_data() const {
  511. Vector<Vector2> dvr;
  512. dvr.resize(point_count);
  513. for (int i = 0; i < point_count; i++) {
  514. dvr.set(i, points[i].pos);
  515. }
  516. return dvr;
  517. }
  518. GodotConvexPolygonShape2D::~GodotConvexPolygonShape2D() {
  519. if (points) {
  520. memdelete_arr(points);
  521. }
  522. }
  523. //////////////////////////////////////////////////
  524. void GodotConcavePolygonShape2D::get_supports(const Vector2 &p_normal, Vector2 *r_supports, int &r_amount) const {
  525. real_t d = -1e10;
  526. int idx = -1;
  527. for (int i = 0; i < points.size(); i++) {
  528. real_t ld = p_normal.dot(points[i]);
  529. if (ld > d) {
  530. d = ld;
  531. idx = i;
  532. }
  533. }
  534. r_amount = 1;
  535. ERR_FAIL_COND(idx == -1);
  536. *r_supports = points[idx];
  537. }
  538. bool GodotConcavePolygonShape2D::contains_point(const Vector2 &p_point) const {
  539. return false; //sorry
  540. }
  541. bool GodotConcavePolygonShape2D::intersect_segment(const Vector2 &p_begin, const Vector2 &p_end, Vector2 &r_point, Vector2 &r_normal) const {
  542. if (segments.size() == 0 || points.size() == 0) {
  543. return false;
  544. }
  545. uint32_t *stack = (uint32_t *)alloca(sizeof(int) * bvh_depth);
  546. enum {
  547. TEST_AABB_BIT = 0,
  548. VISIT_LEFT_BIT = 1,
  549. VISIT_RIGHT_BIT = 2,
  550. VISIT_DONE_BIT = 3,
  551. VISITED_BIT_SHIFT = 29,
  552. NODE_IDX_MASK = (1 << VISITED_BIT_SHIFT) - 1,
  553. VISITED_BIT_MASK = ~NODE_IDX_MASK,
  554. };
  555. Vector2 n = (p_end - p_begin).normalized();
  556. real_t d = 1e10;
  557. bool inters = false;
  558. /*
  559. for(int i=0;i<bvh_depth;i++)
  560. stack[i]=0;
  561. */
  562. int level = 0;
  563. const Segment *segmentptr = &segments[0];
  564. const Vector2 *pointptr = &points[0];
  565. const BVH *bvhptr = &bvh[0];
  566. stack[0] = 0;
  567. while (true) {
  568. uint32_t node = stack[level] & NODE_IDX_MASK;
  569. const BVH &bvh2 = bvhptr[node];
  570. bool done = false;
  571. switch (stack[level] >> VISITED_BIT_SHIFT) {
  572. case TEST_AABB_BIT: {
  573. bool valid = bvh2.aabb.intersects_segment(p_begin, p_end);
  574. if (!valid) {
  575. stack[level] = (VISIT_DONE_BIT << VISITED_BIT_SHIFT) | node;
  576. } else {
  577. if (bvh2.left < 0) {
  578. const Segment &s = segmentptr[bvh2.right];
  579. Vector2 a = pointptr[s.points[0]];
  580. Vector2 b = pointptr[s.points[1]];
  581. Vector2 res;
  582. if (Geometry2D::segment_intersects_segment(p_begin, p_end, a, b, &res)) {
  583. real_t nd = n.dot(res);
  584. if (nd < d) {
  585. d = nd;
  586. r_point = res;
  587. r_normal = (b - a).orthogonal().normalized();
  588. inters = true;
  589. }
  590. }
  591. stack[level] = (VISIT_DONE_BIT << VISITED_BIT_SHIFT) | node;
  592. } else {
  593. stack[level] = (VISIT_LEFT_BIT << VISITED_BIT_SHIFT) | node;
  594. }
  595. }
  596. }
  597. continue;
  598. case VISIT_LEFT_BIT: {
  599. stack[level] = (VISIT_RIGHT_BIT << VISITED_BIT_SHIFT) | node;
  600. stack[level + 1] = bvh2.left | TEST_AABB_BIT;
  601. level++;
  602. }
  603. continue;
  604. case VISIT_RIGHT_BIT: {
  605. stack[level] = (VISIT_DONE_BIT << VISITED_BIT_SHIFT) | node;
  606. stack[level + 1] = bvh2.right | TEST_AABB_BIT;
  607. level++;
  608. }
  609. continue;
  610. case VISIT_DONE_BIT: {
  611. if (level == 0) {
  612. done = true;
  613. break;
  614. } else {
  615. level--;
  616. }
  617. }
  618. continue;
  619. }
  620. if (done) {
  621. break;
  622. }
  623. }
  624. if (inters) {
  625. if (n.dot(r_normal) > 0) {
  626. r_normal = -r_normal;
  627. }
  628. }
  629. return inters;
  630. }
  631. int GodotConcavePolygonShape2D::_generate_bvh(BVH *p_bvh, int p_len, int p_depth) {
  632. if (p_len == 1) {
  633. bvh_depth = MAX(p_depth, bvh_depth);
  634. bvh.push_back(*p_bvh);
  635. return bvh.size() - 1;
  636. }
  637. //else sort best
  638. Rect2 global_aabb = p_bvh[0].aabb;
  639. for (int i = 1; i < p_len; i++) {
  640. global_aabb = global_aabb.merge(p_bvh[i].aabb);
  641. }
  642. if (global_aabb.size.x > global_aabb.size.y) {
  643. SortArray<BVH, BVH_CompareX> sort;
  644. sort.sort(p_bvh, p_len);
  645. } else {
  646. SortArray<BVH, BVH_CompareY> sort;
  647. sort.sort(p_bvh, p_len);
  648. }
  649. int median = p_len / 2;
  650. BVH node;
  651. node.aabb = global_aabb;
  652. int node_idx = bvh.size();
  653. bvh.push_back(node);
  654. int l = _generate_bvh(p_bvh, median, p_depth + 1);
  655. int r = _generate_bvh(&p_bvh[median], p_len - median, p_depth + 1);
  656. bvh.write[node_idx].left = l;
  657. bvh.write[node_idx].right = r;
  658. return node_idx;
  659. }
  660. void GodotConcavePolygonShape2D::set_data(const Variant &p_data) {
  661. #ifdef REAL_T_IS_DOUBLE
  662. ERR_FAIL_COND(p_data.get_type() != Variant::PACKED_VECTOR2_ARRAY && p_data.get_type() != Variant::PACKED_FLOAT64_ARRAY);
  663. #else
  664. ERR_FAIL_COND(p_data.get_type() != Variant::PACKED_VECTOR2_ARRAY && p_data.get_type() != Variant::PACKED_FLOAT32_ARRAY);
  665. #endif
  666. Rect2 aabb_new;
  667. if (p_data.get_type() == Variant::PACKED_VECTOR2_ARRAY) {
  668. Vector<Vector2> p2arr = p_data;
  669. int len = p2arr.size();
  670. ERR_FAIL_COND(len % 2);
  671. segments.clear();
  672. points.clear();
  673. bvh.clear();
  674. bvh_depth = 1;
  675. if (len == 0) {
  676. configure(aabb_new);
  677. return;
  678. }
  679. const Vector2 *arr = p2arr.ptr();
  680. HashMap<Point2, int> pointmap;
  681. for (int i = 0; i < len; i += 2) {
  682. Point2 p1 = arr[i];
  683. Point2 p2 = arr[i + 1];
  684. int idx_p1, idx_p2;
  685. if (pointmap.has(p1)) {
  686. idx_p1 = pointmap[p1];
  687. } else {
  688. idx_p1 = pointmap.size();
  689. pointmap[p1] = idx_p1;
  690. }
  691. if (pointmap.has(p2)) {
  692. idx_p2 = pointmap[p2];
  693. } else {
  694. idx_p2 = pointmap.size();
  695. pointmap[p2] = idx_p2;
  696. }
  697. Segment s;
  698. s.points[0] = idx_p1;
  699. s.points[1] = idx_p2;
  700. segments.push_back(s);
  701. }
  702. points.resize(pointmap.size());
  703. aabb_new.position = pointmap.begin()->key;
  704. for (const KeyValue<Point2, int> &E : pointmap) {
  705. aabb_new.expand_to(E.key);
  706. points.write[E.value] = E.key;
  707. }
  708. Vector<BVH> main_vbh;
  709. main_vbh.resize(segments.size());
  710. for (int i = 0; i < main_vbh.size(); i++) {
  711. main_vbh.write[i].aabb.position = points[segments[i].points[0]];
  712. main_vbh.write[i].aabb.expand_to(points[segments[i].points[1]]);
  713. main_vbh.write[i].left = -1;
  714. main_vbh.write[i].right = i;
  715. }
  716. _generate_bvh(main_vbh.ptrw(), main_vbh.size(), 1);
  717. } else {
  718. //dictionary with arrays
  719. }
  720. configure(aabb_new);
  721. }
  722. Variant GodotConcavePolygonShape2D::get_data() const {
  723. Vector<Vector2> rsegments;
  724. int len = segments.size();
  725. rsegments.resize(len * 2);
  726. Vector2 *w = rsegments.ptrw();
  727. for (int i = 0; i < len; i++) {
  728. w[(i << 1) + 0] = points[segments[i].points[0]];
  729. w[(i << 1) + 1] = points[segments[i].points[1]];
  730. }
  731. return rsegments;
  732. }
  733. void GodotConcavePolygonShape2D::cull(const Rect2 &p_local_aabb, QueryCallback p_callback, void *p_userdata) const {
  734. uint32_t *stack = (uint32_t *)alloca(sizeof(int) * bvh_depth);
  735. enum {
  736. TEST_AABB_BIT = 0,
  737. VISIT_LEFT_BIT = 1,
  738. VISIT_RIGHT_BIT = 2,
  739. VISIT_DONE_BIT = 3,
  740. VISITED_BIT_SHIFT = 29,
  741. NODE_IDX_MASK = (1 << VISITED_BIT_SHIFT) - 1,
  742. VISITED_BIT_MASK = ~NODE_IDX_MASK,
  743. };
  744. /*
  745. for(int i=0;i<bvh_depth;i++)
  746. stack[i]=0;
  747. */
  748. if (segments.size() == 0 || points.size() == 0 || bvh.size() == 0) {
  749. return;
  750. }
  751. int level = 0;
  752. const Segment *segmentptr = &segments[0];
  753. const Vector2 *pointptr = &points[0];
  754. const BVH *bvhptr = &bvh[0];
  755. stack[0] = 0;
  756. while (true) {
  757. uint32_t node = stack[level] & NODE_IDX_MASK;
  758. const BVH &bvh2 = bvhptr[node];
  759. switch (stack[level] >> VISITED_BIT_SHIFT) {
  760. case TEST_AABB_BIT: {
  761. bool valid = p_local_aabb.intersects(bvh2.aabb);
  762. if (!valid) {
  763. stack[level] = (VISIT_DONE_BIT << VISITED_BIT_SHIFT) | node;
  764. } else {
  765. if (bvh2.left < 0) {
  766. const Segment &s = segmentptr[bvh2.right];
  767. Vector2 a = pointptr[s.points[0]];
  768. Vector2 b = pointptr[s.points[1]];
  769. GodotSegmentShape2D ss(a, b, (b - a).orthogonal().normalized());
  770. if (p_callback(p_userdata, &ss)) {
  771. return;
  772. }
  773. stack[level] = (VISIT_DONE_BIT << VISITED_BIT_SHIFT) | node;
  774. } else {
  775. stack[level] = (VISIT_LEFT_BIT << VISITED_BIT_SHIFT) | node;
  776. }
  777. }
  778. }
  779. continue;
  780. case VISIT_LEFT_BIT: {
  781. stack[level] = (VISIT_RIGHT_BIT << VISITED_BIT_SHIFT) | node;
  782. stack[level + 1] = bvh2.left | TEST_AABB_BIT;
  783. level++;
  784. }
  785. continue;
  786. case VISIT_RIGHT_BIT: {
  787. stack[level] = (VISIT_DONE_BIT << VISITED_BIT_SHIFT) | node;
  788. stack[level + 1] = bvh2.right | TEST_AABB_BIT;
  789. level++;
  790. }
  791. continue;
  792. case VISIT_DONE_BIT: {
  793. if (level == 0) {
  794. return;
  795. } else {
  796. level--;
  797. }
  798. }
  799. continue;
  800. }
  801. }
  802. }