shape_2d_sw.h 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  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-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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. if (r_amount == 1) {
  85. if (Math::abs(p_normal.dot(p_cast.normalized())) < (1.0 - _SEGMENT_IS_VALID_SUPPORT_THRESHOLD)) {
  86. //make line because they are parallel
  87. r_amount = 2;
  88. r_supports[1] = r_supports[0] + p_cast;
  89. } else if (p_cast.dot(p_normal) > 0) {
  90. //normal points towards cast, add cast
  91. r_supports[0] += p_cast;
  92. }
  93. } else {
  94. if (Math::abs(p_normal.dot(p_cast.normalized())) < (1.0 - _SEGMENT_IS_VALID_SUPPORT_THRESHOLD)) {
  95. //optimize line and make it larger because they are parallel
  96. if ((r_supports[1] - r_supports[0]).dot(p_cast) > 0) {
  97. //larger towards 1
  98. r_supports[1] += p_cast;
  99. } else {
  100. //larger towards 0
  101. r_supports[0] += p_cast;
  102. }
  103. } else if (p_cast.dot(p_normal) > 0) {
  104. //normal points towards cast, add cast
  105. r_supports[0] += p_cast;
  106. r_supports[1] += p_cast;
  107. }
  108. }
  109. }
  110. Shape2DSW();
  111. virtual ~Shape2DSW();
  112. };
  113. //let the optimizer do the magic
  114. #define DEFAULT_PROJECT_RANGE_CAST \
  115. 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 { \
  116. project_range_cast(p_cast, p_normal, p_transform, r_min, r_max); \
  117. } \
  118. _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 { \
  119. \
  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. if (d < r_min)
  262. r_min = d;
  263. }
  264. }
  265. _FORCE_INLINE_ Vector2 get_circle_axis(const Transform2D &p_xform, const Transform2D &p_xform_inv, const Vector2 &p_circle) const {
  266. Vector2 local_v = p_xform_inv.xform(p_circle);
  267. Vector2 he(
  268. (local_v.x < 0) ? -half_extents.x : half_extents.x,
  269. (local_v.y < 0) ? -half_extents.y : half_extents.y);
  270. return (p_xform.xform(he) - p_circle).normalized();
  271. }
  272. _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 {
  273. Vector2 a, b;
  274. {
  275. Vector2 local_v = p_xform_inv.xform(p_B_xform.get_origin());
  276. Vector2 he(
  277. (local_v.x < 0) ? -half_extents.x : half_extents.x,
  278. (local_v.y < 0) ? -half_extents.y : half_extents.y);
  279. a = p_xform.xform(he);
  280. }
  281. {
  282. Vector2 local_v = p_B_xform_inv.xform(p_xform.get_origin());
  283. Vector2 he(
  284. (local_v.x < 0) ? -p_B->half_extents.x : p_B->half_extents.x,
  285. (local_v.y < 0) ? -p_B->half_extents.y : p_B->half_extents.y);
  286. b = p_B_xform.xform(he);
  287. }
  288. return (a - b).normalized();
  289. }
  290. DEFAULT_PROJECT_RANGE_CAST
  291. };
  292. class CapsuleShape2DSW : public Shape2DSW {
  293. real_t radius;
  294. real_t height;
  295. public:
  296. _FORCE_INLINE_ const real_t &get_radius() const { return radius; }
  297. _FORCE_INLINE_ const real_t &get_height() const { return height; }
  298. virtual Physics2DServer::ShapeType get_type() const { return Physics2DServer::SHAPE_CAPSULE; }
  299. 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); }
  300. virtual void get_supports(const Vector2 &p_normal, Vector2 *r_supports, int &r_amount) const;
  301. virtual bool contains_point(const Vector2 &p_point) const;
  302. virtual bool intersect_segment(const Vector2 &p_begin, const Vector2 &p_end, Vector2 &r_point, Vector2 &r_normal) const;
  303. virtual real_t get_moment_of_inertia(real_t p_mass, const Size2 &p_scale) const;
  304. virtual void set_data(const Variant &p_data);
  305. virtual Variant get_data() const;
  306. _FORCE_INLINE_ void project_range(const Vector2 &p_normal, const Transform2D &p_transform, real_t &r_min, real_t &r_max) const {
  307. // no matter the angle, the box is mirrored anyway
  308. Vector2 n = p_transform.basis_xform_inv(p_normal).normalized();
  309. real_t h = (n.y > 0) ? height : -height;
  310. n *= radius;
  311. n.y += h * 0.5;
  312. r_max = p_normal.dot(p_transform.xform(n));
  313. r_min = p_normal.dot(p_transform.xform(-n));
  314. if (r_max < r_min) {
  315. SWAP(r_max, r_min);
  316. }
  317. //ERR_FAIL_COND( r_max < r_min );
  318. }
  319. DEFAULT_PROJECT_RANGE_CAST
  320. };
  321. class ConvexPolygonShape2DSW : public Shape2DSW {
  322. struct Point {
  323. Vector2 pos;
  324. Vector2 normal; //normal to next segment
  325. };
  326. Point *points;
  327. int point_count;
  328. public:
  329. _FORCE_INLINE_ int get_point_count() const { return point_count; }
  330. _FORCE_INLINE_ const Vector2 &get_point(int p_idx) const { return points[p_idx].pos; }
  331. _FORCE_INLINE_ const Vector2 &get_segment_normal(int p_idx) const { return points[p_idx].normal; }
  332. _FORCE_INLINE_ Vector2 get_xformed_segment_normal(const Transform2D &p_xform, int p_idx) const {
  333. Vector2 a = points[p_idx].pos;
  334. p_idx++;
  335. Vector2 b = points[p_idx == point_count ? 0 : p_idx].pos;
  336. return (p_xform.xform(b) - p_xform.xform(a)).normalized().tangent();
  337. }
  338. virtual Physics2DServer::ShapeType get_type() const { return Physics2DServer::SHAPE_CONVEX_POLYGON; }
  339. 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); }
  340. virtual void get_supports(const Vector2 &p_normal, Vector2 *r_supports, int &r_amount) const;
  341. virtual bool contains_point(const Vector2 &p_point) const;
  342. virtual bool intersect_segment(const Vector2 &p_begin, const Vector2 &p_end, Vector2 &r_point, Vector2 &r_normal) const;
  343. virtual real_t get_moment_of_inertia(real_t p_mass, const Size2 &p_scale) const;
  344. virtual void set_data(const Variant &p_data);
  345. virtual Variant get_data() const;
  346. _FORCE_INLINE_ void project_range(const Vector2 &p_normal, const Transform2D &p_transform, real_t &r_min, real_t &r_max) const {
  347. // no matter the angle, the box is mirrored anyway
  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. if (d < r_min)
  354. r_min = d;
  355. }
  356. }
  357. DEFAULT_PROJECT_RANGE_CAST
  358. ConvexPolygonShape2DSW();
  359. ~ConvexPolygonShape2DSW();
  360. };
  361. class ConcaveShape2DSW : public Shape2DSW {
  362. public:
  363. virtual bool is_concave() const { return true; }
  364. typedef void (*Callback)(void *p_userdata, Shape2DSW *p_convex);
  365. virtual void cull(const Rect2 &p_local_aabb, Callback p_callback, void *p_userdata) const = 0;
  366. };
  367. class ConcavePolygonShape2DSW : public ConcaveShape2DSW {
  368. struct Segment {
  369. int points[2];
  370. };
  371. Vector<Segment> segments;
  372. Vector<Point2> points;
  373. struct BVH {
  374. Rect2 aabb;
  375. int left, right;
  376. };
  377. Vector<BVH> bvh;
  378. int bvh_depth;
  379. struct BVH_CompareX {
  380. _FORCE_INLINE_ bool operator()(const BVH &a, const BVH &b) const {
  381. return (a.aabb.position.x + a.aabb.size.x * 0.5) < (b.aabb.position.x + b.aabb.size.x * 0.5);
  382. }
  383. };
  384. struct BVH_CompareY {
  385. _FORCE_INLINE_ bool operator()(const BVH &a, const BVH &b) const {
  386. return (a.aabb.position.y + a.aabb.size.y * 0.5) < (b.aabb.position.y + b.aabb.size.y * 0.5);
  387. }
  388. };
  389. int _generate_bvh(BVH *p_bvh, int p_len, int p_depth);
  390. public:
  391. virtual Physics2DServer::ShapeType get_type() const { return Physics2DServer::SHAPE_CONCAVE_POLYGON; }
  392. 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);*/
  393. }
  394. 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);*/
  395. }
  396. virtual void get_supports(const Vector2 &p_normal, Vector2 *r_supports, int &r_amount) const;
  397. virtual bool contains_point(const Vector2 &p_point) const;
  398. virtual bool intersect_segment(const Vector2 &p_begin, const Vector2 &p_end, Vector2 &r_point, Vector2 &r_normal) const;
  399. virtual real_t get_moment_of_inertia(real_t p_mass, const Size2 &p_scale) const { return 0; }
  400. virtual void set_data(const Variant &p_data);
  401. virtual Variant get_data() const;
  402. virtual void cull(const Rect2 &p_local_aabb, Callback p_callback, void *p_userdata) const;
  403. DEFAULT_PROJECT_RANGE_CAST
  404. };
  405. #undef DEFAULT_PROJECT_RANGE_CAST
  406. #endif // SHAPE_2D_2DSW_H