godot_shape_2d.h 22 KB

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