godot_shape_2d.h 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  1. /**************************************************************************/
  2. /* godot_shape_2d.h */
  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. #ifndef GODOT_SHAPE_2D_H
  31. #define GODOT_SHAPE_2D_H
  32. #include "servers/physics_server_2d.h"
  33. #define _SEGMENT_IS_VALID_SUPPORT_THRESHOLD 0.99998
  34. class GodotShape2D;
  35. class GodotShapeOwner2D {
  36. public:
  37. virtual void _shape_changed() = 0;
  38. virtual void remove_shape(GodotShape2D *p_shape) = 0;
  39. virtual ~GodotShapeOwner2D() {}
  40. };
  41. class GodotShape2D {
  42. RID self;
  43. Rect2 aabb;
  44. bool configured = false;
  45. real_t custom_bias = 0.0;
  46. HashMap<GodotShapeOwner2D *, int> owners;
  47. protected:
  48. void configure(const Rect2 &p_aabb);
  49. public:
  50. _FORCE_INLINE_ void set_self(const RID &p_self) { self = p_self; }
  51. _FORCE_INLINE_ RID get_self() const { return self; }
  52. virtual PhysicsServer2D::ShapeType get_type() const = 0;
  53. _FORCE_INLINE_ Rect2 get_aabb() const { return aabb; }
  54. _FORCE_INLINE_ bool is_configured() const { return configured; }
  55. virtual bool allows_one_way_collision() const { return true; }
  56. virtual bool is_concave() const { return false; }
  57. virtual bool contains_point(const Vector2 &p_point) const = 0;
  58. virtual void project_rangev(const Vector2 &p_normal, const Transform2D &p_transform, real_t &r_min, real_t &r_max) const = 0;
  59. virtual void project_range_castv(const Vector2 &p_cast, const Vector2 &p_normal, const Transform2D &p_transform, real_t &r_min, real_t &r_max) const = 0;
  60. virtual Vector2 get_support(const Vector2 &p_normal) const;
  61. virtual void get_supports(const Vector2 &p_normal, Vector2 *r_supports, int &r_amount) const = 0;
  62. virtual bool intersect_segment(const Vector2 &p_begin, const Vector2 &p_end, Vector2 &r_point, Vector2 &r_normal) const = 0;
  63. virtual real_t get_moment_of_inertia(real_t p_mass, const Size2 &p_scale) const = 0;
  64. virtual void set_data(const Variant &p_data) = 0;
  65. virtual Variant get_data() const = 0;
  66. _FORCE_INLINE_ void set_custom_bias(real_t p_bias) { custom_bias = p_bias; }
  67. _FORCE_INLINE_ real_t get_custom_bias() const { return custom_bias; }
  68. void add_owner(GodotShapeOwner2D *p_owner);
  69. void remove_owner(GodotShapeOwner2D *p_owner);
  70. bool is_owner(GodotShapeOwner2D *p_owner) const;
  71. const HashMap<GodotShapeOwner2D *, int> &get_owners() const;
  72. _FORCE_INLINE_ void get_supports_transformed_cast(const Vector2 &p_cast, const Vector2 &p_normal, const Transform2D &p_xform, Vector2 *r_supports, int &r_amount) const {
  73. get_supports(p_xform.basis_xform_inv(p_normal).normalized(), r_supports, r_amount);
  74. for (int i = 0; i < r_amount; i++) {
  75. r_supports[i] = p_xform.xform(r_supports[i]);
  76. }
  77. if (r_amount == 1) {
  78. if (Math::abs(p_normal.dot(p_cast.normalized())) < (1.0 - _SEGMENT_IS_VALID_SUPPORT_THRESHOLD)) {
  79. //make line because they are parallel
  80. r_amount = 2;
  81. r_supports[1] = r_supports[0] + p_cast;
  82. } else if (p_cast.dot(p_normal) > 0) {
  83. //normal points towards cast, add cast
  84. r_supports[0] += p_cast;
  85. }
  86. } else {
  87. if (Math::abs(p_normal.dot(p_cast.normalized())) < (1.0 - _SEGMENT_IS_VALID_SUPPORT_THRESHOLD)) {
  88. //optimize line and make it larger because they are parallel
  89. if ((r_supports[1] - r_supports[0]).dot(p_cast) > 0) {
  90. //larger towards 1
  91. r_supports[1] += p_cast;
  92. } else {
  93. //larger towards 0
  94. r_supports[0] += p_cast;
  95. }
  96. } else if (p_cast.dot(p_normal) > 0) {
  97. //normal points towards cast, add cast
  98. r_supports[0] += p_cast;
  99. r_supports[1] += p_cast;
  100. }
  101. }
  102. }
  103. GodotShape2D() {}
  104. virtual ~GodotShape2D();
  105. };
  106. //let the optimizer do the magic
  107. #define DEFAULT_PROJECT_RANGE_CAST \
  108. virtual void project_range_castv(const Vector2 &p_cast, const Vector2 &p_normal, const Transform2D &p_transform, real_t &r_min, real_t &r_max) const override { \
  109. project_range_cast(p_cast, p_normal, p_transform, r_min, r_max); \
  110. } \
  111. _FORCE_INLINE_ void project_range_cast(const Vector2 &p_cast, const Vector2 &p_normal, const Transform2D &p_transform, real_t &r_min, real_t &r_max) const { \
  112. real_t mina, maxa; \
  113. real_t minb, maxb; \
  114. Transform2D ofsb = p_transform; \
  115. ofsb.columns[2] += p_cast; \
  116. project_range(p_normal, p_transform, mina, maxa); \
  117. project_range(p_normal, ofsb, minb, maxb); \
  118. r_min = MIN(mina, minb); \
  119. r_max = MAX(maxa, maxb); \
  120. }
  121. class GodotWorldBoundaryShape2D : public GodotShape2D {
  122. Vector2 normal;
  123. real_t d = 0.0;
  124. public:
  125. _FORCE_INLINE_ Vector2 get_normal() const { return normal; }
  126. _FORCE_INLINE_ real_t get_d() const { return d; }
  127. virtual PhysicsServer2D::ShapeType get_type() const override { return PhysicsServer2D::SHAPE_WORLD_BOUNDARY; }
  128. virtual void project_rangev(const Vector2 &p_normal, const Transform2D &p_transform, real_t &r_min, real_t &r_max) const override { project_range(p_normal, p_transform, r_min, r_max); }
  129. virtual void get_supports(const Vector2 &p_normal, Vector2 *r_supports, int &r_amount) const override;
  130. virtual bool contains_point(const Vector2 &p_point) const override;
  131. virtual bool intersect_segment(const Vector2 &p_begin, const Vector2 &p_end, Vector2 &r_point, Vector2 &r_normal) const override;
  132. virtual real_t get_moment_of_inertia(real_t p_mass, const Size2 &p_scale) const override;
  133. virtual void set_data(const Variant &p_data) override;
  134. virtual Variant get_data() const override;
  135. _FORCE_INLINE_ void project_range(const Vector2 &p_normal, const Transform2D &p_transform, real_t &r_min, real_t &r_max) const {
  136. //real large
  137. r_min = -1e10;
  138. r_max = 1e10;
  139. }
  140. virtual void project_range_castv(const Vector2 &p_cast, const Vector2 &p_normal, const Transform2D &p_transform, real_t &r_min, real_t &r_max) const override {
  141. project_range_cast(p_cast, p_normal, p_transform, r_min, r_max);
  142. }
  143. _FORCE_INLINE_ void project_range_cast(const Vector2 &p_cast, const Vector2 &p_normal, const Transform2D &p_transform, real_t &r_min, real_t &r_max) const {
  144. //real large
  145. r_min = -1e10;
  146. r_max = 1e10;
  147. }
  148. };
  149. class GodotSeparationRayShape2D : public GodotShape2D {
  150. real_t length = 0.0;
  151. bool slide_on_slope = false;
  152. public:
  153. _FORCE_INLINE_ real_t get_length() const { return length; }
  154. _FORCE_INLINE_ bool get_slide_on_slope() const { return slide_on_slope; }
  155. virtual PhysicsServer2D::ShapeType get_type() const override { return PhysicsServer2D::SHAPE_SEPARATION_RAY; }
  156. virtual bool allows_one_way_collision() const override { return false; }
  157. virtual void project_rangev(const Vector2 &p_normal, const Transform2D &p_transform, real_t &r_min, real_t &r_max) const override { project_range(p_normal, p_transform, r_min, r_max); }
  158. virtual void get_supports(const Vector2 &p_normal, Vector2 *r_supports, int &r_amount) const override;
  159. virtual bool contains_point(const Vector2 &p_point) const override;
  160. virtual bool intersect_segment(const Vector2 &p_begin, const Vector2 &p_end, Vector2 &r_point, Vector2 &r_normal) const override;
  161. virtual real_t get_moment_of_inertia(real_t p_mass, const Size2 &p_scale) const override;
  162. virtual void set_data(const Variant &p_data) override;
  163. virtual Variant get_data() const override;
  164. _FORCE_INLINE_ void project_range(const Vector2 &p_normal, const Transform2D &p_transform, real_t &r_min, real_t &r_max) const {
  165. //real large
  166. r_max = p_normal.dot(p_transform.get_origin());
  167. r_min = p_normal.dot(p_transform.xform(Vector2(0, length)));
  168. if (r_max < r_min) {
  169. SWAP(r_max, r_min);
  170. }
  171. }
  172. DEFAULT_PROJECT_RANGE_CAST
  173. _FORCE_INLINE_ GodotSeparationRayShape2D() {}
  174. _FORCE_INLINE_ GodotSeparationRayShape2D(real_t p_length) { length = p_length; }
  175. };
  176. class GodotSegmentShape2D : public GodotShape2D {
  177. Vector2 a;
  178. Vector2 b;
  179. Vector2 n;
  180. public:
  181. _FORCE_INLINE_ const Vector2 &get_a() const { return a; }
  182. _FORCE_INLINE_ const Vector2 &get_b() const { return b; }
  183. _FORCE_INLINE_ const Vector2 &get_normal() const { return n; }
  184. virtual PhysicsServer2D::ShapeType get_type() const override { return PhysicsServer2D::SHAPE_SEGMENT; }
  185. _FORCE_INLINE_ Vector2 get_xformed_normal(const Transform2D &p_xform) const {
  186. return (p_xform.xform(b) - p_xform.xform(a)).normalized().orthogonal();
  187. }
  188. virtual void project_rangev(const Vector2 &p_normal, const Transform2D &p_transform, real_t &r_min, real_t &r_max) const override { project_range(p_normal, p_transform, r_min, r_max); }
  189. virtual void get_supports(const Vector2 &p_normal, Vector2 *r_supports, int &r_amount) const override;
  190. virtual bool contains_point(const Vector2 &p_point) const override;
  191. virtual bool intersect_segment(const Vector2 &p_begin, const Vector2 &p_end, Vector2 &r_point, Vector2 &r_normal) const override;
  192. virtual real_t get_moment_of_inertia(real_t p_mass, const Size2 &p_scale) const override;
  193. virtual void set_data(const Variant &p_data) override;
  194. virtual Variant get_data() const override;
  195. _FORCE_INLINE_ void project_range(const Vector2 &p_normal, const Transform2D &p_transform, real_t &r_min, real_t &r_max) const {
  196. //real large
  197. r_max = p_normal.dot(p_transform.xform(a));
  198. r_min = p_normal.dot(p_transform.xform(b));
  199. if (r_max < r_min) {
  200. SWAP(r_max, r_min);
  201. }
  202. }
  203. DEFAULT_PROJECT_RANGE_CAST
  204. _FORCE_INLINE_ GodotSegmentShape2D() {}
  205. _FORCE_INLINE_ GodotSegmentShape2D(const Vector2 &p_a, const Vector2 &p_b, const Vector2 &p_n) {
  206. a = p_a;
  207. b = p_b;
  208. n = p_n;
  209. }
  210. };
  211. class GodotCircleShape2D : public GodotShape2D {
  212. real_t radius;
  213. public:
  214. _FORCE_INLINE_ const real_t &get_radius() const { return radius; }
  215. virtual PhysicsServer2D::ShapeType get_type() const override { return PhysicsServer2D::SHAPE_CIRCLE; }
  216. virtual void project_rangev(const Vector2 &p_normal, const Transform2D &p_transform, real_t &r_min, real_t &r_max) const override { project_range(p_normal, p_transform, r_min, r_max); }
  217. virtual void get_supports(const Vector2 &p_normal, Vector2 *r_supports, int &r_amount) const override;
  218. virtual bool contains_point(const Vector2 &p_point) const override;
  219. virtual bool intersect_segment(const Vector2 &p_begin, const Vector2 &p_end, Vector2 &r_point, Vector2 &r_normal) const override;
  220. virtual real_t get_moment_of_inertia(real_t p_mass, const Size2 &p_scale) const override;
  221. virtual void set_data(const Variant &p_data) override;
  222. virtual Variant get_data() const override;
  223. _FORCE_INLINE_ void project_range(const Vector2 &p_normal, const Transform2D &p_transform, real_t &r_min, real_t &r_max) const {
  224. //real large
  225. real_t d = p_normal.dot(p_transform.get_origin());
  226. // figure out scale at point
  227. Vector2 local_normal = p_transform.basis_xform_inv(p_normal);
  228. real_t scale = local_normal.length();
  229. r_min = d - (radius)*scale;
  230. r_max = d + (radius)*scale;
  231. }
  232. DEFAULT_PROJECT_RANGE_CAST
  233. };
  234. class GodotRectangleShape2D : public GodotShape2D {
  235. Vector2 half_extents;
  236. public:
  237. _FORCE_INLINE_ const Vector2 &get_half_extents() const { return half_extents; }
  238. virtual PhysicsServer2D::ShapeType get_type() const override { return PhysicsServer2D::SHAPE_RECTANGLE; }
  239. virtual void project_rangev(const Vector2 &p_normal, const Transform2D &p_transform, real_t &r_min, real_t &r_max) const override { project_range(p_normal, p_transform, r_min, r_max); }
  240. virtual void get_supports(const Vector2 &p_normal, Vector2 *r_supports, int &r_amount) const override;
  241. virtual bool contains_point(const Vector2 &p_point) const override;
  242. virtual bool intersect_segment(const Vector2 &p_begin, const Vector2 &p_end, Vector2 &r_point, Vector2 &r_normal) const override;
  243. virtual real_t get_moment_of_inertia(real_t p_mass, const Size2 &p_scale) const override;
  244. virtual void set_data(const Variant &p_data) override;
  245. virtual Variant get_data() const override;
  246. _FORCE_INLINE_ void project_range(const Vector2 &p_normal, const Transform2D &p_transform, real_t &r_min, real_t &r_max) const {
  247. // no matter the angle, the box is mirrored anyway
  248. r_max = -1e20;
  249. r_min = 1e20;
  250. for (int i = 0; i < 4; i++) {
  251. real_t d = p_normal.dot(p_transform.xform(Vector2(((i & 1) * 2 - 1) * half_extents.x, ((i >> 1) * 2 - 1) * half_extents.y)));
  252. if (d > r_max) {
  253. r_max = d;
  254. }
  255. if (d < r_min) {
  256. r_min = d;
  257. }
  258. }
  259. }
  260. _FORCE_INLINE_ Vector2 get_circle_axis(const Transform2D &p_xform, const Transform2D &p_xform_inv, const Vector2 &p_circle) const {
  261. Vector2 local_v = p_xform_inv.xform(p_circle);
  262. Vector2 he(
  263. (local_v.x < 0) ? -half_extents.x : half_extents.x,
  264. (local_v.y < 0) ? -half_extents.y : half_extents.y);
  265. return (p_xform.xform(he) - p_circle).normalized();
  266. }
  267. _FORCE_INLINE_ Vector2 get_box_axis(const Transform2D &p_xform, const Transform2D &p_xform_inv, const GodotRectangleShape2D *p_B, const Transform2D &p_B_xform, const Transform2D &p_B_xform_inv) const {
  268. Vector2 a, b;
  269. {
  270. Vector2 local_v = p_xform_inv.xform(p_B_xform.get_origin());
  271. Vector2 he(
  272. (local_v.x < 0) ? -half_extents.x : half_extents.x,
  273. (local_v.y < 0) ? -half_extents.y : half_extents.y);
  274. a = p_xform.xform(he);
  275. }
  276. {
  277. Vector2 local_v = p_B_xform_inv.xform(p_xform.get_origin());
  278. Vector2 he(
  279. (local_v.x < 0) ? -p_B->half_extents.x : p_B->half_extents.x,
  280. (local_v.y < 0) ? -p_B->half_extents.y : p_B->half_extents.y);
  281. b = p_B_xform.xform(he);
  282. }
  283. return (a - b).normalized();
  284. }
  285. DEFAULT_PROJECT_RANGE_CAST
  286. };
  287. class GodotCapsuleShape2D : public GodotShape2D {
  288. real_t radius = 0.0;
  289. real_t height = 0.0;
  290. public:
  291. _FORCE_INLINE_ const real_t &get_radius() const { return radius; }
  292. _FORCE_INLINE_ const real_t &get_height() const { return height; }
  293. virtual PhysicsServer2D::ShapeType get_type() const override { return PhysicsServer2D::SHAPE_CAPSULE; }
  294. virtual void project_rangev(const Vector2 &p_normal, const Transform2D &p_transform, real_t &r_min, real_t &r_max) const override { project_range(p_normal, p_transform, r_min, r_max); }
  295. virtual void get_supports(const Vector2 &p_normal, Vector2 *r_supports, int &r_amount) const override;
  296. virtual bool contains_point(const Vector2 &p_point) const override;
  297. virtual bool intersect_segment(const Vector2 &p_begin, const Vector2 &p_end, Vector2 &r_point, Vector2 &r_normal) const override;
  298. virtual real_t get_moment_of_inertia(real_t p_mass, const Size2 &p_scale) const override;
  299. virtual void set_data(const Variant &p_data) override;
  300. virtual Variant get_data() const override;
  301. _FORCE_INLINE_ void project_range(const Vector2 &p_normal, const Transform2D &p_transform, real_t &r_min, real_t &r_max) const {
  302. // no matter the angle, the box is mirrored anyway
  303. Vector2 n = p_transform.basis_xform_inv(p_normal).normalized();
  304. real_t h = height * 0.5 - radius;
  305. n *= radius;
  306. n.y += (n.y > 0) ? h : -h;
  307. r_max = p_normal.dot(p_transform.xform(n));
  308. r_min = p_normal.dot(p_transform.xform(-n));
  309. if (r_max < r_min) {
  310. SWAP(r_max, r_min);
  311. }
  312. //ERR_FAIL_COND( r_max < r_min );
  313. }
  314. DEFAULT_PROJECT_RANGE_CAST
  315. };
  316. class GodotConvexPolygonShape2D : public GodotShape2D {
  317. struct Point {
  318. Vector2 pos;
  319. Vector2 normal; //normal to next segment
  320. };
  321. Point *points = nullptr;
  322. int point_count = 0;
  323. public:
  324. _FORCE_INLINE_ int get_point_count() const { return point_count; }
  325. _FORCE_INLINE_ const Vector2 &get_point(int p_idx) const { return points[p_idx].pos; }
  326. _FORCE_INLINE_ const Vector2 &get_segment_normal(int p_idx) const { return points[p_idx].normal; }
  327. _FORCE_INLINE_ Vector2 get_xformed_segment_normal(const Transform2D &p_xform, int p_idx) const {
  328. Vector2 a = points[p_idx].pos;
  329. p_idx++;
  330. Vector2 b = points[p_idx == point_count ? 0 : p_idx].pos;
  331. return (p_xform.xform(b) - p_xform.xform(a)).normalized().orthogonal();
  332. }
  333. virtual PhysicsServer2D::ShapeType get_type() const override { return PhysicsServer2D::SHAPE_CONVEX_POLYGON; }
  334. virtual void project_rangev(const Vector2 &p_normal, const Transform2D &p_transform, real_t &r_min, real_t &r_max) const override { project_range(p_normal, p_transform, r_min, r_max); }
  335. virtual void get_supports(const Vector2 &p_normal, Vector2 *r_supports, int &r_amount) const override;
  336. virtual bool contains_point(const Vector2 &p_point) const override;
  337. virtual bool intersect_segment(const Vector2 &p_begin, const Vector2 &p_end, Vector2 &r_point, Vector2 &r_normal) const override;
  338. virtual real_t get_moment_of_inertia(real_t p_mass, const Size2 &p_scale) const override;
  339. virtual void set_data(const Variant &p_data) override;
  340. virtual Variant get_data() const override;
  341. _FORCE_INLINE_ void project_range(const Vector2 &p_normal, const Transform2D &p_transform, real_t &r_min, real_t &r_max) const {
  342. if (!points || point_count <= 0) {
  343. r_min = r_max = 0;
  344. return;
  345. }
  346. r_min = r_max = p_normal.dot(p_transform.xform(points[0].pos));
  347. for (int i = 1; i < point_count; i++) {
  348. real_t d = p_normal.dot(p_transform.xform(points[i].pos));
  349. if (d > r_max) {
  350. r_max = d;
  351. }
  352. if (d < r_min) {
  353. r_min = d;
  354. }
  355. }
  356. }
  357. DEFAULT_PROJECT_RANGE_CAST
  358. GodotConvexPolygonShape2D() {}
  359. ~GodotConvexPolygonShape2D();
  360. };
  361. class GodotConcaveShape2D : public GodotShape2D {
  362. public:
  363. virtual bool is_concave() const override { return true; }
  364. // Returns true to stop the query.
  365. typedef bool (*QueryCallback)(void *p_userdata, GodotShape2D *p_convex);
  366. virtual void cull(const Rect2 &p_local_aabb, QueryCallback p_callback, void *p_userdata) const = 0;
  367. };
  368. class GodotConcavePolygonShape2D : public GodotConcaveShape2D {
  369. struct Segment {
  370. int points[2] = {};
  371. };
  372. Vector<Segment> segments;
  373. Vector<Point2> points;
  374. struct BVH {
  375. Rect2 aabb;
  376. int left = 0, right = 0;
  377. };
  378. Vector<BVH> bvh;
  379. int bvh_depth = 0;
  380. struct BVH_CompareX {
  381. _FORCE_INLINE_ bool operator()(const BVH &a, const BVH &b) const {
  382. return (a.aabb.position.x + a.aabb.size.x * 0.5) < (b.aabb.position.x + b.aabb.size.x * 0.5);
  383. }
  384. };
  385. struct BVH_CompareY {
  386. _FORCE_INLINE_ bool operator()(const BVH &a, const BVH &b) const {
  387. return (a.aabb.position.y + a.aabb.size.y * 0.5) < (b.aabb.position.y + b.aabb.size.y * 0.5);
  388. }
  389. };
  390. int _generate_bvh(BVH *p_bvh, int p_len, int p_depth);
  391. public:
  392. virtual PhysicsServer2D::ShapeType get_type() const override { return PhysicsServer2D::SHAPE_CONCAVE_POLYGON; }
  393. virtual void project_rangev(const Vector2 &p_normal, const Transform2D &p_transform, real_t &r_min, real_t &r_max) const override {
  394. r_min = 0;
  395. r_max = 0;
  396. ERR_FAIL_MSG("Unsupported call to project_rangev in GodotConcavePolygonShape2D");
  397. }
  398. void project_range(const Vector2 &p_normal, const Transform2D &p_transform, real_t &r_min, real_t &r_max) const {
  399. r_min = 0;
  400. r_max = 0;
  401. ERR_FAIL_MSG("Unsupported call to project_range in GodotConcavePolygonShape2D");
  402. }
  403. virtual void get_supports(const Vector2 &p_normal, Vector2 *r_supports, int &r_amount) const override;
  404. virtual bool contains_point(const Vector2 &p_point) const override;
  405. virtual bool intersect_segment(const Vector2 &p_begin, const Vector2 &p_end, Vector2 &r_point, Vector2 &r_normal) const override;
  406. virtual real_t get_moment_of_inertia(real_t p_mass, const Size2 &p_scale) const override { return 0; }
  407. virtual void set_data(const Variant &p_data) override;
  408. virtual Variant get_data() const override;
  409. virtual void cull(const Rect2 &p_local_aabb, QueryCallback p_callback, void *p_userdata) const override;
  410. DEFAULT_PROJECT_RANGE_CAST
  411. };
  412. #undef DEFAULT_PROJECT_RANGE_CAST
  413. #endif // GODOT_SHAPE_2D_H