physics_server_3d.h 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841
  1. /*************************************************************************/
  2. /* physics_server_3d.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #ifndef PHYSICS_SERVER_H
  31. #define PHYSICS_SERVER_H
  32. #include "core/io/resource.h"
  33. #include "core/object/class_db.h"
  34. class PhysicsDirectSpaceState3D;
  35. class PhysicsDirectBodyState3D : public Object {
  36. GDCLASS(PhysicsDirectBodyState3D, Object);
  37. protected:
  38. static void _bind_methods();
  39. public:
  40. virtual Vector3 get_total_gravity() const = 0;
  41. virtual real_t get_total_angular_damp() const = 0;
  42. virtual real_t 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 real_t 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_position = Vector3()) = 0;
  56. virtual void add_torque(const Vector3 &p_torque) = 0;
  57. virtual void apply_central_impulse(const Vector3 &p_impulse) = 0;
  58. virtual void apply_impulse(const Vector3 &p_impulse, const Vector3 &p_position = Vector3()) = 0;
  59. virtual void apply_torque_impulse(const Vector3 &p_impulse) = 0;
  60. virtual void set_sleep_state(bool p_sleep) = 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 real_t 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 PhysicsDirectSpaceState3D *get_space_state() = 0;
  76. PhysicsDirectBodyState3D();
  77. };
  78. class PhysicsShapeQueryResult3D;
  79. class PhysicsShapeQueryParameters3D : public Reference {
  80. GDCLASS(PhysicsShapeQueryParameters3D, Reference);
  81. friend class PhysicsDirectSpaceState3D;
  82. RES shape_ref;
  83. RID shape;
  84. Transform transform;
  85. real_t margin;
  86. Set<RID> exclude;
  87. uint32_t collision_mask;
  88. bool collide_with_bodies;
  89. bool collide_with_areas;
  90. protected:
  91. static void _bind_methods();
  92. public:
  93. void set_shape(const RES &p_shape_ref);
  94. RES get_shape() const;
  95. void set_shape_rid(const RID &p_shape);
  96. RID get_shape_rid() const;
  97. void set_transform(const Transform &p_transform);
  98. Transform get_transform() const;
  99. void set_margin(real_t p_margin);
  100. real_t get_margin() const;
  101. void set_collision_mask(int p_collision_mask);
  102. int get_collision_mask() const;
  103. void set_exclude(const Vector<RID> &p_exclude);
  104. Vector<RID> get_exclude() const;
  105. void set_collide_with_bodies(bool p_enable);
  106. bool is_collide_with_bodies_enabled() const;
  107. void set_collide_with_areas(bool p_enable);
  108. bool is_collide_with_areas_enabled() const;
  109. PhysicsShapeQueryParameters3D();
  110. };
  111. class PhysicsDirectSpaceState3D : public Object {
  112. GDCLASS(PhysicsDirectSpaceState3D, Object);
  113. private:
  114. 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);
  115. Array _intersect_shape(const Ref<PhysicsShapeQueryParameters3D> &p_shape_query, int p_max_results = 32);
  116. Array _cast_motion(const Ref<PhysicsShapeQueryParameters3D> &p_shape_query, const Vector3 &p_motion);
  117. Array _collide_shape(const Ref<PhysicsShapeQueryParameters3D> &p_shape_query, int p_max_results = 32);
  118. Dictionary _get_rest_info(const Ref<PhysicsShapeQueryParameters3D> &p_shape_query);
  119. protected:
  120. static void _bind_methods();
  121. public:
  122. struct ShapeResult {
  123. RID rid;
  124. ObjectID collider_id;
  125. Object *collider;
  126. int shape;
  127. };
  128. 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;
  129. struct RayResult {
  130. Vector3 position;
  131. Vector3 normal;
  132. RID rid;
  133. ObjectID collider_id;
  134. Object *collider;
  135. int shape;
  136. };
  137. 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;
  138. virtual int intersect_shape(const RID &p_shape, const Transform &p_xform, real_t 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;
  139. struct ShapeRestInfo {
  140. Vector3 point;
  141. Vector3 normal;
  142. RID rid;
  143. ObjectID collider_id;
  144. int shape;
  145. Vector3 linear_velocity; //velocity at contact point
  146. };
  147. virtual bool cast_motion(const RID &p_shape, const Transform &p_xform, const Vector3 &p_motion, real_t p_margin, real_t &p_closest_safe, real_t &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;
  148. virtual bool collide_shape(RID p_shape, const Transform &p_shape_xform, real_t 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;
  149. virtual bool rest_info(RID p_shape, const Transform &p_shape_xform, real_t 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;
  150. virtual Vector3 get_closest_point_to_object_volume(RID p_object, const Vector3 p_point) const = 0;
  151. PhysicsDirectSpaceState3D();
  152. };
  153. class PhysicsShapeQueryResult3D : public Reference {
  154. GDCLASS(PhysicsShapeQueryResult3D, Reference);
  155. Vector<PhysicsDirectSpaceState3D::ShapeResult> result;
  156. friend class PhysicsDirectSpaceState3D;
  157. protected:
  158. static void _bind_methods();
  159. public:
  160. int get_result_count() const;
  161. RID get_result_rid(int p_idx) const;
  162. ObjectID get_result_object_id(int p_idx) const;
  163. Object *get_result_object(int p_idx) const;
  164. int get_result_object_shape(int p_idx) const;
  165. PhysicsShapeQueryResult3D();
  166. };
  167. class PhysicsServer3D : public Object {
  168. GDCLASS(PhysicsServer3D, Object);
  169. static PhysicsServer3D *singleton;
  170. protected:
  171. static void _bind_methods();
  172. public:
  173. static PhysicsServer3D *get_singleton();
  174. enum ShapeType {
  175. SHAPE_PLANE, ///< plane:"plane"
  176. SHAPE_RAY, ///< float:"length"
  177. SHAPE_SPHERE, ///< float:"radius"
  178. SHAPE_BOX, ///< vec3:"extents"
  179. SHAPE_CAPSULE, ///< dict( float:"radius", float:"height"):capsule
  180. SHAPE_CYLINDER, ///< dict( float:"radius", float:"height"):cylinder
  181. SHAPE_CONVEX_POLYGON, ///< array of planes:"planes"
  182. SHAPE_CONCAVE_POLYGON, ///< vector3 array:"triangles" , or Dictionary with "indices" (int array) and "triangles" (Vector3 array)
  183. SHAPE_HEIGHTMAP, ///< dict( int:"width", int:"depth",float:"cell_size", float_array:"heights"
  184. SHAPE_CUSTOM, ///< Server-Implementation based custom shape, calling shape_create() with this value will result in an error
  185. };
  186. RID shape_create(ShapeType p_shape);
  187. virtual RID plane_shape_create() = 0;
  188. virtual RID ray_shape_create() = 0;
  189. virtual RID sphere_shape_create() = 0;
  190. virtual RID box_shape_create() = 0;
  191. virtual RID capsule_shape_create() = 0;
  192. virtual RID cylinder_shape_create() = 0;
  193. virtual RID convex_polygon_shape_create() = 0;
  194. virtual RID concave_polygon_shape_create() = 0;
  195. virtual RID heightmap_shape_create() = 0;
  196. virtual RID custom_shape_create() = 0;
  197. virtual void shape_set_data(RID p_shape, const Variant &p_data) = 0;
  198. virtual void shape_set_custom_solver_bias(RID p_shape, real_t p_bias) = 0;
  199. virtual ShapeType shape_get_type(RID p_shape) const = 0;
  200. virtual Variant shape_get_data(RID p_shape) const = 0;
  201. virtual void shape_set_margin(RID p_shape, real_t p_margin) = 0;
  202. virtual real_t shape_get_margin(RID p_shape) const = 0;
  203. virtual real_t shape_get_custom_solver_bias(RID p_shape) const = 0;
  204. /* SPACE API */
  205. virtual RID space_create() = 0;
  206. virtual void space_set_active(RID p_space, bool p_active) = 0;
  207. virtual bool space_is_active(RID p_space) const = 0;
  208. enum SpaceParameter {
  209. SPACE_PARAM_CONTACT_RECYCLE_RADIUS,
  210. SPACE_PARAM_CONTACT_MAX_SEPARATION,
  211. SPACE_PARAM_BODY_MAX_ALLOWED_PENETRATION,
  212. SPACE_PARAM_BODY_LINEAR_VELOCITY_SLEEP_THRESHOLD,
  213. SPACE_PARAM_BODY_ANGULAR_VELOCITY_SLEEP_THRESHOLD,
  214. SPACE_PARAM_BODY_TIME_TO_SLEEP,
  215. SPACE_PARAM_BODY_ANGULAR_VELOCITY_DAMP_RATIO,
  216. SPACE_PARAM_CONSTRAINT_DEFAULT_BIAS,
  217. SPACE_PARAM_TEST_MOTION_MIN_CONTACT_DEPTH
  218. };
  219. virtual void space_set_param(RID p_space, SpaceParameter p_param, real_t p_value) = 0;
  220. virtual real_t space_get_param(RID p_space, SpaceParameter p_param) const = 0;
  221. // this function only works on physics process, errors and returns null otherwise
  222. virtual PhysicsDirectSpaceState3D *space_get_direct_state(RID p_space) = 0;
  223. virtual void space_set_debug_contacts(RID p_space, int p_max_contacts) = 0;
  224. virtual Vector<Vector3> space_get_contacts(RID p_space) const = 0;
  225. virtual int space_get_contact_count(RID p_space) const = 0;
  226. //missing space parameters
  227. /* AREA API */
  228. //missing attenuation? missing better override?
  229. enum AreaParameter {
  230. AREA_PARAM_GRAVITY,
  231. AREA_PARAM_GRAVITY_VECTOR,
  232. AREA_PARAM_GRAVITY_IS_POINT,
  233. AREA_PARAM_GRAVITY_DISTANCE_SCALE,
  234. AREA_PARAM_GRAVITY_POINT_ATTENUATION,
  235. AREA_PARAM_LINEAR_DAMP,
  236. AREA_PARAM_ANGULAR_DAMP,
  237. AREA_PARAM_PRIORITY
  238. };
  239. virtual RID area_create() = 0;
  240. virtual void area_set_space(RID p_area, RID p_space) = 0;
  241. virtual RID area_get_space(RID p_area) const = 0;
  242. enum AreaSpaceOverrideMode {
  243. AREA_SPACE_OVERRIDE_DISABLED,
  244. AREA_SPACE_OVERRIDE_COMBINE,
  245. AREA_SPACE_OVERRIDE_COMBINE_REPLACE,
  246. AREA_SPACE_OVERRIDE_REPLACE,
  247. AREA_SPACE_OVERRIDE_REPLACE_COMBINE
  248. };
  249. virtual void area_set_space_override_mode(RID p_area, AreaSpaceOverrideMode p_mode) = 0;
  250. virtual AreaSpaceOverrideMode area_get_space_override_mode(RID p_area) const = 0;
  251. virtual void area_add_shape(RID p_area, RID p_shape, const Transform &p_transform = Transform(), bool p_disabled = false) = 0;
  252. virtual void area_set_shape(RID p_area, int p_shape_idx, RID p_shape) = 0;
  253. virtual void area_set_shape_transform(RID p_area, int p_shape_idx, const Transform &p_transform) = 0;
  254. virtual int area_get_shape_count(RID p_area) const = 0;
  255. virtual RID area_get_shape(RID p_area, int p_shape_idx) const = 0;
  256. virtual Transform area_get_shape_transform(RID p_area, int p_shape_idx) const = 0;
  257. virtual void area_remove_shape(RID p_area, int p_shape_idx) = 0;
  258. virtual void area_clear_shapes(RID p_area) = 0;
  259. virtual void area_set_shape_disabled(RID p_area, int p_shape_idx, bool p_disabled) = 0;
  260. virtual void area_attach_object_instance_id(RID p_area, ObjectID p_id) = 0;
  261. virtual ObjectID area_get_object_instance_id(RID p_area) const = 0;
  262. virtual void area_set_param(RID p_area, AreaParameter p_param, const Variant &p_value) = 0;
  263. virtual void area_set_transform(RID p_area, const Transform &p_transform) = 0;
  264. virtual Variant area_get_param(RID p_parea, AreaParameter p_param) const = 0;
  265. virtual Transform area_get_transform(RID p_area) const = 0;
  266. virtual void area_set_collision_mask(RID p_area, uint32_t p_mask) = 0;
  267. virtual void area_set_collision_layer(RID p_area, uint32_t p_layer) = 0;
  268. virtual void area_set_monitorable(RID p_area, bool p_monitorable) = 0;
  269. virtual void area_set_monitor_callback(RID p_area, Object *p_receiver, const StringName &p_method) = 0;
  270. virtual void area_set_area_monitor_callback(RID p_area, Object *p_receiver, const StringName &p_method) = 0;
  271. virtual void area_set_ray_pickable(RID p_area, bool p_enable) = 0;
  272. /* BODY API */
  273. //missing ccd?
  274. enum BodyMode {
  275. BODY_MODE_STATIC,
  276. BODY_MODE_KINEMATIC,
  277. BODY_MODE_RIGID,
  278. BODY_MODE_CHARACTER
  279. };
  280. virtual RID body_create() = 0;
  281. virtual void body_set_space(RID p_body, RID p_space) = 0;
  282. virtual RID body_get_space(RID p_body) const = 0;
  283. virtual void body_set_mode(RID p_body, BodyMode p_mode) = 0;
  284. virtual BodyMode body_get_mode(RID p_body) const = 0;
  285. virtual void body_add_shape(RID p_body, RID p_shape, const Transform &p_transform = Transform(), bool p_disabled = false) = 0;
  286. virtual void body_set_shape(RID p_body, int p_shape_idx, RID p_shape) = 0;
  287. virtual void body_set_shape_transform(RID p_body, int p_shape_idx, const Transform &p_transform) = 0;
  288. virtual int body_get_shape_count(RID p_body) const = 0;
  289. virtual RID body_get_shape(RID p_body, int p_shape_idx) const = 0;
  290. virtual Transform body_get_shape_transform(RID p_body, int p_shape_idx) const = 0;
  291. virtual void body_remove_shape(RID p_body, int p_shape_idx) = 0;
  292. virtual void body_clear_shapes(RID p_body) = 0;
  293. virtual void body_set_shape_disabled(RID p_body, int p_shape_idx, bool p_disabled) = 0;
  294. virtual void body_attach_object_instance_id(RID p_body, ObjectID p_id) = 0;
  295. virtual ObjectID body_get_object_instance_id(RID p_body) const = 0;
  296. virtual void body_set_enable_continuous_collision_detection(RID p_body, bool p_enable) = 0;
  297. virtual bool body_is_continuous_collision_detection_enabled(RID p_body) const = 0;
  298. virtual void body_set_collision_layer(RID p_body, uint32_t p_layer) = 0;
  299. virtual uint32_t body_get_collision_layer(RID p_body) const = 0;
  300. virtual void body_set_collision_mask(RID p_body, uint32_t p_mask) = 0;
  301. virtual uint32_t body_get_collision_mask(RID p_body) const = 0;
  302. virtual void body_set_user_flags(RID p_body, uint32_t p_flags) = 0;
  303. virtual uint32_t body_get_user_flags(RID p_body) const = 0;
  304. // common body variables
  305. enum BodyParameter {
  306. BODY_PARAM_BOUNCE,
  307. BODY_PARAM_FRICTION,
  308. BODY_PARAM_MASS, ///< unused for static, always infinite
  309. BODY_PARAM_GRAVITY_SCALE,
  310. BODY_PARAM_LINEAR_DAMP,
  311. BODY_PARAM_ANGULAR_DAMP,
  312. BODY_PARAM_MAX,
  313. };
  314. virtual void body_set_param(RID p_body, BodyParameter p_param, real_t p_value) = 0;
  315. virtual real_t body_get_param(RID p_body, BodyParameter p_param) const = 0;
  316. virtual void body_set_kinematic_safe_margin(RID p_body, real_t p_margin) = 0;
  317. virtual real_t body_get_kinematic_safe_margin(RID p_body) const = 0;
  318. //state
  319. enum BodyState {
  320. BODY_STATE_TRANSFORM,
  321. BODY_STATE_LINEAR_VELOCITY,
  322. BODY_STATE_ANGULAR_VELOCITY,
  323. BODY_STATE_SLEEPING,
  324. BODY_STATE_CAN_SLEEP
  325. };
  326. virtual void body_set_state(RID p_body, BodyState p_state, const Variant &p_variant) = 0;
  327. virtual Variant body_get_state(RID p_body, BodyState p_state) const = 0;
  328. //do something about it
  329. virtual void body_set_applied_force(RID p_body, const Vector3 &p_force) = 0;
  330. virtual Vector3 body_get_applied_force(RID p_body) const = 0;
  331. virtual void body_set_applied_torque(RID p_body, const Vector3 &p_torque) = 0;
  332. virtual Vector3 body_get_applied_torque(RID p_body) const = 0;
  333. virtual void body_add_central_force(RID p_body, const Vector3 &p_force) = 0;
  334. virtual void body_add_force(RID p_body, const Vector3 &p_force, const Vector3 &p_position = Vector3()) = 0;
  335. virtual void body_add_torque(RID p_body, const Vector3 &p_torque) = 0;
  336. virtual void body_apply_central_impulse(RID p_body, const Vector3 &p_impulse) = 0;
  337. virtual void body_apply_impulse(RID p_body, const Vector3 &p_impulse, const Vector3 &p_position = Vector3()) = 0;
  338. virtual void body_apply_torque_impulse(RID p_body, const Vector3 &p_impulse) = 0;
  339. virtual void body_set_axis_velocity(RID p_body, const Vector3 &p_axis_velocity) = 0;
  340. enum BodyAxis {
  341. BODY_AXIS_LINEAR_X = 1 << 0,
  342. BODY_AXIS_LINEAR_Y = 1 << 1,
  343. BODY_AXIS_LINEAR_Z = 1 << 2,
  344. BODY_AXIS_ANGULAR_X = 1 << 3,
  345. BODY_AXIS_ANGULAR_Y = 1 << 4,
  346. BODY_AXIS_ANGULAR_Z = 1 << 5
  347. };
  348. virtual void body_set_axis_lock(RID p_body, BodyAxis p_axis, bool p_lock) = 0;
  349. virtual bool body_is_axis_locked(RID p_body, BodyAxis p_axis) const = 0;
  350. //fix
  351. virtual void body_add_collision_exception(RID p_body, RID p_body_b) = 0;
  352. virtual void body_remove_collision_exception(RID p_body, RID p_body_b) = 0;
  353. virtual void body_get_collision_exceptions(RID p_body, List<RID> *p_exceptions) = 0;
  354. virtual void body_set_max_contacts_reported(RID p_body, int p_contacts) = 0;
  355. virtual int body_get_max_contacts_reported(RID p_body) const = 0;
  356. //missing remove
  357. virtual void body_set_contacts_reported_depth_threshold(RID p_body, real_t p_threshold) = 0;
  358. virtual real_t body_get_contacts_reported_depth_threshold(RID p_body) const = 0;
  359. virtual void body_set_omit_force_integration(RID p_body, bool p_omit) = 0;
  360. virtual bool body_is_omitting_force_integration(RID p_body) const = 0;
  361. virtual void body_set_force_integration_callback(RID p_body, Object *p_receiver, const StringName &p_method, const Variant &p_udata = Variant()) = 0;
  362. virtual void body_set_ray_pickable(RID p_body, bool p_enable) = 0;
  363. // this function only works on physics process, errors and returns null otherwise
  364. virtual PhysicsDirectBodyState3D *body_get_direct_state(RID p_body) = 0;
  365. struct MotionResult {
  366. Vector3 motion;
  367. Vector3 remainder;
  368. Vector3 collision_point;
  369. Vector3 collision_normal;
  370. Vector3 collider_velocity;
  371. int collision_local_shape;
  372. ObjectID collider_id;
  373. RID collider;
  374. int collider_shape;
  375. Variant collider_metadata;
  376. MotionResult() {
  377. collision_local_shape = 0;
  378. collider_id = ObjectID();
  379. collider_shape = 0;
  380. }
  381. };
  382. 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) = 0;
  383. struct SeparationResult {
  384. real_t collision_depth;
  385. Vector3 collision_point;
  386. Vector3 collision_normal;
  387. Vector3 collider_velocity;
  388. int collision_local_shape;
  389. ObjectID collider_id;
  390. RID collider;
  391. int collider_shape;
  392. Variant collider_metadata;
  393. };
  394. 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, real_t p_margin = 0.001) = 0;
  395. /* SOFT BODY */
  396. virtual RID soft_body_create() = 0;
  397. virtual void soft_body_update_rendering_server(RID p_body, class SoftBodyRenderingServerHandler *p_rendering_server_handler) = 0;
  398. virtual void soft_body_set_space(RID p_body, RID p_space) = 0;
  399. virtual RID soft_body_get_space(RID p_body) const = 0;
  400. virtual void soft_body_set_mesh(RID p_body, const REF &p_mesh) = 0;
  401. virtual void soft_body_set_collision_layer(RID p_body, uint32_t p_layer) = 0;
  402. virtual uint32_t soft_body_get_collision_layer(RID p_body) const = 0;
  403. virtual void soft_body_set_collision_mask(RID p_body, uint32_t p_mask) = 0;
  404. virtual uint32_t soft_body_get_collision_mask(RID p_body) const = 0;
  405. virtual void soft_body_add_collision_exception(RID p_body, RID p_body_b) = 0;
  406. virtual void soft_body_remove_collision_exception(RID p_body, RID p_body_b) = 0;
  407. virtual void soft_body_get_collision_exceptions(RID p_body, List<RID> *p_exceptions) = 0;
  408. virtual void soft_body_set_state(RID p_body, BodyState p_state, const Variant &p_variant) = 0;
  409. virtual Variant soft_body_get_state(RID p_body, BodyState p_state) const = 0;
  410. virtual void soft_body_set_transform(RID p_body, const Transform &p_transform) = 0;
  411. virtual Vector3 soft_body_get_vertex_position(RID p_body, int vertex_index) const = 0;
  412. virtual void soft_body_set_ray_pickable(RID p_body, bool p_enable) = 0;
  413. virtual void soft_body_set_simulation_precision(RID p_body, int p_simulation_precision) = 0;
  414. virtual int soft_body_get_simulation_precision(RID p_body) const = 0;
  415. virtual void soft_body_set_total_mass(RID p_body, real_t p_total_mass) = 0;
  416. virtual real_t soft_body_get_total_mass(RID p_body) const = 0;
  417. virtual void soft_body_set_linear_stiffness(RID p_body, real_t p_stiffness) = 0;
  418. virtual real_t soft_body_get_linear_stiffness(RID p_body) const = 0;
  419. virtual void soft_body_set_angular_stiffness(RID p_body, real_t p_stiffness) = 0;
  420. virtual real_t soft_body_get_angular_stiffness(RID p_body) const = 0;
  421. virtual void soft_body_set_volume_stiffness(RID p_body, real_t p_stiffness) = 0;
  422. virtual real_t soft_body_get_volume_stiffness(RID p_body) const = 0;
  423. virtual void soft_body_set_pressure_coefficient(RID p_body, real_t p_pressure_coefficient) = 0;
  424. virtual real_t soft_body_get_pressure_coefficient(RID p_body) const = 0;
  425. virtual void soft_body_set_pose_matching_coefficient(RID p_body, real_t p_pose_matching_coefficient) = 0;
  426. virtual real_t soft_body_get_pose_matching_coefficient(RID p_body) const = 0;
  427. virtual void soft_body_set_damping_coefficient(RID p_body, real_t p_damping_coefficient) = 0;
  428. virtual real_t soft_body_get_damping_coefficient(RID p_body) const = 0;
  429. virtual void soft_body_set_drag_coefficient(RID p_body, real_t p_drag_coefficient) = 0;
  430. virtual real_t soft_body_get_drag_coefficient(RID p_body) const = 0;
  431. virtual void soft_body_move_point(RID p_body, int p_point_index, const Vector3 &p_global_position) = 0;
  432. virtual Vector3 soft_body_get_point_global_position(RID p_body, int p_point_index) const = 0;
  433. virtual Vector3 soft_body_get_point_offset(RID p_body, int p_point_index) const = 0;
  434. virtual void soft_body_remove_all_pinned_points(RID p_body) = 0;
  435. virtual void soft_body_pin_point(RID p_body, int p_point_index, bool p_pin) = 0;
  436. virtual bool soft_body_is_point_pinned(RID p_body, int p_point_index) const = 0;
  437. /* JOINT API */
  438. enum JointType {
  439. JOINT_TYPE_PIN,
  440. JOINT_TYPE_HINGE,
  441. JOINT_TYPE_SLIDER,
  442. JOINT_TYPE_CONE_TWIST,
  443. JOINT_TYPE_6DOF,
  444. JOINT_TYPE_MAX,
  445. };
  446. virtual RID joint_create() = 0;
  447. virtual void joint_clear(RID p_joint) = 0;
  448. virtual JointType joint_get_type(RID p_joint) const = 0;
  449. virtual void joint_set_solver_priority(RID p_joint, int p_priority) = 0;
  450. virtual int joint_get_solver_priority(RID p_joint) const = 0;
  451. virtual void joint_disable_collisions_between_bodies(RID p_joint, const bool p_disable) = 0;
  452. virtual bool joint_is_disabled_collisions_between_bodies(RID p_joint) const = 0;
  453. 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) = 0;
  454. enum PinJointParam {
  455. PIN_JOINT_BIAS,
  456. PIN_JOINT_DAMPING,
  457. PIN_JOINT_IMPULSE_CLAMP
  458. };
  459. virtual void pin_joint_set_param(RID p_joint, PinJointParam p_param, real_t p_value) = 0;
  460. virtual real_t pin_joint_get_param(RID p_joint, PinJointParam p_param) const = 0;
  461. virtual void pin_joint_set_local_a(RID p_joint, const Vector3 &p_A) = 0;
  462. virtual Vector3 pin_joint_get_local_a(RID p_joint) const = 0;
  463. virtual void pin_joint_set_local_b(RID p_joint, const Vector3 &p_B) = 0;
  464. virtual Vector3 pin_joint_get_local_b(RID p_joint) const = 0;
  465. enum HingeJointParam {
  466. HINGE_JOINT_BIAS,
  467. HINGE_JOINT_LIMIT_UPPER,
  468. HINGE_JOINT_LIMIT_LOWER,
  469. HINGE_JOINT_LIMIT_BIAS,
  470. HINGE_JOINT_LIMIT_SOFTNESS,
  471. HINGE_JOINT_LIMIT_RELAXATION,
  472. HINGE_JOINT_MOTOR_TARGET_VELOCITY,
  473. HINGE_JOINT_MOTOR_MAX_IMPULSE,
  474. HINGE_JOINT_MAX
  475. };
  476. enum HingeJointFlag {
  477. HINGE_JOINT_FLAG_USE_LIMIT,
  478. HINGE_JOINT_FLAG_ENABLE_MOTOR,
  479. HINGE_JOINT_FLAG_MAX
  480. };
  481. virtual void joint_make_hinge(RID p_joint, RID p_body_A, const Transform &p_hinge_A, RID p_body_B, const Transform &p_hinge_B) = 0;
  482. 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) = 0;
  483. virtual void hinge_joint_set_param(RID p_joint, HingeJointParam p_param, real_t p_value) = 0;
  484. virtual real_t hinge_joint_get_param(RID p_joint, HingeJointParam p_param) const = 0;
  485. virtual void hinge_joint_set_flag(RID p_joint, HingeJointFlag p_flag, bool p_value) = 0;
  486. virtual bool hinge_joint_get_flag(RID p_joint, HingeJointFlag p_flag) const = 0;
  487. enum SliderJointParam {
  488. SLIDER_JOINT_LINEAR_LIMIT_UPPER,
  489. SLIDER_JOINT_LINEAR_LIMIT_LOWER,
  490. SLIDER_JOINT_LINEAR_LIMIT_SOFTNESS,
  491. SLIDER_JOINT_LINEAR_LIMIT_RESTITUTION,
  492. SLIDER_JOINT_LINEAR_LIMIT_DAMPING,
  493. SLIDER_JOINT_LINEAR_MOTION_SOFTNESS,
  494. SLIDER_JOINT_LINEAR_MOTION_RESTITUTION,
  495. SLIDER_JOINT_LINEAR_MOTION_DAMPING,
  496. SLIDER_JOINT_LINEAR_ORTHOGONAL_SOFTNESS,
  497. SLIDER_JOINT_LINEAR_ORTHOGONAL_RESTITUTION,
  498. SLIDER_JOINT_LINEAR_ORTHOGONAL_DAMPING,
  499. SLIDER_JOINT_ANGULAR_LIMIT_UPPER,
  500. SLIDER_JOINT_ANGULAR_LIMIT_LOWER,
  501. SLIDER_JOINT_ANGULAR_LIMIT_SOFTNESS,
  502. SLIDER_JOINT_ANGULAR_LIMIT_RESTITUTION,
  503. SLIDER_JOINT_ANGULAR_LIMIT_DAMPING,
  504. SLIDER_JOINT_ANGULAR_MOTION_SOFTNESS,
  505. SLIDER_JOINT_ANGULAR_MOTION_RESTITUTION,
  506. SLIDER_JOINT_ANGULAR_MOTION_DAMPING,
  507. SLIDER_JOINT_ANGULAR_ORTHOGONAL_SOFTNESS,
  508. SLIDER_JOINT_ANGULAR_ORTHOGONAL_RESTITUTION,
  509. SLIDER_JOINT_ANGULAR_ORTHOGONAL_DAMPING,
  510. SLIDER_JOINT_MAX
  511. };
  512. virtual void joint_make_slider(RID p_joint, 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
  513. virtual void slider_joint_set_param(RID p_joint, SliderJointParam p_param, real_t p_value) = 0;
  514. virtual real_t slider_joint_get_param(RID p_joint, SliderJointParam p_param) const = 0;
  515. enum ConeTwistJointParam {
  516. CONE_TWIST_JOINT_SWING_SPAN,
  517. CONE_TWIST_JOINT_TWIST_SPAN,
  518. CONE_TWIST_JOINT_BIAS,
  519. CONE_TWIST_JOINT_SOFTNESS,
  520. CONE_TWIST_JOINT_RELAXATION,
  521. CONE_TWIST_MAX
  522. };
  523. virtual void joint_make_cone_twist(RID p_joint, 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
  524. virtual void cone_twist_joint_set_param(RID p_joint, ConeTwistJointParam p_param, real_t p_value) = 0;
  525. virtual real_t cone_twist_joint_get_param(RID p_joint, ConeTwistJointParam p_param) const = 0;
  526. enum G6DOFJointAxisParam {
  527. G6DOF_JOINT_LINEAR_LOWER_LIMIT,
  528. G6DOF_JOINT_LINEAR_UPPER_LIMIT,
  529. G6DOF_JOINT_LINEAR_LIMIT_SOFTNESS,
  530. G6DOF_JOINT_LINEAR_RESTITUTION,
  531. G6DOF_JOINT_LINEAR_DAMPING,
  532. G6DOF_JOINT_LINEAR_MOTOR_TARGET_VELOCITY,
  533. G6DOF_JOINT_LINEAR_MOTOR_FORCE_LIMIT,
  534. G6DOF_JOINT_LINEAR_SPRING_STIFFNESS,
  535. G6DOF_JOINT_LINEAR_SPRING_DAMPING,
  536. G6DOF_JOINT_LINEAR_SPRING_EQUILIBRIUM_POINT,
  537. G6DOF_JOINT_ANGULAR_LOWER_LIMIT,
  538. G6DOF_JOINT_ANGULAR_UPPER_LIMIT,
  539. G6DOF_JOINT_ANGULAR_LIMIT_SOFTNESS,
  540. G6DOF_JOINT_ANGULAR_DAMPING,
  541. G6DOF_JOINT_ANGULAR_RESTITUTION,
  542. G6DOF_JOINT_ANGULAR_FORCE_LIMIT,
  543. G6DOF_JOINT_ANGULAR_ERP,
  544. G6DOF_JOINT_ANGULAR_MOTOR_TARGET_VELOCITY,
  545. G6DOF_JOINT_ANGULAR_MOTOR_FORCE_LIMIT,
  546. G6DOF_JOINT_ANGULAR_SPRING_STIFFNESS,
  547. G6DOF_JOINT_ANGULAR_SPRING_DAMPING,
  548. G6DOF_JOINT_ANGULAR_SPRING_EQUILIBRIUM_POINT,
  549. G6DOF_JOINT_MAX
  550. };
  551. enum G6DOFJointAxisFlag {
  552. G6DOF_JOINT_FLAG_ENABLE_LINEAR_LIMIT,
  553. G6DOF_JOINT_FLAG_ENABLE_ANGULAR_LIMIT,
  554. G6DOF_JOINT_FLAG_ENABLE_ANGULAR_SPRING,
  555. G6DOF_JOINT_FLAG_ENABLE_LINEAR_SPRING,
  556. G6DOF_JOINT_FLAG_ENABLE_MOTOR,
  557. G6DOF_JOINT_FLAG_ENABLE_LINEAR_MOTOR,
  558. G6DOF_JOINT_FLAG_MAX
  559. };
  560. virtual void joint_make_generic_6dof(RID p_joint, 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
  561. virtual void generic_6dof_joint_set_param(RID p_joint, Vector3::Axis, G6DOFJointAxisParam p_param, real_t p_value) = 0;
  562. virtual real_t generic_6dof_joint_get_param(RID p_joint, Vector3::Axis, G6DOFJointAxisParam p_param) const = 0;
  563. virtual void generic_6dof_joint_set_flag(RID p_joint, Vector3::Axis, G6DOFJointAxisFlag p_flag, bool p_enable) = 0;
  564. virtual bool generic_6dof_joint_get_flag(RID p_joint, Vector3::Axis, G6DOFJointAxisFlag p_flag) const = 0;
  565. /* QUERY API */
  566. enum AreaBodyStatus {
  567. AREA_BODY_ADDED,
  568. AREA_BODY_REMOVED
  569. };
  570. /* MISC */
  571. virtual void free(RID p_rid) = 0;
  572. virtual void set_active(bool p_active) = 0;
  573. virtual void init() = 0;
  574. virtual void step(real_t p_step) = 0;
  575. virtual void sync() = 0;
  576. virtual void flush_queries() = 0;
  577. virtual void end_sync() = 0;
  578. virtual void finish() = 0;
  579. virtual bool is_flushing_queries() const = 0;
  580. enum ProcessInfo {
  581. INFO_ACTIVE_OBJECTS,
  582. INFO_COLLISION_PAIRS,
  583. INFO_ISLAND_COUNT
  584. };
  585. virtual int get_process_info(ProcessInfo p_info) = 0;
  586. PhysicsServer3D();
  587. ~PhysicsServer3D();
  588. };
  589. typedef PhysicsServer3D *(*CreatePhysicsServer3DCallback)();
  590. class PhysicsServer3DManager {
  591. struct ClassInfo {
  592. String name;
  593. CreatePhysicsServer3DCallback create_callback = nullptr;
  594. ClassInfo() {}
  595. ClassInfo(String p_name, CreatePhysicsServer3DCallback p_create_callback) :
  596. name(p_name),
  597. create_callback(p_create_callback) {}
  598. ClassInfo(const ClassInfo &p_ci) :
  599. name(p_ci.name),
  600. create_callback(p_ci.create_callback) {}
  601. ClassInfo &operator=(const ClassInfo &p_ci) {
  602. name = p_ci.name;
  603. create_callback = p_ci.create_callback;
  604. return *this;
  605. }
  606. };
  607. static Vector<ClassInfo> physics_servers;
  608. static int default_server_id;
  609. static int default_server_priority;
  610. public:
  611. static const String setting_property_name;
  612. private:
  613. static void on_servers_changed();
  614. public:
  615. static void register_server(const String &p_name, CreatePhysicsServer3DCallback p_creat_callback);
  616. static void set_default_server(const String &p_name, int p_priority = 0);
  617. static int find_server_id(const String &p_name);
  618. static int get_servers_count();
  619. static String get_server_name(int p_id);
  620. static PhysicsServer3D *new_default_server();
  621. static PhysicsServer3D *new_server(const String &p_name);
  622. };
  623. VARIANT_ENUM_CAST(PhysicsServer3D::ShapeType);
  624. VARIANT_ENUM_CAST(PhysicsServer3D::SpaceParameter);
  625. VARIANT_ENUM_CAST(PhysicsServer3D::AreaParameter);
  626. VARIANT_ENUM_CAST(PhysicsServer3D::AreaSpaceOverrideMode);
  627. VARIANT_ENUM_CAST(PhysicsServer3D::BodyMode);
  628. VARIANT_ENUM_CAST(PhysicsServer3D::BodyParameter);
  629. VARIANT_ENUM_CAST(PhysicsServer3D::BodyState);
  630. VARIANT_ENUM_CAST(PhysicsServer3D::BodyAxis);
  631. VARIANT_ENUM_CAST(PhysicsServer3D::PinJointParam);
  632. VARIANT_ENUM_CAST(PhysicsServer3D::JointType);
  633. VARIANT_ENUM_CAST(PhysicsServer3D::HingeJointParam);
  634. VARIANT_ENUM_CAST(PhysicsServer3D::HingeJointFlag);
  635. VARIANT_ENUM_CAST(PhysicsServer3D::SliderJointParam);
  636. VARIANT_ENUM_CAST(PhysicsServer3D::ConeTwistJointParam);
  637. VARIANT_ENUM_CAST(PhysicsServer3D::G6DOFJointAxisParam);
  638. VARIANT_ENUM_CAST(PhysicsServer3D::G6DOFJointAxisFlag);
  639. VARIANT_ENUM_CAST(PhysicsServer3D::AreaBodyStatus);
  640. VARIANT_ENUM_CAST(PhysicsServer3D::ProcessInfo);
  641. #endif