Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add optional delta parameter to move_and_slide #84665

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
3 changes: 3 additions & 0 deletions doc/classes/CharacterBody2D.xml
Original file line number Diff line number Diff line change
Expand Up @@ -135,12 +135,15 @@
</method>
<method name="move_and_slide">
<return type="bool" />
<param index="0" name="delta" type="float" default="-1.0" />
<description>
Moves the body based on [member velocity]. If the body collides with another, it will slide along the other body (by default only on floor) rather than stop immediately. If the other body is a [CharacterBody2D] or [RigidBody2D], it will also be affected by the motion of the other body. You can use this to make moving and rotating platforms, or to make nodes push other nodes.
Modifies [member velocity] if a slide collision occurred. To get the latest collision call [method get_last_slide_collision], for detailed information about collisions that occurred, use [method get_slide_collision].
When the body touches a moving platform, the platform's velocity is automatically added to the body motion. If a collision occurs due to the platform's motion, it will always be first in the slide collisions.
The general behavior and available properties change according to the [member motion_mode].
Returns [code]true[/code] if the body collided, otherwise, returns [code]false[/code].
By default, the time delta used for motion depends on the context the method is called from. When called from [method Node._physics_process], the delta is the time passed between the current and last [i]physics[/i] frame. When called from [method Node._process], the delta is the time passed between the current and last [i]render[/i] frame. This can be overridden by passing a custom [param delta] value to the method.
[b]Note:[/b] [param delta] must be a positive number. Zero or negative values will be ignored.
</description>
</method>
</methods>
Expand Down
3 changes: 3 additions & 0 deletions doc/classes/CharacterBody3D.xml
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,14 @@
</method>
<method name="move_and_slide">
<return type="bool" />
<param index="0" name="delta" type="float" default="-1.0" />
<description>
Moves the body based on [member velocity]. If the body collides with another, it will slide along the other body rather than stop immediately. If the other body is a [CharacterBody3D] or [RigidBody3D], it will also be affected by the motion of the other body. You can use this to make moving and rotating platforms, or to make nodes push other nodes.
Modifies [member velocity] if a slide collision occurred. To get the latest collision call [method get_last_slide_collision], for more detailed information about collisions that occurred, use [method get_slide_collision].
When the body touches a moving platform, the platform's velocity is automatically added to the body motion. If a collision occurs due to the platform's motion, it will always be first in the slide collisions.
Returns [code]true[/code] if the body collided, otherwise, returns [code]false[/code].
By default, the time delta used for motion depends on the context the method is called from. When called from [method Node._physics_process], the delta is the time passed between the current and last [i]physics[/i] frame. When called from [method Node._process], the delta is the time passed between the current and last [i]render[/i] frame. This can be overridden by passing a custom [param delta] value to the method.
[b]Note:[/b] [param delta] must be a positive number. Zero or negative values will treated as if no value was passed.
</description>
</method>
</methods>
Expand Down
11 changes: 7 additions & 4 deletions scene/2d/physics_body_2d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1106,9 +1106,12 @@ void RigidBody2D::_reload_physics_characteristics() {
// So, if you pass 45 as limit, avoid numerical precision errors when angle is 45.
#define FLOOR_ANGLE_THRESHOLD 0.01

bool CharacterBody2D::move_and_slide() {
// Hack in order to work with calling from _process as well as from _physics_process; calling from thread is risky.
double delta = Engine::get_singleton()->is_in_physics_frame() ? get_physics_process_delta_time() : get_process_delta_time();
bool CharacterBody2D::move_and_slide(double delta) {
// Non-positive delta value means nothing was passed (default is -1) or an invalid value, so we infer it from context
if (!(delta > 0)) {
// Hack in order to work with calling from _process as well as from _physics_process; calling from thread is risky.
delta = Engine::get_singleton()->is_in_physics_frame() ? get_physics_process_delta_time() : get_process_delta_time();
}

Vector2 current_platform_velocity = platform_velocity;
Transform2D gt = get_global_transform();
Expand Down Expand Up @@ -1719,7 +1722,7 @@ void CharacterBody2D::_notification(int p_what) {
}

void CharacterBody2D::_bind_methods() {
ClassDB::bind_method(D_METHOD("move_and_slide"), &CharacterBody2D::move_and_slide);
ClassDB::bind_method(D_METHOD("move_and_slide", "delta"), &CharacterBody2D::move_and_slide, DEFVAL(-1.0));
ClassDB::bind_method(D_METHOD("apply_floor_snap"), &CharacterBody2D::apply_floor_snap);

ClassDB::bind_method(D_METHOD("set_velocity", "velocity"), &CharacterBody2D::set_velocity);
Expand Down
2 changes: 1 addition & 1 deletion scene/2d/physics_body_2d.h
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ class CharacterBody2D : public PhysicsBody2D {
PLATFORM_ON_LEAVE_ADD_UPWARD_VELOCITY,
PLATFORM_ON_LEAVE_DO_NOTHING,
};
bool move_and_slide();
bool move_and_slide(double delta = -1);
void apply_floor_snap();

const Vector2 &get_velocity() const;
Expand Down
11 changes: 7 additions & 4 deletions scene/3d/physics_body_3d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1170,9 +1170,12 @@ void RigidBody3D::_reload_physics_characteristics() {
//so, if you pass 45 as limit, avoid numerical precision errors when angle is 45.
#define FLOOR_ANGLE_THRESHOLD 0.01

bool CharacterBody3D::move_and_slide() {
// Hack in order to work with calling from _process as well as from _physics_process; calling from thread is risky
double delta = Engine::get_singleton()->is_in_physics_frame() ? get_physics_process_delta_time() : get_process_delta_time();
bool CharacterBody3D::move_and_slide(double delta) {
// Non-positive delta value means nothing was passed (default is -1) or an invalid value, so we infer it from context
if (!(delta > 0)) {
// Hack in order to work with calling from _process as well as from _physics_process; calling from thread is risky.
delta = Engine::get_singleton()->is_in_physics_frame() ? get_physics_process_delta_time() : get_process_delta_time();
}

for (int i = 0; i < 3; i++) {
if (locked_axis & (1 << i)) {
Expand Down Expand Up @@ -1970,7 +1973,7 @@ void CharacterBody3D::_notification(int p_what) {
}

void CharacterBody3D::_bind_methods() {
ClassDB::bind_method(D_METHOD("move_and_slide"), &CharacterBody3D::move_and_slide);
ClassDB::bind_method(D_METHOD("move_and_slide", "delta"), &CharacterBody3D::move_and_slide, DEFVAL(-1.0));
ClassDB::bind_method(D_METHOD("apply_floor_snap"), &CharacterBody3D::apply_floor_snap);

ClassDB::bind_method(D_METHOD("set_velocity", "velocity"), &CharacterBody3D::set_velocity);
Expand Down
2 changes: 1 addition & 1 deletion scene/3d/physics_body_3d.h
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ class CharacterBody3D : public PhysicsBody3D {
PLATFORM_ON_LEAVE_ADD_UPWARD_VELOCITY,
PLATFORM_ON_LEAVE_DO_NOTHING,
};
bool move_and_slide();
bool move_and_slide(double delta = -1);
void apply_floor_snap();

const Vector3 &get_velocity() const;
Expand Down
Loading