Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/scene/2d/physics/character_body_2d.h
9906 views
1
/**************************************************************************/
2
/* character_body_2d.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
31
#pragma once
32
33
#include "scene/2d/physics/kinematic_collision_2d.h"
34
#include "scene/2d/physics/physics_body_2d.h"
35
36
class CharacterBody2D : public PhysicsBody2D {
37
GDCLASS(CharacterBody2D, PhysicsBody2D);
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 Vector2 &get_velocity() const;
53
void set_velocity(const Vector2 &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 Vector2 &get_last_motion() const;
62
Vector2 get_position_delta() const;
63
const Vector2 &get_floor_normal() const;
64
const Vector2 &get_wall_normal() const;
65
const Vector2 &get_real_velocity() const;
66
67
real_t get_floor_angle(const Vector2 &p_up_direction = Vector2(0.0, -1.0)) const;
68
const Vector2 &get_platform_velocity() const;
69
70
int get_slide_collision_count() const;
71
PhysicsServer2D::MotionResult get_slide_collision(int p_bounce) const;
72
73
void set_safe_margin(real_t p_margin);
74
real_t get_safe_margin() const;
75
76
bool is_floor_stop_on_slope_enabled() const;
77
void set_floor_stop_on_slope_enabled(bool p_enabled);
78
79
bool is_floor_constant_speed_enabled() const;
80
void set_floor_constant_speed_enabled(bool p_enabled);
81
82
bool is_floor_block_on_wall_enabled() const;
83
void set_floor_block_on_wall_enabled(bool p_enabled);
84
85
bool is_slide_on_ceiling_enabled() const;
86
void set_slide_on_ceiling_enabled(bool p_enabled);
87
88
int get_max_slides() const;
89
void set_max_slides(int p_max_slides);
90
91
real_t get_floor_max_angle() const;
92
void set_floor_max_angle(real_t p_radians);
93
94
real_t get_floor_snap_length();
95
void set_floor_snap_length(real_t p_floor_snap_length);
96
97
real_t get_wall_min_slide_angle() const;
98
void set_wall_min_slide_angle(real_t p_radians);
99
100
uint32_t get_platform_floor_layers() const;
101
void set_platform_floor_layers(const uint32_t p_exclude_layer);
102
103
uint32_t get_platform_wall_layers() const;
104
void set_platform_wall_layers(const uint32_t p_exclude_layer);
105
106
void set_motion_mode(MotionMode p_mode);
107
MotionMode get_motion_mode() const;
108
109
void set_platform_on_leave(PlatformOnLeave p_on_leave_velocity);
110
PlatformOnLeave get_platform_on_leave() const;
111
112
CharacterBody2D();
113
114
private:
115
real_t margin = 0.08;
116
MotionMode motion_mode = MOTION_MODE_GROUNDED;
117
PlatformOnLeave platform_on_leave = PLATFORM_ON_LEAVE_ADD_VELOCITY;
118
119
bool floor_constant_speed = false;
120
bool floor_stop_on_slope = true;
121
bool floor_block_on_wall = true;
122
bool slide_on_ceiling = true;
123
int max_slides = 4;
124
int platform_layer = 0;
125
real_t floor_max_angle = Math::deg_to_rad((real_t)45.0);
126
real_t floor_snap_length = 1;
127
real_t wall_min_slide_angle = Math::deg_to_rad((real_t)15.0);
128
Vector2 up_direction = Vector2(0.0, -1.0);
129
uint32_t platform_floor_layers = UINT32_MAX;
130
uint32_t platform_wall_layers = 0;
131
Vector2 velocity;
132
133
Vector2 floor_normal;
134
Vector2 platform_velocity;
135
Vector2 wall_normal;
136
Vector2 last_motion;
137
Vector2 previous_position;
138
Vector2 real_velocity;
139
140
RID platform_rid;
141
ObjectID platform_object_id;
142
bool on_floor = false;
143
bool on_ceiling = false;
144
bool on_wall = false;
145
146
Vector<PhysicsServer2D::MotionResult> motion_results;
147
Vector<Ref<KinematicCollision2D>> slide_colliders;
148
149
void _move_and_slide_floating(double p_delta);
150
void _move_and_slide_grounded(double p_delta, bool p_was_on_floor);
151
152
Ref<KinematicCollision2D> _get_slide_collision(int p_bounce);
153
Ref<KinematicCollision2D> _get_last_slide_collision();
154
const Vector2 &get_up_direction() const;
155
bool _on_floor_if_snapped(bool p_was_on_floor, bool p_vel_dir_facing_up);
156
void set_up_direction(const Vector2 &p_up_direction);
157
void _set_collision_direction(const PhysicsServer2D::MotionResult &p_result);
158
void _set_platform_data(const PhysicsServer2D::MotionResult &p_result);
159
void _apply_floor_snap(bool p_wall_as_floor = false);
160
void _snap_on_floor(bool p_was_on_floor, bool p_vel_dir_facing_up, bool p_wall_as_floor = false);
161
162
protected:
163
void _notification(int p_what);
164
static void _bind_methods();
165
void _validate_property(PropertyInfo &p_property) const;
166
};
167
168
VARIANT_ENUM_CAST(CharacterBody2D::MotionMode);
169
VARIANT_ENUM_CAST(CharacterBody2D::PlatformOnLeave);
170
171