shape_sw.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  1. /*************************************************************************/
  2. /* shape_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_SW_H
  31. #define SHAPE_SW_H
  32. #include "core/math/bsp_tree.h"
  33. #include "core/math/geometry.h"
  34. #include "servers/physics_server.h"
  35. /*
  36. SHAPE_LINE, ///< plane:"plane"
  37. SHAPE_SEGMENT, ///< real_t:"length"
  38. SHAPE_CIRCLE, ///< real_t:"radius"
  39. SHAPE_RECTANGLE, ///< vec3:"extents"
  40. SHAPE_CONVEX_POLYGON, ///< array of planes:"planes"
  41. SHAPE_CONCAVE_POLYGON, ///< Vector3 array:"triangles" , or Dictionary with "indices" (int array) and "triangles" (Vector3 array)
  42. SHAPE_CUSTOM, ///< Server-Implementation based custom shape, calling shape_create() with this value will result in an error
  43. */
  44. class ShapeSW;
  45. class ShapeOwnerSW : public RID_Data {
  46. public:
  47. virtual void _shape_changed() = 0;
  48. virtual void remove_shape(ShapeSW *p_shape) = 0;
  49. virtual ~ShapeOwnerSW() {}
  50. };
  51. class ShapeSW : public RID_Data {
  52. RID self;
  53. AABB aabb;
  54. bool configured;
  55. real_t custom_bias;
  56. Map<ShapeOwnerSW *, int> owners;
  57. protected:
  58. void configure(const AABB &p_aabb);
  59. public:
  60. enum {
  61. MAX_SUPPORTS = 8
  62. };
  63. virtual real_t get_area() const { return aabb.get_area(); }
  64. _FORCE_INLINE_ void set_self(const RID &p_self) { self = p_self; }
  65. _FORCE_INLINE_ RID get_self() const { return self; }
  66. virtual PhysicsServer::ShapeType get_type() const = 0;
  67. _FORCE_INLINE_ AABB get_aabb() const { return aabb; }
  68. _FORCE_INLINE_ bool is_configured() const { return configured; }
  69. virtual bool is_concave() const { return false; }
  70. virtual void project_range(const Vector3 &p_normal, const Transform &p_transform, real_t &r_min, real_t &r_max) const = 0;
  71. virtual Vector3 get_support(const Vector3 &p_normal) const;
  72. virtual void get_supports(const Vector3 &p_normal, int p_max, Vector3 *r_supports, int &r_amount) const = 0;
  73. virtual Vector3 get_closest_point_to(const Vector3 &p_point) const = 0;
  74. virtual bool intersect_segment(const Vector3 &p_begin, const Vector3 &p_end, Vector3 &r_point, Vector3 &r_normal) const = 0;
  75. virtual bool intersect_point(const Vector3 &p_point) const = 0;
  76. virtual Vector3 get_moment_of_inertia(real_t p_mass) const = 0;
  77. virtual void set_data(const Variant &p_data) = 0;
  78. virtual Variant get_data() const = 0;
  79. _FORCE_INLINE_ void set_custom_bias(real_t p_bias) { custom_bias = p_bias; }
  80. _FORCE_INLINE_ real_t get_custom_bias() const { return custom_bias; }
  81. void add_owner(ShapeOwnerSW *p_owner);
  82. void remove_owner(ShapeOwnerSW *p_owner);
  83. bool is_owner(ShapeOwnerSW *p_owner) const;
  84. const Map<ShapeOwnerSW *, int> &get_owners() const;
  85. ShapeSW();
  86. virtual ~ShapeSW();
  87. };
  88. class ConcaveShapeSW : public ShapeSW {
  89. public:
  90. virtual bool is_concave() const { return true; }
  91. typedef void (*Callback)(void *p_userdata, ShapeSW *p_convex);
  92. virtual void get_supports(const Vector3 &p_normal, int p_max, Vector3 *r_supports, int &r_amount) const { r_amount = 0; }
  93. virtual void cull(const AABB &p_local_aabb, Callback p_callback, void *p_userdata) const = 0;
  94. ConcaveShapeSW() {}
  95. };
  96. class PlaneShapeSW : public ShapeSW {
  97. Plane plane;
  98. void _setup(const Plane &p_plane);
  99. public:
  100. Plane get_plane() const;
  101. virtual real_t get_area() const { return Math_INF; }
  102. virtual PhysicsServer::ShapeType get_type() const { return PhysicsServer::SHAPE_PLANE; }
  103. virtual void project_range(const Vector3 &p_normal, const Transform &p_transform, real_t &r_min, real_t &r_max) const;
  104. virtual Vector3 get_support(const Vector3 &p_normal) const;
  105. virtual void get_supports(const Vector3 &p_normal, int p_max, Vector3 *r_supports, int &r_amount) const { r_amount = 0; }
  106. virtual bool intersect_segment(const Vector3 &p_begin, const Vector3 &p_end, Vector3 &r_result, Vector3 &r_normal) const;
  107. virtual bool intersect_point(const Vector3 &p_point) const;
  108. virtual Vector3 get_closest_point_to(const Vector3 &p_point) const;
  109. virtual Vector3 get_moment_of_inertia(real_t p_mass) const;
  110. virtual void set_data(const Variant &p_data);
  111. virtual Variant get_data() const;
  112. PlaneShapeSW();
  113. };
  114. class RayShapeSW : public ShapeSW {
  115. real_t length;
  116. bool slips_on_slope;
  117. void _setup(real_t p_length, bool p_slips_on_slope);
  118. public:
  119. real_t get_length() const;
  120. bool get_slips_on_slope() const;
  121. virtual real_t get_area() const { return 0.0; }
  122. virtual PhysicsServer::ShapeType get_type() const { return PhysicsServer::SHAPE_RAY; }
  123. virtual void project_range(const Vector3 &p_normal, const Transform &p_transform, real_t &r_min, real_t &r_max) const;
  124. virtual Vector3 get_support(const Vector3 &p_normal) const;
  125. virtual void get_supports(const Vector3 &p_normal, int p_max, Vector3 *r_supports, int &r_amount) const;
  126. virtual bool intersect_segment(const Vector3 &p_begin, const Vector3 &p_end, Vector3 &r_result, Vector3 &r_normal) const;
  127. virtual bool intersect_point(const Vector3 &p_point) const;
  128. virtual Vector3 get_closest_point_to(const Vector3 &p_point) const;
  129. virtual Vector3 get_moment_of_inertia(real_t p_mass) const;
  130. virtual void set_data(const Variant &p_data);
  131. virtual Variant get_data() const;
  132. RayShapeSW();
  133. };
  134. class SphereShapeSW : public ShapeSW {
  135. real_t radius;
  136. void _setup(real_t p_radius);
  137. public:
  138. real_t get_radius() const;
  139. virtual real_t get_area() const { return 4.0 / 3.0 * Math_PI * radius * radius * radius; }
  140. virtual PhysicsServer::ShapeType get_type() const { return PhysicsServer::SHAPE_SPHERE; }
  141. virtual void project_range(const Vector3 &p_normal, const Transform &p_transform, real_t &r_min, real_t &r_max) const;
  142. virtual Vector3 get_support(const Vector3 &p_normal) const;
  143. virtual void get_supports(const Vector3 &p_normal, int p_max, Vector3 *r_supports, int &r_amount) const;
  144. virtual bool intersect_segment(const Vector3 &p_begin, const Vector3 &p_end, Vector3 &r_result, Vector3 &r_normal) const;
  145. virtual bool intersect_point(const Vector3 &p_point) const;
  146. virtual Vector3 get_closest_point_to(const Vector3 &p_point) const;
  147. virtual Vector3 get_moment_of_inertia(real_t p_mass) const;
  148. virtual void set_data(const Variant &p_data);
  149. virtual Variant get_data() const;
  150. SphereShapeSW();
  151. };
  152. class BoxShapeSW : public ShapeSW {
  153. Vector3 half_extents;
  154. void _setup(const Vector3 &p_half_extents);
  155. public:
  156. _FORCE_INLINE_ Vector3 get_half_extents() const { return half_extents; }
  157. virtual real_t get_area() const { return 8 * half_extents.x * half_extents.y * half_extents.z; }
  158. virtual PhysicsServer::ShapeType get_type() const { return PhysicsServer::SHAPE_BOX; }
  159. virtual void project_range(const Vector3 &p_normal, const Transform &p_transform, real_t &r_min, real_t &r_max) const;
  160. virtual Vector3 get_support(const Vector3 &p_normal) const;
  161. virtual void get_supports(const Vector3 &p_normal, int p_max, Vector3 *r_supports, int &r_amount) const;
  162. virtual bool intersect_segment(const Vector3 &p_begin, const Vector3 &p_end, Vector3 &r_result, Vector3 &r_normal) const;
  163. virtual bool intersect_point(const Vector3 &p_point) const;
  164. virtual Vector3 get_closest_point_to(const Vector3 &p_point) const;
  165. virtual Vector3 get_moment_of_inertia(real_t p_mass) const;
  166. virtual void set_data(const Variant &p_data);
  167. virtual Variant get_data() const;
  168. BoxShapeSW();
  169. };
  170. class CapsuleShapeSW : public ShapeSW {
  171. real_t height;
  172. real_t radius;
  173. void _setup(real_t p_height, real_t p_radius);
  174. public:
  175. _FORCE_INLINE_ real_t get_height() const { return height; }
  176. _FORCE_INLINE_ real_t get_radius() const { return radius; }
  177. virtual real_t get_area() const { return 4.0 / 3.0 * Math_PI * radius * radius * radius + height * Math_PI * radius * radius; }
  178. virtual PhysicsServer::ShapeType get_type() const { return PhysicsServer::SHAPE_CAPSULE; }
  179. virtual void project_range(const Vector3 &p_normal, const Transform &p_transform, real_t &r_min, real_t &r_max) const;
  180. virtual Vector3 get_support(const Vector3 &p_normal) const;
  181. virtual void get_supports(const Vector3 &p_normal, int p_max, Vector3 *r_supports, int &r_amount) const;
  182. virtual bool intersect_segment(const Vector3 &p_begin, const Vector3 &p_end, Vector3 &r_result, Vector3 &r_normal) const;
  183. virtual bool intersect_point(const Vector3 &p_point) const;
  184. virtual Vector3 get_closest_point_to(const Vector3 &p_point) const;
  185. virtual Vector3 get_moment_of_inertia(real_t p_mass) const;
  186. virtual void set_data(const Variant &p_data);
  187. virtual Variant get_data() const;
  188. CapsuleShapeSW();
  189. };
  190. struct ConvexPolygonShapeSW : public ShapeSW {
  191. Geometry::MeshData mesh;
  192. void _setup(const Vector<Vector3> &p_vertices);
  193. public:
  194. const Geometry::MeshData &get_mesh() const { return mesh; }
  195. virtual PhysicsServer::ShapeType get_type() const { return PhysicsServer::SHAPE_CONVEX_POLYGON; }
  196. virtual void project_range(const Vector3 &p_normal, const Transform &p_transform, real_t &r_min, real_t &r_max) const;
  197. virtual Vector3 get_support(const Vector3 &p_normal) const;
  198. virtual void get_supports(const Vector3 &p_normal, int p_max, Vector3 *r_supports, int &r_amount) const;
  199. virtual bool intersect_segment(const Vector3 &p_begin, const Vector3 &p_end, Vector3 &r_result, Vector3 &r_normal) const;
  200. virtual bool intersect_point(const Vector3 &p_point) const;
  201. virtual Vector3 get_closest_point_to(const Vector3 &p_point) const;
  202. virtual Vector3 get_moment_of_inertia(real_t p_mass) const;
  203. virtual void set_data(const Variant &p_data);
  204. virtual Variant get_data() const;
  205. ConvexPolygonShapeSW();
  206. };
  207. struct _VolumeSW_BVH;
  208. struct FaceShapeSW;
  209. struct ConcavePolygonShapeSW : public ConcaveShapeSW {
  210. // always a trimesh
  211. struct Face {
  212. Vector3 normal;
  213. int indices[3];
  214. };
  215. PoolVector<Face> faces;
  216. PoolVector<Vector3> vertices;
  217. struct BVH {
  218. AABB aabb;
  219. int left;
  220. int right;
  221. int face_index;
  222. };
  223. PoolVector<BVH> bvh;
  224. struct _CullParams {
  225. AABB aabb;
  226. Callback callback;
  227. void *userdata;
  228. const Face *faces;
  229. const Vector3 *vertices;
  230. const BVH *bvh;
  231. FaceShapeSW *face;
  232. };
  233. struct _SegmentCullParams {
  234. Vector3 from;
  235. Vector3 to;
  236. const Face *faces;
  237. const Vector3 *vertices;
  238. const BVH *bvh;
  239. Vector3 dir;
  240. Vector3 result;
  241. Vector3 normal;
  242. real_t min_d;
  243. int collisions;
  244. };
  245. void _cull_segment(int p_idx, _SegmentCullParams *p_params) const;
  246. void _cull(int p_idx, _CullParams *p_params) const;
  247. void _fill_bvh(_VolumeSW_BVH *p_bvh_tree, BVH *p_bvh_array, int &p_idx);
  248. void _setup(PoolVector<Vector3> p_faces);
  249. public:
  250. PoolVector<Vector3> get_faces() const;
  251. virtual PhysicsServer::ShapeType get_type() const { return PhysicsServer::SHAPE_CONCAVE_POLYGON; }
  252. virtual void project_range(const Vector3 &p_normal, const Transform &p_transform, real_t &r_min, real_t &r_max) const;
  253. virtual Vector3 get_support(const Vector3 &p_normal) const;
  254. virtual bool intersect_segment(const Vector3 &p_begin, const Vector3 &p_end, Vector3 &r_result, Vector3 &r_normal) const;
  255. virtual bool intersect_point(const Vector3 &p_point) const;
  256. virtual Vector3 get_closest_point_to(const Vector3 &p_point) const;
  257. virtual void cull(const AABB &p_local_aabb, Callback p_callback, void *p_userdata) const;
  258. virtual Vector3 get_moment_of_inertia(real_t p_mass) const;
  259. virtual void set_data(const Variant &p_data);
  260. virtual Variant get_data() const;
  261. ConcavePolygonShapeSW();
  262. };
  263. struct HeightMapShapeSW : public ConcaveShapeSW {
  264. PoolVector<real_t> heights;
  265. int width;
  266. int depth;
  267. real_t cell_size;
  268. //void _cull_segment(int p_idx,_SegmentCullParams *p_params) const;
  269. //void _cull(int p_idx,_CullParams *p_params) const;
  270. void _setup(PoolVector<real_t> p_heights, int p_width, int p_depth, real_t p_cell_size);
  271. public:
  272. PoolVector<real_t> get_heights() const;
  273. int get_width() const;
  274. int get_depth() const;
  275. real_t get_cell_size() const;
  276. virtual PhysicsServer::ShapeType get_type() const { return PhysicsServer::SHAPE_HEIGHTMAP; }
  277. virtual void project_range(const Vector3 &p_normal, const Transform &p_transform, real_t &r_min, real_t &r_max) const;
  278. virtual Vector3 get_support(const Vector3 &p_normal) const;
  279. virtual bool intersect_segment(const Vector3 &p_begin, const Vector3 &p_end, Vector3 &r_point, Vector3 &r_normal) const;
  280. virtual bool intersect_point(const Vector3 &p_point) const;
  281. virtual Vector3 get_closest_point_to(const Vector3 &p_point) const;
  282. virtual void cull(const AABB &p_local_aabb, Callback p_callback, void *p_userdata) const;
  283. virtual Vector3 get_moment_of_inertia(real_t p_mass) const;
  284. virtual void set_data(const Variant &p_data);
  285. virtual Variant get_data() const;
  286. HeightMapShapeSW();
  287. };
  288. //used internally
  289. struct FaceShapeSW : public ShapeSW {
  290. Vector3 normal; //cache
  291. Vector3 vertex[3];
  292. virtual PhysicsServer::ShapeType get_type() const { return PhysicsServer::SHAPE_CONCAVE_POLYGON; }
  293. const Vector3 &get_vertex(int p_idx) const { return vertex[p_idx]; }
  294. void project_range(const Vector3 &p_normal, const Transform &p_transform, real_t &r_min, real_t &r_max) const;
  295. Vector3 get_support(const Vector3 &p_normal) const;
  296. virtual void get_supports(const Vector3 &p_normal, int p_max, Vector3 *r_supports, int &r_amount) const;
  297. bool intersect_segment(const Vector3 &p_begin, const Vector3 &p_end, Vector3 &r_result, Vector3 &r_normal) const;
  298. virtual bool intersect_point(const Vector3 &p_point) const;
  299. virtual Vector3 get_closest_point_to(const Vector3 &p_point) const;
  300. Vector3 get_moment_of_inertia(real_t p_mass) const;
  301. virtual void set_data(const Variant &p_data) {}
  302. virtual Variant get_data() const { return Variant(); }
  303. FaceShapeSW();
  304. };
  305. struct MotionShapeSW : public ShapeSW {
  306. ShapeSW *shape;
  307. Vector3 motion;
  308. virtual PhysicsServer::ShapeType get_type() const { return PhysicsServer::SHAPE_CONVEX_POLYGON; }
  309. void project_range(const Vector3 &p_normal, const Transform &p_transform, real_t &r_min, real_t &r_max) const {
  310. Vector3 cast = p_transform.basis.xform(motion);
  311. real_t mina, maxa;
  312. real_t minb, maxb;
  313. Transform ofsb = p_transform;
  314. ofsb.origin += cast;
  315. shape->project_range(p_normal, p_transform, mina, maxa);
  316. shape->project_range(p_normal, ofsb, minb, maxb);
  317. r_min = MIN(mina, minb);
  318. r_max = MAX(maxa, maxb);
  319. }
  320. Vector3 get_support(const Vector3 &p_normal) const {
  321. Vector3 support = shape->get_support(p_normal);
  322. if (p_normal.dot(motion) > 0) {
  323. support += motion;
  324. }
  325. return support;
  326. }
  327. virtual void get_supports(const Vector3 &p_normal, int p_max, Vector3 *r_supports, int &r_amount) const { r_amount = 0; }
  328. bool intersect_segment(const Vector3 &p_begin, const Vector3 &p_end, Vector3 &r_result, Vector3 &r_normal) const { return false; }
  329. virtual bool intersect_point(const Vector3 &p_point) const { return false; }
  330. virtual Vector3 get_closest_point_to(const Vector3 &p_point) const { return p_point; }
  331. Vector3 get_moment_of_inertia(real_t p_mass) const { return Vector3(); }
  332. virtual void set_data(const Variant &p_data) {}
  333. virtual Variant get_data() const { return Variant(); }
  334. MotionShapeSW() { configure(AABB()); }
  335. };
  336. struct _ShapeTestConvexBSPSW {
  337. const BSP_Tree *bsp;
  338. const ShapeSW *shape;
  339. Transform transform;
  340. _FORCE_INLINE_ void project_range(const Vector3 &p_normal, real_t &r_min, real_t &r_max) const {
  341. shape->project_range(p_normal, transform, r_min, r_max);
  342. }
  343. };
  344. #endif // SHAPESW_H