shape_sw.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. /*************************************************************************/
  2. /* shape_sw.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #ifndef SHAPE_SW_H
  30. #define SHAPE_SW_H
  31. #include "servers/physics_server.h"
  32. #include "bsp_tree.h"
  33. #include "geometry.h"
  34. /*
  35. SHAPE_LINE, ///< plane:"plane"
  36. SHAPE_SEGMENT, ///< float:"length"
  37. SHAPE_CIRCLE, ///< float:"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 ShapeSW;
  44. class ShapeOwnerSW {
  45. public:
  46. virtual void _shape_changed()=0;
  47. virtual void remove_shape(ShapeSW *p_shape)=0;
  48. virtual ~ShapeOwnerSW() {}
  49. };
  50. class ShapeSW {
  51. RID self;
  52. AABB aabb;
  53. bool configured;
  54. real_t custom_bias;
  55. Map<ShapeOwnerSW*,int> owners;
  56. protected:
  57. void configure(const AABB& p_aabb);
  58. public:
  59. enum {
  60. MAX_SUPPORTS=8
  61. };
  62. _FORCE_INLINE_ void set_self(const RID& p_self) { self=p_self; }
  63. _FORCE_INLINE_ RID get_self() const {return self; }
  64. virtual PhysicsServer::ShapeType get_type() const=0;
  65. _FORCE_INLINE_ AABB get_aabb() const { return aabb; }
  66. _FORCE_INLINE_ bool is_configured() const { return configured; }
  67. virtual bool is_concave() const { return false; }
  68. virtual void project_range(const Vector3& p_normal, const Transform& p_transform, real_t &r_min, real_t &r_max) const=0;
  69. virtual Vector3 get_support(const Vector3& p_normal) const;
  70. virtual void get_supports(const Vector3& p_normal,int p_max,Vector3 *r_supports,int & r_amount) const=0;
  71. virtual bool intersect_segment(const Vector3& p_begin,const Vector3& p_end,Vector3 &r_point, Vector3 &r_normal) const=0;
  72. virtual Vector3 get_moment_of_inertia(float p_mass) const=0;
  73. virtual void set_data(const Variant& p_data)=0;
  74. virtual Variant get_data() const=0;
  75. _FORCE_INLINE_ void set_custom_bias(real_t p_bias) { custom_bias=p_bias; }
  76. _FORCE_INLINE_ real_t get_custom_bias() const { return custom_bias; }
  77. void add_owner(ShapeOwnerSW *p_owner);
  78. void remove_owner(ShapeOwnerSW *p_owner);
  79. bool is_owner(ShapeOwnerSW *p_owner) const;
  80. const Map<ShapeOwnerSW*,int>& get_owners() const;
  81. ShapeSW();
  82. virtual ~ShapeSW();
  83. };
  84. class ConcaveShapeSW : public ShapeSW {
  85. public:
  86. virtual bool is_concave() const { return true; }
  87. typedef void (*Callback)(void* p_userdata,ShapeSW *p_convex);
  88. virtual void get_supports(const Vector3& p_normal,int p_max,Vector3 *r_supports,int & r_amount) const { r_amount=0; }
  89. virtual void cull(const AABB& p_local_aabb,Callback p_callback,void* p_userdata) const=0;
  90. ConcaveShapeSW() {}
  91. };
  92. class PlaneShapeSW : public ShapeSW {
  93. Plane plane;
  94. void _setup(const Plane& p_plane);
  95. public:
  96. Plane get_plane() const;
  97. virtual PhysicsServer::ShapeType get_type() const { return PhysicsServer::SHAPE_PLANE; }
  98. virtual void project_range(const Vector3& p_normal, const Transform& p_transform, real_t &r_min, real_t &r_max) const;
  99. virtual Vector3 get_support(const Vector3& p_normal) const;
  100. virtual void get_supports(const Vector3& p_normal,int p_max,Vector3 *r_supports,int & r_amount) const { r_amount=0; }
  101. virtual bool intersect_segment(const Vector3& p_begin,const Vector3& p_end,Vector3 &r_result, Vector3 &r_normal) const;
  102. virtual Vector3 get_moment_of_inertia(float p_mass) const;
  103. virtual void set_data(const Variant& p_data);
  104. virtual Variant get_data() const;
  105. PlaneShapeSW();
  106. };
  107. class RayShapeSW : public ShapeSW {
  108. float length;
  109. void _setup(float p_length);
  110. public:
  111. float get_length() const;
  112. virtual PhysicsServer::ShapeType get_type() const { return PhysicsServer::SHAPE_RAY; }
  113. virtual void project_range(const Vector3& p_normal, const Transform& p_transform, real_t &r_min, real_t &r_max) const;
  114. virtual Vector3 get_support(const Vector3& p_normal) const;
  115. virtual void get_supports(const Vector3& p_normal,int p_max,Vector3 *r_supports,int & r_amount) const;
  116. virtual bool intersect_segment(const Vector3& p_begin,const Vector3& p_end,Vector3 &r_result, Vector3 &r_normal) const;
  117. virtual Vector3 get_moment_of_inertia(float p_mass) const;
  118. virtual void set_data(const Variant& p_data);
  119. virtual Variant get_data() const;
  120. RayShapeSW();
  121. };
  122. class SphereShapeSW : public ShapeSW {
  123. real_t radius;
  124. void _setup(real_t p_radius);
  125. public:
  126. real_t get_radius() const;
  127. virtual PhysicsServer::ShapeType get_type() const { return PhysicsServer::SHAPE_SPHERE; }
  128. virtual void project_range(const Vector3& p_normal, const Transform& p_transform, real_t &r_min, real_t &r_max) const;
  129. virtual Vector3 get_support(const Vector3& p_normal) const;
  130. virtual void get_supports(const Vector3& p_normal,int p_max,Vector3 *r_supports,int & r_amount) const;
  131. virtual bool intersect_segment(const Vector3& p_begin,const Vector3& p_end,Vector3 &r_result, Vector3 &r_normal) const;
  132. virtual Vector3 get_moment_of_inertia(float p_mass) const;
  133. virtual void set_data(const Variant& p_data);
  134. virtual Variant get_data() const;
  135. SphereShapeSW();
  136. };
  137. class BoxShapeSW : public ShapeSW {
  138. Vector3 half_extents;
  139. void _setup(const Vector3& p_half_extents);
  140. public:
  141. _FORCE_INLINE_ Vector3 get_half_extents() const { return half_extents; }
  142. virtual PhysicsServer::ShapeType get_type() const { return PhysicsServer::SHAPE_BOX; }
  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) const;
  146. virtual bool intersect_segment(const Vector3& p_begin,const Vector3& p_end,Vector3 &r_result, Vector3 &r_normal) const;
  147. virtual Vector3 get_moment_of_inertia(float p_mass) const;
  148. virtual void set_data(const Variant& p_data);
  149. virtual Variant get_data() const;
  150. BoxShapeSW();
  151. };
  152. class CapsuleShapeSW : public ShapeSW {
  153. real_t height;
  154. real_t radius;
  155. void _setup(real_t p_height,real_t p_radius);
  156. public:
  157. _FORCE_INLINE_ real_t get_height() const { return height; }
  158. _FORCE_INLINE_ real_t get_radius() const { return radius; }
  159. virtual PhysicsServer::ShapeType get_type() const { return PhysicsServer::SHAPE_CAPSULE; }
  160. virtual void project_range(const Vector3& p_normal, const Transform& p_transform, real_t &r_min, real_t &r_max) const;
  161. virtual Vector3 get_support(const Vector3& p_normal) const;
  162. virtual void get_supports(const Vector3& p_normal,int p_max,Vector3 *r_supports,int & r_amount) const;
  163. virtual bool intersect_segment(const Vector3& p_begin,const Vector3& p_end,Vector3 &r_result, Vector3 &r_normal) const;
  164. virtual Vector3 get_moment_of_inertia(float p_mass) const;
  165. virtual void set_data(const Variant& p_data);
  166. virtual Variant get_data() const;
  167. CapsuleShapeSW();
  168. };
  169. struct ConvexPolygonShapeSW : public ShapeSW {
  170. Geometry::MeshData mesh;
  171. void _setup(const Vector<Vector3>& p_vertices);
  172. public:
  173. const Geometry::MeshData& get_mesh() const { return mesh; }
  174. virtual PhysicsServer::ShapeType get_type() const { return PhysicsServer::SHAPE_CONVEX_POLYGON; }
  175. virtual void project_range(const Vector3& p_normal, const Transform& p_transform, real_t &r_min, real_t &r_max) const;
  176. virtual Vector3 get_support(const Vector3& p_normal) const;
  177. virtual void get_supports(const Vector3& p_normal,int p_max,Vector3 *r_supports,int & r_amount) const;
  178. virtual bool intersect_segment(const Vector3& p_begin,const Vector3& p_end,Vector3 &r_result, Vector3 &r_normal) const;
  179. virtual Vector3 get_moment_of_inertia(float p_mass) const;
  180. virtual void set_data(const Variant& p_data);
  181. virtual Variant get_data() const;
  182. ConvexPolygonShapeSW();
  183. };
  184. struct _VolumeSW_BVH;
  185. struct FaceShapeSW;
  186. struct ConcavePolygonShapeSW : public ConcaveShapeSW {
  187. // always a trimesh
  188. struct Face {
  189. Vector3 normal;
  190. int indices[3];
  191. };
  192. DVector<Face> faces;
  193. DVector<Vector3> vertices;
  194. struct BVH {
  195. AABB aabb;
  196. int left;
  197. int right;
  198. int face_index;
  199. };
  200. DVector<BVH> bvh;
  201. struct _CullParams {
  202. AABB aabb;
  203. Callback callback;
  204. void *userdata;
  205. const Face *faces;
  206. const Vector3 *vertices;
  207. const BVH *bvh;
  208. FaceShapeSW *face;
  209. };
  210. struct _SegmentCullParams {
  211. Vector3 from;
  212. Vector3 to;
  213. const Face *faces;
  214. const Vector3 *vertices;
  215. const BVH *bvh;
  216. Vector3 result;
  217. Vector3 normal;
  218. real_t min_d;
  219. int collisions;
  220. };
  221. void _cull_segment(int p_idx,_SegmentCullParams *p_params) const;
  222. void _cull(int p_idx,_CullParams *p_params) const;
  223. void _fill_bvh(_VolumeSW_BVH* p_bvh_tree,BVH* p_bvh_array,int& p_idx);
  224. void _setup(DVector<Vector3> p_faces);
  225. public:
  226. DVector<Vector3> get_faces() const;
  227. virtual PhysicsServer::ShapeType get_type() const { return PhysicsServer::SHAPE_CONCAVE_POLYGON; }
  228. virtual void project_range(const Vector3& p_normal, const Transform& p_transform, real_t &r_min, real_t &r_max) const;
  229. virtual Vector3 get_support(const Vector3& p_normal) const;
  230. virtual bool intersect_segment(const Vector3& p_begin,const Vector3& p_end,Vector3 &r_result, Vector3 &r_normal) const;
  231. virtual void cull(const AABB& p_local_aabb,Callback p_callback,void* p_userdata) const;
  232. virtual Vector3 get_moment_of_inertia(float p_mass) const;
  233. virtual void set_data(const Variant& p_data);
  234. virtual Variant get_data() const;
  235. ConcavePolygonShapeSW();
  236. };
  237. struct HeightMapShapeSW : public ConcaveShapeSW {
  238. DVector<real_t> heights;
  239. int width;
  240. int depth;
  241. float cell_size;
  242. // void _cull_segment(int p_idx,_SegmentCullParams *p_params) const;
  243. // void _cull(int p_idx,_CullParams *p_params) const;
  244. void _setup(DVector<float> p_heights,int p_width,int p_depth,float p_cell_size);
  245. public:
  246. DVector<real_t> get_heights() const;
  247. int get_width() const;
  248. int get_depth() const;
  249. float get_cell_size() const;
  250. virtual PhysicsServer::ShapeType get_type() const { return PhysicsServer::SHAPE_HEIGHTMAP; }
  251. virtual void project_range(const Vector3& p_normal, const Transform& p_transform, real_t &r_min, real_t &r_max) const;
  252. virtual Vector3 get_support(const Vector3& p_normal) const;
  253. virtual bool intersect_segment(const Vector3& p_begin,const Vector3& p_end,Vector3 &r_result, Vector3 &r_normal) const;
  254. virtual void cull(const AABB& p_local_aabb,Callback p_callback,void* p_userdata) const;
  255. virtual Vector3 get_moment_of_inertia(float p_mass) const;
  256. virtual void set_data(const Variant& p_data);
  257. virtual Variant get_data() const;
  258. HeightMapShapeSW();
  259. };
  260. //used internally
  261. struct FaceShapeSW : public ShapeSW {
  262. Vector3 normal; //cache
  263. Vector3 vertex[3];
  264. virtual PhysicsServer::ShapeType get_type() const { return PhysicsServer::SHAPE_CONCAVE_POLYGON; }
  265. const Vector3& get_vertex(int p_idx) const { return vertex[p_idx]; }
  266. void project_range(const Vector3& p_normal, const Transform& p_transform, real_t &r_min, real_t &r_max) const;
  267. Vector3 get_support(const Vector3& p_normal) const;
  268. virtual void get_supports(const Vector3& p_normal,int p_max,Vector3 *r_supports,int & r_amount) const;
  269. bool intersect_segment(const Vector3& p_begin,const Vector3& p_end,Vector3 &r_result, Vector3 &r_normal) const;
  270. Vector3 get_moment_of_inertia(float p_mass) const;
  271. virtual void set_data(const Variant& p_data) {}
  272. virtual Variant get_data() const { return Variant(); }
  273. FaceShapeSW();
  274. };
  275. struct _ShapeTestConvexBSPSW {
  276. const BSP_Tree *bsp;
  277. const ShapeSW *shape;
  278. Transform transform;
  279. _FORCE_INLINE_ void project_range(const Vector3& p_normal, real_t& r_min, real_t& r_max) const {
  280. shape->project_range(p_normal,transform,r_min,r_max);
  281. }
  282. };
  283. #endif // SHAPESW_H