Skip to content

Commit

Permalink
Improved RigidDynamicBody linear/angular damping override
Browse files Browse the repository at this point in the history
Damping values are now non-negative.

Add new properties linear_damp_mode and angular_damp_mode to set the way
RigidDynamicBody and PhysicalBone (2D & 3D) use damping values.
It can now be Combine (default) to add to the default/areas, or Replace
to override the value completely (current behavior).
  • Loading branch information
pouleyKetchoupp committed Oct 26, 2021
1 parent 24fdedf commit daf7dca
Show file tree
Hide file tree
Showing 20 changed files with 377 additions and 122 deletions.
22 changes: 18 additions & 4 deletions doc/classes/PhysicalBone3D.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,12 @@
</method>
</methods>
<members>
<member name="angular_damp" type="float" setter="set_angular_damp" getter="get_angular_damp" default="-1.0">
Damps the body's rotation if greater than [code]0[/code].
<member name="angular_damp" type="float" setter="set_angular_damp" getter="get_angular_damp" default="0.0">
Damps the body's rotation. By default, the body will use the [b]Default Angular Damp[/b] in [b]Project &gt; Project Settings &gt; Physics &gt; 3d[/b] or any value override set by an [Area3D] the body is in. Depending on [member angular_damp_mode], you can set [member angular_damp] to be added to or to replace the body's damping value.
See [member ProjectSettings.physics/3d/default_angular_damp] for more details about damping.
</member>
<member name="angular_damp_mode" type="int" setter="set_angular_damp_mode" getter="get_angular_damp_mode" enum="PhysicalBone3D.DampMode" default="0">
Defines how [member angular_damp] is applied. See [enum DampMode] for possible values.
</member>
<member name="body_offset" type="Transform3D" setter="set_body_offset" getter="get_body_offset" default="Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0)">
Sets the body's transform.
Expand All @@ -64,14 +68,24 @@
<member name="joint_type" type="int" setter="set_joint_type" getter="get_joint_type" enum="PhysicalBone3D.JointType" default="0">
Sets the joint type. See [enum JointType] for possible values.
</member>
<member name="linear_damp" type="float" setter="set_linear_damp" getter="get_linear_damp" default="-1.0">
Damps the body's movement if greater than [code]0[/code].
<member name="linear_damp" type="float" setter="set_linear_damp" getter="get_linear_damp" default="0.0">
Damps the body's movement. By default, the body will use the [b]Default Linear Damp[/b] in [b]Project &gt; Project Settings &gt; Physics &gt; 3d[/b] or any value override set by an [Area3D] the body is in. Depending on [member linear_damp_mode], you can set [member linear_damp] to be added to or to replace the body's damping value.
See [member ProjectSettings.physics/3d/default_linear_damp] for more details about damping.
</member>
<member name="linear_damp_mode" type="int" setter="set_linear_damp_mode" getter="get_linear_damp_mode" enum="PhysicalBone3D.DampMode" default="0">
Defines how [member linear_damp] is applied. See [enum DampMode] for possible values.
</member>
<member name="mass" type="float" setter="set_mass" getter="get_mass" default="1.0">
The body's mass.
</member>
</members>
<constants>
<constant name="DAMP_MODE_COMBINE" value="0" enum="DampMode">
In this mode, the body's damping value is added to any value set in areas or the default value.
</constant>
<constant name="DAMP_MODE_REPLACE" value="1" enum="DampMode">
In this mode, the body's damping value replaces any value set in areas or the default value.
</constant>
<constant name="JOINT_TYPE_NONE" value="0" enum="JointType">
</constant>
<constant name="JOINT_TYPE_PIN" value="1" enum="JointType">
Expand Down
18 changes: 15 additions & 3 deletions doc/classes/PhysicsServer2D.xml
Original file line number Diff line number Diff line change
Expand Up @@ -926,15 +926,27 @@
<constant name="BODY_PARAM_GRAVITY_SCALE" value="5" enum="BodyParameter">
Constant to set/get a body's gravity multiplier.
</constant>
<constant name="BODY_PARAM_LINEAR_DAMP" value="6" enum="BodyParameter">
<constant name="BODY_PARAM_LINEAR_DAMP_MODE" value="6" enum="BodyParameter">
Constant to set/get a body's linear dampening mode. See [enum BodyDampMode] for possible values.
</constant>
<constant name="BODY_PARAM_ANGULAR_DAMP_MODE" value="7" enum="BodyParameter">
Constant to set/get a body's angular dampening mode. See [enum BodyDampMode] for possible values.
</constant>
<constant name="BODY_PARAM_LINEAR_DAMP" value="8" enum="BodyParameter">
Constant to set/get a body's linear dampening factor.
</constant>
<constant name="BODY_PARAM_ANGULAR_DAMP" value="7" enum="BodyParameter">
<constant name="BODY_PARAM_ANGULAR_DAMP" value="9" enum="BodyParameter">
Constant to set/get a body's angular dampening factor.
</constant>
<constant name="BODY_PARAM_MAX" value="8" enum="BodyParameter">
<constant name="BODY_PARAM_MAX" value="10" enum="BodyParameter">
Represents the size of the [enum BodyParameter] enum.
</constant>
<constant name="BODY_DAMP_MODE_COMBINE" value="0" enum="BodyDampMode">
The body's damping value is added to any value set in areas or the default value.
</constant>
<constant name="BODY_DAMP_MODE_REPLACE" value="1" enum="BodyDampMode">
The body's damping value replaces any value set in areas or the default value.
</constant>
<constant name="BODY_STATE_TRANSFORM" value="0" enum="BodyState">
Constant to set/get the current transform matrix of the body.
</constant>
Expand Down
18 changes: 15 additions & 3 deletions doc/classes/PhysicsServer3D.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1294,15 +1294,27 @@
<constant name="BODY_PARAM_GRAVITY_SCALE" value="5" enum="BodyParameter">
Constant to set/get a body's gravity multiplier.
</constant>
<constant name="BODY_PARAM_LINEAR_DAMP" value="6" enum="BodyParameter">
<constant name="BODY_PARAM_LINEAR_DAMP_MODE" value="6" enum="BodyParameter">
Constant to set/get a body's linear dampening mode. See [enum BodyDampMode] for possible values.
</constant>
<constant name="BODY_PARAM_ANGULAR_DAMP_MODE" value="7" enum="BodyParameter">
Constant to set/get a body's angular dampening mode. See [enum BodyDampMode] for possible values.
</constant>
<constant name="BODY_PARAM_LINEAR_DAMP" value="8" enum="BodyParameter">
Constant to set/get a body's linear dampening factor.
</constant>
<constant name="BODY_PARAM_ANGULAR_DAMP" value="7" enum="BodyParameter">
<constant name="BODY_PARAM_ANGULAR_DAMP" value="9" enum="BodyParameter">
Constant to set/get a body's angular dampening factor.
</constant>
<constant name="BODY_PARAM_MAX" value="8" enum="BodyParameter">
<constant name="BODY_PARAM_MAX" value="10" enum="BodyParameter">
Represents the size of the [enum BodyParameter] enum.
</constant>
<constant name="BODY_DAMP_MODE_COMBINE" value="0" enum="BodyDampMode">
The body's damping value is added to any value set in areas or the default value.
</constant>
<constant name="BODY_DAMP_MODE_REPLACE" value="1" enum="BodyDampMode">
The body's damping value replaces any value set in areas or the default value.
</constant>
<constant name="BODY_STATE_TRANSFORM" value="0" enum="BodyState">
Constant to set/get the current transform matrix of the body.
</constant>
Expand Down
20 changes: 16 additions & 4 deletions doc/classes/RigidDynamicBody2D.xml
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,13 @@
</method>
</methods>
<members>
<member name="angular_damp" type="float" setter="set_angular_damp" getter="get_angular_damp" default="-1.0">
Damps the body's [member angular_velocity]. If [code]-1[/code], the body will use the [b]Default Angular Damp[/b] defined in [b]Project &gt; Project Settings &gt; Physics &gt; 2d[/b].
<member name="angular_damp" type="float" setter="set_angular_damp" getter="get_angular_damp" default="0.0">
Damps the body's rotation. By default, the body will use the [b]Default Angular Damp[/b] in [b]Project &gt; Project Settings &gt; Physics &gt; 2d[/b] or any value override set by an [Area2D] the body is in. Depending on [member angular_damp_mode], you can set [member angular_damp] to be added to or to replace the body's damping value.
See [member ProjectSettings.physics/2d/default_angular_damp] for more details about damping.
</member>
<member name="angular_damp_mode" type="int" setter="set_angular_damp_mode" getter="get_angular_damp_mode" enum="RigidDynamicBody2D.DampMode" default="0">
Defines how [member angular_damp] is applied. See [enum DampMode] for possible values.
</member>
<member name="angular_velocity" type="float" setter="set_angular_velocity" getter="get_angular_velocity" default="0.0">
The body's rotational velocity.
</member>
Expand Down Expand Up @@ -135,10 +138,13 @@
The body's moment of inertia. This is like mass, but for rotation: it determines how much torque it takes to rotate the body. The moment of inertia is usually computed automatically from the mass and the shapes, but this property allows you to set a custom value.
If set to [code]0[/code], inertia is automatically computed (default value).
</member>
<member name="linear_damp" type="float" setter="set_linear_damp" getter="get_linear_damp" default="-1.0">
Damps the body's [member linear_velocity]. If [code]-1[/code], the body will use the [b]Default Linear Damp[/b] in [b]Project &gt; Project Settings &gt; Physics &gt; 2d[/b].
<member name="linear_damp" type="float" setter="set_linear_damp" getter="get_linear_damp" default="0.0">
Damps the body's movement. By default, the body will use the [b]Default Linear Damp[/b] in [b]Project &gt; Project Settings &gt; Physics &gt; 2d[/b] or any value override set by an [Area2D] the body is in. Depending on [member linear_damp_mode], you can set [member linear_damp] to be added to or to replace the body's damping value.
See [member ProjectSettings.physics/2d/default_linear_damp] for more details about damping.
</member>
<member name="linear_damp_mode" type="int" setter="set_linear_damp_mode" getter="get_linear_damp_mode" enum="RigidDynamicBody2D.DampMode" default="0">
Defines how [member linear_damp] is applied. See [enum DampMode] for possible values.
</member>
<member name="linear_velocity" type="Vector2" setter="set_linear_velocity" getter="get_linear_velocity" default="Vector2(0, 0)">
The body's linear velocity.
</member>
Expand Down Expand Up @@ -217,6 +223,12 @@
<constant name="CENTER_OF_MASS_MODE_CUSTOM" value="1" enum="CenterOfMassMode">
In this mode, the body's center of mass is set through [member center_of_mass]. Defaults to the body's origin position.
</constant>
<constant name="DAMP_MODE_COMBINE" value="0" enum="DampMode">
In this mode, the body's damping value is added to any value set in areas or the default value.
</constant>
<constant name="DAMP_MODE_REPLACE" value="1" enum="DampMode">
In this mode, the body's damping value replaces any value set in areas or the default value.
</constant>
<constant name="CCD_MODE_DISABLED" value="0" enum="CCDMode">
Continuous collision detection disabled. This is the fastest way to detect body collisions, but can miss small, fast-moving objects.
</constant>
Expand Down
20 changes: 16 additions & 4 deletions doc/classes/RigidDynamicBody3D.xml
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,13 @@
</method>
</methods>
<members>
<member name="angular_damp" type="float" setter="set_angular_damp" getter="get_angular_damp" default="-1.0">
Damps RigidDynamicBody3D's rotational forces.
<member name="angular_damp" type="float" setter="set_angular_damp" getter="get_angular_damp" default="0.0">
Damps the body's rotation. By default, the body will use the [b]Default Angular Damp[/b] in [b]Project &gt; Project Settings &gt; Physics &gt; 3d[/b] or any value override set by an [Area3D] the body is in. Depending on [member angular_damp_mode], you can set [member angular_damp] to be added to or to replace the body's damping value.
See [member ProjectSettings.physics/3d/default_angular_damp] for more details about damping.
</member>
<member name="angular_damp_mode" type="int" setter="set_angular_damp_mode" getter="get_angular_damp_mode" enum="RigidDynamicBody3D.DampMode" default="0">
Defines how [member angular_damp] is applied. See [enum DampMode] for possible values.
</member>
<member name="angular_velocity" type="Vector3" setter="set_angular_velocity" getter="get_angular_velocity" default="Vector3(0, 0, 0)">
RigidDynamicBody3D's rotational velocity.
</member>
Expand Down Expand Up @@ -138,10 +141,13 @@
The body's moment of inertia. This is like mass, but for rotation: it determines how much torque it takes to rotate the body on each axis. The moment of inertia is usually computed automatically from the mass and the shapes, but this property allows you to set a custom value.
If set to [code]Vector3.ZERO[/code], inertia is automatically computed (default value).
</member>
<member name="linear_damp" type="float" setter="set_linear_damp" getter="get_linear_damp" default="-1.0">
The body's linear damp. Cannot be less than -1.0. If this value is different from -1.0, any linear damp derived from the world or areas will be overridden.
<member name="linear_damp" type="float" setter="set_linear_damp" getter="get_linear_damp" default="0.0">
Damps the body's movement. By default, the body will use the [b]Default Linear Damp[/b] in [b]Project &gt; Project Settings &gt; Physics &gt; 3d[/b] or any value override set by an [Area3D] the body is in. Depending on [member linear_damp_mode], you can set [member linear_damp] to be added to or to replace the body's damping value.
See [member ProjectSettings.physics/3d/default_linear_damp] for more details about damping.
</member>
<member name="linear_damp_mode" type="int" setter="set_linear_damp_mode" getter="get_linear_damp_mode" enum="RigidDynamicBody3D.DampMode" default="0">
Defines how [member linear_damp] is applied. See [enum DampMode] for possible values.
</member>
<member name="linear_velocity" type="Vector3" setter="set_linear_velocity" getter="get_linear_velocity" default="Vector3(0, 0, 0)">
The body's linear velocity. Can be used sporadically, but [b]don't set this every frame[/b], because physics may run in another thread and runs at a different granularity. Use [method _integrate_forces] as your process loop for precise control of the body state.
</member>
Expand Down Expand Up @@ -222,5 +228,11 @@
<constant name="CENTER_OF_MASS_MODE_CUSTOM" value="1" enum="CenterOfMassMode">
In this mode, the body's center of mass is set through [member center_of_mass]. Defaults to the body's origin position.
</constant>
<constant name="DAMP_MODE_COMBINE" value="0" enum="DampMode">
In this mode, the body's damping value is added to any value set in areas or the default value.
</constant>
<constant name="DAMP_MODE_REPLACE" value="1" enum="DampMode">
In this mode, the body's damping value replaces any value set in areas or the default value.
</constant>
</constants>
</class>
29 changes: 29 additions & 0 deletions scene/2d/physics_body_2d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -684,6 +684,24 @@ real_t RigidDynamicBody2D::get_gravity_scale() const {
return gravity_scale;
}

