shape_2d_sw.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  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. public:
  160. _FORCE_INLINE_ real_t get_length() const { return length; }
  161. virtual Physics2DServer::ShapeType get_type() const { return Physics2DServer::SHAPE_RAY; }
  162. 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); }
  163. virtual void get_supports(const Vector2 &p_normal, Vector2 *r_supports, int &r_amount) const;
  164. virtual bool contains_point(const Vector2 &p_point) const;
  165. virtual bool intersect_segment(const Vector2 &p_begin, const Vector2 &p_end, Vector2 &r_point, Vector2 &r_normal) const;
  166. virtual real_t get_moment_of_inertia(real_t p_mass, const Size2 &p_scale) const;
  167. virtual void set_data(const Variant &p_data);
  168. virtual Variant get_data() const;
  169. _FORCE_INLINE_ void project_range(const Vector2 &p_normal, const Transform2D &p_transform, real_t &r_min, real_t &r_max) const {
  170. //real large
  171. r_max = p_normal.dot(p_transform.get_origin());
  172. r_min = p_normal.dot(p_transform.xform(Vector2(0, length)));
  173. if (r_max < r_min) {
  174. SWAP(r_max, r_min);
  175. }
  176. }
  177. DEFAULT_PROJECT_RANGE_CAST
  178. _FORCE_INLINE_ RayShape2DSW() {}
  179. _FORCE_INLINE_ RayShape2DSW(real_t p_length) { length = p_length; }
  180. };
  181. class SegmentShape2DSW : public Shape2DSW {
  182. Vector2 a;
  183. Vector2 b;
  184. Vector2 n;
  185. public:
  186. _FORCE_INLINE_ const Vector2 &get_a() const { return a; }
  187. _FORCE_INLINE_ const Vector2 &get_b() const { return b; }
  188. _FORCE_INLINE_ const Vector2 &get_normal() const { return n; }
  189. virtual Physics2DServer::ShapeType get_type() const { return Physics2DServer::SHAPE_SEGMENT; }
  190. _FORCE_INLINE_ Vector2 get_xformed_normal(const Transform2D &p_xform) const {
  191. return (p_xform.xform(b) - p_xform.xform(a)).normalized().tangent();
  192. }
  193. 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); }
  194. virtual void get_supports(const Vector2 &p_normal, Vector2 *r_supports, int &r_amount) const;
  195. virtual bool contains_point(const Vector2 &p_point) const;
  196. virtual bool intersect_segment(const Vector2 &p_begin, const Vector2 &p_end, Vector2 &r_point, Vector2 &r_normal) const;
  197. virtual real_t get_moment_of_inertia(real_t p_mass, const Size2 &p_scale) const;
  198. virtual void set_data(const Variant &p_data);
  199. virtual Variant get_data() const;
  200. _FORCE_INLINE_ void project_range(const Vector2 &p_normal, const Transform2D &p_transform, real_t &r_min, real_t &r_max) const {
  201. //real large
  202. r_max = p_normal.dot(p_transform.xform(a));
  203. r_min = p_normal.dot(p_transform.xform(b));
  204. if (r_max < r_min) {
  205. SWAP(r_max, r_min);
  206. }
  207. }
  208. DEFAULT_PROJECT_RANGE_CAST
  209. _FORCE_INLINE_ SegmentShape2DSW() {}
  210. _FORCE_INLINE_ SegmentShape2DSW(const Vector2 &p_a, const Vector2 &p_b, const Vector2 &p_n) {
  211. a = p_a;
  212. b = p_b;
  213. n = p_n;
  214. }
  215. };
  216. class CircleShape2DSW : public Shape2DSW {
  217. real_t radius;
  218. public:
  219. _FORCE_INLINE_ const real_t &get_radius() const { return radius; }
  220. virtual Physics2DServer::ShapeType get_type() const { return Physics2DServer::SHAPE_CIRCLE; }
  221. 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); }
  222. virtual void get_supports(const Vector2 &p_normal, Vector2 *r_supports, int &r_amount) const;
  223. virtual bool contains_point(const Vector2 &p_point) const;
  224. virtual bool intersect_segment(const Vector2 &p_begin, const Vector2 &p_end, Vector2 &r_point, Vector2 &r_normal) const;
  225. virtual real_t get_moment_of_inertia(real_t p_mass, const Size2 &p_scale) const;
  226. virtual void set_data(const Variant &p_data);
  227. virtual Variant get_data() const;
  228. _FORCE_INLINE_ void project_range(const Vector2 &p_normal, const Transform2D &p_transform, real_t &r_min, real_t &r_max) const {
  229. //real large
  230. real_t d = p_normal.dot(p_transform.get_origin());
  231. // figure out scale at point
  232. Vector2 local_normal = p_transform.basis_xform_inv(p_normal);
  233. real_t scale = local_normal.length();
  234. r_min = d - (radius)*scale;
  235. r_max = d + (radius)*scale;
  236. }
  237. DEFAULT_PROJECT_RANGE_CAST
  238. };
  239. class RectangleShape2DSW : public Shape2DSW {
  240. Vector2 half_extents;
  241. public:
  242. _FORCE_INLINE_ const Vector2 &get_half_extents() const { return half_extents; }
  243. virtual Physics2DServer::ShapeType get_type() const { return Physics2DServer::SHAPE_RECTANGLE; }
  244. 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); }
  245. virtual void get_supports(const Vector2 &p_normal, Vector2 *r_supports, int &r_amount) const;
  246. virtual bool contains_point(const Vector2 &p_point) const;
  247. virtual bool intersect_segment(const Vector2 &p_begin, const Vector2 &p_end, Vector2 &r_point, Vector2 &r_normal) const;
  248. virtual real_t get_moment_of_inertia(real_t p_mass, const Size2 &p_scale) const;
  249. virtual void set_data(const Variant &p_data);
  250. virtual Variant get_data() const;
  251. _FORCE_INLINE_ void project_range(const Vector2 &p_normal, const Transform2D &p_transform, real_t &r_min, real_t &r_max) const {
  252. // no matter the angle, the box is mirrored anyway
  253. r_max = -1e20;
  254. r_min = 1e20;
  255. for (int i = 0; i < 4; i++) {
  256. real_t d = p_normal.dot(p_transform.xform(Vector2(((i & 1) * 2 - 1) * half_extents.x, ((i >> 1) * 2 - 1) * half_extents.y)));
  257. if (d > r_max)
  258. r_max = d;
  259. if (d < r_min)
  260. r_min = d;
  261. }
  262. }
  263. _FORCE_INLINE_ Vector2 get_circle_axis(const Transform2D &p_xform, const Transform2D &p_xform_inv, const Vector2 &p_circle) const {
  264. Vector2 local_v = p_xform_inv.xform(p_circle);
  265. Vector2 he(
  266. (local_v.x < 0) ? -half_extents.x : half_extents.x,
  267. (local_v.y < 0) ? -half_extents.y : half_extents.y);
  268. return (p_xform.xform(he) - p_circle).normalized();
  269. }
  270. _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 {
  271. Vector2 a, b;
  272. {
  273. Vector2 local_v = p_xform_inv.xform(p_B_xform.get_origin());
  274. Vector2 he(
  275. (local_v.x < 0) ? -half_extents.x : half_extents.x,
  276. (local_v.y < 0) ? -half_extents.y : half_extents.y);
  277. a = p_xform.xform(he);
  278. }
  279. {
  280. Vector2 local_v = p_B_xform_inv.xform(p_xform.get_origin());
  281. Vector2 he(
  282. (local_v.x < 0) ? -p_B->half_extents.x : p_B->half_extents.x,
  283. (local_v.y < 0) ? -p_B->half_extents.y : p_B->half_extents.y);
  284. b = p_B_xform.xform(he);
  285. }
  286. return (a - b).normalized();
  287. }
  288. DEFAULT_PROJECT_RANGE_CAST
  289. };
  290. class CapsuleShape2DSW : public Shape2DSW {
  291. real_t radius;
  292. real_t height;
  293. public:
  294. _FORCE_INLINE_ const real_t &get_radius() const { return radius; }
  295. _FORCE_INLINE_ const real_t &get_height() const { return height; }
  296. virtual Physics2DServer::ShapeType get_type() const { return Physics2DServer::SHAPE_CAPSULE; }
  297. 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); }
  298. virtual void get_supports(const Vector2 &p_normal, Vector2 *r_supports, int &r_amount) const;
  299. virtual bool contains_point(const Vector2 &p_point) const;
  300. virtual bool intersect_segment(const Vector2 &p_begin, const Vector2 &p_end, Vector2 &r_point, Vector2 &r_normal) const;
  301. virtual real_t get_moment_of_inertia(real_t p_mass, const Size2 &p_scale) const;
  302. virtual void set_data(const Variant &p_data);
  303. virtual Variant get_data() const;
  304. _FORCE_INLINE_ void project_range(const Vector2 &p_normal, const Transform2D &p_transform, real_t &r_min, real_t &r_max) const {
  305. // no matter the angle, the box is mirrored anyway
  306. Vector2 n = p_transform.basis_xform_inv(p_normal).normalized();
  307. real_t h = (n.y > 0) ? height : -height;
  308. n *= radius;
  309. n.y += h * 0.5;
  310. r_max = p_normal.dot(p_transform.xform(n));
  311. r_min = p_normal.dot(p_transform.xform(-n));
  312. if (r_max < r_min) {
  313. SWAP(r_max, r_min);
  314. }
  315. //ERR_FAIL_COND( r_max < r_min );
  316. }
  317. DEFAULT_PROJECT_RANGE_CAST
  318. };
  319. class ConvexPolygonShape2DSW : public Shape2DSW {
  320. struct Point {
  321. Vector2 pos;
  322. Vector2 normal; //normal to next segment
  323. };
  324. Point *points;
  325. int point_count;
  326. public:
  327. _FORCE_INLINE_ int get_point_count() const { return point_count; }
  328. _FORCE_INLINE_ const Vector2 &get_point(int p_idx) const { return points[p_idx].pos; }
  329. _FORCE_INLINE_ const Vector2 &get_segment_normal(int p_idx) const { return points[p_idx].normal; }
  330. _FORCE_INLINE_ Vector2 get_xformed_segment_normal(const Transform2D &p_xform, int p_idx) const {
  331. Vector2 a = points[p_idx].pos;
  332. p_idx++;
  333. Vector2 b = points[p_idx == point_count ? 0 : p_idx].pos;
  334. return (p_xform.xform(b) - p_xform.xform(a)).normalized().tangent();
  335. }
  336. virtual Physics2DServer::ShapeType get_type() const { return Physics2DServer::SHAPE_CONVEX_POLYGON; }
  337. 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); }
  338. virtual void get_supports(const Vector2 &p_normal, Vector2 *r_supports, int &r_amount) const;
  339. virtual bool contains_point(const Vector2 &p_point) const;
  340. virtual bool intersect_segment(const Vector2 &p_begin, const Vector2 &p_end, Vector2 &r_point, Vector2 &r_normal) const;
  341. virtual real_t get_moment_of_inertia(real_t p_mass, const Size2 &p_scale) const;
  342. virtual void set_data(const Variant &p_data);
  343. virtual Variant get_data() const;
  344. _FORCE_INLINE_ void project_range(const Vector2 &p_normal, const Transform2D &p_transform, real_t &r_min, real_t &r_max) const {
  345. // no matter the angle, the box is mirrored anyway
  346. r_min = r_max = p_normal.dot(p_transform.xform(points[0].pos));
  347. for (int i = 1; i < point_count; i++) {
  348. real_t d = p_normal.dot(p_transform.xform(points[i].pos));
  349. if (d > r_max)
  350. r_max = d;
  351. if (d < r_min)
  352. r_min = d;
  353. }
  354. }
  355. DEFAULT_PROJECT_RANGE_CAST
  356. ConvexPolygonShape2DSW();
  357. ~ConvexPolygonShape2DSW();
  358. };
  359. class ConcaveShape2DSW : public Shape2DSW {
  360. public:
  361. virtual bool is_concave() const { return true; }
  362. typedef void (*Callback)(void *p_userdata, Shape2DSW *p_convex);
  363. virtual void cull(const Rect2 &p_local_aabb, Callback p_callback, void *p_userdata) const = 0;
  364. };
  365. class ConcavePolygonShape2DSW : public ConcaveShape2DSW {
  366. struct Segment {
  367. int points[2];
  368. };
  369. Vector<Segment> segments;
  370. Vector<Point2> points;
  371. struct BVH {
  372. Rect2 aabb;
  373. int left, right;
  374. };
  375. Vector<BVH> bvh;
  376. int bvh_depth;
  377. struct BVH_CompareX {
  378. _FORCE_INLINE_ bool operator()(const BVH &a, const BVH &b) const {
  379. return (a.aabb.position.x + a.aabb.size.x * 0.5) < (b.aabb.position.x + b.aabb.size.x * 0.5);
  380. }
  381. };
  382. struct BVH_CompareY {
  383. _FORCE_INLINE_ bool operator()(const BVH &a, const BVH &b) const {
  384. return (a.aabb.position.y + a.aabb.size.y * 0.5) < (b.aabb.position.y + b.aabb.size.y * 0.5);
  385. }
  386. };
  387. int _generate_bvh(BVH *p_bvh, int p_len, int p_depth);
  388. public:
  389. virtual Physics2DServer::ShapeType get_type() const { return Physics2DServer::SHAPE_CONCAVE_POLYGON; }
  390. 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);*/
  391. }
  392. 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);*/
  393. }
  394. virtual void get_supports(const Vector2 &p_normal, Vector2 *r_supports, int &r_amount) const;
  395. virtual bool contains_point(const Vector2 &p_point) const;
  396. virtual bool intersect_segment(const Vector2 &p_begin, const Vector2 &p_end, Vector2 &r_point, Vector2 &r_normal) const;
  397. virtual real_t get_moment_of_inertia(real_t p_mass, const Size2 &p_scale) const { return 0; }
  398. virtual void set_data(const Variant &p_data);
  399. virtual Variant get_data() const;
  400. virtual void cull(const Rect2 &p_local_aabb, Callback p_callback, void *p_userdata) const;
  401. DEFAULT_PROJECT_RANGE_CAST
  402. };
  403. #undef DEFAULT_PROJECT_RANGE_CAST
  404. #endif // SHAPE_2D_2DSW_H