| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
#pragma once |
| 32 |
|
| 33 |
#include "scene/3d/physics/kinematic_collision_3d.h" |
| 34 |
#include "scene/3d/physics/physics_body_3d.h" |
| 35 |
|
| 36 |
class CharacterBody3D : public PhysicsBody3D { |
| 37 |
GDCLASS(CharacterBody3D, PhysicsBody3D); |
| 38 |
|
| 39 |
public: |
| 40 |
enum MotionMode { |
| 41 |
MOTION_MODE_GROUNDED, |
| 42 |
MOTION_MODE_FLOATING, |
| 43 |
}; |
| 44 |
enum PlatformOnLeave { |
| 45 |
PLATFORM_ON_LEAVE_ADD_VELOCITY, |
| 46 |
PLATFORM_ON_LEAVE_ADD_UPWARD_VELOCITY, |
| 47 |
PLATFORM_ON_LEAVE_DO_NOTHING, |
| 48 |
}; |
| 49 |
bool move_and_slide(); |
| 50 |
void apply_floor_snap(); |
| 51 |
|
| 52 |
const Vector3 &get_velocity() const; |
| 53 |
void set_velocity(const Vector3 &p_velocity); |
| 54 |
|
| 55 |
bool is_on_floor() const; |
| 56 |
bool is_on_floor_only() const; |
| 57 |
bool is_on_wall() const; |
| 58 |
bool is_on_wall_only() const; |
| 59 |
bool is_on_ceiling() const; |
| 60 |
bool is_on_ceiling_only() const; |
| 61 |
const Vector3 &get_last_motion() const; |
| 62 |
Vector3 get_position_delta() const; |
| 63 |
const Vector3 &get_floor_normal() const; |
| 64 |
const Vector3 &get_wall_normal() const; |
| 65 |
const Vector3 &get_real_velocity() const; |
| 66 |
real_t get_floor_angle(const Vector3 &p_up_direction = Vector3(0.0, 1.0, 0.0)) const; |
| 67 |
const Vector3 &get_platform_velocity() const; |
| 68 |
const Vector3 &get_platform_angular_velocity() const; |
| 69 |
|
| 70 |
virtual Vector3 get_linear_velocity() const override; |
| 71 |
|
| 72 |
int get_slide_collision_count() const; |
| 73 |
PhysicsServer3D::MotionResult get_slide_collision(int p_bounce) const; |
| 74 |
|
| 75 |
void set_safe_margin(real_t p_margin); |
| 76 |
real_t get_safe_margin() const; |
| 77 |
|
| 78 |
bool is_floor_stop_on_slope_enabled() const; |
| 79 |
void set_floor_stop_on_slope_enabled(bool p_enabled); |
| 80 |
|
| 81 |
bool is_floor_constant_speed_enabled() const; |
| 82 |
void set_floor_constant_speed_enabled(bool p_enabled); |
| 83 |
|
| 84 |
bool is_floor_block_on_wall_enabled() const; |
| 85 |
void set_floor_block_on_wall_enabled(bool p_enabled); |
| 86 |
|
| 87 |
bool is_slide_on_ceiling_enabled() const; |
| 88 |
void set_slide_on_ceiling_enabled(bool p_enabled); |
| 89 |
|
| 90 |
int get_max_slides() const; |
| 91 |
void set_max_slides(int p_max_slides); |
| 92 |
|
| 93 |
real_t get_floor_max_angle() const; |
| 94 |
void set_floor_max_angle(real_t p_radians); |
| 95 |
|
| 96 |
real_t get_floor_snap_length(); |
| 97 |
void set_floor_snap_length(real_t p_floor_snap_length); |
| 98 |
|
| 99 |
real_t get_wall_min_slide_angle() const; |
| 100 |
void set_wall_min_slide_angle(real_t p_radians); |
| 101 |
|
| 102 |
uint32_t get_platform_floor_layers() const; |
| 103 |
void set_platform_floor_layers(const uint32_t p_exclude_layer); |
| 104 |
|
| 105 |
uint32_t get_platform_wall_layers() const; |
| 106 |
void set_platform_wall_layers(const uint32_t p_exclude_layer); |
| 107 |
|
| 108 |
void set_motion_mode(MotionMode p_mode); |
| 109 |
MotionMode get_motion_mode() const; |
| 110 |
|
| 111 |
void set_platform_on_leave(PlatformOnLeave p_on_leave_velocity); |
| 112 |
PlatformOnLeave get_platform_on_leave() const; |
| 113 |
|
| 114 |
CharacterBody3D(); |
| 115 |
|
| 116 |
private: |
| 117 |
real_t margin = 0.001; |
| 118 |
MotionMode motion_mode = MOTION_MODE_GROUNDED; |
| 119 |
PlatformOnLeave platform_on_leave = PLATFORM_ON_LEAVE_ADD_VELOCITY; |
| 120 |
union CollisionState { |
| 121 |
uint32_t state = 0; |
| 122 |
struct { |
| 123 |
bool floor; |
| 124 |
bool wall; |
| 125 |
bool ceiling; |
| 126 |
}; |
| 127 |
|
| 128 |
CollisionState() { |
| 129 |
} |
| 130 |
|
| 131 |
CollisionState(bool p_floor, bool p_wall, bool p_ceiling) { |
| 132 |
floor = p_floor; |
| 133 |
wall = p_wall; |
| 134 |
ceiling = p_ceiling; |
| 135 |
} |
| 136 |
}; |
| 137 |
|
| 138 |
CollisionState collision_state; |
| 139 |
bool floor_constant_speed = false; |
| 140 |
bool floor_stop_on_slope = true; |
| 141 |
bool floor_block_on_wall = true; |
| 142 |
bool slide_on_ceiling = true; |
| 143 |
int max_slides = 6; |
| 144 |
int platform_layer = 0; |
| 145 |
RID platform_rid; |
| 146 |
ObjectID platform_object_id; |
| 147 |
uint32_t platform_floor_layers = UINT32_MAX; |
| 148 |
uint32_t platform_wall_layers = 0; |
| 149 |
real_t floor_snap_length = 0.1; |
| 150 |
real_t floor_max_angle = Math::deg_to_rad((real_t)45.0); |
| 151 |
real_t wall_min_slide_angle = Math::deg_to_rad((real_t)15.0); |
| 152 |
Vector3 up_direction = Vector3(0.0, 1.0, 0.0); |
| 153 |
Vector3 velocity; |
| 154 |
Vector3 floor_normal; |
| 155 |
Vector3 wall_normal; |
| 156 |
Vector3 ceiling_normal; |
| 157 |
Vector3 last_motion; |
| 158 |
Vector3 platform_velocity; |
| 159 |
Vector3 platform_angular_velocity; |
| 160 |
Vector3 platform_ceiling_velocity; |
| 161 |
Vector3 previous_position; |
| 162 |
Vector3 real_velocity; |
| 163 |
|
| 164 |
Vector<PhysicsServer3D::MotionResult> motion_results; |
| 165 |
Vector<Ref<KinematicCollision3D>> slide_colliders; |
| 166 |
|
| 167 |
void _move_and_slide_floating(double p_delta); |
| 168 |
void _move_and_slide_grounded(double p_delta, bool p_was_on_floor); |
| 169 |
|
| 170 |
Ref<KinematicCollision3D> _get_slide_collision(int p_bounce); |
| 171 |
Ref<KinematicCollision3D> _get_last_slide_collision(); |
| 172 |
const Vector3 &get_up_direction() const; |
| 173 |
bool _on_floor_if_snapped(bool p_was_on_floor, bool p_vel_dir_facing_up); |
| 174 |
void set_up_direction(const Vector3 &p_up_direction); |
| 175 |
void _set_collision_direction(const PhysicsServer3D::MotionResult &p_result, CollisionState &r_state, CollisionState p_apply_state = CollisionState(true, true, true)); |
| 176 |
void _set_platform_data(const PhysicsServer3D::MotionCollision &p_collision); |
| 177 |
void _snap_on_floor(bool p_was_on_floor, bool p_vel_dir_facing_up); |
| 178 |
|
| 179 |
protected: |
| 180 |
void _notification(int p_what); |
| 181 |
static void _bind_methods(); |
| 182 |
void _validate_property(PropertyInfo &p_property) const; |
| 183 |
}; |
| 184 |
|
| 185 |
VARIANT_ENUM_CAST(CharacterBody3D::MotionMode); |
| 186 |
VARIANT_ENUM_CAST(CharacterBody3D::PlatformOnLeave); |