summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAaron Franke <arnfranke@yahoo.com>2023-11-08 16:56:19 -0600
committerAaron Franke <arnfranke@yahoo.com>2024-01-30 23:57:38 -0600
commitaed5ea946036e6c2c8a0165265bb6b038b3db8cb (patch)
tree97c2e825533617a2c3b12a45e6c5a09e06886b0b
parent313f623b9d102cc8c411ae7cab9518f98c2f87f2 (diff)
downloadredot-engine-aed5ea946036e6c2c8a0165265bb6b038b3db8cb.tar.gz
Expose a method to get gravity for any physics body
-rw-r--r--doc/classes/PhysicsBody2D.xml6
-rw-r--r--doc/classes/PhysicsBody3D.xml6
-rw-r--r--modules/gdscript/editor/script_templates/CharacterBody2D/basic_movement.gd5
-rw-r--r--modules/gdscript/editor/script_templates/CharacterBody3D/basic_movement.gd5
-rw-r--r--modules/mono/editor/script_templates/CharacterBody2D/basic_movement.cs9
-rw-r--r--modules/mono/editor/script_templates/CharacterBody3D/basic_movement.cs9
-rw-r--r--scene/2d/physics_body_2d.cpp5
-rw-r--r--scene/2d/physics_body_2d.h1
-rw-r--r--scene/3d/physics_body_3d.cpp5
-rw-r--r--scene/3d/physics_body_3d.h1
10 files changed, 36 insertions, 16 deletions
diff --git a/doc/classes/PhysicsBody2D.xml b/doc/classes/PhysicsBody2D.xml
index adfdfdc445..eef159d44d 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.
</description>
</method>
+ <method name="get_gravity" qualifiers="const">
+ <return type="Vector2" />
+ <description>
+ 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.
+ </description>
+ </method>
<method name="move_and_collide">
<return type="KinematicCollision2D" />
<param index="0" name="motion" type="Vector2" />
diff --git a/doc/classes/PhysicsBody3D.xml b/doc/classes/PhysicsBody3D.xml
index ff994fe6c5..866b3e298c 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.
</description>
</method>
+ <method name="get_gravity" qualifiers="const">
+ <return type="Vector3" />
+ <description>
+ 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.
+ </description>
+ </method>
<method name="move_and_collide">
<return type="KinematicCollision3D" />
<param index="0" name="motion" type="Vector3" />
diff --git a/modules/gdscript/editor/script_templates/CharacterBody2D/basic_movement.gd b/modules/gdscript/editor/script_templates/CharacterBody2D/basic_movement.gd
index 28ab080dd2..bd4816827f 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 9b0e4be4ed..f9c4f70a24 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 87468fb433..698157c6b4 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 ddeb9d7e00..30dabd31d9 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 6af5a8dd80..7a131916e8 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> PhysicsBody2D::get_collision_exceptions() {
List<RID> 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 208e72c40f..62636b02f4 100644
--- a/scene/2d/physics_body_2d.h
+++ b/scene/2d/physics_body_2d.h
@@ -52,6 +52,7 @@ protected:
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<KinematicCollision2D> &r_collision = Ref<KinematicCollision2D>(), real_t p_margin = 0.08, bool p_recovery_as_collision = false);
+ Vector2 get_gravity() const;
TypedArray<PhysicsBody2D> 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 ed64c16564..b11dfec104 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 e8d5ef2103..e8373d5907 100644
--- a/scene/3d/physics_body_3d.h
+++ b/scene/3d/physics_body_3d.h
@@ -55,6 +55,7 @@ protected:
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<KinematicCollision3D> &r_collision = Ref<KinematicCollision3D>(), 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;