void RigidDynamicBody2D::set_linear_damp_mode(DampMode p_mode) {
linear_damp_mode = p_mode;
PhysicsServer2D::get_singleton()->body_set_param(get_rid(), PhysicsServer2D::BODY_PARAM_LINEAR_DAMP_MODE, linear_damp_mode);
}

RigidDynamicBody2D::DampMode RigidDynamicBody2D::get_linear_damp_mode() const {
return linear_damp_mode;
}

void RigidDynamicBody2D::set_angular_damp_mode(DampMode p_mode) {
angular_damp_mode = p_mode;
PhysicsServer2D::get_singleton()->body_set_param(get_rid(), PhysicsServer2D::BODY_PARAM_ANGULAR_DAMP_MODE, angular_damp_mode);
}

RigidDynamicBody2D::DampMode RigidDynamicBody2D::get_angular_damp_mode() const {
return angular_damp_mode;
}

void RigidDynamicBody2D::set_linear_damp(real_t p_linear_damp) {
ERR_FAIL_COND(p_linear_damp < -1);
linear_damp = p_linear_damp;
Expand Down Expand Up @@ -916,6 +934,12 @@ void RigidDynamicBody2D::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_gravity_scale", "gravity_scale"), &RigidDynamicBody2D::set_gravity_scale);
ClassDB::bind_method(D_METHOD("get_gravity_scale"), &RigidDynamicBody2D::get_gravity_scale);

ClassDB::bind_method(D_METHOD("set_linear_damp_mode", "linear_damp_mode"), &RigidDynamicBody2D::set_linear_damp_mode);
ClassDB::bind_method(D_METHOD("get_linear_damp_mode"), &RigidDynamicBody2D::get_linear_damp_mode);

ClassDB::bind_method(D_METHOD("set_angular_damp_mode", "angular_damp_mode"), &RigidDynamicBody2D::set_angular_damp_mode);
ClassDB::bind_method(D_METHOD("get_angular_damp_mode"), &RigidDynamicBody2D::get_angular_damp_mode);

ClassDB::bind_method(D_METHOD("set_linear_damp", "linear_damp"), &RigidDynamicBody2D::set_linear_damp);
ClassDB::bind_method(D_METHOD("get_linear_damp"), &RigidDynamicBody2D::get_linear_damp);

Expand Down Expand Up @@ -992,9 +1016,11 @@ void RigidDynamicBody2D::_bind_methods() {
ADD_PROPERTY(PropertyInfo(Variant::INT, "freeze_mode", PROPERTY_HINT_ENUM, "Static,Kinematic"), "set_freeze_mode", "get_freeze_mode");
ADD_GROUP("Linear", "linear_");
ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "linear_velocity"), "set_linear_velocity", "get_linear_velocity");
ADD_PROPERTY(PropertyInfo(Variant::INT, "linear_damp_mode", PROPERTY_HINT_ENUM, "Combine,Replace"), "set_linear_damp_mode", "get_linear_damp_mode");
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "linear_damp", PROPERTY_HINT_RANGE, "-1,100,0.001,or_greater"), "set_linear_damp", "get_linear_damp");
ADD_GROUP("Angular", "angular_");
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "angular_velocity"), "set_angular_velocity", "get_angular_velocity");
ADD_PROPERTY(PropertyInfo(Variant::INT, "angular_damp_mode", PROPERTY_HINT_ENUM, "Combine,Replace"), "set_angular_damp_mode", "get_angular_damp_mode");
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "angular_damp", PROPERTY_HINT_RANGE, "-1,100,0.001,or_greater"), "set_angular_damp", "get_angular_damp");
ADD_GROUP("Applied Forces", "applied_");
ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "applied_force"), "set_applied_force", "get_applied_force");
Expand All @@ -1012,6 +1038,9 @@ void RigidDynamicBody2D::_bind_methods() {
BIND_ENUM_CONSTANT(CENTER_OF_MASS_MODE_AUTO);
BIND_ENUM_CONSTANT(CENTER_OF_MASS_MODE_CUSTOM);

BIND_ENUM_CONSTANT(DAMP_MODE_COMBINE);
BIND_ENUM_CONSTANT(DAMP_MODE_REPLACE);

BIND_ENUM_CONSTANT(CCD_MODE_DISABLED);
BIND_ENUM_CONSTANT(CCD_MODE_CAST_RAY);
BIND_ENUM_CONSTANT(CCD_MODE_CAST_SHAPE);
Expand Down
20 changes: 18 additions & 2 deletions scene/2d/physics_body_2d.h
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,11 @@ class RigidDynamicBody2D : public PhysicsBody2D {
CENTER_OF_MASS_MODE_CUSTOM,
};

enum DampMode {
DAMP_MODE_COMBINE,
DAMP_MODE_REPLACE,
};

enum CCDMode {
CCD_MODE_DISABLED,
CCD_MODE_CAST_RAY,
Expand All @@ -146,8 +151,12 @@ class RigidDynamicBody2D : public PhysicsBody2D {

Ref<PhysicsMaterial> physics_material_override;
real_t gravity_scale = 1.0;
real_t linear_damp = -1.0;
real_t angular_damp = -1.0;

DampMode linear_damp_mode = DAMP_MODE_COMBINE;
DampMode angular_damp_mode = DAMP_MODE_COMBINE;

real_t linear_damp = 0.0;
real_t angular_damp = 0.0;

Vector2 linear_velocity;
real_t angular_velocity = 0.0;
Expand Down Expand Up @@ -241,6 +250,12 @@ class RigidDynamicBody2D : public PhysicsBody2D {
void set_gravity_scale(real_t p_gravity_scale);
real_t get_gravity_scale() const;

void set_linear_damp_mode(DampMode p_mode);
DampMode get_linear_damp_mode() const;

void set_angular_damp_mode(DampMode p_mode);
DampMode get_angular_damp_mode() const;

void set_linear_damp(real_t p_linear_damp);
real_t get_linear_damp() const;

Expand Down Expand Up @@ -300,6 +315,7 @@ class RigidDynamicBody2D : public PhysicsBody2D {

VARIANT_ENUM_CAST(RigidDynamicBody2D::FreezeMode);
VARIANT_ENUM_CAST(RigidDynamicBody2D::CenterOfMassMode);
VARIANT_ENUM_CAST(RigidDynamicBody2D::DampMode);
VARIANT_ENUM_CAST(RigidDynamicBody2D::CCDMode);

class CharacterBody2D : public PhysicsBody2D {
Expand Down
Loading

0 comments on commit daf7dca

Please sign in to comment.