physics_server.h 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845
  1. /**************************************************************************/
  2. /* physics_server.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 PHYSICS_SERVER_H
  31. #define PHYSICS_SERVER_H
  32. #include "core/object.h"
  33. #include "core/resource.h"
  34. class PhysicsDirectSpaceState;
  35. class PhysicsDirectBodyState : public Object {
  36. GDCLASS(PhysicsDirectBodyState, Object);
  37. protected:
  38. static void _bind_methods();
  39. public:
  40. virtual Vector3 get_total_gravity() const = 0;
  41. virtual float get_total_angular_damp() const = 0;
  42. virtual float get_total_linear_damp() const = 0;
  43. virtual Vector3 get_center_of_mass() const = 0;
  44. virtual Basis get_principal_inertia_axes() const = 0;
  45. virtual float get_inverse_mass() const = 0; // get the mass
  46. virtual Vector3 get_inverse_inertia() const = 0; // get density of this body space
  47. virtual Basis get_inverse_inertia_tensor() const = 0; // get density of this body space
  48. virtual void set_linear_velocity(const Vector3 &p_velocity) = 0;
  49. virtual Vector3 get_linear_velocity() const = 0;
  50. virtual void set_angular_velocity(const Vector3 &p_velocity) = 0;
  51. virtual Vector3 get_angular_velocity() const = 0;
  52. virtual void set_transform(const Transform &p_transform) = 0;
  53. virtual Transform get_transform() const = 0;
  54. virtual Vector3 get_velocity_at_local_position(const Vector3 &p_position) const = 0;
  55. virtual void add_central_force(const Vector3 &p_force) = 0;
  56. virtual void add_force(const Vector3 &p_force, const Vector3 &p_pos) = 0;
  57. virtual void add_torque(const Vector3 &p_torque) = 0;
  58. virtual void apply_central_impulse(const Vector3 &p_j) = 0;
  59. virtual void apply_impulse(const Vector3 &p_pos, const Vector3 &p_j) = 0;
  60. virtual void apply_torque_impulse(const Vector3 &p_j) = 0;
  61. virtual void set_sleep_state(bool p_enable) = 0;
  62. virtual bool is_sleeping() const = 0;
  63. virtual int get_contact_count() const = 0;
  64. virtual Vector3 get_contact_local_position(int p_contact_idx) const = 0;
  65. virtual Vector3 get_contact_local_normal(int p_contact_idx) const = 0;
  66. virtual float get_contact_impulse(int p_contact_idx) const = 0;
  67. virtual int get_contact_local_shape(int p_contact_idx) const = 0;
  68. virtual RID get_contact_collider(int p_contact_idx) const = 0;
  69. virtual Vector3 get_contact_collider_position(int p_contact_idx) const = 0;
  70. virtual ObjectID get_contact_collider_id(int p_contact_idx) const = 0;
  71. virtual Object *get_contact_collider_object(int p_contact_idx) const;
  72. virtual int get_contact_collider_shape(int p_contact_idx) const = 0;
  73. virtual Vector3 get_contact_collider_velocity_at_position(int p_contact_idx) const = 0;
  74. virtual real_t get_step() const = 0;
  75. virtual void integrate_forces();
  76. virtual PhysicsDirectSpaceState *get_space_state() = 0;
  77. PhysicsDirectBodyState();
  78. };
  79. class PhysicsShapeQueryParameters : public Reference {
  80. GDCLASS(PhysicsShapeQueryParameters, Reference);
  81. friend class PhysicsDirectSpaceState;
  82. RID shape;
  83. Transform transform;
  84. float margin;
  85. Set<RID> exclude;
  86. uint32_t collision_mask;
  87. bool collide_with_bodies;
  88. bool collide_with_areas;
  89. protected:
  90. static void _bind_methods();
  91. public:
  92. void set_shape(const RES &p_shape);
  93. void set_shape_rid(const RID &p_shape);
  94. RID get_shape_rid() const;
  95. void set_transform(const Transform &p_transform);
  96. Transform get_transform() const;
  97. void set_margin(float p_margin);
  98. float get_margin() const;
  99. void set_collision_mask(uint32_t p_collision_mask);
  100. uint32_t get_collision_mask() const;
  101. void set_exclude(const Vector<RID> &p_exclude);
  102. Vector<RID> get_exclude() const;
  103. void set_collide_with_bodies(bool p_enable);
  104. bool is_collide_with_bodies_enabled() const;
  105. void set_collide_with_areas(bool p_enable);
  106. bool is_collide_with_areas_enabled() const;
  107. PhysicsShapeQueryParameters();
  108. };
  109. class PhysicsDirectSpaceState : public Object {
  110. GDCLASS(PhysicsDirectSpaceState, Object);
  111. private:
  112. Dictionary _intersect_ray(const Vector3 &p_from, const Vector3 &p_to, const Vector<RID> &p_exclude = Vector<RID>(), uint32_t p_collision_mask = 0, bool p_collide_with_bodies = true, bool p_collide_with_areas = false);
  113. Array _intersect_point(const Vector3 &p_point, int p_max_results = 32, const Vector<RID> &p_exclude = Vector<RID>(), uint32_t p_layers = 0, bool p_collide_with_bodies = true, bool p_collide_with_areas = false);
  114. Array _intersect_shape(const Ref<PhysicsShapeQueryParameters> &p_shape_query, int p_max_results = 32);
  115. Array _cast_motion(const Ref<PhysicsShapeQueryParameters> &p_shape_query, const Vector3 &p_motion);
  116. Array _collide_shape(const Ref<PhysicsShapeQueryParameters> &p_shape_query, int p_max_results = 32);
  117. Dictionary _get_rest_info(const Ref<PhysicsShapeQueryParameters> &p_shape_query);
  118. protected:
  119. static void _bind_methods();
  120. public:
  121. struct ShapeResult {
  122. RID rid;
  123. ObjectID collider_id = 0;
  124. Object *collider = nullptr;
  125. int shape = 0;
  126. };
  127. virtual int intersect_point(const Vector3 &p_point, ShapeResult *r_results, int p_result_max, const Set<RID> &p_exclude = Set<RID>(), uint32_t p_collision_mask = 0xFFFFFFFF, bool p_collide_with_bodies = true, bool p_collide_with_areas = false) = 0;
  128. struct RayResult {
  129. Vector3 position;
  130. Vector3 normal;
  131. RID rid;
  132. ObjectID collider_id = 0;
  133. Object *collider = nullptr;
  134. int shape = 0;
  135. };
  136. virtual bool intersect_ray(const Vector3 &p_from, const Vector3 &p_to, RayResult &r_result, const Set<RID> &p_exclude = Set<RID>(), uint32_t p_collision_mask = 0xFFFFFFFF, bool p_collide_with_bodies = true, bool p_collide_with_areas = false, bool p_pick_ray = false) = 0;
  137. virtual int intersect_shape(const RID &p_shape, const Transform &p_xform, float p_margin, ShapeResult *r_results, int p_result_max, const Set<RID> &p_exclude = Set<RID>(), uint32_t p_collision_mask = 0xFFFFFFFF, bool p_collide_with_bodies = true, bool p_collide_with_areas = false) = 0;
  138. struct ShapeRestInfo {
  139. Vector3 point;
  140. Vector3 normal;
  141. RID rid;
  142. ObjectID collider_id = 0;
  143. int shape = 0;
  144. Vector3 linear_velocity; //velocity at contact point
  145. };
  146. virtual bool cast_motion(const RID &p_shape, const Transform &p_xform, const Vector3 &p_motion, float p_margin, float &p_closest_safe, float &p_closest_unsafe, const Set<RID> &p_exclude = Set<RID>(), uint32_t p_collision_mask = 0xFFFFFFFF, bool p_collide_with_bodies = true, bool p_collide_with_areas = false, ShapeRestInfo *r_info = nullptr) = 0;
  147. virtual bool collide_shape(RID p_shape, const Transform &p_shape_xform, float p_margin, Vector3 *r_results, int p_result_max, int &r_result_count, const Set<RID> &p_exclude = Set<RID>(), uint32_t p_collision_mask = 0xFFFFFFFF, bool p_collide_with_bodies = true, bool p_collide_with_areas = false) = 0;
  148. virtual bool rest_info(RID p_shape, const Transform &p_shape_xform, float p_margin, ShapeRestInfo *r_info, const Set<RID> &p_exclude = Set<RID>(), uint32_t p_collision_mask = 0xFFFFFFFF, bool p_collide_with_bodies = true, bool p_collide_with_areas = false) = 0;
  149. virtual Vector3 get_closest_point_to_object_volume(RID p_object, const Vector3 p_point) const = 0;
  150. PhysicsDirectSpaceState();
  151. };
  152. class PhysicsTestMotionResult;
  153. class PhysicsServer : public Object {
  154. GDCLASS(PhysicsServer, Object);
  155. static PhysicsServer *singleton;
  156. virtual bool _body_test_motion(RID p_body, const Transform &p_from, const Vector3 &p_motion, bool p_infinite_inertia, const Ref<PhysicsTestMotionResult> &p_result = Ref<PhysicsTestMotionResult>(), bool p_exclude_raycast_shapes = true, const Vector<RID> &p_exclude = Vector<RID>());
  157. protected:
  158. static void _bind_methods();
  159. public:
  160. static PhysicsServer *get_singleton();
  161. enum ShapeType {
  162. SHAPE_PLANE, ///< plane:"plane"
  163. SHAPE_RAY, ///< float:"length"
  164. SHAPE_SPHERE, ///< float:"radius"
  165. SHAPE_BOX, ///< vec3:"extents"
  166. SHAPE_CAPSULE, ///< dict( float:"radius", float:"height"):capsule
  167. SHAPE_CYLINDER, ///< dict( float:"radius", float:"height"):cylinder
  168. SHAPE_CONVEX_POLYGON, ///< array of planes:"planes"
  169. SHAPE_CONCAVE_POLYGON, ///< vector3 array:"triangles" , or Dictionary with "indices" (int array) and "triangles" (Vector3 array)
  170. SHAPE_HEIGHTMAP, ///< dict( int:"width", int:"depth",float:"cell_size", float_array:"heights"
  171. SHAPE_CUSTOM, ///< Server-Implementation based custom shape, calling shape_create() with this value will result in an error
  172. };
  173. virtual RID shape_create(ShapeType p_shape) = 0;
  174. virtual void shape_set_data(RID p_shape, const Variant &p_data) = 0;
  175. virtual void shape_set_custom_solver_bias(RID p_shape, real_t p_bias) = 0;
  176. virtual ShapeType shape_get_type(RID p_shape) const = 0;
  177. virtual Variant shape_get_data(RID p_shape) const = 0;
  178. virtual void shape_set_margin(RID p_shape, real_t p_margin) = 0;
  179. virtual real_t shape_get_margin(RID p_shape) const = 0;
  180. virtual real_t shape_get_custom_solver_bias(RID p_shape) const = 0;
  181. /* SPACE API */
  182. virtual RID space_create() = 0;
  183. virtual void space_set_active(RID p_space, bool p_active) = 0;
  184. virtual bool space_is_active(RID p_space) const = 0;
  185. enum SpaceParameter {
  186. SPACE_PARAM_CONTACT_RECYCLE_RADIUS,
  187. SPACE_PARAM_CONTACT_MAX_SEPARATION,
  188. SPACE_PARAM_BODY_MAX_ALLOWED_PENETRATION,
  189. SPACE_PARAM_BODY_LINEAR_VELOCITY_SLEEP_THRESHOLD,
  190. SPACE_PARAM_BODY_ANGULAR_VELOCITY_SLEEP_THRESHOLD,
  191. SPACE_PARAM_BODY_TIME_TO_SLEEP,
  192. SPACE_PARAM_BODY_ANGULAR_VELOCITY_DAMP_RATIO,
  193. SPACE_PARAM_CONSTRAINT_DEFAULT_BIAS,
  194. };
  195. virtual void space_set_param(RID p_space, SpaceParameter p_param, real_t p_value) = 0;
  196. virtual real_t space_get_param(RID p_space, SpaceParameter p_param) const = 0;
  197. // this function only works on physics process, errors and returns null otherwise
  198. virtual PhysicsDirectSpaceState *space_get_direct_state(RID p_space) = 0;
  199. virtual void space_set_debug_contacts(RID p_space, int p_max_contacts) = 0;
  200. virtual Vector<Vector3> space_get_contacts(RID p_space) const = 0;
  201. virtual int space_get_contact_count(RID p_space) const = 0;
  202. //missing space parameters
  203. /* AREA API */
  204. //missing attenuation? missing better override?
  205. enum AreaParameter {
  206. AREA_PARAM_GRAVITY,
  207. AREA_PARAM_GRAVITY_VECTOR,
  208. AREA_PARAM_GRAVITY_IS_POINT,
  209. AREA_PARAM_GRAVITY_DISTANCE_SCALE,
  210. AREA_PARAM_GRAVITY_POINT_ATTENUATION,
  211. AREA_PARAM_LINEAR_DAMP,
  212. AREA_PARAM_ANGULAR_DAMP,
  213. AREA_PARAM_PRIORITY
  214. };
  215. virtual RID area_create() = 0;
  216. virtual void area_set_space(RID p_area, RID p_space) = 0;
  217. virtual RID area_get_space(RID p_area) const = 0;
  218. enum AreaSpaceOverrideMode {
  219. AREA_SPACE_OVERRIDE_DISABLED,
  220. AREA_SPACE_OVERRIDE_COMBINE,
  221. AREA_SPACE_OVERRIDE_COMBINE_REPLACE,
  222. AREA_SPACE_OVERRIDE_REPLACE,
  223. AREA_SPACE_OVERRIDE_REPLACE_COMBINE
  224. };
  225. virtual void area_set_space_override_mode(RID p_area, AreaSpaceOverrideMode p_mode) = 0;
  226. virtual AreaSpaceOverrideMode area_get_space_override_mode(RID p_area) const = 0;
  227. virtual void area_add_shape(RID p_area, RID p_shape, const Transform &p_transform = Transform(), bool p_disabled = false) = 0;
  228. virtual void area_set_shape(RID p_area, int p_shape_idx, RID p_shape) = 0;
  229. virtual void area_set_shape_transform(RID p_area, int p_shape_idx, const Transform &p_transform) = 0;
  230. virtual int area_get_shape_count(RID p_area) const = 0;
  231. virtual RID area_get_shape(RID p_area, int p_shape_idx) const = 0;
  232. virtual Transform area_get_shape_transform(RID p_area, int p_shape_idx) const = 0;
  233. virtual void area_remove_shape(RID p_area, int p_shape_idx) = 0;
  234. virtual void area_clear_shapes(RID p_area) = 0;
  235. virtual void area_set_shape_disabled(RID p_area, int p_shape_idx, bool p_disabled) = 0;
  236. virtual void area_attach_object_instance_id(RID p_area, ObjectID p_id) = 0;
  237. virtual ObjectID area_get_object_instance_id(RID p_area) const = 0;
  238. virtual void area_set_param(RID p_area, AreaParameter p_param, const Variant &p_value) = 0;
  239. virtual void area_set_transform(RID p_area, const Transform &p_transform) = 0;
  240. virtual Variant area_get_param(RID p_parea, AreaParameter p_param) const = 0;
  241. virtual Transform area_get_transform(RID p_area) const = 0;
  242. virtual void area_set_collision_mask(RID p_area, uint32_t p_mask) = 0;
  243. virtual void area_set_collision_layer(RID p_area, uint32_t p_layer) = 0;
  244. virtual void area_set_monitorable(RID p_area, bool p_monitorable) = 0;
  245. virtual void area_set_monitor_callback(RID p_area, Object *p_receiver, const StringName &p_method) = 0;
  246. virtual void area_set_area_monitor_callback(RID p_area, Object *p_receiver, const StringName &p_method) = 0;
  247. virtual void area_set_ray_pickable(RID p_area, bool p_enable) = 0;
  248. virtual bool area_is_ray_pickable(RID p_area) const = 0;
  249. /* BODY API */
  250. //missing ccd?
  251. enum BodyMode {
  252. BODY_MODE_STATIC,
  253. BODY_MODE_KINEMATIC,
  254. BODY_MODE_RIGID,
  255. BODY_MODE_CHARACTER
  256. };
  257. virtual RID body_create(BodyMode p_mode = BODY_MODE_RIGID, bool p_init_sleeping = false) = 0;
  258. virtual void body_set_space(RID p_body, RID p_space) = 0;
  259. virtual RID body_get_space(RID p_body) const = 0;
  260. virtual void body_set_mode(RID p_body, BodyMode p_mode) = 0;
  261. virtual BodyMode body_get_mode(RID p_body) const = 0;
  262. virtual void body_add_shape(RID p_body, RID p_shape, const Transform &p_transform = Transform(), bool p_disabled = false) = 0;
  263. virtual void body_set_shape(RID p_body, int p_shape_idx, RID p_shape) = 0;
  264. virtual void body_set_shape_transform(RID p_body, int p_shape_idx, const Transform &p_transform) = 0;
  265. virtual int body_get_shape_count(RID p_body) const = 0;
  266. virtual RID body_get_shape(RID p_body, int p_shape_idx) const = 0;
  267. virtual Transform body_get_shape_transform(RID p_body, int p_shape_idx) const = 0;
  268. virtual void body_remove_shape(RID p_body, int p_shape_idx) = 0;
  269. virtual void body_clear_shapes(RID p_body) = 0;
  270. virtual void body_set_shape_disabled(RID p_body, int p_shape_idx, bool p_disabled) = 0;
  271. virtual void body_attach_object_instance_id(RID p_body, uint32_t p_id) = 0;
  272. virtual uint32_t body_get_object_instance_id(RID p_body) const = 0;
  273. virtual void body_set_enable_continuous_collision_detection(RID p_body, bool p_enable) = 0;
  274. virtual bool body_is_continuous_collision_detection_enabled(RID p_body) const = 0;
  275. virtual void body_set_collision_layer(RID p_body, uint32_t p_layer) = 0;
  276. virtual uint32_t body_get_collision_layer(RID p_body) const = 0;
  277. virtual void body_set_collision_mask(RID p_body, uint32_t p_mask) = 0;
  278. virtual uint32_t body_get_collision_mask(RID p_body) const = 0;
  279. virtual void body_set_user_flags(RID p_body, uint32_t p_flags) = 0;
  280. virtual uint32_t body_get_user_flags(RID p_body) const = 0;
  281. // common body variables
  282. enum BodyParameter {
  283. BODY_PARAM_BOUNCE,
  284. BODY_PARAM_FRICTION,
  285. BODY_PARAM_MASS, ///< unused for static, always infinite
  286. BODY_PARAM_GRAVITY_SCALE,
  287. BODY_PARAM_LINEAR_DAMP,
  288. BODY_PARAM_ANGULAR_DAMP,
  289. BODY_PARAM_MAX,
  290. };
  291. virtual void body_set_param(RID p_body, BodyParameter p_param, float p_value) = 0;
  292. virtual float body_get_param(RID p_body, BodyParameter p_param) const = 0;
  293. virtual void body_set_kinematic_safe_margin(RID p_body, real_t p_margin) = 0;
  294. virtual real_t body_get_kinematic_safe_margin(RID p_body) const = 0;
  295. //state
  296. enum BodyState {
  297. BODY_STATE_TRANSFORM,
  298. BODY_STATE_LINEAR_VELOCITY,
  299. BODY_STATE_ANGULAR_VELOCITY,
  300. BODY_STATE_SLEEPING,
  301. BODY_STATE_CAN_SLEEP
  302. };
  303. virtual void body_set_state(RID p_body, BodyState p_state, const Variant &p_variant) = 0;
  304. virtual Variant body_get_state(RID p_body, BodyState p_state) const = 0;
  305. //do something about it
  306. virtual void body_set_applied_force(RID p_body, const Vector3 &p_force) = 0;
  307. virtual Vector3 body_get_applied_force(RID p_body) const = 0;
  308. virtual void body_set_applied_torque(RID p_body, const Vector3 &p_torque) = 0;
  309. virtual Vector3 body_get_applied_torque(RID p_body) const = 0;
  310. virtual void body_add_central_force(RID p_body, const Vector3 &p_force) = 0;
  311. virtual void body_add_force(RID p_body, const Vector3 &p_force, const Vector3 &p_pos) = 0;
  312. virtual void body_add_torque(RID p_body, const Vector3 &p_torque) = 0;
  313. virtual void body_apply_central_impulse(RID p_body, const Vector3 &p_impulse) = 0;
  314. virtual void body_apply_impulse(RID p_body, const Vector3 &p_pos, const Vector3 &p_impulse) = 0;
  315. virtual void body_apply_torque_impulse(RID p_body, const Vector3 &p_impulse) = 0;
  316. virtual void body_set_axis_velocity(RID p_body, const Vector3 &p_axis_velocity) = 0;
  317. enum BodyAxis {
  318. BODY_AXIS_LINEAR_X = 1 << 0,
  319. BODY_AXIS_LINEAR_Y = 1 << 1,
  320. BODY_AXIS_LINEAR_Z = 1 << 2,
  321. BODY_AXIS_ANGULAR_X = 1 << 3,
  322. BODY_AXIS_ANGULAR_Y = 1 << 4,
  323. BODY_AXIS_ANGULAR_Z = 1 << 5
  324. };
  325. virtual void body_set_axis_lock(RID p_body, BodyAxis p_axis, bool p_lock) = 0;
  326. virtual bool body_is_axis_locked(RID p_body, BodyAxis p_axis) const = 0;
  327. //fix
  328. virtual void body_add_collision_exception(RID p_body, RID p_body_b) = 0;
  329. virtual void body_remove_collision_exception(RID p_body, RID p_body_b) = 0;
  330. virtual void body_get_collision_exceptions(RID p_body, List<RID> *p_exceptions) = 0;
  331. virtual void body_set_max_contacts_reported(RID p_body, int p_contacts) = 0;
  332. virtual int body_get_max_contacts_reported(RID p_body) const = 0;
  333. //missing remove
  334. virtual void body_set_contacts_reported_depth_threshold(RID p_body, float p_threshold) = 0;
  335. virtual float body_get_contacts_reported_depth_threshold(RID p_body) const = 0;
  336. virtual void body_set_omit_force_integration(RID p_body, bool p_omit) = 0;
  337. virtual bool body_is_omitting_force_integration(RID p_body) const = 0;
  338. virtual void body_set_force_integration_callback(RID p_body, Object *p_receiver, const StringName &p_method, const Variant &p_udata = Variant()) = 0;
  339. virtual void body_set_ray_pickable(RID p_body, bool p_enable) = 0;
  340. virtual bool body_is_ray_pickable(RID p_body) const = 0;
  341. // this function only works on physics process, errors and returns null otherwise
  342. virtual PhysicsDirectBodyState *body_get_direct_state(RID p_body) = 0;
  343. struct MotionResult {
  344. Vector3 motion;
  345. Vector3 remainder;
  346. Vector3 collision_point;
  347. Vector3 collision_normal;
  348. Vector3 collider_velocity;
  349. real_t collision_depth = 0.0;
  350. real_t collision_safe_fraction = 0.0;
  351. real_t collision_unsafe_fraction = 0.0;
  352. int collision_local_shape = 0;
  353. ObjectID collider_id = 0;
  354. RID collider;
  355. int collider_shape = 0;
  356. Variant collider_metadata;
  357. };
  358. virtual bool body_test_motion(RID p_body, const Transform &p_from, const Vector3 &p_motion, bool p_infinite_inertia, MotionResult *r_result = nullptr, bool p_exclude_raycast_shapes = true, const Set<RID> &p_exclude = Set<RID>()) = 0;
  359. struct SeparationResult {
  360. float collision_depth;
  361. Vector3 collision_point;
  362. Vector3 collision_normal;
  363. Vector3 collider_velocity;
  364. int collision_local_shape;
  365. ObjectID collider_id;
  366. RID collider;
  367. int collider_shape;
  368. Variant collider_metadata;
  369. };
  370. virtual int body_test_ray_separation(RID p_body, const Transform &p_transform, bool p_infinite_inertia, Vector3 &r_recover_motion, SeparationResult *r_results, int p_result_max, float p_margin = 0.001) = 0;
  371. /* SOFT BODY */
  372. virtual RID soft_body_create(bool p_init_sleeping = false) = 0;
  373. virtual void soft_body_update_visual_server(RID p_body, class SoftBodyVisualServerHandler *p_visual_server_handler) = 0;
  374. virtual void soft_body_set_space(RID p_body, RID p_space) = 0;
  375. virtual RID soft_body_get_space(RID p_body) const = 0;
  376. virtual void soft_body_set_mesh(RID p_body, const REF &p_mesh) = 0;
  377. virtual void soft_body_set_collision_layer(RID p_body, uint32_t p_layer) = 0;
  378. virtual uint32_t soft_body_get_collision_layer(RID p_body) const = 0;
  379. virtual void soft_body_set_collision_mask(RID p_body, uint32_t p_mask) = 0;
  380. virtual uint32_t soft_body_get_collision_mask(RID p_body) const = 0;
  381. virtual void soft_body_add_collision_exception(RID p_body, RID p_body_b) = 0;
  382. virtual void soft_body_remove_collision_exception(RID p_body, RID p_body_b) = 0;
  383. virtual void soft_body_get_collision_exceptions(RID p_body, List<RID> *p_exceptions) = 0;
  384. virtual void soft_body_set_state(RID p_body, BodyState p_state, const Variant &p_variant) = 0;
  385. virtual Variant soft_body_get_state(RID p_body, BodyState p_state) const = 0;
  386. virtual void soft_body_set_transform(RID p_body, const Transform &p_transform) = 0;
  387. virtual Vector3 soft_body_get_vertex_position(RID p_body, int vertex_index) const = 0;
  388. virtual void soft_body_set_ray_pickable(RID p_body, bool p_enable) = 0;
  389. virtual bool soft_body_is_ray_pickable(RID p_body) const = 0;
  390. virtual void soft_body_set_simulation_precision(RID p_body, int p_simulation_precision) = 0;
  391. virtual int soft_body_get_simulation_precision(RID p_body) = 0;
  392. virtual void soft_body_set_total_mass(RID p_body, real_t p_total_mass) = 0;
  393. virtual real_t soft_body_get_total_mass(RID p_body) = 0;
  394. virtual void soft_body_set_linear_stiffness(RID p_body, real_t p_stiffness) = 0;
  395. virtual real_t soft_body_get_linear_stiffness(RID p_body) = 0;
  396. virtual void soft_body_set_areaAngular_stiffness(RID p_body, real_t p_stiffness) = 0;
  397. virtual real_t soft_body_get_areaAngular_stiffness(RID p_body) = 0;
  398. virtual void soft_body_set_volume_stiffness(RID p_body, real_t p_stiffness) = 0;
  399. virtual real_t soft_body_get_volume_stiffness(RID p_body) = 0;
  400. virtual void soft_body_set_pressure_coefficient(RID p_body, real_t p_pressure_coefficient) = 0;
  401. virtual real_t soft_body_get_pressure_coefficient(RID p_body) = 0;
  402. virtual void soft_body_set_pose_matching_coefficient(RID p_body, real_t p_pose_matching_coefficient) = 0;
  403. virtual real_t soft_body_get_pose_matching_coefficient(RID p_body) = 0;
  404. virtual void soft_body_set_damping_coefficient(RID p_body, real_t p_damping_coefficient) = 0;
  405. virtual real_t soft_body_get_damping_coefficient(RID p_body) = 0;
  406. virtual void soft_body_set_drag_coefficient(RID p_body, real_t p_drag_coefficient) = 0;
  407. virtual real_t soft_body_get_drag_coefficient(RID p_body) = 0;
  408. virtual void soft_body_move_point(RID p_body, int p_point_index, const Vector3 &p_global_position) = 0;
  409. virtual Vector3 soft_body_get_point_global_position(RID p_body, int p_point_index) = 0;
  410. virtual Vector3 soft_body_get_point_offset(RID p_body, int p_point_index) const = 0;
  411. virtual void soft_body_remove_all_pinned_points(RID p_body) = 0;
  412. virtual void soft_body_pin_point(RID p_body, int p_point_index, bool p_pin) = 0;
  413. virtual bool soft_body_is_point_pinned(RID p_body, int p_point_index) = 0;
  414. /* JOINT API */
  415. enum JointType {
  416. JOINT_PIN,
  417. JOINT_HINGE,
  418. JOINT_SLIDER,
  419. JOINT_CONE_TWIST,
  420. JOINT_6DOF
  421. };
  422. virtual JointType joint_get_type(RID p_joint) const = 0;
  423. virtual void joint_set_solver_priority(RID p_joint, int p_priority) = 0;
  424. virtual int joint_get_solver_priority(RID p_joint) const = 0;
  425. virtual void joint_disable_collisions_between_bodies(RID p_joint, const bool p_disable) = 0;
  426. virtual bool joint_is_disabled_collisions_between_bodies(RID p_joint) const = 0;
  427. virtual RID joint_create_pin(RID p_body_A, const Vector3 &p_local_A, RID p_body_B, const Vector3 &p_local_B) = 0;
  428. enum PinJointParam {
  429. PIN_JOINT_BIAS,
  430. PIN_JOINT_DAMPING,
  431. PIN_JOINT_IMPULSE_CLAMP
  432. };
  433. virtual void pin_joint_set_param(RID p_joint, PinJointParam p_param, float p_value) = 0;
  434. virtual float pin_joint_get_param(RID p_joint, PinJointParam p_param) const = 0;
  435. virtual void pin_joint_set_local_a(RID p_joint, const Vector3 &p_A) = 0;
  436. virtual Vector3 pin_joint_get_local_a(RID p_joint) const = 0;
  437. virtual void pin_joint_set_local_b(RID p_joint, const Vector3 &p_B) = 0;
  438. virtual Vector3 pin_joint_get_local_b(RID p_joint) const = 0;
  439. enum HingeJointParam {
  440. HINGE_JOINT_BIAS,
  441. HINGE_JOINT_LIMIT_UPPER,
  442. HINGE_JOINT_LIMIT_LOWER,
  443. HINGE_JOINT_LIMIT_BIAS,
  444. HINGE_JOINT_LIMIT_SOFTNESS,
  445. HINGE_JOINT_LIMIT_RELAXATION,
  446. HINGE_JOINT_MOTOR_TARGET_VELOCITY,
  447. HINGE_JOINT_MOTOR_MAX_IMPULSE,
  448. HINGE_JOINT_MAX
  449. };
  450. enum HingeJointFlag {
  451. HINGE_JOINT_FLAG_USE_LIMIT,
  452. HINGE_JOINT_FLAG_ENABLE_MOTOR,
  453. HINGE_JOINT_FLAG_MAX
  454. };
  455. virtual RID joint_create_hinge(RID p_body_A, const Transform &p_hinge_A, RID p_body_B, const Transform &p_hinge_B) = 0;
  456. virtual RID joint_create_hinge_simple(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) = 0;
  457. virtual void hinge_joint_set_param(RID p_joint, HingeJointParam p_param, float p_value) = 0;
  458. virtual float hinge_joint_get_param(RID p_joint, HingeJointParam p_param) const = 0;
  459. virtual void hinge_joint_set_flag(RID p_joint, HingeJointFlag p_flag, bool p_value) = 0;
  460. virtual bool hinge_joint_get_flag(RID p_joint, HingeJointFlag p_flag) const = 0;
  461. enum SliderJointParam {
  462. SLIDER_JOINT_LINEAR_LIMIT_UPPER,
  463. SLIDER_JOINT_LINEAR_LIMIT_LOWER,
  464. SLIDER_JOINT_LINEAR_LIMIT_SOFTNESS,
  465. SLIDER_JOINT_LINEAR_LIMIT_RESTITUTION,
  466. SLIDER_JOINT_LINEAR_LIMIT_DAMPING,
  467. SLIDER_JOINT_LINEAR_MOTION_SOFTNESS,
  468. SLIDER_JOINT_LINEAR_MOTION_RESTITUTION,
  469. SLIDER_JOINT_LINEAR_MOTION_DAMPING,
  470. SLIDER_JOINT_LINEAR_ORTHOGONAL_SOFTNESS,
  471. SLIDER_JOINT_LINEAR_ORTHOGONAL_RESTITUTION,
  472. SLIDER_JOINT_LINEAR_ORTHOGONAL_DAMPING,
  473. SLIDER_JOINT_ANGULAR_LIMIT_UPPER,
  474. SLIDER_JOINT_ANGULAR_LIMIT_LOWER,
  475. SLIDER_JOINT_ANGULAR_LIMIT_SOFTNESS,
  476. SLIDER_JOINT_ANGULAR_LIMIT_RESTITUTION,
  477. SLIDER_JOINT_ANGULAR_LIMIT_DAMPING,
  478. SLIDER_JOINT_ANGULAR_MOTION_SOFTNESS,
  479. SLIDER_JOINT_ANGULAR_MOTION_RESTITUTION,
  480. SLIDER_JOINT_ANGULAR_MOTION_DAMPING,
  481. SLIDER_JOINT_ANGULAR_ORTHOGONAL_SOFTNESS,
  482. SLIDER_JOINT_ANGULAR_ORTHOGONAL_RESTITUTION,
  483. SLIDER_JOINT_ANGULAR_ORTHOGONAL_DAMPING,
  484. SLIDER_JOINT_MAX
  485. };
  486. virtual RID joint_create_slider(RID p_body_A, const Transform &p_local_frame_A, RID p_body_B, const Transform &p_local_frame_B) = 0; //reference frame is A
  487. virtual void slider_joint_set_param(RID p_joint, SliderJointParam p_param, float p_value) = 0;
  488. virtual float slider_joint_get_param(RID p_joint, SliderJointParam p_param) const = 0;
  489. enum ConeTwistJointParam {
  490. CONE_TWIST_JOINT_SWING_SPAN,
  491. CONE_TWIST_JOINT_TWIST_SPAN,
  492. CONE_TWIST_JOINT_BIAS,
  493. CONE_TWIST_JOINT_SOFTNESS,
  494. CONE_TWIST_JOINT_RELAXATION,
  495. CONE_TWIST_MAX
  496. };
  497. virtual RID joint_create_cone_twist(RID p_body_A, const Transform &p_local_frame_A, RID p_body_B, const Transform &p_local_frame_B) = 0; //reference frame is A
  498. virtual void cone_twist_joint_set_param(RID p_joint, ConeTwistJointParam p_param, float p_value) = 0;
  499. virtual float cone_twist_joint_get_param(RID p_joint, ConeTwistJointParam p_param) const = 0;
  500. enum G6DOFJointAxisParam {
  501. G6DOF_JOINT_LINEAR_LOWER_LIMIT,
  502. G6DOF_JOINT_LINEAR_UPPER_LIMIT,
  503. G6DOF_JOINT_LINEAR_LIMIT_SOFTNESS,
  504. G6DOF_JOINT_LINEAR_RESTITUTION,
  505. G6DOF_JOINT_LINEAR_DAMPING,
  506. G6DOF_JOINT_LINEAR_MOTOR_TARGET_VELOCITY,
  507. G6DOF_JOINT_LINEAR_MOTOR_FORCE_LIMIT,
  508. G6DOF_JOINT_LINEAR_SPRING_STIFFNESS,
  509. G6DOF_JOINT_LINEAR_SPRING_DAMPING,
  510. G6DOF_JOINT_LINEAR_SPRING_EQUILIBRIUM_POINT,
  511. G6DOF_JOINT_ANGULAR_LOWER_LIMIT,
  512. G6DOF_JOINT_ANGULAR_UPPER_LIMIT,
  513. G6DOF_JOINT_ANGULAR_LIMIT_SOFTNESS,
  514. G6DOF_JOINT_ANGULAR_DAMPING,
  515. G6DOF_JOINT_ANGULAR_RESTITUTION,
  516. G6DOF_JOINT_ANGULAR_FORCE_LIMIT,
  517. G6DOF_JOINT_ANGULAR_ERP,
  518. G6DOF_JOINT_ANGULAR_MOTOR_TARGET_VELOCITY,
  519. G6DOF_JOINT_ANGULAR_MOTOR_FORCE_LIMIT,
  520. G6DOF_JOINT_ANGULAR_SPRING_STIFFNESS,
  521. G6DOF_JOINT_ANGULAR_SPRING_DAMPING,
  522. G6DOF_JOINT_ANGULAR_SPRING_EQUILIBRIUM_POINT,
  523. G6DOF_JOINT_MAX
  524. };
  525. enum G6DOFJointAxisFlag {
  526. G6DOF_JOINT_FLAG_ENABLE_LINEAR_LIMIT,
  527. G6DOF_JOINT_FLAG_ENABLE_ANGULAR_LIMIT,
  528. G6DOF_JOINT_FLAG_ENABLE_ANGULAR_SPRING,
  529. G6DOF_JOINT_FLAG_ENABLE_LINEAR_SPRING,
  530. G6DOF_JOINT_FLAG_ENABLE_MOTOR,
  531. G6DOF_JOINT_FLAG_ENABLE_LINEAR_MOTOR,
  532. G6DOF_JOINT_FLAG_MAX
  533. };
  534. virtual RID joint_create_generic_6dof(RID p_body_A, const Transform &p_local_frame_A, RID p_body_B, const Transform &p_local_frame_B) = 0; //reference frame is A
  535. virtual void generic_6dof_joint_set_param(RID p_joint, Vector3::Axis, G6DOFJointAxisParam p_param, float p_value) = 0;
  536. virtual float generic_6dof_joint_get_param(RID p_joint, Vector3::Axis, G6DOFJointAxisParam p_param) = 0;
  537. virtual void generic_6dof_joint_set_flag(RID p_joint, Vector3::Axis, G6DOFJointAxisFlag p_flag, bool p_enable) = 0;
  538. virtual bool generic_6dof_joint_get_flag(RID p_joint, Vector3::Axis, G6DOFJointAxisFlag p_flag) = 0;
  539. /* QUERY API */
  540. enum AreaBodyStatus {
  541. AREA_BODY_ADDED,
  542. AREA_BODY_REMOVED
  543. };
  544. /* MISC */
  545. virtual void free(RID p_rid) = 0;
  546. virtual void set_active(bool p_active) = 0;
  547. virtual void init() = 0;
  548. virtual void step(float p_step) = 0;
  549. virtual void flush_queries() = 0;
  550. virtual void finish() = 0;
  551. virtual bool is_flushing_queries() const = 0;
  552. virtual void set_collision_iterations(int p_iterations) = 0;
  553. enum ProcessInfo {
  554. INFO_ACTIVE_OBJECTS,
  555. INFO_COLLISION_PAIRS,
  556. INFO_ISLAND_COUNT
  557. };
  558. virtual int get_process_info(ProcessInfo p_info) = 0;
  559. PhysicsServer();
  560. ~PhysicsServer();
  561. };
  562. class PhysicsTestMotionResult : public Reference {
  563. GDCLASS(PhysicsTestMotionResult, Reference);
  564. PhysicsServer::MotionResult result;
  565. bool colliding = false;
  566. friend class PhysicsServer;
  567. protected:
  568. static void _bind_methods();
  569. public:
  570. PhysicsServer::MotionResult *get_result_ptr() const { return const_cast<PhysicsServer::MotionResult *>(&result); }
  571. //bool is_colliding() const;
  572. Vector3 get_motion() const;
  573. Vector3 get_motion_remainder() const;
  574. Vector3 get_collision_point() const;
  575. Vector3 get_collision_normal() const;
  576. Vector3 get_collider_velocity() const;
  577. ObjectID get_collider_id() const;
  578. RID get_collider_rid() const;
  579. Object *get_collider() const;
  580. int get_collider_shape() const;
  581. real_t get_collision_depth() const;
  582. real_t get_collision_safe_fraction() const;
  583. real_t get_collision_unsafe_fraction() const;
  584. };
  585. typedef PhysicsServer *(*CreatePhysicsServerCallback)();
  586. class PhysicsServerManager {
  587. struct ClassInfo {
  588. String name;
  589. CreatePhysicsServerCallback create_callback;
  590. ClassInfo() :
  591. name(""),
  592. create_callback(nullptr) {}
  593. ClassInfo(String p_name, CreatePhysicsServerCallback p_create_callback) :
  594. name(p_name),
  595. create_callback(p_create_callback) {}
  596. ClassInfo(const ClassInfo &p_ci) :
  597. name(p_ci.name),
  598. create_callback(p_ci.create_callback) {}
  599. ClassInfo operator=(const ClassInfo &p_ci) {
  600. name = p_ci.name;
  601. create_callback = p_ci.create_callback;
  602. return *this;
  603. }
  604. };
  605. static Vector<ClassInfo> physics_servers;
  606. static int default_server_id;
  607. static int default_server_priority;
  608. public:
  609. static const String setting_property_name;
  610. static int current_server_id;
  611. private:
  612. static void on_servers_changed();
  613. public:
  614. static void register_server(const String &p_name, CreatePhysicsServerCallback p_creat_callback);
  615. static void set_default_server(const String &p_name, int p_priority = 0);
  616. static int find_server_id(const String &p_name);
  617. static int get_servers_count();
  618. static String get_server_name(int p_id);
  619. static PhysicsServer *new_default_server();
  620. static PhysicsServer *new_server(const String &p_name);
  621. };
  622. VARIANT_ENUM_CAST(PhysicsServer::ShapeType);
  623. VARIANT_ENUM_CAST(PhysicsServer::SpaceParameter);
  624. VARIANT_ENUM_CAST(PhysicsServer::AreaParameter);
  625. VARIANT_ENUM_CAST(PhysicsServer::AreaSpaceOverrideMode);
  626. VARIANT_ENUM_CAST(PhysicsServer::BodyMode);
  627. VARIANT_ENUM_CAST(PhysicsServer::BodyParameter);
  628. VARIANT_ENUM_CAST(PhysicsServer::BodyState);
  629. VARIANT_ENUM_CAST(PhysicsServer::BodyAxis);
  630. VARIANT_ENUM_CAST(PhysicsServer::PinJointParam);
  631. VARIANT_ENUM_CAST(PhysicsServer::JointType);
  632. VARIANT_ENUM_CAST(PhysicsServer::HingeJointParam);
  633. VARIANT_ENUM_CAST(PhysicsServer::HingeJointFlag);
  634. VARIANT_ENUM_CAST(PhysicsServer::SliderJointParam);
  635. VARIANT_ENUM_CAST(PhysicsServer::ConeTwistJointParam);
  636. VARIANT_ENUM_CAST(PhysicsServer::G6DOFJointAxisParam);
  637. VARIANT_ENUM_CAST(PhysicsServer::G6DOFJointAxisFlag);
  638. VARIANT_ENUM_CAST(PhysicsServer::AreaBodyStatus);
  639. VARIANT_ENUM_CAST(PhysicsServer::ProcessInfo);
  640. #endif // PHYSICS_SERVER_H