Merge pull request #52754 from nekomatata/dynamic-body-modes

Clarify RigidDynamicBody modes
This commit is contained in:
Camille Mohr-Daurat 2021-09-28 17:11:17 -07:00 committed by GitHub
commit 341b532d5e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
19 changed files with 234 additions and 154 deletions

View file

@ -926,8 +926,8 @@
<constant name="BODY_MODE_DYNAMIC" value="2" enum="BodyMode">
Constant for dynamic bodies. In this mode, a body can be pushed by other bodies and has forces applied.
</constant>
<constant name="BODY_MODE_DYNAMIC_LOCKED" value="3" enum="BodyMode">
Constant for locked dynamic bodies. In this mode, a body is dynamic but can not rotate, and only its linear velocity is affected by external forces.
<constant name="BODY_MODE_DYNAMIC_LINEAR" value="3" enum="BodyMode">
Constant for linear dynamic bodies. In this mode, a body is dynamic but can not rotate, and only its linear velocity is affected by external forces.
</constant>
<constant name="BODY_PARAM_BOUNCE" value="0" enum="BodyParameter">
Constant to set/get a body's bounce factor.

View file

@ -1278,8 +1278,8 @@
<constant name="BODY_MODE_DYNAMIC" value="2" enum="BodyMode">
Constant for dynamic bodies. In this mode, a body can be pushed by other bodies and has forces applied.
</constant>
<constant name="BODY_MODE_DYNAMIC_LOCKED" value="3" enum="BodyMode">
Constant for locked dynamic bodies. In this mode, a body is dynamic but can not rotate, and only its linear velocity is affected by external forces.
<constant name="BODY_MODE_DYNAMIC_LINEAR" value="3" enum="BodyMode">
Constant for linear dynamic bodies. In this mode, a body is dynamic but can not rotate, and only its linear velocity is affected by external forces.
</constant>
<constant name="BODY_PARAM_BOUNCE" value="0" enum="BodyParameter">
Constant to set/get a body's bounce factor.

View file

@ -5,11 +5,10 @@
</brief_description>
<description>
This node implements simulated 2D physics. You do not control a RigidDynamicBody2D directly. Instead, you apply forces to it (gravity, impulses, etc.) and the physics simulation calculates the resulting movement based on its mass, friction, and other physical properties.
A RigidDynamicBody2D has 4 behavior [member mode]s: Dynamic, Static, DynamicLocked, and Kinematic.
You can switch the body's behavior using [member lock_rotation], [member freeze], and [member freeze_mode].
[b]Note:[/b] You should not change a RigidDynamicBody2D's [code]position[/code] or [code]linear_velocity[/code] every frame or even very often. If you need to directly affect the body's state, use [method _integrate_forces], which allows you to directly access the physics state.
Please also keep in mind that physics bodies manage their own transform which overwrites the ones you set. So any direct or indirect transformation (including scaling of the node or its parent) will be visible in the editor only, and immediately reset at runtime.
If you need to override the default physics behavior or add a transformation at runtime, you can write a custom force integration. See [member custom_integrator].
The center of mass is always located at the node's origin without taking into account the [CollisionShape2D] centroid offsets.
</description>
<tutorials>
<link title="2D Physics Platformer Demo">https://godotengine.org/asset-library/asset/119</link>
@ -120,6 +119,15 @@
<member name="custom_integrator" type="bool" setter="set_use_custom_integrator" getter="is_using_custom_integrator" default="false">
If [code]true[/code], internal force integration is disabled for this body. Aside from collision response, the body will only move as determined by the [method _integrate_forces] function.
</member>
<member name="freeze" type="bool" setter="set_freeze_enabled" getter="is_freeze_enabled" default="false">
If [code]true[/code], the body is frozen. Gravity and forces are not applied anymore.
See [member freeze_mode] to set the body's behavior when frozen.
For a body that is always frozen, use [StaticBody2D] or [AnimatableBody2D] instead.
</member>
<member name="freeze_mode" type="int" setter="set_freeze_mode" getter="get_freeze_mode" enum="RigidDynamicBody2D.FreezeMode" default="0">
The body's freeze mode. Can be used to set the body's behavior when [member freeze] is enabled. See [enum FreezeMode] for possible values.
For a body that is always frozen, use [StaticBody2D] or [AnimatableBody2D] instead.
</member>
<member name="gravity_scale" type="float" setter="set_gravity_scale" getter="get_gravity_scale" default="1.0">
Multiplies the gravity applied to the body. The body's gravity is calculated from the [b]Default Gravity[/b] value in [b]Project &gt; Project Settings &gt; Physics &gt; 2d[/b] and/or any additional gravity vector applied by [Area2D]s.
</member>
@ -134,13 +142,12 @@
<member name="linear_velocity" type="Vector2" setter="set_linear_velocity" getter="get_linear_velocity" default="Vector2(0, 0)">
The body's linear velocity.
</member>
<member name="lock_rotation" type="bool" setter="set_lock_rotation_enabled" getter="is_lock_rotation_enabled" default="false">
If [code]true[/code], the body cannot rotate. Gravity and forces only apply linear movement.
</member>
<member name="mass" type="float" setter="set_mass" getter="get_mass" default="1.0">
The body's mass.
</member>
<member name="mode" type="int" setter="set_mode" getter="get_mode" enum="RigidDynamicBody2D.Mode" default="0">
The body's mode. See [enum Mode] for possible values.
For a body that uses only Static or Kinematic mode, use [StaticBody2D] or [AnimatableBody2D] instead.
</member>
<member name="physics_material_override" type="PhysicsMaterial" setter="set_physics_material_override" getter="get_physics_material_override">
The physics material override for the body.
If a material is assigned to this property, it will be used instead of any other physics material, such as an inherited one.
@ -198,17 +205,11 @@
</signal>
</signals>
<constants>
<constant name="MODE_DYNAMIC" value="0" enum="Mode">
Dynamic body mode. This is the default mode of a rigid body. It is affected by forces, and can move, rotate, and be affected by user code.
<constant name="FREEZE_MODE_STATIC" value="0" enum="FreezeMode">
Static body freeze mode (default). The body is not affected by gravity and forces. It can be only moved by user code and doesn't collide with other bodies along its path.
</constant>
<constant name="MODE_STATIC" value="1" enum="Mode">
Static body mode. The body behaves like a [StaticBody2D], and must be moved by code.
</constant>
<constant name="MODE_DYNAMIC_LOCKED" value="2" enum="Mode">
Locked dynamic body mode. Similar to [constant MODE_DYNAMIC], but the body can not rotate.
</constant>
<constant name="MODE_KINEMATIC" value="3" enum="Mode">
Kinematic body mode. The body behaves like a [AnimatableBody2D], and must be moved by code.
<constant name="FREEZE_MODE_KINEMATIC" value="1" enum="FreezeMode">
Kinematic body freeze mode. Similar to [constant FREEZE_MODE_STATIC], but collides with other bodies along its path when moved. Useful for a frozen body that needs to be animated.
</constant>
<constant name="CENTER_OF_MASS_MODE_AUTO" value="0" enum="CenterOfMassMode">
In this mode, the body's center of mass is calculated automatically based on its shapes.

