physics_body.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655
  1. /*************************************************************************/
  2. /* physics_body.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_BODY__H
  31. #define PHYSICS_BODY__H
  32. #include "core/vset.h"
  33. #include "scene/3d/collision_object.h"
  34. #include "scene/resources/physics_material.h"
  35. #include "servers/physics_server.h"
  36. #include "skeleton.h"
  37. class PhysicsBody : public CollisionObject {
  38. GDCLASS(PhysicsBody, CollisionObject);
  39. uint32_t collision_layer;
  40. uint32_t collision_mask;
  41. void _set_layers(uint32_t p_mask);
  42. uint32_t _get_layers() const;
  43. protected:
  44. static void _bind_methods();
  45. void _notification(int p_what);
  46. PhysicsBody(PhysicsServer::BodyMode p_mode);
  47. public:
  48. virtual Vector3 get_linear_velocity() const;
  49. virtual Vector3 get_angular_velocity() const;
  50. virtual float get_inverse_mass() const;
  51. void set_collision_layer(uint32_t p_layer);
  52. uint32_t get_collision_layer() const;
  53. void set_collision_mask(uint32_t p_mask);
  54. uint32_t get_collision_mask() const;
  55. void set_collision_layer_bit(int p_bit, bool p_value);
  56. bool get_collision_layer_bit(int p_bit) const;
  57. void set_collision_mask_bit(int p_bit, bool p_value);
  58. bool get_collision_mask_bit(int p_bit) const;
  59. Array get_collision_exceptions();
  60. void add_collision_exception_with(Node *p_node); //must be physicsbody
  61. void remove_collision_exception_with(Node *p_node);
  62. PhysicsBody();
  63. };
  64. class StaticBody : public PhysicsBody {
  65. GDCLASS(StaticBody, PhysicsBody);
  66. Vector3 constant_linear_velocity;
  67. Vector3 constant_angular_velocity;
  68. Ref<PhysicsMaterial> physics_material_override;
  69. protected:
  70. static void _bind_methods();
  71. public:
  72. #ifndef DISABLE_DEPRECATED
  73. void set_friction(real_t p_friction);
  74. real_t get_friction() const;
  75. void set_bounce(real_t p_bounce);
  76. real_t get_bounce() const;
  77. #endif
  78. void set_physics_material_override(const Ref<PhysicsMaterial> &p_physics_material_override);
  79. Ref<PhysicsMaterial> get_physics_material_override() const;
  80. void set_constant_linear_velocity(const Vector3 &p_vel);
  81. void set_constant_angular_velocity(const Vector3 &p_vel);
  82. Vector3 get_constant_linear_velocity() const;
  83. Vector3 get_constant_angular_velocity() const;
  84. StaticBody();
  85. ~StaticBody();
  86. private:
  87. void _reload_physics_characteristics();
  88. };
  89. class RigidBody : public PhysicsBody {
  90. GDCLASS(RigidBody, PhysicsBody);
  91. public:
  92. enum Mode {
  93. MODE_RIGID,
  94. MODE_STATIC,
  95. MODE_CHARACTER,
  96. MODE_KINEMATIC,
  97. };
  98. protected:
  99. bool can_sleep;
  100. PhysicsDirectBodyState *state;
  101. Mode mode;
  102. real_t mass;
  103. Ref<PhysicsMaterial> physics_material_override;
  104. Vector3 linear_velocity;
  105. Vector3 angular_velocity;
  106. real_t gravity_scale;
  107. real_t linear_damp;
  108. real_t angular_damp;
  109. bool sleeping;
  110. bool ccd;
  111. int max_contacts_reported;
  112. bool custom_integrator;
  113. struct ShapePair {
  114. int body_shape;
  115. int local_shape;
  116. bool tagged;
  117. bool operator<(const ShapePair &p_sp) const {
  118. if (body_shape == p_sp.body_shape)
  119. return local_shape < p_sp.local_shape;
  120. else
  121. return body_shape < p_sp.body_shape;
  122. }
  123. ShapePair() {}
  124. ShapePair(int p_bs, int p_ls) {
  125. body_shape = p_bs;
  126. local_shape = p_ls;
  127. }
  128. };
  129. struct RigidBody_RemoveAction {
  130. ObjectID body_id;
  131. ShapePair pair;
  132. };
  133. struct BodyState {
  134. //int rc;
  135. bool in_tree;
  136. VSet<ShapePair> shapes;
  137. };
  138. struct ContactMonitor {
  139. bool locked;
  140. Map<ObjectID, BodyState> body_map;
  141. };
  142. ContactMonitor *contact_monitor;
  143. void _body_enter_tree(ObjectID p_id);
  144. void _body_exit_tree(ObjectID p_id);
  145. void _body_inout(int p_status, ObjectID p_instance, int p_body_shape, int p_local_shape);
  146. virtual void _direct_state_changed(Object *p_state);
  147. void _notification(int p_what);
  148. static void _bind_methods();
  149. public:
  150. void set_mode(Mode p_mode);
  151. Mode get_mode() const;
  152. void set_mass(real_t p_mass);
  153. real_t get_mass() const;
  154. virtual float get_inverse_mass() const { return 1.0 / mass; }
  155. void set_weight(real_t p_weight);
  156. real_t get_weight() const;
  157. #ifndef DISABLE_DEPRECATED
  158. void set_friction(real_t p_friction);
  159. real_t get_friction() const;
  160. void set_bounce(real_t p_bounce);
  161. real_t get_bounce() const;
  162. #endif
  163. void set_physics_material_override(const Ref<PhysicsMaterial> &p_physics_material_override);
  164. Ref<PhysicsMaterial> get_physics_material_override() const;
  165. void set_linear_velocity(const Vector3 &p_velocity);
  166. Vector3 get_linear_velocity() const;
  167. void set_axis_velocity(const Vector3 &p_axis);
  168. void set_angular_velocity(const Vector3 &p_velocity);
  169. Vector3 get_angular_velocity() const;
  170. void set_gravity_scale(real_t p_gravity_scale);
  171. real_t get_gravity_scale() const;
  172. void set_linear_damp(real_t p_linear_damp);
  173. real_t get_linear_damp() const;
  174. void set_angular_damp(real_t p_angular_damp);
  175. real_t get_angular_damp() const;
  176. void set_use_custom_integrator(bool p_enable);
  177. bool is_using_custom_integrator();
  178. void set_sleeping(bool p_sleeping);
  179. bool is_sleeping() const;
  180. void set_can_sleep(bool p_active);
  181. bool is_able_to_sleep() const;
  182. void set_contact_monitor(bool p_enabled);
  183. bool is_contact_monitor_enabled() const;
  184. void set_max_contacts_reported(int p_amount);
  185. int get_max_contacts_reported() const;
  186. void set_use_continuous_collision_detection(bool p_enable);
  187. bool is_using_continuous_collision_detection() const;
  188. void set_axis_lock(PhysicsServer::BodyAxis p_axis, bool p_lock);
  189. bool get_axis_lock(PhysicsServer::BodyAxis p_axis) const;
  190. Array get_colliding_bodies() const;
  191. void add_central_force(const Vector3 &p_force);
  192. void add_force(const Vector3 &p_force, const Vector3 &p_pos);
  193. void add_torque(const Vector3 &p_torque);
  194. void apply_central_impulse(const Vector3 &p_impulse);
  195. void apply_impulse(const Vector3 &p_pos, const Vector3 &p_impulse);
  196. void apply_torque_impulse(const Vector3 &p_impulse);
  197. virtual String get_configuration_warning() const;
  198. RigidBody();
  199. ~RigidBody();
  200. private:
  201. void _reload_physics_characteristics();
  202. };
  203. VARIANT_ENUM_CAST(RigidBody::Mode);
  204. class KinematicCollision;
  205. class KinematicBody : public PhysicsBody {
  206. GDCLASS(KinematicBody, PhysicsBody);
  207. public:
  208. struct Collision {
  209. Vector3 collision;
  210. Vector3 normal;
  211. Vector3 collider_vel;
  212. ObjectID collider;
  213. RID collider_rid;
  214. int collider_shape;
  215. Variant collider_metadata;
  216. Vector3 remainder;
  217. Vector3 travel;
  218. int local_shape;
  219. };
  220. private:
  221. uint16_t locked_axis;
  222. float margin;
  223. Vector3 floor_velocity;
  224. RID on_floor_body;
  225. bool on_floor;
  226. bool on_ceiling;
  227. bool on_wall;
  228. Vector<Collision> colliders;
  229. Vector<Ref<KinematicCollision> > slide_colliders;
  230. Ref<KinematicCollision> motion_cache;
  231. _FORCE_INLINE_ bool _ignores_mode(PhysicsServer::BodyMode) const;
  232. Ref<KinematicCollision> _move(const Vector3 &p_motion, bool p_infinite_inertia = true, bool p_test_only = false);
  233. Ref<KinematicCollision> _get_slide_collision(int p_bounce);
  234. protected:
  235. static void _bind_methods();
  236. public:
  237. bool move_and_collide(const Vector3 &p_motion, bool p_infinite_inertia, Collision &r_collisionz, bool p_test_only = false);
  238. bool test_move(const Transform &p_from, const Vector3 &p_motion, bool p_infinite_inertia);
  239. bool separate_raycast_shapes(bool p_infinite_inertia, Collision &r_collision);
  240. void set_axis_lock(PhysicsServer::BodyAxis p_axis, bool p_lock);
  241. bool get_axis_lock(PhysicsServer::BodyAxis p_axis) const;
  242. void set_safe_margin(float p_margin);
  243. float get_safe_margin() const;
  244. Vector3 move_and_slide(const Vector3 &p_linear_velocity, const Vector3 &p_floor_direction = Vector3(0, 0, 0), bool p_stop_on_slope = false, int p_max_slides = 4, float p_floor_max_angle = Math::deg2rad((float)45), bool p_infinite_inertia = true);
  245. Vector3 move_and_slide_with_snap(const Vector3 &p_linear_velocity, const Vector3 &p_snap, const Vector3 &p_floor_direction = Vector3(0, 0, 0), bool p_stop_on_slope = false, int p_max_slides = 4, float p_floor_max_angle = Math::deg2rad((float)45), bool p_infinite_inertia = true);
  246. bool is_on_floor() const;
  247. bool is_on_wall() const;
  248. bool is_on_ceiling() const;
  249. Vector3 get_floor_velocity() const;
  250. int get_slide_count() const;
  251. Collision get_slide_collision(int p_bounce) const;
  252. KinematicBody();
  253. ~KinematicBody();
  254. };
  255. class KinematicCollision : public Reference {
  256. GDCLASS(KinematicCollision, Reference);
  257. KinematicBody *owner;
  258. friend class KinematicBody;
  259. KinematicBody::Collision collision;
  260. protected:
  261. static void _bind_methods();
  262. public:
  263. Vector3 get_position() const;
  264. Vector3 get_normal() const;
  265. Vector3 get_travel() const;
  266. Vector3 get_remainder() const;
  267. Object *get_local_shape() const;
  268. Object *get_collider() const;
  269. ObjectID get_collider_id() const;
  270. Object *get_collider_shape() const;
  271. int get_collider_shape_index() const;
  272. Vector3 get_collider_velocity() const;
  273. Variant get_collider_metadata() const;
  274. KinematicCollision();
  275. };
  276. class PhysicalBone : public PhysicsBody {
  277. GDCLASS(PhysicalBone, PhysicsBody);
  278. public:
  279. enum JointType {
  280. JOINT_TYPE_NONE,
  281. JOINT_TYPE_PIN,
  282. JOINT_TYPE_CONE,
  283. JOINT_TYPE_HINGE,
  284. JOINT_TYPE_SLIDER,
  285. JOINT_TYPE_6DOF
  286. };
  287. struct JointData {
  288. virtual JointType get_joint_type() { return JOINT_TYPE_NONE; }
  289. /// "j" is used to set the parameter inside the PhysicsServer
  290. virtual bool _set(const StringName &p_name, const Variant &p_value, RID j = RID());
  291. virtual bool _get(const StringName &p_name, Variant &r_ret) const;
  292. virtual void _get_property_list(List<PropertyInfo> *p_list) const;
  293. virtual ~JointData() {}
  294. };
  295. struct PinJointData : public JointData {
  296. virtual JointType get_joint_type() { return JOINT_TYPE_PIN; }
  297. virtual bool _set(const StringName &p_name, const Variant &p_value, RID j = RID());
  298. virtual bool _get(const StringName &p_name, Variant &r_ret) const;
  299. virtual void _get_property_list(List<PropertyInfo> *p_list) const;
  300. real_t bias;
  301. real_t damping;
  302. real_t impulse_clamp;
  303. PinJointData() :
  304. bias(0.3),
  305. damping(1.),
  306. impulse_clamp(0) {}
  307. };
  308. struct ConeJointData : public JointData {
  309. virtual JointType get_joint_type() { return JOINT_TYPE_CONE; }
  310. virtual bool _set(const StringName &p_name, const Variant &p_value, RID j = RID());
  311. virtual bool _get(const StringName &p_name, Variant &r_ret) const;
  312. virtual void _get_property_list(List<PropertyInfo> *p_list) const;
  313. real_t swing_span;
  314. real_t twist_span;
  315. real_t bias;
  316. real_t softness;
  317. real_t relaxation;
  318. ConeJointData() :
  319. swing_span(Math_PI * 0.25),
  320. twist_span(Math_PI),
  321. bias(0.3),
  322. softness(0.8),
  323. relaxation(1.) {}
  324. };
  325. struct HingeJointData : public JointData {
  326. virtual JointType get_joint_type() { return JOINT_TYPE_HINGE; }
  327. virtual bool _set(const StringName &p_name, const Variant &p_value, RID j = RID());
  328. virtual bool _get(const StringName &p_name, Variant &r_ret) const;
  329. virtual void _get_property_list(List<PropertyInfo> *p_list) const;
  330. bool angular_limit_enabled;
  331. real_t angular_limit_upper;
  332. real_t angular_limit_lower;
  333. real_t angular_limit_bias;
  334. real_t angular_limit_softness;
  335. real_t angular_limit_relaxation;
  336. HingeJointData() :
  337. angular_limit_enabled(false),
  338. angular_limit_upper(Math_PI * 0.5),
  339. angular_limit_lower(-Math_PI * 0.5),
  340. angular_limit_bias(0.3),
  341. angular_limit_softness(0.9),
  342. angular_limit_relaxation(1.) {}
  343. };
  344. struct SliderJointData : public JointData {
  345. virtual JointType get_joint_type() { return JOINT_TYPE_SLIDER; }
  346. virtual bool _set(const StringName &p_name, const Variant &p_value, RID j = RID());
  347. virtual bool _get(const StringName &p_name, Variant &r_ret) const;
  348. virtual void _get_property_list(List<PropertyInfo> *p_list) const;
  349. real_t linear_limit_upper;
  350. real_t linear_limit_lower;
  351. real_t linear_limit_softness;
  352. real_t linear_limit_restitution;
  353. real_t linear_limit_damping;
  354. real_t angular_limit_upper;
  355. real_t angular_limit_lower;
  356. real_t angular_limit_softness;
  357. real_t angular_limit_restitution;
  358. real_t angular_limit_damping;
  359. SliderJointData() :
  360. linear_limit_upper(1.),
  361. linear_limit_lower(-1.),
  362. linear_limit_softness(1.),
  363. linear_limit_restitution(0.7),
  364. linear_limit_damping(1.),
  365. angular_limit_upper(0),
  366. angular_limit_lower(0),
  367. angular_limit_softness(1.),
  368. angular_limit_restitution(0.7),
  369. angular_limit_damping(1.) {}
  370. };
  371. struct SixDOFJointData : public JointData {
  372. struct SixDOFAxisData {
  373. bool linear_limit_enabled;
  374. real_t linear_limit_upper;
  375. real_t linear_limit_lower;
  376. real_t linear_limit_softness;
  377. real_t linear_restitution;
  378. real_t linear_damping;
  379. bool linear_spring_enabled;
  380. real_t linear_spring_stiffness;
  381. real_t linear_spring_damping;
  382. real_t linear_equilibrium_point;
  383. bool angular_limit_enabled;
  384. real_t angular_limit_upper;
  385. real_t angular_limit_lower;
  386. real_t angular_limit_softness;
  387. real_t angular_restitution;
  388. real_t angular_damping;
  389. real_t erp;
  390. bool angular_spring_enabled;
  391. real_t angular_spring_stiffness;
  392. real_t angular_spring_damping;
  393. real_t angular_equilibrium_point;
  394. SixDOFAxisData() :
  395. linear_limit_enabled(true),
  396. linear_limit_upper(0),
  397. linear_limit_lower(0),
  398. linear_limit_softness(0.7),
  399. linear_restitution(0.5),
  400. linear_damping(1.),
  401. linear_spring_enabled(false),
  402. linear_spring_stiffness(0),
  403. linear_spring_damping(0),
  404. linear_equilibrium_point(0),
  405. angular_limit_enabled(true),
  406. angular_limit_upper(0),
  407. angular_limit_lower(0),
  408. angular_limit_softness(0.5),
  409. angular_restitution(0),
  410. angular_damping(1.),
  411. erp(0.5),
  412. angular_spring_enabled(false),
  413. angular_spring_stiffness(0),
  414. angular_spring_damping(0.),
  415. angular_equilibrium_point(0) {}
  416. };
  417. virtual JointType get_joint_type() { return JOINT_TYPE_6DOF; }
  418. virtual bool _set(const StringName &p_name, const Variant &p_value, RID j = RID());
  419. virtual bool _get(const StringName &p_name, Variant &r_ret) const;
  420. virtual void _get_property_list(List<PropertyInfo> *p_list) const;
  421. SixDOFAxisData axis_data[3];
  422. SixDOFJointData() {}
  423. };
  424. private:
  425. #ifdef TOOLS_ENABLED
  426. // if false gizmo move body
  427. bool gizmo_move_joint;
  428. #endif
  429. JointData *joint_data;
  430. Transform joint_offset;
  431. RID joint;
  432. Skeleton *parent_skeleton;
  433. Transform body_offset;
  434. Transform body_offset_inverse;
  435. bool static_body;
  436. bool _internal_static_body;
  437. bool simulate_physics;
  438. bool _internal_simulate_physics;
  439. int bone_id;
  440. String bone_name;
  441. real_t bounce;
  442. real_t mass;
  443. real_t friction;
  444. real_t gravity_scale;
  445. protected:
  446. bool _set(const StringName &p_name, const Variant &p_value);
  447. bool _get(const StringName &p_name, Variant &r_ret) const;
  448. void _get_property_list(List<PropertyInfo> *p_list) const;
  449. void _notification(int p_what);
  450. void _direct_state_changed(Object *p_state);
  451. static void _bind_methods();
  452. private:
  453. static Skeleton *find_skeleton_parent(Node *p_parent);
  454. void _fix_joint_offset();
  455. void _reload_joint();
  456. public:
  457. void _on_bone_parent_changed();
  458. void _set_gizmo_move_joint(bool p_move_joint);
  459. public:
  460. #ifdef TOOLS_ENABLED
  461. virtual Transform get_global_gizmo_transform() const;
  462. virtual Transform get_local_gizmo_transform() const;
  463. #endif
  464. const JointData *get_joint_data() const;
  465. Skeleton *find_skeleton_parent();
  466. int get_bone_id() const { return bone_id; }
  467. void set_joint_type(JointType p_joint_type);
  468. JointType get_joint_type() const;
  469. void set_joint_offset(const Transform &p_offset);
  470. const Transform &get_joint_offset() const;
  471. void set_body_offset(const Transform &p_offset);
  472. const Transform &get_body_offset() const;
  473. void set_static_body(bool p_static);
  474. bool is_static_body();
  475. void set_simulate_physics(bool p_simulate);
  476. bool get_simulate_physics();
  477. bool is_simulating_physics();
  478. void set_bone_name(const String &p_name);
  479. const String &get_bone_name() const;
  480. void set_mass(real_t p_mass);
  481. real_t get_mass() const;
  482. void set_weight(real_t p_weight);
  483. real_t get_weight() const;
  484. void set_friction(real_t p_friction);
  485. real_t get_friction() const;
  486. void set_bounce(real_t p_bounce);
  487. real_t get_bounce() const;
  488. void set_gravity_scale(real_t p_gravity_scale);
  489. real_t get_gravity_scale() const;
  490. PhysicalBone();
  491. ~PhysicalBone();
  492. private:
  493. void update_bone_id();
  494. void update_offset();
  495. void reset_to_rest_position();
  496. void _reset_physics_simulation_state();
  497. void _reset_staticness_state();
  498. void _start_physics_simulation();
  499. void _stop_physics_simulation();
  500. };
  501. VARIANT_ENUM_CAST(PhysicalBone::JointType);
  502. #endif // PHYSICS_BODY__H