godot_physics_server_3d.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. /**************************************************************************/
  2. /* godot_physics_server_3d.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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 GODOT_PHYSICS_SERVER_3D_H
  31. #define GODOT_PHYSICS_SERVER_3D_H
  32. #include "godot_joint_3d.h"
  33. #include "godot_shape_3d.h"
  34. #include "godot_space_3d.h"
  35. #include "godot_step_3d.h"
  36. #include "core/templates/rid_owner.h"
  37. #include "servers/physics_server_3d.h"
  38. class GodotPhysicsServer3D : public PhysicsServer3D {
  39. GDCLASS(GodotPhysicsServer3D, PhysicsServer3D);
  40. friend class GodotPhysicsDirectSpaceState3D;
  41. bool active = true;
  42. int island_count = 0;
  43. int active_objects = 0;
  44. int collision_pairs = 0;
  45. bool using_threads = false;
  46. bool doing_sync = false;
  47. bool flushing_queries = false;
  48. GodotStep3D *stepper = nullptr;
  49. HashSet<const GodotSpace3D *> active_spaces;
  50. mutable RID_PtrOwner<GodotShape3D, true> shape_owner;
  51. mutable RID_PtrOwner<GodotSpace3D, true> space_owner;
  52. mutable RID_PtrOwner<GodotArea3D, true> area_owner;
  53. mutable RID_PtrOwner<GodotBody3D, true> body_owner;
  54. mutable RID_PtrOwner<GodotSoftBody3D, true> soft_body_owner;
  55. mutable RID_PtrOwner<GodotJoint3D, true> joint_owner;
  56. //void _clear_query(QuerySW *p_query);
  57. friend class GodotCollisionObject3D;
  58. SelfList<GodotCollisionObject3D>::List pending_shape_update_list;
  59. void _update_shapes();
  60. static GodotPhysicsServer3D *godot_singleton;
  61. public:
  62. struct CollCbkData {
  63. int max;
  64. int amount;
  65. Vector3 *ptr = nullptr;
  66. };
  67. static void _shape_col_cbk(const Vector3 &p_point_A, int p_index_A, const Vector3 &p_point_B, int p_index_B, const Vector3 &normal, void *p_userdata);
  68. virtual RID world_boundary_shape_create() override;
  69. virtual RID separation_ray_shape_create() override;
  70. virtual RID sphere_shape_create() override;
  71. virtual RID box_shape_create() override;
  72. virtual RID capsule_shape_create() override;
  73. virtual RID cylinder_shape_create() override;
  74. virtual RID convex_polygon_shape_create() override;
  75. virtual RID concave_polygon_shape_create() override;
  76. virtual RID heightmap_shape_create() override;
  77. virtual RID custom_shape_create() override;
  78. virtual void shape_set_data(RID p_shape, const Variant &p_data) override;
  79. virtual void shape_set_custom_solver_bias(RID p_shape, real_t p_bias) override;
  80. virtual ShapeType shape_get_type(RID p_shape) const override;
  81. virtual Variant shape_get_data(RID p_shape) const override;
  82. virtual void shape_set_margin(RID p_shape, real_t p_margin) override;
  83. virtual real_t shape_get_margin(RID p_shape) const override;
  84. virtual real_t shape_get_custom_solver_bias(RID p_shape) const override;
  85. /* SPACE API */
  86. virtual RID space_create() override;
  87. virtual void space_set_active(RID p_space, bool p_active) override;
  88. virtual bool space_is_active(RID p_space) const override;
  89. virtual void space_set_param(RID p_space, SpaceParameter p_param, real_t p_value) override;
  90. virtual real_t space_get_param(RID p_space, SpaceParameter p_param) const override;
  91. // this function only works on physics process, errors and returns null otherwise
  92. virtual PhysicsDirectSpaceState3D *space_get_direct_state(RID p_space) override;
  93. virtual void space_set_debug_contacts(RID p_space, int p_max_contacts) override;
  94. virtual Vector<Vector3> space_get_contacts(RID p_space) const override;
  95. virtual int space_get_contact_count(RID p_space) const override;
  96. /* AREA API */
  97. virtual RID area_create() override;
  98. virtual void area_set_space(RID p_area, RID p_space) override;
  99. virtual RID area_get_space(RID p_area) const override;
  100. virtual void area_add_shape(RID p_area, RID p_shape, const Transform3D &p_transform = Transform3D(), bool p_disabled = false) override;
  101. virtual void area_set_shape(RID p_area, int p_shape_idx, RID p_shape) override;
  102. virtual void area_set_shape_transform(RID p_area, int p_shape_idx, const Transform3D &p_transform) override;
  103. virtual int area_get_shape_count(RID p_area) const override;
  104. virtual RID area_get_shape(RID p_area, int p_shape_idx) const override;
  105. virtual Transform3D area_get_shape_transform(RID p_area, int p_shape_idx) const override;
  106. virtual void area_remove_shape(RID p_area, int p_shape_idx) override;
  107. virtual void area_clear_shapes(RID p_area) override;
  108. virtual void area_set_shape_disabled(RID p_area, int p_shape_idx, bool p_disabled) override;
  109. virtual void area_attach_object_instance_id(RID p_area, ObjectID p_id) override;
  110. virtual ObjectID area_get_object_instance_id(RID p_area) const override;
  111. virtual void area_set_param(RID p_area, AreaParameter p_param, const Variant &p_value) override;
  112. virtual void area_set_transform(RID p_area, const Transform3D &p_transform) override;
  113. virtual Variant area_get_param(RID p_area, AreaParameter p_param) const override;
  114. virtual Transform3D area_get_transform(RID p_area) const override;
  115. virtual void area_set_ray_pickable(RID p_area, bool p_enable) override;
  116. virtual void area_set_collision_layer(RID p_area, uint32_t p_layer) override;
  117. virtual uint32_t area_get_collision_layer(RID p_area) const override;
  118. virtual void area_set_collision_mask(RID p_area, uint32_t p_mask) override;
  119. virtual uint32_t area_get_collision_mask(RID p_area) const override;
  120. virtual void area_set_monitorable(RID p_area, bool p_monitorable) override;
  121. virtual void area_set_monitor_callback(RID p_area, const Callable &p_callback) override;
  122. virtual void area_set_area_monitor_callback(RID p_area, const Callable &p_callback) override;
  123. /* BODY API */
  124. // create a body of a given type
  125. virtual RID body_create() override;
  126. virtual void body_set_space(RID p_body, RID p_space) override;
  127. virtual RID body_get_space(RID p_body) const override;
  128. virtual void body_set_mode(RID p_body, BodyMode p_mode) override;
  129. virtual BodyMode body_get_mode(RID p_body) const override;
  130. virtual void body_add_shape(RID p_body, RID p_shape, const Transform3D &p_transform = Transform3D(), bool p_disabled = false) override;
  131. virtual void body_set_shape(RID p_body, int p_shape_idx, RID p_shape) override;
  132. virtual void body_set_shape_transform(RID p_body, int p_shape_idx, const Transform3D &p_transform) override;
  133. virtual int body_get_shape_count(RID p_body) const override;
  134. virtual RID body_get_shape(RID p_body, int p_shape_idx) const override;
  135. virtual Transform3D body_get_shape_transform(RID p_body, int p_shape_idx) const override;
  136. virtual void body_set_shape_disabled(RID p_body, int p_shape_idx, bool p_disabled) override;
  137. virtual void body_remove_shape(RID p_body, int p_shape_idx) override;
  138. virtual void body_clear_shapes(RID p_body) override;
  139. virtual void body_attach_object_instance_id(RID p_body, ObjectID p_id) override;
  140. virtual ObjectID body_get_object_instance_id(RID p_body) const override;
  141. virtual void body_set_enable_continuous_collision_detection(RID p_body, bool p_enable) override;
  142. virtual bool body_is_continuous_collision_detection_enabled(RID p_body) const override;
  143. virtual void body_set_collision_layer(RID p_body, uint32_t p_layer) override;
  144. virtual uint32_t body_get_collision_layer(RID p_body) const override;
  145. virtual void body_set_collision_mask(RID p_body, uint32_t p_mask) override;
  146. virtual uint32_t body_get_collision_mask(RID p_body) const override;
  147. virtual void body_set_collision_priority(RID p_body, real_t p_priority) override;
  148. virtual real_t body_get_collision_priority(RID p_body) const override;
  149. virtual void body_set_user_flags(RID p_body, uint32_t p_flags) override;
  150. virtual uint32_t body_get_user_flags(RID p_body) const override;
  151. virtual void body_set_param(RID p_body, BodyParameter p_param, const Variant &p_value) override;
  152. virtual Variant body_get_param(RID p_body, BodyParameter p_param) const override;
  153. virtual void body_reset_mass_properties(RID p_body) override;
  154. virtual void body_set_state(RID p_body, BodyState p_state, const Variant &p_variant) override;
  155. virtual Variant body_get_state(RID p_body, BodyState p_state) const override;
  156. virtual void body_apply_central_impulse(RID p_body, const Vector3 &p_impulse) override;
  157. virtual void body_apply_impulse(RID p_body, const Vector3 &p_impulse, const Vector3 &p_position = Vector3()) override;
  158. virtual void body_apply_torque_impulse(RID p_body, const Vector3 &p_impulse) override;
  159. virtual void body_apply_central_force(RID p_body, const Vector3 &p_force) override;
  160. virtual void body_apply_force(RID p_body, const Vector3 &p_force, const Vector3 &p_position = Vector3()) override;
  161. virtual void body_apply_torque(RID p_body, const Vector3 &p_torque) override;
  162. virtual void body_add_constant_central_force(RID p_body, const Vector3 &p_force) override;
  163. virtual void body_add_constant_force(RID p_body, const Vector3 &p_force, const Vector3 &p_position = Vector3()) override;
  164. virtual void body_add_constant_torque(RID p_body, const Vector3 &p_torque) override;
  165. virtual void body_set_constant_force(RID p_body, const Vector3 &p_force) override;
  166. virtual Vector3 body_get_constant_force(RID p_body) const override;
  167. virtual void body_set_constant_torque(RID p_body, const Vector3 &p_torque) override;
  168. virtual Vector3 body_get_constant_torque(RID p_body) const override;
  169. virtual void body_set_axis_velocity(RID p_body, const Vector3 &p_axis_velocity) override;
  170. virtual void body_set_axis_lock(RID p_body, BodyAxis p_axis, bool p_lock) override;
  171. virtual bool body_is_axis_locked(RID p_body, BodyAxis p_axis) const override;
  172. virtual void body_add_collision_exception(RID p_body, RID p_body_b) override;
  173. virtual void body_remove_collision_exception(RID p_body, RID p_body_b) override;
  174. virtual void body_get_collision_exceptions(RID p_body, List<RID> *p_exceptions) override;
  175. virtual void body_set_contacts_reported_depth_threshold(RID p_body, real_t p_threshold) override;
  176. virtual real_t body_get_contacts_reported_depth_threshold(RID p_body) const override;
  177. virtual void body_set_omit_force_integration(RID p_body, bool p_omit) override;
  178. virtual bool body_is_omitting_force_integration(RID p_body) const override;
  179. virtual void body_set_max_contacts_reported(RID p_body, int p_contacts) override;
  180. virtual int body_get_max_contacts_reported(RID p_body) const override;
  181. virtual void body_set_state_sync_callback(RID p_body, const Callable &p_callable) override;
  182. virtual void body_set_force_integration_callback(RID p_body, const Callable &p_callable, const Variant &p_udata = Variant()) override;
  183. virtual void body_set_ray_pickable(RID p_body, bool p_enable) override;
  184. virtual bool body_test_motion(RID p_body, const MotionParameters &p_parameters, MotionResult *r_result = nullptr) override;
  185. // this function only works on physics process, errors and returns null otherwise
  186. virtual PhysicsDirectBodyState3D *body_get_direct_state(RID p_body) override;
  187. /* SOFT BODY */
  188. virtual RID soft_body_create() override;
  189. virtual void soft_body_update_rendering_server(RID p_body, PhysicsServer3DRenderingServerHandler *p_rendering_server_handler) override;
  190. virtual void soft_body_set_space(RID p_body, RID p_space) override;
  191. virtual RID soft_body_get_space(RID p_body) const override;
  192. virtual void soft_body_set_collision_layer(RID p_body, uint32_t p_layer) override;
  193. virtual uint32_t soft_body_get_collision_layer(RID p_body) const override;
  194. virtual void soft_body_set_collision_mask(RID p_body, uint32_t p_mask) override;
  195. virtual uint32_t soft_body_get_collision_mask(RID p_body) const override;
  196. virtual void soft_body_add_collision_exception(RID p_body, RID p_body_b) override;
  197. virtual void soft_body_remove_collision_exception(RID p_body, RID p_body_b) override;
  198. virtual void soft_body_get_collision_exceptions(RID p_body, List<RID> *p_exceptions) override;
  199. virtual void soft_body_set_state(RID p_body, BodyState p_state, const Variant &p_variant) override;
  200. virtual Variant soft_body_get_state(RID p_body, BodyState p_state) const override;
  201. virtual void soft_body_set_transform(RID p_body, const Transform3D &p_transform) override;
  202. virtual void soft_body_set_ray_pickable(RID p_body, bool p_enable) override;
  203. virtual void soft_body_set_simulation_precision(RID p_body, int p_simulation_precision) override;
  204. virtual int soft_body_get_simulation_precision(RID p_body) const override;
  205. virtual void soft_body_set_total_mass(RID p_body, real_t p_total_mass) override;
  206. virtual real_t soft_body_get_total_mass(RID p_body) const override;
  207. virtual void soft_body_set_linear_stiffness(RID p_body, real_t p_stiffness) override;
  208. virtual real_t soft_body_get_linear_stiffness(RID p_body) const override;
  209. virtual void soft_body_set_pressure_coefficient(RID p_body, real_t p_pressure_coefficient) override;
  210. virtual real_t soft_body_get_pressure_coefficient(RID p_body) const override;
  211. virtual void soft_body_set_damping_coefficient(RID p_body, real_t p_damping_coefficient) override;
  212. virtual real_t soft_body_get_damping_coefficient(RID p_body) const override;
  213. virtual void soft_body_set_drag_coefficient(RID p_body, real_t p_drag_coefficient) override;
  214. virtual real_t soft_body_get_drag_coefficient(RID p_body) const override;
  215. virtual void soft_body_set_mesh(RID p_body, RID p_mesh) override;
  216. virtual AABB soft_body_get_bounds(RID p_body) const override;
  217. virtual void soft_body_move_point(RID p_body, int p_point_index, const Vector3 &p_global_position) override;
  218. virtual Vector3 soft_body_get_point_global_position(RID p_body, int p_point_index) const override;
  219. virtual void soft_body_remove_all_pinned_points(RID p_body) override;
  220. virtual void soft_body_pin_point(RID p_body, int p_point_index, bool p_pin) override;
  221. virtual bool soft_body_is_point_pinned(RID p_body, int p_point_index) const override;
  222. /* JOINT API */
  223. virtual RID joint_create() override;
  224. virtual void joint_clear(RID p_joint) override; //resets type
  225. virtual void joint_make_pin(RID p_joint, RID p_body_A, const Vector3 &p_local_A, RID p_body_B, const Vector3 &p_local_B) override;
  226. virtual void pin_joint_set_param(RID p_joint, PinJointParam p_param, real_t p_value) override;
  227. virtual real_t pin_joint_get_param(RID p_joint, PinJointParam p_param) const override;
  228. virtual void pin_joint_set_local_a(RID p_joint, const Vector3 &p_A) override;
  229. virtual Vector3 pin_joint_get_local_a(RID p_joint) const override;
  230. virtual void pin_joint_set_local_b(RID p_joint, const Vector3 &p_B) override;
  231. virtual Vector3 pin_joint_get_local_b(RID p_joint) const override;
  232. virtual void joint_make_hinge(RID p_joint, RID p_body_A, const Transform3D &p_frame_A, RID p_body_B, const Transform3D &p_frame_B) override;
  233. virtual void joint_make_hinge_simple(RID p_joint, RID p_body_A, const Vector3 &p_pivot_A, const Vector3 &p_axis_A, RID p_body_B, const Vector3 &p_pivot_B, const Vector3 &p_axis_B) override;
  234. virtual void hinge_joint_set_param(RID p_joint, HingeJointParam p_param, real_t p_value) override;
  235. virtual real_t hinge_joint_get_param(RID p_joint, HingeJointParam p_param) const override;
  236. virtual void hinge_joint_set_flag(RID p_joint, HingeJointFlag p_flag, bool p_value) override;
  237. virtual bool hinge_joint_get_flag(RID p_joint, HingeJointFlag p_flag) const override;
  238. virtual void joint_make_slider(RID p_joint, RID p_body_A, const Transform3D &p_local_frame_A, RID p_body_B, const Transform3D &p_local_frame_B) override; //reference frame is A
  239. virtual void slider_joint_set_param(RID p_joint, SliderJointParam p_param, real_t p_value) override;
  240. virtual real_t slider_joint_get_param(RID p_joint, SliderJointParam p_param) const override;
  241. virtual void joint_make_cone_twist(RID p_joint, RID p_body_A, const Transform3D &p_local_frame_A, RID p_body_B, const Transform3D &p_local_frame_B) override; //reference frame is A
  242. virtual void cone_twist_joint_set_param(RID p_joint, ConeTwistJointParam p_param, real_t p_value) override;
  243. virtual real_t cone_twist_joint_get_param(RID p_joint, ConeTwistJointParam p_param) const override;
  244. virtual void joint_make_generic_6dof(RID p_joint, RID p_body_A, const Transform3D &p_local_frame_A, RID p_body_B, const Transform3D &p_local_frame_B) override; //reference frame is A
  245. virtual void generic_6dof_joint_set_param(RID p_joint, Vector3::Axis, G6DOFJointAxisParam p_param, real_t p_value) override;
  246. virtual real_t generic_6dof_joint_get_param(RID p_joint, Vector3::Axis, G6DOFJointAxisParam p_param) const override;
  247. virtual void generic_6dof_joint_set_flag(RID p_joint, Vector3::Axis, G6DOFJointAxisFlag p_flag, bool p_enable) override;
  248. virtual bool generic_6dof_joint_get_flag(RID p_joint, Vector3::Axis, G6DOFJointAxisFlag p_flag) const override;
  249. virtual JointType joint_get_type(RID p_joint) const override;
  250. virtual void joint_set_solver_priority(RID p_joint, int p_priority) override;
  251. virtual int joint_get_solver_priority(RID p_joint) const override;
  252. virtual void joint_disable_collisions_between_bodies(RID p_joint, bool p_disable) override;
  253. virtual bool joint_is_disabled_collisions_between_bodies(RID p_joint) const override;
  254. /* MISC */
  255. virtual void free(RID p_rid) override;
  256. virtual void set_active(bool p_active) override;
  257. virtual void init() override;
  258. virtual void step(real_t p_step) override;
  259. virtual void sync() override;
  260. virtual void flush_queries() override;
  261. virtual void end_sync() override;
  262. virtual void finish() override;
  263. virtual bool is_flushing_queries() const override { return flushing_queries; }
  264. int get_process_info(ProcessInfo p_info) override;
  265. GodotPhysicsServer3D(bool p_using_threads = false);
  266. ~GodotPhysicsServer3D() {}
  267. };
  268. #endif // GODOT_PHYSICS_SERVER_3D_H