Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/scene/2d/physics/rigid_body_2d.h
9906 views
1
/**************************************************************************/
2
/* rigid_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 "core/templates/vset.h"
34
#include "scene/2d/physics/physics_body_2d.h"
35
36
class RigidBody2D : public PhysicsBody2D {
37
GDCLASS(RigidBody2D, PhysicsBody2D);
38
39
public:
40
enum FreezeMode {
41
FREEZE_MODE_STATIC,
42
FREEZE_MODE_KINEMATIC,
43
};
44
45
enum CenterOfMassMode {
46
CENTER_OF_MASS_MODE_AUTO,
47
CENTER_OF_MASS_MODE_CUSTOM,
48
};
49
50
enum DampMode {
51
DAMP_MODE_COMBINE,
52
DAMP_MODE_REPLACE,
53
};
54
55
enum CCDMode {
56
CCD_MODE_DISABLED,
57
CCD_MODE_CAST_RAY,
58
CCD_MODE_CAST_SHAPE,
59
};
60
61
private:
62
bool can_sleep = true;
63
bool lock_rotation = false;
64
bool freeze = false;
65
FreezeMode freeze_mode = FREEZE_MODE_STATIC;
66
67
real_t mass = 1.0;
68
real_t inertia = 0.0;
69
CenterOfMassMode center_of_mass_mode = CENTER_OF_MASS_MODE_AUTO;
70
Vector2 center_of_mass;
71
72
Ref<PhysicsMaterial> physics_material_override;
73
real_t gravity_scale = 1.0;
74
75
DampMode linear_damp_mode = DAMP_MODE_COMBINE;
76
DampMode angular_damp_mode = DAMP_MODE_COMBINE;
77
78
real_t linear_damp = 0.0;
79
real_t angular_damp = 0.0;
80
81
Vector2 linear_velocity;
82
real_t angular_velocity = 0.0;
83
bool sleeping = false;
84
85
int max_contacts_reported = 0;
86
int contact_count = 0;
87
88
bool custom_integrator = false;
89
90
CCDMode ccd_mode = CCD_MODE_DISABLED;
91
92
struct ShapePair {
93
int body_shape = 0;
94
int local_shape = 0;
95
bool tagged = false;
96
bool operator<(const ShapePair &p_sp) const {
97
if (body_shape == p_sp.body_shape) {
98
return local_shape < p_sp.local_shape;
99
}
100
101
return body_shape < p_sp.body_shape;
102
}
103
104
ShapePair() {}
105
ShapePair(int p_bs, int p_ls) {
106
body_shape = p_bs;
107
local_shape = p_ls;
108
}
109
};
110
struct RigidBody2D_RemoveAction {
111
RID rid;
112
ObjectID body_id;
113
ShapePair pair;
114
};
115
struct BodyState {
116
RID rid;
117
//int rc;
118
bool in_scene = false;
119
VSet<ShapePair> shapes;
120
};
121
122
struct ContactMonitor {
123
bool locked = false;
124
HashMap<ObjectID, BodyState> body_map;
125
};
126
127
ContactMonitor *contact_monitor = nullptr;
128
void _body_enter_tree(ObjectID p_id);
129
void _body_exit_tree(ObjectID p_id);
130
131
void _body_inout(int p_status, const RID &p_body, ObjectID p_instance, int p_body_shape, int p_local_shape);
132
133
static void _body_state_changed_callback(void *p_instance, PhysicsDirectBodyState2D *p_state);
134
void _body_state_changed(PhysicsDirectBodyState2D *p_state);
135
136
void _sync_body_state(PhysicsDirectBodyState2D *p_state);
137
138
protected:
139
void _notification(int p_what);
140
static void _bind_methods();
141
142
void _validate_property(PropertyInfo &p_property) const;
143
144
GDVIRTUAL1(_integrate_forces, PhysicsDirectBodyState2D *)
145
146
void _apply_body_mode();
147
148
public:
149
void set_lock_rotation_enabled(bool p_lock_rotation);
150
bool is_lock_rotation_enabled() const;
151
152
void set_freeze_enabled(bool p_freeze);
153
bool is_freeze_enabled() const;
154
155
void set_freeze_mode(FreezeMode p_freeze_mode);
156
FreezeMode get_freeze_mode() const;
157
158
void set_mass(real_t p_mass);
159
real_t get_mass() const;
160
161
void set_inertia(real_t p_inertia);
162
real_t get_inertia() const;
163
164
void set_center_of_mass_mode(CenterOfMassMode p_mode);
165
CenterOfMassMode get_center_of_mass_mode() const;
166
167
void set_center_of_mass(const Vector2 &p_center_of_mass);
168
const Vector2 &get_center_of_mass() const;
169
170
void set_physics_material_override(const Ref<PhysicsMaterial> &p_physics_material_override);
171
Ref<PhysicsMaterial> get_physics_material_override() const;
172
173
void set_gravity_scale(real_t p_gravity_scale);
174
real_t get_gravity_scale() const;
175
176
void set_linear_damp_mode(DampMode p_mode);
177
DampMode get_linear_damp_mode() const;
178
179
void set_angular_damp_mode(DampMode p_mode);
180
DampMode get_angular_damp_mode() const;
181
182
void set_linear_damp(real_t p_linear_damp);
183
real_t get_linear_damp() const;
184
185
void set_angular_damp(real_t p_angular_damp);
186
real_t get_angular_damp() const;
187
188
void set_linear_velocity(const Vector2 &p_velocity);
189
Vector2 get_linear_velocity() const;
190
191
void set_axis_velocity(const Vector2 &p_axis);
192
193
void set_angular_velocity(real_t p_velocity);
194
real_t get_angular_velocity() const;
195
196
void set_use_custom_integrator(bool p_enable);
197
bool is_using_custom_integrator();
198
199
void set_sleeping(bool p_sleeping);
200
bool is_sleeping() const;
201
202
void set_can_sleep(bool p_active);
203
bool is_able_to_sleep() const;
204
205
void set_contact_monitor(bool p_enabled);
206
bool is_contact_monitor_enabled() const;
207
208
void set_max_contacts_reported(int p_amount);
209
int get_max_contacts_reported() const;
210
int get_contact_count() const;
211
212
void set_continuous_collision_detection_mode(CCDMode p_mode);
213
CCDMode get_continuous_collision_detection_mode() const;
214
215
void apply_central_impulse(const Vector2 &p_impulse);
216
void apply_impulse(const Vector2 &p_impulse, const Vector2 &p_position = Vector2());
217
void apply_torque_impulse(real_t p_torque);
218
219
void apply_central_force(const Vector2 &p_force);
220
void apply_force(const Vector2 &p_force, const Vector2 &p_position = Vector2());
221
void apply_torque(real_t p_torque);
222
223
void add_constant_central_force(const Vector2 &p_force);
224
void add_constant_force(const Vector2 &p_force, const Vector2 &p_position = Vector2());
225
void add_constant_torque(real_t p_torque);
226
227
void set_constant_force(const Vector2 &p_force);
228
Vector2 get_constant_force() const;
229
230
void set_constant_torque(real_t p_torque);
231
real_t get_constant_torque() const;
232
233
TypedArray<Node2D> get_colliding_bodies() const; //function for script
234
235
virtual PackedStringArray get_configuration_warnings() const override;
236
237
RigidBody2D();
238
~RigidBody2D();
239
240
private:
241
void _reload_physics_characteristics();
242
};
243
244
VARIANT_ENUM_CAST(RigidBody2D::FreezeMode);
245
VARIANT_ENUM_CAST(RigidBody2D::CenterOfMassMode);
246
VARIANT_ENUM_CAST(RigidBody2D::DampMode);
247
VARIANT_ENUM_CAST(RigidBody2D::CCDMode);
248
249