View file

@ -5,10 +5,9 @@
</brief_description>
<description>
This is the node that implements full 3D physics. This means that you do not control a RigidDynamicBody3D directly. Instead, you can apply forces to it (gravity, impulses, etc.), and the physics simulation will calculate the resulting movement, collision, bouncing, rotating, etc.
A RigidDynamicBody3D has 4 behavior [member mode]s: Dynamic, Static, DynamicLocked, and Kinematic.
You can switch the body's behavior using [member lock_rotation], [member freeze], and [member freeze_mode].
[b]Note:[/b] Don't change a RigidDynamicBody3D's position every frame or very often. Sporadic changes work fine, but physics runs at a different granularity (fixed Hz) than usual rendering (process callback) and maybe even in a separate thread, so changing this from a process loop may result in strange behavior. If you need to directly affect the body's state, use [method _integrate_forces], which allows you to directly access the physics state.
If you need to override the default physics behavior, you can write a custom force integration function. See [member custom_integrator].
With Bullet physics (the default), the center of mass is the RigidDynamicBody3D center. With GodotPhysics, the center of mass is the average of the [CollisionShape3D] centers.
</description>
<tutorials>
<link title="Physics introduction">https://docs.godotengine.org/en/latest/tutorials/physics/physics_introduction.html</link>
@ -123,6 +122,15 @@
<member name="custom_integrator" type="bool" setter="set_use_custom_integrator" getter="is_using_custom_integrator" default="false">
If [code]true[/code], internal force integration will be disabled (like gravity or air friction) for this body. Other than collision response, the body will only move as determined by the [method _integrate_forces] function, if defined.
</member>
<member name="freeze" type="bool" setter="set_freeze_enabled" getter="is_freeze_enabled" default="false">
If [code]true[/code], the body is frozen. Gravity and forces are not applied anymore.
See [member freeze_mode] to set the body's behavior when frozen.
For a body that is always frozen, use [StaticBody3D] or [AnimatableBody3D] instead.
</member>
<member name="freeze_mode" type="int" setter="set_freeze_mode" getter="get_freeze_mode" enum="RigidDynamicBody3D.FreezeMode" default="0">
The body's freeze mode. Can be used to set the body's behavior when [member freeze] is enabled. See [enum FreezeMode] for possible values.
For a body that is always frozen, use [StaticBody3D] or [AnimatableBody3D] instead.
</member>
<member name="gravity_scale" type="float" setter="set_gravity_scale" getter="get_gravity_scale" default="1.0">
This is multiplied by the global 3D gravity setting found in [b]Project &gt; Project Settings &gt; Physics &gt; 3d[/b] to produce RigidDynamicBody3D's gravity. For example, a value of 1 will be normal gravity, 2 will apply double gravity, and 0.5 will apply half gravity to this object.
</member>
@ -137,13 +145,12 @@
<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>
<member name="lock_rotation" type="bool" setter="set_lock_rotation_enabled" getter="is_lock_rotation_enabled" default="false">
If [code]true[/code], the body cannot rotate. Gravity and forces only apply linear movement.
</member>
<member name="mass" type="float" setter="set_mass" getter="get_mass" default="1.0">
The body's mass.
</member>
<member name="mode" type="int" setter="set_mode" getter="get_mode" enum="RigidDynamicBody3D.Mode" default="0">
The body's mode. See [enum Mode] for possible values.
For a body that uses only Static or Kinematic mode, use [StaticBody3D] or [AnimatableBody3D] instead.
</member>
<member name="physics_material_override" type="PhysicsMaterial" setter="set_physics_material_override" getter="get_physics_material_override">
The physics material override for the body.
If a material is assigned to this property, it will be used instead of any other physics material, such as an inherited one.
@ -203,17 +210,11 @@
</signal>
</signals>
<constants>
<constant name="MODE_DYNAMIC" value="0" enum="Mode">
Dynamic body mode. This is the default mode of a rigid body. It is affected by forces, and can move, rotate, and be affected by user code.
<constant name="FREEZE_MODE_STATIC" value="0" enum="FreezeMode">
Static body freeze mode (default). The body is not affected by gravity and forces. It can be only moved by user code and doesn't collide with other bodies along its path.
</constant>
<constant name="MODE_STATIC" value="1" enum="Mode">
Static body mode. The body behaves like a [StaticBody3D], and can only move by user code.
</constant>
<constant name="MODE_DYNAMIC_LOCKED" value="2" enum="Mode">
Locked dynamic body mode. Similar to [constant MODE_DYNAMIC], but the body can not rotate.
</constant>
<constant name="MODE_KINEMATIC" value="3" enum="Mode">
Kinematic body mode. The body behaves like a [AnimatableBody3D], and can only move by user code.
<constant name="FREEZE_MODE_KINEMATIC" value="1" enum="FreezeMode">
Kinematic body freeze mode. Similar to [constant FREEZE_MODE_STATIC], but collides with other bodies along its path when moved. Useful for a frozen body that needs to be animated.
</constant>
<constant name="CENTER_OF_MASS_MODE_AUTO" value="0" enum="CenterOfMassMode">
In this mode, the body's center of mass is calculated automatically based on its shapes.

