From aed5ea946036e6c2c8a0165265bb6b038b3db8cb Mon Sep 17 00:00:00 2001 From: Aaron Franke Date: Wed, 8 Nov 2023 16:56:19 -0600 Subject: [PATCH] Expose a method to get gravity for any physics body --- doc/classes/PhysicsBody2D.xml | 6 ++++++ doc/classes/PhysicsBody3D.xml | 6 ++++++ .../script_templates/CharacterBody2D/basic_movement.gd | 5 +---- .../script_templates/CharacterBody3D/basic_movement.gd | 5 +---- .../script_templates/CharacterBody2D/basic_movement.cs | 9 +++++---- .../script_templates/CharacterBody3D/basic_movement.cs | 9 +++++---- scene/2d/physics_body_2d.cpp | 5 +++++ scene/2d/physics_body_2d.h | 1 + scene/3d/physics_body_3d.cpp | 5 +++++ scene/3d/physics_body_3d.h | 1 + 10 files changed, 36 insertions(+), 16 deletions(-) diff --git a/doc/classes/PhysicsBody2D.xml b/doc/classes/PhysicsBody2D.xml index adfdfdc4452e..eef159d44dd0 100644 --- a/doc/classes/PhysicsBody2D.xml +++ b/doc/classes/PhysicsBody2D.xml @@ -23,6 +23,12 @@ Returns an array of nodes that were added as collision exceptions for this body. + + + + Returns the gravity vector computed from all sources that can affect the body, including all gravity overrides from [Area2D] nodes and the global world gravity. + + diff --git a/doc/classes/PhysicsBody3D.xml b/doc/classes/PhysicsBody3D.xml index ff994fe6c518..866b3e298c9f 100644 --- a/doc/classes/PhysicsBody3D.xml +++ b/doc/classes/PhysicsBody3D.xml @@ -31,6 +31,12 @@ Returns an array of nodes that were added as collision exceptions for this body. + + + + Returns the gravity vector computed from all sources that can affect the body, including all gravity overrides from [Area3D] nodes and the global world gravity. + + diff --git a/modules/gdscript/editor/script_templates/CharacterBody2D/basic_movement.gd b/modules/gdscript/editor/script_templates/CharacterBody2D/basic_movement.gd index 28ab080dd2ca..bd4816827fdd 100644 --- a/modules/gdscript/editor/script_templates/CharacterBody2D/basic_movement.gd +++ b/modules/gdscript/editor/script_templates/CharacterBody2D/basic_movement.gd @@ -6,14 +6,11 @@ extends _BASE_ const SPEED = 300.0 const JUMP_VELOCITY = -400.0 -# Get the gravity from the project settings to be synced with RigidBody nodes. -var gravity: int = ProjectSettings.get_setting("physics/2d/default_gravity") - func _physics_process(delta: float) -> void: # Add the gravity. if not is_on_floor(): - velocity.y += gravity * delta + velocity += get_gravity() * delta # Handle jump. if Input.is_action_just_pressed("ui_accept") and is_on_floor(): diff --git a/modules/gdscript/editor/script_templates/CharacterBody3D/basic_movement.gd b/modules/gdscript/editor/script_templates/CharacterBody3D/basic_movement.gd index 9b0e4be4ed3a..f9c4f70a24c0 100644 --- a/modules/gdscript/editor/script_templates/CharacterBody3D/basic_movement.gd +++ b/modules/gdscript/editor/script_templates/CharacterBody3D/basic_movement.gd @@ -6,14 +6,11 @@ extends _BASE_ const SPEED = 5.0 const JUMP_VELOCITY = 4.5 -# Get the gravity from the project settings to be synced with RigidBody nodes. -var gravity: float = ProjectSettings.get_setting("physics/3d/default_gravity") - func _physics_process(delta: float) -> void: # Add the gravity. if not is_on_floor(): - velocity.y -= gravity * delta + velocity += get_gravity() * delta # Handle jump. if Input.is_action_just_pressed("ui_accept") and is_on_floor(): diff --git a/modules/mono/editor/script_templates/CharacterBody2D/basic_movement.cs b/modules/mono/editor/script_templates/CharacterBody2D/basic_movement.cs index 87468fb43313..698157c6b4de 100644 --- a/modules/mono/editor/script_templates/CharacterBody2D/basic_movement.cs +++ b/modules/mono/editor/script_templates/CharacterBody2D/basic_movement.cs @@ -8,20 +8,21 @@ public partial class _CLASS_ : _BASE_ public const float Speed = 300.0f; public const float JumpVelocity = -400.0f; - // Get the gravity from the project settings to be synced with RigidBody nodes. - public float gravity = ProjectSettings.GetSetting("physics/2d/default_gravity").AsSingle(); - public override void _PhysicsProcess(double delta) { Vector2 velocity = Velocity; // Add the gravity. if (!IsOnFloor()) - velocity.Y += gravity * (float)delta; + { + velocity += GetGravity() * (float)delta; + } // Handle Jump. if (Input.IsActionJustPressed("ui_accept") && IsOnFloor()) + { velocity.Y = JumpVelocity; + } // Get the input direction and handle the movement/deceleration. // As good practice, you should replace UI actions with custom gameplay actions. diff --git a/modules/mono/editor/script_templates/CharacterBody3D/basic_movement.cs b/modules/mono/editor/script_templates/CharacterBody3D/basic_movement.cs index ddeb9d7e0004..30dabd31d9f3 100644 --- a/modules/mono/editor/script_templates/CharacterBody3D/basic_movement.cs +++ b/modules/mono/editor/script_templates/CharacterBody3D/basic_movement.cs @@ -8,20 +8,21 @@ public partial class _CLASS_ : _BASE_ public const float Speed = 5.0f; public const float JumpVelocity = 4.5f; - // Get the gravity from the project settings to be synced with RigidBody nodes. - public float gravity = ProjectSettings.GetSetting("physics/3d/default_gravity").AsSingle(); - public override void _PhysicsProcess(double delta) { Vector3 velocity = Velocity; // Add the gravity. if (!IsOnFloor()) - velocity.Y -= gravity * (float)delta; + { + velocity += GetGravity() * (float)delta; + } // Handle Jump. if (Input.IsActionJustPressed("ui_accept") && IsOnFloor()) + { velocity.Y = JumpVelocity; + } // Get the input direction and handle the movement/deceleration. // As good practice, you should replace UI actions with custom gameplay actions. diff --git a/scene/2d/physics_body_2d.cpp b/scene/2d/physics_body_2d.cpp index 6af5a8dd8090..7a131916e834 100644 --- a/scene/2d/physics_body_2d.cpp +++ b/scene/2d/physics_body_2d.cpp @@ -35,6 +35,7 @@ void PhysicsBody2D::_bind_methods() { 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)); 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)); + ClassDB::bind_method(D_METHOD("get_gravity"), &PhysicsBody2D::get_gravity); ClassDB::bind_method(D_METHOD("get_collision_exceptions"), &PhysicsBody2D::get_collision_exceptions); ClassDB::bind_method(D_METHOD("add_collision_exception_with", "body"), &PhysicsBody2D::add_collision_exception_with); @@ -145,6 +146,10 @@ bool PhysicsBody2D::test_move(const Transform2D &p_from, const Vector2 &p_motion return PhysicsServer2D::get_singleton()->body_test_motion(get_rid(), parameters, r); } +Vector2 PhysicsBody2D::get_gravity() const { + return PhysicsServer2D::get_singleton()->body_get_direct_state(get_rid())->get_total_gravity(); +} + TypedArray PhysicsBody2D::get_collision_exceptions() { List exceptions; PhysicsServer2D::get_singleton()->body_get_collision_exceptions(get_rid(), &exceptions); diff --git a/scene/2d/physics_body_2d.h b/scene/2d/physics_body_2d.h index 208e72c40ffc..62636b02f453 100644 --- a/scene/2d/physics_body_2d.h +++ b/scene/2d/physics_body_2d.h @@ -52,6 +52,7 @@ class PhysicsBody2D : public CollisionObject2D { public: bool move_and_collide(const PhysicsServer2D::MotionParameters &p_parameters, PhysicsServer2D::MotionResult &r_result, bool p_test_only = false, bool p_cancel_sliding = true); bool test_move(const Transform2D &p_from, const Vector2 &p_motion, const Ref &r_collision = Ref(), real_t p_margin = 0.08, bool p_recovery_as_collision = false); + Vector2 get_gravity() const; TypedArray get_collision_exceptions(); void add_collision_exception_with(Node *p_node); //must be physicsbody diff --git a/scene/3d/physics_body_3d.cpp b/scene/3d/physics_body_3d.cpp index ed64c1656400..b11dfec10408 100644 --- a/scene/3d/physics_body_3d.cpp +++ b/scene/3d/physics_body_3d.cpp @@ -35,6 +35,7 @@ void PhysicsBody3D::_bind_methods() { ClassDB::bind_method(D_METHOD("move_and_collide", "motion", "test_only", "safe_margin", "recovery_as_collision", "max_collisions"), &PhysicsBody3D::_move, DEFVAL(false), DEFVAL(0.001), DEFVAL(false), DEFVAL(1)); ClassDB::bind_method(D_METHOD("test_move", "from", "motion", "collision", "safe_margin", "recovery_as_collision", "max_collisions"), &PhysicsBody3D::test_move, DEFVAL(Variant()), DEFVAL(0.001), DEFVAL(false), DEFVAL(1)); + ClassDB::bind_method(D_METHOD("get_gravity"), &PhysicsBody3D::get_gravity); ClassDB::bind_method(D_METHOD("set_axis_lock", "axis", "lock"), &PhysicsBody3D::set_axis_lock); ClassDB::bind_method(D_METHOD("get_axis_lock", "axis"), &PhysicsBody3D::get_axis_lock); @@ -186,6 +187,10 @@ bool PhysicsBody3D::test_move(const Transform3D &p_from, const Vector3 &p_motion return PhysicsServer3D::get_singleton()->body_test_motion(get_rid(), parameters, r); } +Vector3 PhysicsBody3D::get_gravity() const { + return PhysicsServer3D::get_singleton()->body_get_direct_state(get_rid())->get_total_gravity(); +} + void PhysicsBody3D::set_axis_lock(PhysicsServer3D::BodyAxis p_axis, bool p_lock) { if (p_lock) { locked_axis |= p_axis; diff --git a/scene/3d/physics_body_3d.h b/scene/3d/physics_body_3d.h index e8d5ef2103ca..e8373d59078f 100644 --- a/scene/3d/physics_body_3d.h +++ b/scene/3d/physics_body_3d.h @@ -55,6 +55,7 @@ class PhysicsBody3D : public CollisionObject3D { public: bool move_and_collide(const PhysicsServer3D::MotionParameters &p_parameters, PhysicsServer3D::MotionResult &r_result, bool p_test_only = false, bool p_cancel_sliding = true); bool test_move(const Transform3D &p_from, const Vector3 &p_motion, const Ref &r_collision = Ref(), real_t p_margin = 0.001, bool p_recovery_as_collision = false, int p_max_collisions = 1); + Vector3 get_gravity() const; void set_axis_lock(PhysicsServer3D::BodyAxis p_axis, bool p_lock); bool get_axis_lock(PhysicsServer3D::BodyAxis p_axis) const;