godot_shape_2d.h 22 KB

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