View file

@ -530,26 +530,23 @@ void RigidBodyBullet::set_mode(PhysicsServer3D::BodyMode p_mode) {
can_integrate_forces = false;
destroy_kinematic_utilities();
// The mode change is relevant to its mass
mode = p_mode;
switch (p_mode) {
case PhysicsServer3D::BODY_MODE_KINEMATIC:
mode = PhysicsServer3D::BODY_MODE_KINEMATIC;
reload_axis_lock();
_internal_set_mass(0);
init_kinematic_utilities();
break;
case PhysicsServer3D::BODY_MODE_STATIC:
mode = PhysicsServer3D::BODY_MODE_STATIC;
reload_axis_lock();
_internal_set_mass(0);
break;
case PhysicsServer3D::BODY_MODE_DYNAMIC:
mode = PhysicsServer3D::BODY_MODE_DYNAMIC;
reload_axis_lock();
_internal_set_mass(0 == mass ? 1 : mass);
scratch_space_override_modificator();
break;
case PhysicsServer3D::MODE_DYNAMIC_LOCKED:
mode = PhysicsServer3D::MODE_DYNAMIC_LOCKED;
case PhysicsServer3D::MODE_DYNAMIC_LINEAR:
reload_axis_lock();
_internal_set_mass(0 == mass ? 1 : mass);
scratch_space_override_modificator();
@ -722,7 +719,7 @@ bool RigidBodyBullet::is_axis_locked(PhysicsServer3D::BodyAxis p_axis) const {
void RigidBodyBullet::reload_axis_lock() {
btBody->setLinearFactor(btVector3(btScalar(!is_axis_locked(PhysicsServer3D::BODY_AXIS_LINEAR_X)), btScalar(!is_axis_locked(PhysicsServer3D::BODY_AXIS_LINEAR_Y)), btScalar(!is_axis_locked(PhysicsServer3D::BODY_AXIS_LINEAR_Z))));
if (PhysicsServer3D::MODE_DYNAMIC_LOCKED == mode) {
if (PhysicsServer3D::MODE_DYNAMIC_LINEAR == mode) {
/// When character angular is always locked
btBody->setAngularFactor(btVector3(0., 0., 0.));
} else {
@ -1017,7 +1014,7 @@ void RigidBodyBullet::_internal_set_mass(real_t p_mass) {
// Rigidbody is dynamic if and only if mass is non Zero, otherwise static
const bool isDynamic = p_mass != 0.f;
if (isDynamic) {
if (PhysicsServer3D::BODY_MODE_DYNAMIC != mode && PhysicsServer3D::MODE_DYNAMIC_LOCKED != mode) {
if (PhysicsServer3D::BODY_MODE_DYNAMIC != mode && PhysicsServer3D::MODE_DYNAMIC_LINEAR != mode) {
return;
}

View file

@ -150,27 +150,15 @@ void PhysicalBone2D::_start_physics_simulation() {
return;
}
// Reset to Bone2D position
// Reset to Bone2D position.
_position_at_bone2d();
// Apply the layers and masks
// Apply the layers and masks.
PhysicsServer2D::get_singleton()->body_set_collision_layer(get_rid(), get_collision_layer());
PhysicsServer2D::get_singleton()->body_set_collision_mask(get_rid(), get_collision_mask());
// Apply the correct mode
RigidDynamicBody2D::Mode rigid_mode = get_mode();
if (rigid_mode == RigidDynamicBody2D::MODE_STATIC) {
set_body_mode(PhysicsServer2D::BODY_MODE_STATIC);
} else if (rigid_mode == RigidDynamicBody2D::MODE_DYNAMIC) {
set_body_mode(PhysicsServer2D::BODY_MODE_DYNAMIC);
} else if (rigid_mode == RigidDynamicBody2D::MODE_KINEMATIC) {
set_body_mode(PhysicsServer2D::BODY_MODE_KINEMATIC);
} else if (rigid_mode == RigidDynamicBody2D::MODE_DYNAMIC_LOCKED) {
set_body_mode(PhysicsServer2D::BODY_MODE_DYNAMIC_LOCKED);
} else {
// Default to Dynamic.
set_body_mode(PhysicsServer2D::BODY_MODE_DYNAMIC);
}
// Apply the correct mode.
_apply_body_mode();
_internal_simulate_physics = true;
set_physics_process_internal(true);

View file

@ -436,7 +436,7 @@ void RigidDynamicBody2D::_body_state_changed_callback(void *p_instance, PhysicsD
void RigidDynamicBody2D::_body_state_changed(PhysicsDirectBodyState2D *p_state) {
set_block_transform_notify(true); // don't want notify (would feedback loop)
if (mode != MODE_KINEMATIC) {
if (!freeze || freeze_mode != FREEZE_MODE_KINEMATIC) {
set_global_transform(p_state->get_transform());
}
@ -530,29 +530,60 @@ void RigidDynamicBody2D::_body_state_changed(PhysicsDirectBodyState2D *p_state)
}
}
void RigidDynamicBody2D::set_mode(Mode p_mode) {
mode = p_mode;
switch (p_mode) {
case MODE_DYNAMIC: {
set_body_mode(PhysicsServer2D::BODY_MODE_DYNAMIC);
} break;
case MODE_STATIC: {
void RigidDynamicBody2D::_apply_body_mode() {
if (freeze) {
switch (freeze_mode) {
case FREEZE_MODE_STATIC: {
set_body_mode(PhysicsServer2D::BODY_MODE_STATIC);
} break;
case MODE_KINEMATIC: {
case FREEZE_MODE_KINEMATIC: {
set_body_mode(PhysicsServer2D::BODY_MODE_KINEMATIC);
} break;
case MODE_DYNAMIC_LOCKED: {
set_body_mode(PhysicsServer2D::BODY_MODE_DYNAMIC_LOCKED);
} break;
}
} else if (lock_rotation) {
set_body_mode(PhysicsServer2D::BODY_MODE_DYNAMIC_LINEAR);
} else {
set_body_mode(PhysicsServer2D::BODY_MODE_DYNAMIC);
}
}
RigidDynamicBody2D::Mode RigidDynamicBody2D::get_mode() const {
return mode;
void RigidDynamicBody2D::set_lock_rotation_enabled(bool p_lock_rotation) {
if (p_lock_rotation == lock_rotation) {
return;
}
lock_rotation = p_lock_rotation;
_apply_body_mode();
}
bool RigidDynamicBody2D::is_lock_rotation_enabled() const {
return lock_rotation;
}
void RigidDynamicBody2D::set_freeze_enabled(bool p_freeze) {
if (p_freeze == freeze) {
return;
}
freeze = p_freeze;
_apply_body_mode();
}
bool RigidDynamicBody2D::is_freeze_enabled() const {
return freeze;
}
void RigidDynamicBody2D::set_freeze_mode(FreezeMode p_freeze_mode) {
if (p_freeze_mode == freeze_mode) {
return;
}
freeze_mode = p_freeze_mode;
_apply_body_mode();
}
RigidDynamicBody2D::FreezeMode RigidDynamicBody2D::get_freeze_mode() const {
return freeze_mode;
}
void RigidDynamicBody2D::set_mass(real_t p_mass) {
@ -850,17 +881,14 @@ TypedArray<String> RigidDynamicBody2D::get_configuration_warnings() const {
TypedArray<String> warnings = CollisionObject2D::get_configuration_warnings();
if ((get_mode() == MODE_DYNAMIC || get_mode() == MODE_DYNAMIC_LOCKED) && (ABS(t.elements[0].length() - 1.0) > 0.05 || ABS(t.elements[1].length() - 1.0) > 0.05)) {
warnings.push_back(TTR("Size changes to RigidDynamicBody2D (in dynamic modes) will be overridden by the physics engine when running.\nChange the size in children collision shapes instead."));
if (ABS(t.elements[0].length() - 1.0) > 0.05 || ABS(t.elements[1].length() - 1.0) > 0.05) {
warnings.push_back(TTR("Size changes to RigidDynamicBody2D will be overridden by the physics engine when running.\nChange the size in children collision shapes instead."));
}
return warnings;
}
void RigidDynamicBody2D::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_mode", "mode"), &RigidDynamicBody2D::set_mode);
ClassDB::bind_method(D_METHOD("get_mode"), &RigidDynamicBody2D::get_mode);
ClassDB::bind_method(D_METHOD("set_mass", "mass"), &RigidDynamicBody2D::set_mass);
ClassDB::bind_method(D_METHOD("get_mass"), &RigidDynamicBody2D::get_mass);
@ -924,11 +952,19 @@ void RigidDynamicBody2D::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_can_sleep", "able_to_sleep"), &RigidDynamicBody2D::set_can_sleep);
ClassDB::bind_method(D_METHOD("is_able_to_sleep"), &RigidDynamicBody2D::is_able_to_sleep);
ClassDB::bind_method(D_METHOD("set_lock_rotation_enabled", "lock_rotation"), &RigidDynamicBody2D::set_lock_rotation_enabled);
ClassDB::bind_method(D_METHOD("is_lock_rotation_enabled"), &RigidDynamicBody2D::is_lock_rotation_enabled);
ClassDB::bind_method(D_METHOD("set_freeze_enabled", "freeze_mode"), &RigidDynamicBody2D::set_freeze_enabled);
ClassDB::bind_method(D_METHOD("is_freeze_enabled"), &RigidDynamicBody2D::is_freeze_enabled);
ClassDB::bind_method(D_METHOD("set_freeze_mode", "freeze_mode"), &RigidDynamicBody2D::set_freeze_mode);
ClassDB::bind_method(D_METHOD("get_freeze_mode"), &RigidDynamicBody2D::get_freeze_mode);
ClassDB::bind_method(D_METHOD("get_colliding_bodies"), &RigidDynamicBody2D::get_colliding_bodies);
GDVIRTUAL_BIND(_integrate_forces, "state");
ADD_PROPERTY(PropertyInfo(Variant::INT, "mode", PROPERTY_HINT_ENUM, "Dynamic,Static,DynamicLocked,Kinematic"), "set_mode", "get_mode");
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "mass", PROPERTY_HINT_RANGE, "0.01,1000,0.01,or_greater,exp"), "set_mass", "get_mass");
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "inertia", PROPERTY_HINT_RANGE, "0,1000,0.01,or_greater,exp"), "set_inertia", "get_inertia");
ADD_PROPERTY(PropertyInfo(Variant::INT, "center_of_mass_mode", PROPERTY_HINT_ENUM, "Auto,Custom", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_UPDATE_ALL_IF_MODIFIED), "set_center_of_mass_mode", "get_center_of_mass_mode");
@ -942,6 +978,9 @@ void RigidDynamicBody2D::_bind_methods() {
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "contact_monitor"), "set_contact_monitor", "is_contact_monitor_enabled");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "sleeping"), "set_sleeping", "is_sleeping");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "can_sleep"), "set_can_sleep", "is_able_to_sleep");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "lock_rotation"), "set_lock_rotation_enabled", "is_lock_rotation_enabled");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "freeze"), "set_freeze_enabled", "is_freeze_enabled");
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::FLOAT, "linear_damp", PROPERTY_HINT_RANGE, "-1,100,0.001,or_greater"), "set_linear_damp", "get_linear_damp");
@ -958,10 +997,8 @@ void RigidDynamicBody2D::_bind_methods() {
ADD_SIGNAL(MethodInfo("body_exited", PropertyInfo(Variant::OBJECT, "body", PROPERTY_HINT_RESOURCE_TYPE, "Node")));
ADD_SIGNAL(MethodInfo("sleeping_state_changed"));
BIND_ENUM_CONSTANT(MODE_DYNAMIC);
BIND_ENUM_CONSTANT(MODE_STATIC);
BIND_ENUM_CONSTANT(MODE_DYNAMIC_LOCKED);
BIND_ENUM_CONSTANT(MODE_KINEMATIC);
BIND_ENUM_CONSTANT(FREEZE_MODE_STATIC);
BIND_ENUM_CONSTANT(FREEZE_MODE_KINEMATIC);
BIND_ENUM_CONSTANT(CENTER_OF_MASS_MODE_AUTO);
BIND_ENUM_CONSTANT(CENTER_OF_MASS_MODE_CUSTOM);

View file

@ -117,11 +117,9 @@ class RigidDynamicBody2D : public PhysicsBody2D {
GDCLASS(RigidDynamicBody2D, PhysicsBody2D);
public:
enum Mode {
MODE_DYNAMIC,
MODE_STATIC,
MODE_DYNAMIC_LOCKED,
MODE_KINEMATIC,
enum FreezeMode {
FREEZE_MODE_STATIC,
FREEZE_MODE_KINEMATIC,
};
enum CenterOfMassMode {
@ -137,7 +135,9 @@ public:
private:
bool can_sleep = true;
Mode mode = MODE_DYNAMIC;
bool lock_rotation = false;
bool freeze = false;
FreezeMode freeze_mode = FREEZE_MODE_STATIC;
real_t mass = 1.0;
real_t inertia = 0.0;
@ -211,9 +211,17 @@ protected:
GDVIRTUAL1(_integrate_forces, PhysicsDirectBodyState2D *)
void _apply_body_mode();
public:
void set_mode(Mode p_mode);
Mode get_mode() const;
void set_lock_rotation_enabled(bool p_lock_rotation);
bool is_lock_rotation_enabled() const;
void set_freeze_enabled(bool p_freeze);
bool is_freeze_enabled() const;
void set_freeze_mode(FreezeMode p_freeze_mode);
FreezeMode get_freeze_mode() const;
void set_mass(real_t p_mass);
real_t get_mass() const;
@ -290,7 +298,7 @@ private:
void _reload_physics_characteristics();
};
VARIANT_ENUM_CAST(RigidDynamicBody2D::Mode);
VARIANT_ENUM_CAST(RigidDynamicBody2D::FreezeMode);
VARIANT_ENUM_CAST(RigidDynamicBody2D::CenterOfMassMode);
VARIANT_ENUM_CAST(RigidDynamicBody2D::CCDMode);

View file

@ -124,8 +124,7 @@ TypedArray<String> CollisionShape3D::get_configuration_warnings() const {
if (shape.is_valid() &&
Object::cast_to<RigidDynamicBody3D>(get_parent()) &&
Object::cast_to<ConcavePolygonShape3D>(*shape) &&
Object::cast_to<RigidDynamicBody3D>(get_parent())->get_mode() != RigidDynamicBody3D::MODE_STATIC) {
Object::cast_to<ConcavePolygonShape3D>(*shape)) {
warnings.push_back(TTR("ConcavePolygonShape3D doesn't support RigidDynamicBody3D in another mode than static."));
}

View file

@ -605,27 +605,60 @@ void RigidDynamicBody3D::_notification(int p_what) {
#endif
}
void RigidDynamicBody3D::set_mode(Mode p_mode) {
mode = p_mode;
switch (p_mode) {
case MODE_DYNAMIC: {
set_body_mode(PhysicsServer3D::BODY_MODE_DYNAMIC);
} break;
case MODE_STATIC: {
void RigidDynamicBody3D::_apply_body_mode() {
if (freeze) {
switch (freeze_mode) {
case FREEZE_MODE_STATIC: {
set_body_mode(PhysicsServer3D::BODY_MODE_STATIC);
} break;
case MODE_DYNAMIC_LOCKED: {
set_body_mode(PhysicsServer3D::BODY_MODE_DYNAMIC_LOCKED);
} break;
case MODE_KINEMATIC: {
case FREEZE_MODE_KINEMATIC: {
set_body_mode(PhysicsServer3D::BODY_MODE_KINEMATIC);
} break;
}
update_configuration_warnings();
} else if (lock_rotation) {
set_body_mode(PhysicsServer3D::BODY_MODE_DYNAMIC_LINEAR);
} else {
set_body_mode(PhysicsServer3D::BODY_MODE_DYNAMIC);
}
}
RigidDynamicBody3D::Mode RigidDynamicBody3D::get_mode() const {
return mode;
void RigidDynamicBody3D::set_lock_rotation_enabled(bool p_lock_rotation) {
if (p_lock_rotation == lock_rotation) {
return;
}
lock_rotation = p_lock_rotation;
_apply_body_mode();
}
bool RigidDynamicBody3D::is_lock_rotation_enabled() const {
return lock_rotation;
}
void RigidDynamicBody3D::set_freeze_enabled(bool p_freeze) {
if (p_freeze == freeze) {
return;
}
freeze = p_freeze;
_apply_body_mode();
}
bool RigidDynamicBody3D::is_freeze_enabled() const {
return freeze;
}
void RigidDynamicBody3D::set_freeze_mode(FreezeMode p_freeze_mode) {
if (p_freeze_mode == freeze_mode) {
return;
}
freeze_mode = p_freeze_mode;
_apply_body_mode();
}
RigidDynamicBody3D::FreezeMode RigidDynamicBody3D::get_freeze_mode() const {
return freeze_mode;
}
void RigidDynamicBody3D::set_mass(real_t p_mass) {
@ -898,17 +931,14 @@ TypedArray<String> RigidDynamicBody3D::get_configuration_warnings() const {
TypedArray<String> warnings = Node::get_configuration_warnings();
if ((get_mode() == MODE_DYNAMIC || get_mode() == MODE_DYNAMIC_LOCKED) && (ABS(t.basis.get_axis(0).length() - 1.0) > 0.05 || ABS(t.basis.get_axis(1).length() - 1.0) > 0.05 || ABS(t.basis.get_axis(2).length() - 1.0) > 0.05)) {
warnings.push_back(TTR("Size changes to RigidDynamicBody (in dynamic modes) will be overridden by the physics engine when running.\nChange the size in children collision shapes instead."));
if (ABS(t.basis.get_axis(0).length() - 1.0) > 0.05 || ABS(t.basis.get_axis(1).length() - 1.0) > 0.05 || ABS(t.basis.get_axis(2).length() - 1.0) > 0.05) {
warnings.push_back(TTR("Size changes to RigidDynamicBody will be overridden by the physics engine when running.\nChange the size in children collision shapes instead."));
}
return warnings;
}
void RigidDynamicBody3D::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_mode", "mode"), &RigidDynamicBody3D::set_mode);
ClassDB::bind_method(D_METHOD("get_mode"), &RigidDynamicBody3D::get_mode);
ClassDB::bind_method(D_METHOD("set_mass", "mass"), &RigidDynamicBody3D::set_mass);
ClassDB::bind_method(D_METHOD("get_mass"), &RigidDynamicBody3D::get_mass);
@ -969,11 +999,19 @@ void RigidDynamicBody3D::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_can_sleep", "able_to_sleep"), &RigidDynamicBody3D::set_can_sleep);
ClassDB::bind_method(D_METHOD("is_able_to_sleep"), &RigidDynamicBody3D::is_able_to_sleep);
ClassDB::bind_method(D_METHOD("set_lock_rotation_enabled", "lock_rotation"), &RigidDynamicBody3D::set_lock_rotation_enabled);
ClassDB::bind_method(D_METHOD("is_lock_rotation_enabled"), &RigidDynamicBody3D::is_lock_rotation_enabled);
ClassDB::bind_method(D_METHOD("set_freeze_enabled", "freeze_mode"), &RigidDynamicBody3D::set_freeze_enabled);
ClassDB::bind_method(D_METHOD("is_freeze_enabled"), &RigidDynamicBody3D::is_freeze_enabled);
ClassDB::bind_method(D_METHOD("set_freeze_mode", "freeze_mode"), &RigidDynamicBody3D::set_freeze_mode);
ClassDB::bind_method(D_METHOD("get_freeze_mode"), &RigidDynamicBody3D::get_freeze_mode);
ClassDB::bind_method(D_METHOD("get_colliding_bodies"), &RigidDynamicBody3D::get_colliding_bodies);
GDVIRTUAL_BIND(_integrate_forces, "state");
ADD_PROPERTY(PropertyInfo(Variant::INT, "mode", PROPERTY_HINT_ENUM, "Dynamic,Static,DynamicLocked,Kinematic"), "set_mode", "get_mode");
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "mass", PROPERTY_HINT_RANGE, "0.01,1000,0.01,or_greater,exp"), "set_mass", "get_mass");
ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "inertia", PROPERTY_HINT_RANGE, "0,1000,0.01,or_greater,exp"), "set_inertia", "get_inertia");
ADD_PROPERTY(PropertyInfo(Variant::INT, "center_of_mass_mode", PROPERTY_HINT_ENUM, "Auto,Custom", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_UPDATE_ALL_IF_MODIFIED), "set_center_of_mass_mode", "get_center_of_mass_mode");
@ -987,6 +1025,9 @@ void RigidDynamicBody3D::_bind_methods() {
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "contact_monitor"), "set_contact_monitor", "is_contact_monitor_enabled");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "sleeping"), "set_sleeping", "is_sleeping");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "can_sleep"), "set_can_sleep", "is_able_to_sleep");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "lock_rotation"), "set_lock_rotation_enabled", "is_lock_rotation_enabled");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "freeze"), "set_freeze_enabled", "is_freeze_enabled");
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::VECTOR3, "linear_velocity"), "set_linear_velocity", "get_linear_velocity");
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "linear_damp", PROPERTY_HINT_RANGE, "-1,100,0.001,or_greater"), "set_linear_damp", "get_linear_damp");
@ -1000,10 +1041,8 @@ void RigidDynamicBody3D::_bind_methods() {
ADD_SIGNAL(MethodInfo("body_exited", PropertyInfo(Variant::OBJECT, "body", PROPERTY_HINT_RESOURCE_TYPE, "Node")));
ADD_SIGNAL(MethodInfo("sleeping_state_changed"));
BIND_ENUM_CONSTANT(MODE_DYNAMIC);
BIND_ENUM_CONSTANT(MODE_STATIC);
BIND_ENUM_CONSTANT(MODE_DYNAMIC_LOCKED);
BIND_ENUM_CONSTANT(MODE_KINEMATIC);
BIND_ENUM_CONSTANT(FREEZE_MODE_STATIC);
BIND_ENUM_CONSTANT(FREEZE_MODE_KINEMATIC);
BIND_ENUM_CONSTANT(CENTER_OF_MASS_MODE_AUTO);
BIND_ENUM_CONSTANT(CENTER_OF_MASS_MODE_CUSTOM);

View file

@ -133,11 +133,9 @@ class RigidDynamicBody3D : public PhysicsBody3D {
GDCLASS(RigidDynamicBody3D, PhysicsBody3D);
public:
enum Mode {
MODE_DYNAMIC,
MODE_STATIC,
MODE_DYNAMIC_LOCKED,
MODE_KINEMATIC,
enum FreezeMode {
FREEZE_MODE_STATIC,
FREEZE_MODE_KINEMATIC,
};
enum CenterOfMassMode {
@ -145,11 +143,11 @@ public:
CENTER_OF_MASS_MODE_CUSTOM,
};
GDVIRTUAL1(_integrate_forces, PhysicsDirectBodyState3D *)
protected:
private:
bool can_sleep = true;
Mode mode = MODE_DYNAMIC;
bool lock_rotation = false;
bool freeze = false;
FreezeMode freeze_mode = FREEZE_MODE_STATIC;
real_t mass = 1.0;
Vector3 inertia;
@ -214,16 +212,28 @@ protected:
void _body_inout(int p_status, const RID &p_body, ObjectID p_instance, int p_body_shape, int p_local_shape);
static void _body_state_changed_callback(void *p_instance, PhysicsDirectBodyState3D *p_state);
virtual void _body_state_changed(PhysicsDirectBodyState3D *p_state);
protected:
void _notification(int p_what);
static void _bind_methods();
virtual void _validate_property(PropertyInfo &property) const override;
GDVIRTUAL1(_integrate_forces, PhysicsDirectBodyState3D *)
virtual void _body_state_changed(PhysicsDirectBodyState3D *p_state);
void _apply_body_mode();
public:
void set_mode(Mode p_mode);
Mode get_mode() const;
void set_lock_rotation_enabled(bool p_lock_rotation);
bool is_lock_rotation_enabled() const;
void set_freeze_enabled(bool p_freeze);
bool is_freeze_enabled() const;
void set_freeze_mode(FreezeMode p_freeze_mode);
FreezeMode get_freeze_mode() const;
void set_mass(real_t p_mass);
real_t get_mass() const;
@ -298,7 +308,7 @@ private:
void _reload_physics_characteristics();
};
VARIANT_ENUM_CAST(RigidDynamicBody3D::Mode);
VARIANT_ENUM_CAST(RigidDynamicBody3D::FreezeMode);
VARIANT_ENUM_CAST(RigidDynamicBody3D::CenterOfMassMode);
class KinematicCollision3D;

View file

@ -470,7 +470,7 @@ real_t VehicleBody3D::_ray_cast(int p_idx, PhysicsDirectBodyState3D *s) {
}
void VehicleBody3D::_update_suspension(PhysicsDirectBodyState3D *s) {
real_t chassisMass = mass;
real_t chassisMass = get_mass();
for (int w_it = 0; w_it < wheels.size(); w_it++) {
VehicleWheel3D &wheel_info = *wheels[w_it];
@ -558,7 +558,7 @@ void VehicleBody3D::_resolve_single_bilateral(PhysicsDirectBodyState3D *s, const
rel_pos2,
normal,
s->get_inverse_inertia_tensor().get_main_diagonal(),
1.0 / mass,
1.0 / get_mass(),
b2invinertia,
b2invmass);
@ -584,7 +584,7 @@ void VehicleBody3D::_resolve_single_bilateral(PhysicsDirectBodyState3D *s, const
#define ONLY_USE_LINEAR_MASS
#ifdef ONLY_USE_LINEAR_MASS
real_t massTerm = real_t(1.) / ((1.0 / mass) + b2invmass);
real_t massTerm = real_t(1.) / ((1.0 / get_mass()) + b2invmass);
impulse = -contactDamping * rel_vel * massTerm;
#else
real_t velocityImpulse = -contactDamping * rel_vel * jacDiagABInv;

View file

@ -113,7 +113,7 @@ void Body2DSW::update_mass_properties() {
_inv_inertia = 0;
_inv_mass = 0;
} break;
case PhysicsServer2D::BODY_MODE_DYNAMIC_LOCKED: {
case PhysicsServer2D::BODY_MODE_DYNAMIC_LINEAR: {
_inv_inertia = 0;
_inv_mass = 1.0 / mass;
@ -257,7 +257,7 @@ void Body2DSW::set_mode(PhysicsServer2D::BodyMode p_mode) {
set_active(true);
} break;
case PhysicsServer2D::BODY_MODE_DYNAMIC_LOCKED: {
case PhysicsServer2D::BODY_MODE_DYNAMIC_LINEAR: {
_inv_mass = mass > 0 ? (1.0 / mass) : 0;
_inv_inertia = 0;
angular_velocity = 0;

View file

@ -154,7 +154,7 @@ void Body3DSW::update_mass_properties() {
_inv_inertia = Vector3();
_inv_mass = 0;
} break;
case PhysicsServer3D::BODY_MODE_DYNAMIC_LOCKED: {
case PhysicsServer3D::BODY_MODE_DYNAMIC_LINEAR: {
_inv_inertia_tensor.set_zero();
_inv_mass = 1.0 / mass;
@ -310,7 +310,7 @@ void Body3DSW::set_mode(PhysicsServer3D::BodyMode p_mode) {
set_active(true);
} break;
case PhysicsServer3D::BODY_MODE_DYNAMIC_LOCKED: {
case PhysicsServer3D::BODY_MODE_DYNAMIC_LINEAR: {
_inv_mass = mass > 0 ? (1.0 / mass) : 0;
_inv_inertia = Vector3();
angular_velocity = Vector3();

View file

@ -699,7 +699,7 @@ void PhysicsServer2D::_bind_methods() {
BIND_ENUM_CONSTANT(BODY_MODE_STATIC);
BIND_ENUM_CONSTANT(BODY_MODE_KINEMATIC);
BIND_ENUM_CONSTANT(BODY_MODE_DYNAMIC);
BIND_ENUM_CONSTANT(BODY_MODE_DYNAMIC_LOCKED);
BIND_ENUM_CONSTANT(BODY_MODE_DYNAMIC_LINEAR);
BIND_ENUM_CONSTANT(BODY_PARAM_BOUNCE);
BIND_ENUM_CONSTANT(BODY_PARAM_FRICTION);

View file

@ -349,7 +349,7 @@ public:
BODY_MODE_STATIC,
BODY_MODE_KINEMATIC,
BODY_MODE_DYNAMIC,
BODY_MODE_DYNAMIC_LOCKED,
BODY_MODE_DYNAMIC_LINEAR,
};
virtual RID body_create() = 0;

View file

@ -834,7 +834,7 @@ void PhysicsServer3D::_bind_methods() {
BIND_ENUM_CONSTANT(BODY_MODE_STATIC);
BIND_ENUM_CONSTANT(BODY_MODE_KINEMATIC);
BIND_ENUM_CONSTANT(BODY_MODE_DYNAMIC);
BIND_ENUM_CONSTANT(BODY_MODE_DYNAMIC_LOCKED);
BIND_ENUM_CONSTANT(BODY_MODE_DYNAMIC_LINEAR);
BIND_ENUM_CONSTANT(BODY_PARAM_BOUNCE);
BIND_ENUM_CONSTANT(BODY_PARAM_FRICTION);

View file

@ -360,7 +360,7 @@ public:
BODY_MODE_STATIC,
BODY_MODE_KINEMATIC,
BODY_MODE_DYNAMIC,
BODY_MODE_DYNAMIC_LOCKED,
BODY_MODE_DYNAMIC_LINEAR,
};
virtual RID body_create() = 0;

View file

@ -361,7 +361,7 @@ public:
RID mesh_instance = vs->instance_create2(capsule_mesh, scenario);
character = ps->body_create();
ps->body_set_mode(character, PhysicsServer3D::BODY_MODE_DYNAMIC_LOCKED);
ps->body_set_mode(character, PhysicsServer3D::BODY_MODE_DYNAMIC_LINEAR);
ps->body_set_space(character, space);
//todo add space
ps->body_add_shape(character, capsule_shape);