shape_sw.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  1. /*************************************************************************/
  2. /* shape_sw.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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 FeatureType {
  61. FEATURE_POINT,
  62. FEATURE_EDGE,
  63. FEATURE_FACE,
  64. FEATURE_CIRCLE,
  65. };
  66. virtual real_t get_area() const { return aabb.get_area(); }
  67. _FORCE_INLINE_ void set_self(const RID &p_self) { self = p_self; }
  68. _FORCE_INLINE_ RID get_self() const { return self; }
  69. virtual PhysicsServer::ShapeType get_type() const = 0;
  70. _FORCE_INLINE_ const AABB &get_aabb() const { return aabb; }
  71. _FORCE_INLINE_ bool is_configured() const { return configured; }
  72. virtual bool is_concave() const { return false; }
  73. virtual void project_range(const Vector3 &p_normal, const Transform &p_transform, real_t &r_min, real_t &r_max) const = 0;
  74. virtual Vector3 get_support(const Vector3 &p_normal) const;
  75. virtual void get_supports(const Vector3 &p_normal, int p_max, Vector3 *r_supports, int &r_amount, FeatureType &r_type) const = 0;
  76. virtual Vector3 get_closest_point_to(const Vector3 &p_point) const = 0;
  77. virtual bool intersect_segment(const Vector3 &p_begin, const Vector3 &p_end, Vector3 &r_point, Vector3 &r_normal) const = 0;
  78. virtual bool intersect_point(const Vector3 &p_point) const = 0;
  79. virtual Vector3 get_moment_of_inertia(real_t p_mass) const = 0;
  80. virtual void set_data(const Variant &p_data) = 0;
  81. virtual Variant get_data() const = 0;
  82. _FORCE_INLINE_ void set_custom_bias(real_t p_bias) { custom_bias = p_bias; }
  83. _FORCE_INLINE_ real_t get_custom_bias() const { return custom_bias; }
  84. void add_owner(ShapeOwnerSW *p_owner);
  85. void remove_owner(ShapeOwnerSW *p_owner);
  86. bool is_owner(ShapeOwnerSW *p_owner) const;
  87. const Map<ShapeOwnerSW *, int> &get_owners() const;
  88. ShapeSW();
  89. virtual ~ShapeSW();
  90. };
  91. class ConcaveShapeSW : public ShapeSW {
  92. public:
  93. virtual bool is_concave() const { return true; }
  94. typedef void (*Callback)(void *p_userdata, ShapeSW *p_convex);
  95. virtual void get_supports(const Vector3 &p_normal, int p_max, Vector3 *r_supports, int &r_amount, FeatureType &r_type) const { r_amount = 0; }
  96. virtual void cull(const AABB &p_local_aabb, Callback p_callback, void *p_userdata) const = 0;
  97. ConcaveShapeSW() {}
  98. };
  99. class PlaneShapeSW : public ShapeSW {
  100. Plane plane;
  101. void _setup(const Plane &p_plane);
  102. public:
  103. Plane get_plane() const;
  104. virtual real_t get_area() const { return Math_INF; }
  105. virtual PhysicsServer::ShapeType get_type() const { return PhysicsServer::SHAPE_PLANE; }
  106. virtual void project_range(const Vector3 &p_normal, const Transform &p_transform, real_t &r_min, real_t &r_max) const;
  107. virtual Vector3 get_support(const Vector3 &p_normal) const;
  108. virtual void get_supports(const Vector3 &p_normal, int p_max, Vector3 *r_supports, int &r_amount, FeatureType &r_type) const { r_amount = 0; }
  109. virtual bool intersect_segment(const Vector3 &p_begin, const Vector3 &p_end, Vector3 &r_result, Vector3 &r_normal) const;
  110. virtual bool intersect_point(const Vector3 &p_point) const;
  111. virtual Vector3 get_closest_point_to(const Vector3 &p_point) const;
  112. virtual Vector3 get_moment_of_inertia(real_t p_mass) const;
  113. virtual void set_data(const Variant &p_data);
  114. virtual Variant get_data() const;
  115. PlaneShapeSW();
  116. };
  117. class RayShapeSW : public ShapeSW {
  118. real_t length;
  119. bool slips_on_slope;
  120. void _setup(real_t p_length, bool p_slips_on_slope);
  121. public:
  122. real_t get_length() const;
  123. bool get_slips_on_slope() const;
  124. virtual real_t get_area() const { return 0.0; }
  125. virtual PhysicsServer::ShapeType get_type() const { return PhysicsServer::SHAPE_RAY; }
  126. virtual void project_range(const Vector3 &p_normal, const Transform &p_transform, real_t &r_min, real_t &r_max) const;
  127. virtual Vector3 get_support(const Vector3 &p_normal) const;
  128. virtual void get_supports(const Vector3 &p_normal, int p_max, Vector3 *r_supports, int &r_amount, FeatureType &r_type) const;
  129. virtual bool intersect_segment(const Vector3 &p_begin, const Vector3 &p_end, Vector3 &r_result, Vector3 &r_normal) const;
  130. virtual bool intersect_point(const Vector3 &p_point) const;
  131. virtual Vector3 get_closest_point_to(const Vector3 &p_point) const;
  132. virtual Vector3 get_moment_of_inertia(real_t p_mass) const;
  133. virtual void set_data(const Variant &p_data);
  134. virtual Variant get_data() const;
  135. RayShapeSW();
  136. };
  137. class SphereShapeSW : public ShapeSW {
  138. real_t radius;
  139. void _setup(real_t p_radius);
  140. public:
  141. real_t get_radius() const;
  142. virtual real_t get_area() const { return 4.0 / 3.0 * Math_PI * radius * radius * radius; }
  143. virtual PhysicsServer::ShapeType get_type() const { return PhysicsServer::SHAPE_SPHERE; }
  144. virtual void project_range(const Vector3 &p_normal, const Transform &p_transform, real_t &r_min, real_t &r_max) const;
  145. virtual Vector3 get_support(const Vector3 &p_normal) const;
  146. virtual void get_supports(const Vector3 &p_normal, int p_max, Vector3 *r_supports, int &r_amount, FeatureType &r_type) const;
  147. virtual bool intersect_segment(const Vector3 &p_begin, const Vector3 &p_end, Vector3 &r_result, Vector3 &r_normal) const;
  148. virtual bool intersect_point(const Vector3 &p_point) const;
  149. virtual Vector3 get_closest_point_to(const Vector3 &p_point) const;
  150. virtual Vector3 get_moment_of_inertia(real_t p_mass) const;
  151. virtual void set_data(const Variant &p_data);
  152. virtual Variant get_data() const;
  153. SphereShapeSW();
  154. };
  155. class BoxShapeSW : public ShapeSW {
  156. Vector3 half_extents;
  157. void _setup(const Vector3 &p_half_extents);
  158. public:
  159. _FORCE_INLINE_ Vector3 get_half_extents() const { return half_extents; }
  160. virtual real_t get_area() const { return 8 * half_extents.x * half_extents.y * half_extents.z; }
  161. virtual PhysicsServer::ShapeType get_type() const { return PhysicsServer::SHAPE_BOX; }
  162. virtual void project_range(const Vector3 &p_normal, const Transform &p_transform, real_t &r_min, real_t &r_max) const;
  163. virtual Vector3 get_support(const Vector3 &p_normal) const;
  164. virtual void get_supports(const Vector3 &p_normal, int p_max, Vector3 *r_supports, int &r_amount, FeatureType &r_type) const;
  165. virtual bool intersect_segment(const Vector3 &p_begin, const Vector3 &p_end, Vector3 &r_result, Vector3 &r_normal) const;
  166. virtual bool intersect_point(const Vector3 &p_point) const;
  167. virtual Vector3 get_closest_point_to(const Vector3 &p_point) const;
  168. virtual Vector3 get_moment_of_inertia(real_t p_mass) const;
  169. virtual void set_data(const Variant &p_data);
  170. virtual Variant get_data() const;
  171. BoxShapeSW();
  172. };
  173. class CapsuleShapeSW : public ShapeSW {
  174. real_t height;
  175. real_t radius;
  176. void _setup(real_t p_height, real_t p_radius);
  177. public:
  178. _FORCE_INLINE_ real_t get_height() const { return height; }
  179. _FORCE_INLINE_ real_t get_radius() const { return radius; }
  180. virtual real_t get_area() const { return 4.0 / 3.0 * Math_PI * radius * radius * radius + height * Math_PI * radius * radius; }
  181. virtual PhysicsServer::ShapeType get_type() const { return PhysicsServer::SHAPE_CAPSULE; }
  182. virtual void project_range(const Vector3 &p_normal, const Transform &p_transform, real_t &r_min, real_t &r_max) const;
  183. virtual Vector3 get_support(const Vector3 &p_normal) const;
  184. virtual void get_supports(const Vector3 &p_normal, int p_max, Vector3 *r_supports, int &r_amount, FeatureType &r_type) const;
  185. virtual bool intersect_segment(const Vector3 &p_begin, const Vector3 &p_end, Vector3 &r_result, Vector3 &r_normal) const;
  186. virtual bool intersect_point(const Vector3 &p_point) const;
  187. virtual Vector3 get_closest_point_to(const Vector3 &p_point) const;
  188. virtual Vector3 get_moment_of_inertia(real_t p_mass) const;
  189. virtual void set_data(const Variant &p_data);
  190. virtual Variant get_data() const;
  191. CapsuleShapeSW();
  192. };
  193. class CylinderShapeSW : public ShapeSW {
  194. real_t height;
  195. real_t radius;
  196. void _setup(real_t p_height, real_t p_radius);
  197. public:
  198. _FORCE_INLINE_ real_t get_height() const { return height; }
  199. _FORCE_INLINE_ real_t get_radius() const { return radius; }
  200. virtual real_t get_area() const { return 4.0 / 3.0 * Math_PI * radius * radius * radius + height * Math_PI * radius * radius; }
  201. virtual PhysicsServer::ShapeType get_type() const { return PhysicsServer::SHAPE_CYLINDER; }
  202. virtual void project_range(const Vector3 &p_normal, const Transform &p_transform, real_t &r_min, real_t &r_max) const;
  203. virtual Vector3 get_support(const Vector3 &p_normal) const;
  204. virtual void get_supports(const Vector3 &p_normal, int p_max, Vector3 *r_supports, int &r_amount, FeatureType &r_type) const;
  205. virtual bool intersect_segment(const Vector3 &p_begin, const Vector3 &p_end, Vector3 &r_result, Vector3 &r_normal) const;
  206. virtual bool intersect_point(const Vector3 &p_point) const;
  207. virtual Vector3 get_closest_point_to(const Vector3 &p_point) const;
  208. virtual Vector3 get_moment_of_inertia(real_t p_mass) const;
  209. virtual void set_data(const Variant &p_data);
  210. virtual Variant get_data() const;
  211. CylinderShapeSW();
  212. };
  213. struct ConvexPolygonShapeSW : public ShapeSW {
  214. Geometry::MeshData mesh;
  215. void _setup(const Vector<Vector3> &p_vertices);
  216. public:
  217. const Geometry::MeshData &get_mesh() const { return mesh; }
  218. virtual PhysicsServer::ShapeType get_type() const { return PhysicsServer::SHAPE_CONVEX_POLYGON; }
  219. virtual void project_range(const Vector3 &p_normal, const Transform &p_transform, real_t &r_min, real_t &r_max) const;
  220. virtual Vector3 get_support(const Vector3 &p_normal) const;
  221. virtual void get_supports(const Vector3 &p_normal, int p_max, Vector3 *r_supports, int &r_amount, FeatureType &r_type) const;
  222. virtual bool intersect_segment(const Vector3 &p_begin, const Vector3 &p_end, Vector3 &r_result, Vector3 &r_normal) const;
  223. virtual bool intersect_point(const Vector3 &p_point) const;
  224. virtual Vector3 get_closest_point_to(const Vector3 &p_point) const;
  225. virtual Vector3 get_moment_of_inertia(real_t p_mass) const;
  226. virtual void set_data(const Variant &p_data);
  227. virtual Variant get_data() const;
  228. ConvexPolygonShapeSW();
  229. };
  230. struct _VolumeSW_BVH;
  231. struct FaceShapeSW;
  232. struct ConcavePolygonShapeSW : public ConcaveShapeSW {
  233. // always a trimesh
  234. struct Face {
  235. Vector3 normal;
  236. int indices[3];
  237. };
  238. PoolVector<Face> faces;
  239. PoolVector<Vector3> vertices;
  240. struct BVH {
  241. AABB aabb;
  242. int left;
  243. int right;
  244. int face_index;
  245. };
  246. PoolVector<BVH> bvh;
  247. struct _CullParams {
  248. AABB aabb;
  249. Callback callback;
  250. void *userdata;
  251. const Face *faces;
  252. const Vector3 *vertices;
  253. const BVH *bvh;
  254. FaceShapeSW *face;
  255. };
  256. struct _SegmentCullParams {
  257. Vector3 from;
  258. Vector3 to;
  259. const Face *faces;
  260. const Vector3 *vertices;
  261. const BVH *bvh;
  262. Vector3 dir;
  263. Vector3 result;
  264. Vector3 normal;
  265. real_t min_d;
  266. int collisions;
  267. };
  268. void _cull_segment(int p_idx, _SegmentCullParams *p_params) const;
  269. void _cull(int p_idx, _CullParams *p_params) const;
  270. void _fill_bvh(_VolumeSW_BVH *p_bvh_tree, BVH *p_bvh_array, int &p_idx);
  271. void _setup(PoolVector<Vector3> p_faces);
  272. public:
  273. PoolVector<Vector3> get_faces() const;
  274. virtual PhysicsServer::ShapeType get_type() const { return PhysicsServer::SHAPE_CONCAVE_POLYGON; }
  275. virtual void project_range(const Vector3 &p_normal, const Transform &p_transform, real_t &r_min, real_t &r_max) const;
  276. virtual Vector3 get_support(const Vector3 &p_normal) const;
  277. virtual bool intersect_segment(const Vector3 &p_begin, const Vector3 &p_end, Vector3 &r_result, Vector3 &r_normal) const;
  278. virtual bool intersect_point(const Vector3 &p_point) const;
  279. virtual Vector3 get_closest_point_to(const Vector3 &p_point) const;
  280. virtual void cull(const AABB &p_local_aabb, Callback p_callback, void *p_userdata) const;
  281. virtual Vector3 get_moment_of_inertia(real_t p_mass) const;
  282. virtual void set_data(const Variant &p_data);
  283. virtual Variant get_data() const;
  284. ConcavePolygonShapeSW();
  285. };
  286. struct HeightMapShapeSW : public ConcaveShapeSW {
  287. friend struct _HeightmapSegmentCullParams;
  288. PoolVector<real_t> heights;
  289. int width;
  290. int depth;
  291. Vector3 local_origin;
  292. _FORCE_INLINE_ real_t _get_height(int p_x, int p_z) const {
  293. return heights[(p_z * width) + p_x];
  294. }
  295. _FORCE_INLINE_ void _get_point(int p_x, int p_z, Vector3 &r_point) const {
  296. r_point.x = p_x - 0.5 * (width - 1.0);
  297. r_point.y = _get_height(p_x, p_z);
  298. r_point.z = p_z - 0.5 * (depth - 1.0);
  299. }
  300. void _get_cell(const Vector3 &p_point, int &r_x, int &r_y, int &r_z) const;
  301. void _setup(const PoolVector<real_t> &p_heights, int p_width, int p_depth, real_t p_min_height, real_t p_max_height);
  302. public:
  303. PoolVector<real_t> get_heights() const;
  304. int get_width() const;
  305. int get_depth() const;
  306. virtual PhysicsServer::ShapeType get_type() const { return PhysicsServer::SHAPE_HEIGHTMAP; }
  307. virtual void project_range(const Vector3 &p_normal, const Transform &p_transform, real_t &r_min, real_t &r_max) const;
  308. virtual Vector3 get_support(const Vector3 &p_normal) const;
  309. virtual bool intersect_segment(const Vector3 &p_begin, const Vector3 &p_end, Vector3 &r_point, Vector3 &r_normal) const;
  310. virtual bool intersect_point(const Vector3 &p_point) const;
  311. virtual Vector3 get_closest_point_to(const Vector3 &p_point) const;
  312. virtual void cull(const AABB &p_local_aabb, Callback p_callback, void *p_userdata) const;
  313. virtual Vector3 get_moment_of_inertia(real_t p_mass) const;
  314. virtual void set_data(const Variant &p_data);
  315. virtual Variant get_data() const;
  316. HeightMapShapeSW();
  317. };
  318. //used internally
  319. struct FaceShapeSW : public ShapeSW {
  320. Vector3 normal; //cache
  321. Vector3 vertex[3];
  322. virtual PhysicsServer::ShapeType get_type() const { return PhysicsServer::SHAPE_CONCAVE_POLYGON; }
  323. const Vector3 &get_vertex(int p_idx) const { return vertex[p_idx]; }
  324. void project_range(const Vector3 &p_normal, const Transform &p_transform, real_t &r_min, real_t &r_max) const;
  325. Vector3 get_support(const Vector3 &p_normal) const;
  326. virtual void get_supports(const Vector3 &p_normal, int p_max, Vector3 *r_supports, int &r_amount, FeatureType &r_type) const;
  327. bool intersect_segment(const Vector3 &p_begin, const Vector3 &p_end, Vector3 &r_result, Vector3 &r_normal) const;
  328. virtual bool intersect_point(const Vector3 &p_point) const;
  329. virtual Vector3 get_closest_point_to(const Vector3 &p_point) const;
  330. Vector3 get_moment_of_inertia(real_t p_mass) const;
  331. virtual void set_data(const Variant &p_data) {}
  332. virtual Variant get_data() const { return Variant(); }
  333. FaceShapeSW();
  334. };
  335. struct MotionShapeSW : public ShapeSW {
  336. ShapeSW *shape;
  337. Vector3 motion;
  338. virtual PhysicsServer::ShapeType get_type() const { return PhysicsServer::SHAPE_CONVEX_POLYGON; }
  339. void project_range(const Vector3 &p_normal, const Transform &p_transform, real_t &r_min, real_t &r_max) const {
  340. Vector3 cast = p_transform.basis.xform(motion);
  341. real_t mina, maxa;
  342. real_t minb, maxb;
  343. Transform ofsb = p_transform;
  344. ofsb.origin += cast;
  345. shape->project_range(p_normal, p_transform, mina, maxa);
  346. shape->project_range(p_normal, ofsb, minb, maxb);
  347. r_min = MIN(mina, minb);
  348. r_max = MAX(maxa, maxb);
  349. }
  350. Vector3 get_support(const Vector3 &p_normal) const {
  351. Vector3 support = shape->get_support(p_normal);
  352. if (p_normal.dot(motion) > 0) {
  353. support += motion;
  354. }
  355. return support;
  356. }
  357. virtual void get_supports(const Vector3 &p_normal, int p_max, Vector3 *r_supports, int &r_amount, FeatureType &r_type) const { r_amount = 0; }
  358. bool intersect_segment(const Vector3 &p_begin, const Vector3 &p_end, Vector3 &r_result, Vector3 &r_normal) const { return false; }
  359. virtual bool intersect_point(const Vector3 &p_point) const { return false; }
  360. virtual Vector3 get_closest_point_to(const Vector3 &p_point) const { return p_point; }
  361. Vector3 get_moment_of_inertia(real_t p_mass) const { return Vector3(); }
  362. virtual void set_data(const Variant &p_data) {}
  363. virtual Variant get_data() const { return Variant(); }
  364. MotionShapeSW() { configure(AABB()); }
  365. };
  366. struct _ShapeTestConvexBSPSW {
  367. const BSP_Tree *bsp;
  368. const ShapeSW *shape;
  369. Transform transform;
  370. _FORCE_INLINE_ void project_range(const Vector3 &p_normal, real_t &r_min, real_t &r_max) const {
  371. shape->project_range(p_normal, transform, r_min, r_max);
  372. }
  373. };
  374. #endif // SHAPESW_H