Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/scene/2d/physics/physics_body_2d.cpp
9906 views
1
/**************************************************************************/
2
/* physics_body_2d.cpp */
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
#include "physics_body_2d.h"
32
33
void PhysicsBody2D::_bind_methods() {
34
ClassDB::bind_method(D_METHOD("move_and_collide", "motion", "test_only", "safe_margin", "recovery_as_collision"), &PhysicsBody2D::_move, DEFVAL(false), DEFVAL(0.08), DEFVAL(false));
35
ClassDB::bind_method(D_METHOD("test_move", "from", "motion", "collision", "safe_margin", "recovery_as_collision"), &PhysicsBody2D::test_move, DEFVAL(Variant()), DEFVAL(0.08), DEFVAL(false));
36
ClassDB::bind_method(D_METHOD("get_gravity"), &PhysicsBody2D::get_gravity);
37
38
ClassDB::bind_method(D_METHOD("get_collision_exceptions"), &PhysicsBody2D::get_collision_exceptions);
39
ClassDB::bind_method(D_METHOD("add_collision_exception_with", "body"), &PhysicsBody2D::add_collision_exception_with);
40
ClassDB::bind_method(D_METHOD("remove_collision_exception_with", "body"), &PhysicsBody2D::remove_collision_exception_with);
41
}
42
43
PhysicsBody2D::PhysicsBody2D(PhysicsServer2D::BodyMode p_mode) :
44
CollisionObject2D(PhysicsServer2D::get_singleton()->body_create(), false) {
45
set_body_mode(p_mode);
46
set_pickable(false);
47
}
48
49
Ref<KinematicCollision2D> PhysicsBody2D::_move(const Vector2 &p_motion, bool p_test_only, real_t p_margin, bool p_recovery_as_collision) {
50
PhysicsServer2D::MotionParameters parameters(get_global_transform(), p_motion, p_margin);
51
parameters.recovery_as_collision = p_recovery_as_collision;
52
53
PhysicsServer2D::MotionResult result;
54
55
if (move_and_collide(parameters, result, p_test_only)) {
56
// Create a new instance when the cached reference is invalid or still in use in script.
57
if (motion_cache.is_null() || motion_cache->get_reference_count() > 1) {
58
motion_cache.instantiate();
59
motion_cache->owner_id = get_instance_id();
60
}
61
62
motion_cache->result = result;
63
return motion_cache;
64
}
65
66
return Ref<KinematicCollision2D>();
67
}
68
69
bool PhysicsBody2D::move_and_collide(const PhysicsServer2D::MotionParameters &p_parameters, PhysicsServer2D::MotionResult &r_result, bool p_test_only, bool p_cancel_sliding) {
70
if (is_only_update_transform_changes_enabled()) {
71
ERR_PRINT("Move functions do not work together with 'sync to physics' option. See the documentation for details.");
72
}
73
74
bool colliding = PhysicsServer2D::get_singleton()->body_test_motion(get_rid(), p_parameters, &r_result);
75
76
// Restore direction of motion to be along original motion,
77
// in order to avoid sliding due to recovery,
78
// but only if collision depth is low enough to avoid tunneling.
79
if (p_cancel_sliding) {
80
real_t motion_length = p_parameters.motion.length();
81
real_t precision = 0.001;
82
83
if (colliding) {
84
// Can't just use margin as a threshold because collision depth is calculated on unsafe motion,
85
// so even in normal resting cases the depth can be a bit more than the margin.
86
precision += motion_length * (r_result.collision_unsafe_fraction - r_result.collision_safe_fraction);
87
88
if (r_result.collision_depth > p_parameters.margin + precision) {
89
p_cancel_sliding = false;
90
}
91
}
92
93
if (p_cancel_sliding) {
94
// When motion is null, recovery is the resulting motion.
95
Vector2 motion_normal;
96
if (motion_length > CMP_EPSILON) {
97
motion_normal = p_parameters.motion / motion_length;
98
}
99
100
// Check depth of recovery.
101
real_t projected_length = r_result.travel.dot(motion_normal);
102
Vector2 recovery = r_result.travel - motion_normal * projected_length;
103
real_t recovery_length = recovery.length();
104
// Fixes cases where canceling slide causes the motion to go too deep into the ground,
105
// because we're only taking rest information into account and not general recovery.
106
if (recovery_length < p_parameters.margin + precision) {
107
// Apply adjustment to motion.
108
r_result.travel = motion_normal * projected_length;
109
r_result.remainder = p_parameters.motion - r_result.travel;
110
}
111
}
112
}
113
114
if (!p_test_only) {
115
Transform2D gt = p_parameters.from;
116
gt.columns[2] += r_result.travel;
117
set_global_transform(gt);
118
}
119
120
return colliding;
121
}
122
123
bool PhysicsBody2D::test_move(const Transform2D &p_from, const Vector2 &p_motion, const Ref<KinematicCollision2D> &r_collision, real_t p_margin, bool p_recovery_as_collision) {
124
ERR_FAIL_COND_V(!is_inside_tree(), false);
125
126
PhysicsServer2D::MotionResult *r = nullptr;
127
PhysicsServer2D::MotionResult temp_result;
128
if (r_collision.is_valid()) {
129
r = &r_collision->result;
130
} else {
131
r = &temp_result;
132
}
133
134
PhysicsServer2D::MotionParameters parameters(p_from, p_motion, p_margin);
135
parameters.recovery_as_collision = p_recovery_as_collision;
136
137
return PhysicsServer2D::get_singleton()->body_test_motion(get_rid(), parameters, r);
138
}
139
140
Vector2 PhysicsBody2D::get_gravity() const {
141
PhysicsDirectBodyState2D *state = PhysicsServer2D::get_singleton()->body_get_direct_state(get_rid());
142
ERR_FAIL_NULL_V(state, Vector2());
143
return state->get_total_gravity();
144
}
145
146
TypedArray<PhysicsBody2D> PhysicsBody2D::get_collision_exceptions() {
147
List<RID> exceptions;
148
PhysicsServer2D::get_singleton()->body_get_collision_exceptions(get_rid(), &exceptions);
149
Array ret;
150
for (const RID &body : exceptions) {
151
ObjectID instance_id = PhysicsServer2D::get_singleton()->body_get_object_instance_id(body);
152
Object *obj = ObjectDB::get_instance(instance_id);
153
PhysicsBody2D *physics_body = Object::cast_to<PhysicsBody2D>(obj);
154
ret.append(physics_body);
155
}
156
return ret;
157
}
158
159
void PhysicsBody2D::add_collision_exception_with(Node *p_node) {
160
ERR_FAIL_NULL(p_node);
161
PhysicsBody2D *physics_body = Object::cast_to<PhysicsBody2D>(p_node);
162
ERR_FAIL_NULL_MSG(physics_body, "Collision exception only works between two nodes that inherit from PhysicsBody2D.");
163
PhysicsServer2D::get_singleton()->body_add_collision_exception(get_rid(), physics_body->get_rid());
164
}
165
166
void PhysicsBody2D::remove_collision_exception_with(Node *p_node) {
167
ERR_FAIL_NULL(p_node);
168
PhysicsBody2D *physics_body = Object::cast_to<PhysicsBody2D>(p_node);
169
ERR_FAIL_NULL_MSG(physics_body, "Collision exception only works between two nodes that inherit from PhysicsBody2D.");
170
PhysicsServer2D::get_singleton()->body_remove_collision_exception(get_rid(), physics_body->get_rid());
171
}
172
173
PackedStringArray PhysicsBody2D::get_configuration_warnings() const {
174
PackedStringArray warnings = CollisionObject2D::get_configuration_warnings();
175
176
if (SceneTree::is_fti_enabled_in_project() && !is_physics_interpolated()) {
177
warnings.push_back(RTR("PhysicsBody2D will not work correctly on a non-interpolated branch of the SceneTree.\nCheck the node's inherited physics_interpolation_mode."));
178
}
179
180
return warnings;
181
}
182
183