physics_server.h 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718
  1. /*************************************************************************/
  2. /* physics_server.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 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 "object.h"
  33. #include "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_force(const Vector3 &p_force, const Vector3 &p_pos) = 0;
  55. virtual void apply_impulse(const Vector3 &p_pos, const Vector3 &p_j) = 0;
  56. virtual void apply_torque_impulse(const Vector3 &p_j) = 0;
  57. virtual void set_sleep_state(bool p_enable) = 0;
  58. virtual bool is_sleeping() const = 0;
  59. virtual int get_contact_count() const = 0;
  60. virtual Vector3 get_contact_local_position(int p_contact_idx) const = 0;
  61. virtual Vector3 get_contact_local_normal(int p_contact_idx) const = 0;
  62. virtual int get_contact_local_shape(int p_contact_idx) const = 0;
  63. virtual RID get_contact_collider(int p_contact_idx) const = 0;
  64. virtual Vector3 get_contact_collider_position(int p_contact_idx) const = 0;
  65. virtual ObjectID get_contact_collider_id(int p_contact_idx) const = 0;
  66. virtual Object *get_contact_collider_object(int p_contact_idx) const;
  67. virtual int get_contact_collider_shape(int p_contact_idx) const = 0;
  68. virtual Vector3 get_contact_collider_velocity_at_position(int p_contact_idx) const = 0;
  69. virtual real_t get_step() const = 0;
  70. virtual void integrate_forces();
  71. virtual PhysicsDirectSpaceState *get_space_state() = 0;
  72. PhysicsDirectBodyState();
  73. };
  74. class PhysicsShapeQueryResult;
  75. class PhysicsShapeQueryParameters : public Reference {
  76. GDCLASS(PhysicsShapeQueryParameters, Reference);
  77. friend class PhysicsDirectSpaceState;
  78. RID shape;
  79. Transform transform;
  80. float margin;
  81. Set<RID> exclude;
  82. uint32_t collision_layer;
  83. uint32_t object_type_mask;
  84. protected:
  85. static void _bind_methods();
  86. public:
  87. void set_shape(const RES &p_shape);
  88. void set_shape_rid(const RID &p_shape);
  89. RID get_shape_rid() const;
  90. void set_transform(const Transform &p_transform);
  91. Transform get_transform() const;
  92. void set_margin(float p_margin);
  93. float get_margin() const;
  94. void set_collision_layer(int p_collision_layer);
  95. int get_collision_layer() const;
  96. void set_object_type_mask(int p_object_type_mask);
  97. int get_object_type_mask() const;
  98. void set_exclude(const Vector<RID> &p_exclude);
  99. Vector<RID> get_exclude() const;
  100. PhysicsShapeQueryParameters();
  101. };
  102. class PhysicsDirectSpaceState : public Object {
  103. GDCLASS(PhysicsDirectSpaceState, Object);
  104. public:
  105. enum ObjectTypeMask {
  106. TYPE_MASK_STATIC_BODY = 1 << 0,
  107. TYPE_MASK_KINEMATIC_BODY = 1 << 1,
  108. TYPE_MASK_RIGID_BODY = 1 << 2,
  109. TYPE_MASK_CHARACTER_BODY = 1 << 3,
  110. TYPE_MASK_AREA = 1 << 4,
  111. TYPE_MASK_COLLISION = TYPE_MASK_STATIC_BODY | TYPE_MASK_CHARACTER_BODY | TYPE_MASK_KINEMATIC_BODY | TYPE_MASK_RIGID_BODY
  112. };
  113. private:
  114. Dictionary _intersect_ray(const Vector3 &p_from, const Vector3 &p_to, const Vector<RID> &p_exclude = Vector<RID>(), uint32_t p_layers = 0, uint32_t p_object_type_mask = TYPE_MASK_COLLISION);
  115. Array _intersect_shape(const Ref<PhysicsShapeQueryParameters> &p_shape_query, int p_max_results = 32);
  116. Array _cast_motion(const Ref<PhysicsShapeQueryParameters> &p_shape_query, const Vector3 &p_motion);
  117. Array _collide_shape(const Ref<PhysicsShapeQueryParameters> &p_shape_query, int p_max_results = 32);
  118. Dictionary _get_rest_info(const Ref<PhysicsShapeQueryParameters> &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_layer = 0xFFFFFFFF, uint32_t p_object_type_mask = TYPE_MASK_COLLISION) = 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_layer = 0xFFFFFFFF, uint32_t p_object_type_mask = TYPE_MASK_COLLISION, bool p_pick_ray = false) = 0;
  138. 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_layer = 0xFFFFFFFF, uint32_t p_object_type_mask = TYPE_MASK_COLLISION) = 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, float p_margin, float &p_closest_safe, float &p_closest_unsafe, const Set<RID> &p_exclude = Set<RID>(), uint32_t p_collision_layer = 0xFFFFFFFF, uint32_t p_object_type_mask = TYPE_MASK_COLLISION, ShapeRestInfo *r_info = NULL) = 0;
  148. 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_layer = 0xFFFFFFFF, uint32_t p_object_type_mask = TYPE_MASK_COLLISION) = 0;
  149. 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_layer = 0xFFFFFFFF, uint32_t p_object_type_mask = TYPE_MASK_COLLISION) = 0;
  150. virtual Vector3 get_closest_point_to_object_volume(RID p_object, const Vector3 p_point) const = 0;
  151. PhysicsDirectSpaceState();
  152. };
  153. VARIANT_ENUM_CAST(PhysicsDirectSpaceState::ObjectTypeMask);
  154. class PhysicsShapeQueryResult : public Reference {
  155. GDCLASS(PhysicsShapeQueryResult, Reference);
  156. Vector<PhysicsDirectSpaceState::ShapeResult> result;
  157. friend class PhysicsDirectSpaceState;
  158. protected:
  159. static void _bind_methods();
  160. public:
  161. int get_result_count() const;
  162. RID get_result_rid(int p_idx) const;
  163. ObjectID get_result_object_id(int p_idx) const;
  164. Object *get_result_object(int p_idx) const;
  165. int get_result_object_shape(int p_idx) const;
  166. PhysicsShapeQueryResult();
  167. };
  168. class PhysicsServer : public Object {
  169. GDCLASS(PhysicsServer, Object);
  170. static PhysicsServer *singleton;
  171. protected:
  172. static void _bind_methods();
  173. public:
  174. static PhysicsServer *get_singleton();
  175. enum ShapeType {
  176. SHAPE_PLANE, ///< plane:"plane"
  177. SHAPE_RAY, ///< float:"length"
  178. SHAPE_SPHERE, ///< float:"radius"
  179. SHAPE_BOX, ///< vec3:"extents"
  180. SHAPE_CAPSULE, ///< dict( float:"radius", float:"height"):capsule
  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. virtual RID shape_create(ShapeType p_shape) = 0;
  187. virtual void shape_set_data(RID p_shape, const Variant &p_data) = 0;
  188. virtual void shape_set_custom_solver_bias(RID p_shape, real_t p_bias) = 0;
  189. virtual ShapeType shape_get_type(RID p_shape) const = 0;
  190. virtual Variant shape_get_data(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_SOFT,
  267. BODY_MODE_CHARACTER
  268. };
  269. virtual RID body_create(BodyMode p_mode = BODY_MODE_RIGID, bool p_init_sleeping = false) = 0;
  270. virtual void body_set_space(RID p_body, RID p_space) = 0;
  271. virtual RID body_get_space(RID p_body) const = 0;
  272. virtual void body_set_mode(RID p_body, BodyMode p_mode) = 0;
  273. virtual BodyMode body_get_mode(RID p_body) const = 0;
  274. virtual void body_add_shape(RID p_body, RID p_shape, const Transform &p_transform = Transform()) = 0;
  275. virtual void body_set_shape(RID p_body, int p_shape_idx, RID p_shape) = 0;
  276. virtual void body_set_shape_transform(RID p_body, int p_shape_idx, const Transform &p_transform) = 0;
  277. virtual int body_get_shape_count(RID p_body) const = 0;
  278. virtual RID body_get_shape(RID p_body, int p_shape_idx) const = 0;
  279. virtual Transform body_get_shape_transform(RID p_body, int p_shape_idx) const = 0;
  280. virtual void body_remove_shape(RID p_body, int p_shape_idx) = 0;
  281. virtual void body_clear_shapes(RID p_body) = 0;
  282. virtual void body_set_shape_disabled(RID p_body, int p_shape_idx, bool p_disabled) = 0;
  283. virtual void body_attach_object_instance_id(RID p_body, uint32_t p_ID) = 0;
  284. virtual uint32_t body_get_object_instance_id(RID p_body) const = 0;
  285. virtual void body_set_enable_continuous_collision_detection(RID p_body, bool p_enable) = 0;
  286. virtual bool body_is_continuous_collision_detection_enabled(RID p_body) const = 0;
  287. virtual void body_set_collision_layer(RID p_body, uint32_t p_layer) = 0;
  288. virtual uint32_t body_get_collision_layer(RID p_body) const = 0;
  289. virtual void body_set_collision_mask(RID p_body, uint32_t p_mask) = 0;
  290. virtual uint32_t body_get_collision_mask(RID p_body) const = 0;
  291. virtual void body_set_user_flags(RID p_body, uint32_t p_flags) = 0;
  292. virtual uint32_t body_get_user_flags(RID p_body) const = 0;
  293. // common body variables
  294. enum BodyParameter {
  295. BODY_PARAM_BOUNCE,
  296. BODY_PARAM_FRICTION,
  297. BODY_PARAM_MASS, ///< unused for static, always infinite
  298. BODY_PARAM_GRAVITY_SCALE,
  299. BODY_PARAM_LINEAR_DAMP,
  300. BODY_PARAM_ANGULAR_DAMP,
  301. BODY_PARAM_MAX,
  302. };
  303. virtual void body_set_param(RID p_body, BodyParameter p_param, float p_value) = 0;
  304. virtual float body_get_param(RID p_body, BodyParameter p_param) const = 0;
  305. //state
  306. enum BodyState {
  307. BODY_STATE_TRANSFORM,
  308. BODY_STATE_LINEAR_VELOCITY,
  309. BODY_STATE_ANGULAR_VELOCITY,
  310. BODY_STATE_SLEEPING,
  311. BODY_STATE_CAN_SLEEP
  312. };
  313. virtual void body_set_state(RID p_body, BodyState p_state, const Variant &p_variant) = 0;
  314. virtual Variant body_get_state(RID p_body, BodyState p_state) const = 0;
  315. //do something about it
  316. virtual void body_set_applied_force(RID p_body, const Vector3 &p_force) = 0;
  317. virtual Vector3 body_get_applied_force(RID p_body) const = 0;
  318. virtual void body_set_applied_torque(RID p_body, const Vector3 &p_torque) = 0;
  319. virtual Vector3 body_get_applied_torque(RID p_body) const = 0;
  320. virtual void body_apply_impulse(RID p_body, const Vector3 &p_pos, const Vector3 &p_impulse) = 0;
  321. virtual void body_apply_torque_impulse(RID p_body, const Vector3 &p_impulse) = 0;
  322. virtual void body_set_axis_velocity(RID p_body, const Vector3 &p_axis_velocity) = 0;
  323. enum BodyAxisLock {
  324. BODY_AXIS_LOCK_DISABLED,
  325. BODY_AXIS_LOCK_X,
  326. BODY_AXIS_LOCK_Y,
  327. BODY_AXIS_LOCK_Z,
  328. };
  329. virtual void body_set_axis_lock(RID p_body, BodyAxisLock p_lock) = 0;
  330. virtual BodyAxisLock body_get_axis_lock(RID p_body) const = 0;
  331. //fix
  332. virtual void body_add_collision_exception(RID p_body, RID p_body_b) = 0;
  333. virtual void body_remove_collision_exception(RID p_body, RID p_body_b) = 0;
  334. virtual void body_get_collision_exceptions(RID p_body, List<RID> *p_exceptions) = 0;
  335. virtual void body_set_max_contacts_reported(RID p_body, int p_contacts) = 0;
  336. virtual int body_get_max_contacts_reported(RID p_body) const = 0;
  337. //missing remove
  338. virtual void body_set_contacts_reported_depth_threshold(RID p_body, float p_threshold) = 0;
  339. virtual float body_get_contacts_reported_depth_threshold(RID p_body) const = 0;
  340. virtual void body_set_omit_force_integration(RID p_body, bool p_omit) = 0;
  341. virtual bool body_is_omitting_force_integration(RID p_body) const = 0;
  342. virtual void body_set_force_integration_callback(RID p_body, Object *p_receiver, const StringName &p_method, const Variant &p_udata = Variant()) = 0;
  343. virtual void body_set_ray_pickable(RID p_body, bool p_enable) = 0;
  344. virtual bool body_is_ray_pickable(RID p_body) const = 0;
  345. // this function only works on physics process, errors and returns null otherwise
  346. virtual PhysicsDirectBodyState *body_get_direct_state(RID p_body) = 0;
  347. struct MotionResult {
  348. Vector3 motion;
  349. Vector3 remainder;
  350. Vector3 collision_point;
  351. Vector3 collision_normal;
  352. Vector3 collider_velocity;
  353. int collision_local_shape;
  354. ObjectID collider_id;
  355. RID collider;
  356. int collider_shape;
  357. Variant collider_metadata;
  358. };
  359. virtual bool body_test_motion(RID p_body, const Transform &p_from, const Vector3 &p_motion, float p_margin = 0.001, MotionResult *r_result = NULL) = 0;
  360. /* JOINT API */
  361. enum JointType {
  362. JOINT_PIN,
  363. JOINT_HINGE,
  364. JOINT_SLIDER,
  365. JOINT_CONE_TWIST,
  366. JOINT_6DOF
  367. };
  368. virtual JointType joint_get_type(RID p_joint) const = 0;
  369. virtual void joint_set_solver_priority(RID p_joint, int p_priority) = 0;
  370. virtual int joint_get_solver_priority(RID p_joint) const = 0;
  371. virtual RID joint_create_pin(RID p_body_A, const Vector3 &p_local_A, RID p_body_B, const Vector3 &p_local_B) = 0;
  372. enum PinJointParam {
  373. PIN_JOINT_BIAS,
  374. PIN_JOINT_DAMPING,
  375. PIN_JOINT_IMPULSE_CLAMP
  376. };
  377. virtual void pin_joint_set_param(RID p_joint, PinJointParam p_param, float p_value) = 0;
  378. virtual float pin_joint_get_param(RID p_joint, PinJointParam p_param) const = 0;
  379. virtual void pin_joint_set_local_a(RID p_joint, const Vector3 &p_A) = 0;
  380. virtual Vector3 pin_joint_get_local_a(RID p_joint) const = 0;
  381. virtual void pin_joint_set_local_b(RID p_joint, const Vector3 &p_B) = 0;
  382. virtual Vector3 pin_joint_get_local_b(RID p_joint) const = 0;
  383. enum HingeJointParam {
  384. HINGE_JOINT_BIAS,
  385. HINGE_JOINT_LIMIT_UPPER,
  386. HINGE_JOINT_LIMIT_LOWER,
  387. HINGE_JOINT_LIMIT_BIAS,
  388. HINGE_JOINT_LIMIT_SOFTNESS,
  389. HINGE_JOINT_LIMIT_RELAXATION,
  390. HINGE_JOINT_MOTOR_TARGET_VELOCITY,
  391. HINGE_JOINT_MOTOR_MAX_IMPULSE,
  392. HINGE_JOINT_MAX
  393. };
  394. enum HingeJointFlag {
  395. HINGE_JOINT_FLAG_USE_LIMIT,
  396. HINGE_JOINT_FLAG_ENABLE_MOTOR,
  397. HINGE_JOINT_FLAG_MAX
  398. };
  399. virtual RID joint_create_hinge(RID p_body_A, const Transform &p_hinge_A, RID p_body_B, const Transform &p_hinge_B) = 0;
  400. 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;
  401. virtual void hinge_joint_set_param(RID p_joint, HingeJointParam p_param, float p_value) = 0;
  402. virtual float hinge_joint_get_param(RID p_joint, HingeJointParam p_param) const = 0;
  403. virtual void hinge_joint_set_flag(RID p_joint, HingeJointFlag p_flag, bool p_value) = 0;
  404. virtual bool hinge_joint_get_flag(RID p_joint, HingeJointFlag p_flag) const = 0;
  405. enum SliderJointParam {
  406. SLIDER_JOINT_LINEAR_LIMIT_UPPER,
  407. SLIDER_JOINT_LINEAR_LIMIT_LOWER,
  408. SLIDER_JOINT_LINEAR_LIMIT_SOFTNESS,
  409. SLIDER_JOINT_LINEAR_LIMIT_RESTITUTION,
  410. SLIDER_JOINT_LINEAR_LIMIT_DAMPING,
  411. SLIDER_JOINT_LINEAR_MOTION_SOFTNESS,
  412. SLIDER_JOINT_LINEAR_MOTION_RESTITUTION,
  413. SLIDER_JOINT_LINEAR_MOTION_DAMPING,
  414. SLIDER_JOINT_LINEAR_ORTHOGONAL_SOFTNESS,
  415. SLIDER_JOINT_LINEAR_ORTHOGONAL_RESTITUTION,
  416. SLIDER_JOINT_LINEAR_ORTHOGONAL_DAMPING,
  417. SLIDER_JOINT_ANGULAR_LIMIT_UPPER,
  418. SLIDER_JOINT_ANGULAR_LIMIT_LOWER,
  419. SLIDER_JOINT_ANGULAR_LIMIT_SOFTNESS,
  420. SLIDER_JOINT_ANGULAR_LIMIT_RESTITUTION,
  421. SLIDER_JOINT_ANGULAR_LIMIT_DAMPING,
  422. SLIDER_JOINT_ANGULAR_MOTION_SOFTNESS,
  423. SLIDER_JOINT_ANGULAR_MOTION_RESTITUTION,
  424. SLIDER_JOINT_ANGULAR_MOTION_DAMPING,
  425. SLIDER_JOINT_ANGULAR_ORTHOGONAL_SOFTNESS,
  426. SLIDER_JOINT_ANGULAR_ORTHOGONAL_RESTITUTION,
  427. SLIDER_JOINT_ANGULAR_ORTHOGONAL_DAMPING,
  428. SLIDER_JOINT_MAX
  429. };
  430. 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
  431. virtual void slider_joint_set_param(RID p_joint, SliderJointParam p_param, float p_value) = 0;
  432. virtual float slider_joint_get_param(RID p_joint, SliderJointParam p_param) const = 0;
  433. enum ConeTwistJointParam {
  434. CONE_TWIST_JOINT_SWING_SPAN,
  435. CONE_TWIST_JOINT_TWIST_SPAN,
  436. CONE_TWIST_JOINT_BIAS,
  437. CONE_TWIST_JOINT_SOFTNESS,
  438. CONE_TWIST_JOINT_RELAXATION,
  439. CONE_TWIST_MAX
  440. };
  441. 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
  442. virtual void cone_twist_joint_set_param(RID p_joint, ConeTwistJointParam p_param, float p_value) = 0;
  443. virtual float cone_twist_joint_get_param(RID p_joint, ConeTwistJointParam p_param) const = 0;
  444. enum G6DOFJointAxisParam {
  445. G6DOF_JOINT_LINEAR_LOWER_LIMIT,
  446. G6DOF_JOINT_LINEAR_UPPER_LIMIT,
  447. G6DOF_JOINT_LINEAR_LIMIT_SOFTNESS,
  448. G6DOF_JOINT_LINEAR_RESTITUTION,
  449. G6DOF_JOINT_LINEAR_DAMPING,
  450. G6DOF_JOINT_ANGULAR_LOWER_LIMIT,
  451. G6DOF_JOINT_ANGULAR_UPPER_LIMIT,
  452. G6DOF_JOINT_ANGULAR_LIMIT_SOFTNESS,
  453. G6DOF_JOINT_ANGULAR_DAMPING,
  454. G6DOF_JOINT_ANGULAR_RESTITUTION,
  455. G6DOF_JOINT_ANGULAR_FORCE_LIMIT,
  456. G6DOF_JOINT_ANGULAR_ERP,
  457. G6DOF_JOINT_ANGULAR_MOTOR_TARGET_VELOCITY,
  458. G6DOF_JOINT_ANGULAR_MOTOR_FORCE_LIMIT,
  459. G6DOF_JOINT_MAX
  460. };
  461. enum G6DOFJointAxisFlag {
  462. G6DOF_JOINT_FLAG_ENABLE_LINEAR_LIMIT,
  463. G6DOF_JOINT_FLAG_ENABLE_ANGULAR_LIMIT,
  464. G6DOF_JOINT_FLAG_ENABLE_MOTOR,
  465. G6DOF_JOINT_FLAG_MAX
  466. };
  467. 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
  468. virtual void generic_6dof_joint_set_param(RID p_joint, Vector3::Axis, G6DOFJointAxisParam p_param, float p_value) = 0;
  469. virtual float generic_6dof_joint_get_param(RID p_joint, Vector3::Axis, G6DOFJointAxisParam p_param) = 0;
  470. virtual void generic_6dof_joint_set_flag(RID p_joint, Vector3::Axis, G6DOFJointAxisFlag p_flag, bool p_enable) = 0;
  471. virtual bool generic_6dof_joint_get_flag(RID p_joint, Vector3::Axis, G6DOFJointAxisFlag p_flag) = 0;
  472. /* QUERY API */
  473. enum AreaBodyStatus {
  474. AREA_BODY_ADDED,
  475. AREA_BODY_REMOVED
  476. };
  477. /* MISC */
  478. virtual void free(RID p_rid) = 0;
  479. virtual void set_active(bool p_active) = 0;
  480. virtual void init() = 0;
  481. virtual void step(float p_step) = 0;
  482. virtual void sync() = 0;
  483. virtual void flush_queries() = 0;
  484. virtual void finish() = 0;
  485. enum ProcessInfo {
  486. INFO_ACTIVE_OBJECTS,
  487. INFO_COLLISION_PAIRS,
  488. INFO_ISLAND_COUNT
  489. };
  490. virtual int get_process_info(ProcessInfo p_info) = 0;
  491. PhysicsServer();
  492. ~PhysicsServer();
  493. };
  494. typedef PhysicsServer *(*CreatePhysicsServerCallback)();
  495. class PhysicsServerManager {
  496. struct ClassInfo {
  497. String name;
  498. CreatePhysicsServerCallback create_callback;
  499. ClassInfo()
  500. : name(""), create_callback(NULL) {}
  501. ClassInfo(String p_name, CreatePhysicsServerCallback p_create_callback)
  502. : name(p_name), create_callback(p_create_callback) {}
  503. ClassInfo(const ClassInfo &p_ci)
  504. : name(p_ci.name), create_callback(p_ci.create_callback) {}
  505. };
  506. static Vector<ClassInfo> physics_servers;
  507. static int default_server_id;
  508. static int default_server_priority;
  509. public:
  510. static const String setting_property_name;
  511. private:
  512. static void on_servers_changed();
  513. public:
  514. static void register_server(const String &p_name, CreatePhysicsServerCallback p_creat_callback);
  515. static void set_default_server(const String &p_name, int p_priority = 0);
  516. static int find_server_id(const String &p_name);
  517. static int get_servers_count();
  518. static String get_server_name(int p_id);
  519. static PhysicsServer *new_default_server();
  520. static PhysicsServer *new_server(const String &p_name);
  521. };
  522. VARIANT_ENUM_CAST(PhysicsServer::ShapeType);
  523. VARIANT_ENUM_CAST(PhysicsServer::SpaceParameter);
  524. VARIANT_ENUM_CAST(PhysicsServer::AreaParameter);
  525. VARIANT_ENUM_CAST(PhysicsServer::AreaSpaceOverrideMode);
  526. VARIANT_ENUM_CAST(PhysicsServer::BodyMode);
  527. VARIANT_ENUM_CAST(PhysicsServer::BodyParameter);
  528. VARIANT_ENUM_CAST(PhysicsServer::BodyState);
  529. VARIANT_ENUM_CAST(PhysicsServer::BodyAxisLock);
  530. VARIANT_ENUM_CAST(PhysicsServer::PinJointParam);
  531. VARIANT_ENUM_CAST(PhysicsServer::JointType);
  532. VARIANT_ENUM_CAST(PhysicsServer::HingeJointParam);
  533. VARIANT_ENUM_CAST(PhysicsServer::HingeJointFlag);
  534. VARIANT_ENUM_CAST(PhysicsServer::SliderJointParam);
  535. VARIANT_ENUM_CAST(PhysicsServer::ConeTwistJointParam);
  536. VARIANT_ENUM_CAST(PhysicsServer::G6DOFJointAxisParam);
  537. VARIANT_ENUM_CAST(PhysicsServer::G6DOFJointAxisFlag);
  538. VARIANT_ENUM_CAST(PhysicsServer::AreaBodyStatus);
  539. VARIANT_ENUM_CAST(PhysicsServer::ProcessInfo);
  540. #endif