physics_server.h 32 KB

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