shape_2d_sw.h 21 KB

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