shape_3d_sw.h 18 KB

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