character_body_3d.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /**************************************************************************/
  2. /* character_body_3d.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #ifndef CHARACTER_BODY_3D_H
  31. #define CHARACTER_BODY_3D_H
  32. #include "scene/3d/physics/kinematic_collision_3d.h"
  33. #include "scene/3d/physics/physics_body_3d.h"
  34. class CharacterBody3D : public PhysicsBody3D {
  35. GDCLASS(CharacterBody3D, PhysicsBody3D);
  36. public:
  37. enum MotionMode {
  38. MOTION_MODE_GROUNDED,
  39. MOTION_MODE_FLOATING,
  40. };
  41. enum PlatformOnLeave {
  42. PLATFORM_ON_LEAVE_ADD_VELOCITY,
  43. PLATFORM_ON_LEAVE_ADD_UPWARD_VELOCITY,
  44. PLATFORM_ON_LEAVE_DO_NOTHING,
  45. };
  46. bool move_and_slide();
  47. void apply_floor_snap();
  48. const Vector3 &get_velocity() const;
  49. void set_velocity(const Vector3 &p_velocity);
  50. bool is_on_floor() const;
  51. bool is_on_floor_only() const;
  52. bool is_on_wall() const;
  53. bool is_on_wall_only() const;
  54. bool is_on_ceiling() const;
  55. bool is_on_ceiling_only() const;
  56. const Vector3 &get_last_motion() const;
  57. Vector3 get_position_delta() const;
  58. const Vector3 &get_floor_normal() const;
  59. const Vector3 &get_wall_normal() const;
  60. const Vector3 &get_real_velocity() const;
  61. real_t get_floor_angle(const Vector3 &p_up_direction = Vector3(0.0, 1.0, 0.0)) const;
  62. const Vector3 &get_platform_velocity() const;
  63. const Vector3 &get_platform_angular_velocity() const;
  64. virtual Vector3 get_linear_velocity() const override;
  65. int get_slide_collision_count() const;
  66. PhysicsServer3D::MotionResult get_slide_collision(int p_bounce) const;
  67. void set_safe_margin(real_t p_margin);
  68. real_t get_safe_margin() const;
  69. bool is_floor_stop_on_slope_enabled() const;
  70. void set_floor_stop_on_slope_enabled(bool p_enabled);
  71. bool is_floor_constant_speed_enabled() const;
  72. void set_floor_constant_speed_enabled(bool p_enabled);
  73. bool is_floor_block_on_wall_enabled() const;
  74. void set_floor_block_on_wall_enabled(bool p_enabled);
  75. bool is_slide_on_ceiling_enabled() const;
  76. void set_slide_on_ceiling_enabled(bool p_enabled);
  77. int get_max_slides() const;
  78. void set_max_slides(int p_max_slides);
  79. real_t get_floor_max_angle() const;
  80. void set_floor_max_angle(real_t p_radians);
  81. real_t get_floor_snap_length();
  82. void set_floor_snap_length(real_t p_floor_snap_length);
  83. real_t get_wall_min_slide_angle() const;
  84. void set_wall_min_slide_angle(real_t p_radians);
  85. uint32_t get_platform_floor_layers() const;
  86. void set_platform_floor_layers(const uint32_t p_exclude_layer);
  87. uint32_t get_platform_wall_layers() const;
  88. void set_platform_wall_layers(const uint32_t p_exclude_layer);
  89. void set_motion_mode(MotionMode p_mode);
  90. MotionMode get_motion_mode() const;
  91. void set_platform_on_leave(PlatformOnLeave p_on_leave_velocity);
  92. PlatformOnLeave get_platform_on_leave() const;
  93. CharacterBody3D();
  94. private:
  95. real_t margin = 0.001;
  96. MotionMode motion_mode = MOTION_MODE_GROUNDED;
  97. PlatformOnLeave platform_on_leave = PLATFORM_ON_LEAVE_ADD_VELOCITY;
  98. union CollisionState {
  99. uint32_t state = 0;
  100. struct {
  101. bool floor;
  102. bool wall;
  103. bool ceiling;
  104. };
  105. CollisionState() {
  106. }
  107. CollisionState(bool p_floor, bool p_wall, bool p_ceiling) {
  108. floor = p_floor;
  109. wall = p_wall;
  110. ceiling = p_ceiling;
  111. }
  112. };
  113. CollisionState collision_state;
  114. bool floor_constant_speed = false;
  115. bool floor_stop_on_slope = true;
  116. bool floor_block_on_wall = true;
  117. bool slide_on_ceiling = true;
  118. int max_slides = 6;
  119. int platform_layer = 0;
  120. RID platform_rid;
  121. ObjectID platform_object_id;
  122. uint32_t platform_floor_layers = UINT32_MAX;
  123. uint32_t platform_wall_layers = 0;
  124. real_t floor_snap_length = 0.1;
  125. real_t floor_max_angle = Math::deg_to_rad((real_t)45.0);
  126. real_t wall_min_slide_angle = Math::deg_to_rad((real_t)15.0);
  127. Vector3 up_direction = Vector3(0.0, 1.0, 0.0);
  128. Vector3 velocity;
  129. Vector3 floor_normal;
  130. Vector3 wall_normal;
  131. Vector3 ceiling_normal;
  132. Vector3 last_motion;
  133. Vector3 platform_velocity;
  134. Vector3 platform_angular_velocity;
  135. Vector3 platform_ceiling_velocity;
  136. Vector3 previous_position;
  137. Vector3 real_velocity;
  138. Vector<PhysicsServer3D::MotionResult> motion_results;
  139. Vector<Ref<KinematicCollision3D>> slide_colliders;
  140. void _move_and_slide_floating(double p_delta);
  141. void _move_and_slide_grounded(double p_delta, bool p_was_on_floor);
  142. Ref<KinematicCollision3D> _get_slide_collision(int p_bounce);
  143. Ref<KinematicCollision3D> _get_last_slide_collision();
  144. const Vector3 &get_up_direction() const;
  145. bool _on_floor_if_snapped(bool p_was_on_floor, bool p_vel_dir_facing_up);
  146. void set_up_direction(const Vector3 &p_up_direction);
  147. void _set_collision_direction(const PhysicsServer3D::MotionResult &p_result, CollisionState &r_state, CollisionState p_apply_state = CollisionState(true, true, true));
  148. void _set_platform_data(const PhysicsServer3D::MotionCollision &p_collision);
  149. void _snap_on_floor(bool p_was_on_floor, bool p_vel_dir_facing_up);
  150. protected:
  151. void _notification(int p_what);
  152. static void _bind_methods();
  153. void _validate_property(PropertyInfo &p_property) const;
  154. };
  155. VARIANT_ENUM_CAST(CharacterBody3D::MotionMode);
  156. VARIANT_ENUM_CAST(CharacterBody3D::PlatformOnLeave);
  157. #endif // CHARACTER_BODY_3D_H