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

Allow all physics bodies to use the built-in gravity system #8054

Closed
aaronfranke opened this issue Oct 10, 2023 · 7 comments
Closed

Allow all physics bodies to use the built-in gravity system #8054

aaronfranke opened this issue Oct 10, 2023 · 7 comments
Milestone

Comments

@aaronfranke
Copy link
Member

aaronfranke commented Oct 10, 2023

Describe the project you are working on

I'm working on support for dynamic gravity in Godot and in glTF to allow making games like Super Mario Galaxy. For more information, check out this video: https://www.youtube.com/watch?v=QLH_0T_xv3I&t=12m

Describe the problem or limitation you are having in your project

Godot has its own built-in gravity system. The world can have a specified amount of gravity, and Area(2D/3D) nodes can override this, either adding to it and replacing this, and multiple Area nodes have settings for what their priorities are and how well they play with other Area nodes. This is great!

However, right now the only bodies that can be affected by this are the dynamic RigidBody(2D/3D) nodes. Godot does not provide any way for the user to access the built-in gravity system, so there's no way to use Godot's built-in gravity system with CharacterBody(2D/3D) nodes unless you reimplement the whole thing.

Describe the feature / enhancement and how it helps to overcome the problem or limitation

Add a function to the PhysicsBody2D and PhysicsBody3D that allows the user to grab the gravity computed using the same code that the engine uses internally for RigidBody(2D/3D).

Describe how your proposal will work, with code, pseudo-code, mock-ups, and/or diagrams

Here is a fully working implementation: godotengine/godot#84640

Here is a fully working implementation: godotengine/godot#83087

Here is an example of what can be accomplished. This is a CharacterBody3D node, being controlled by the gravity of Area nodes. The mini-Earth has a point gravity field, and the ramp has gravity fields to ensure the player sticks to it that override the gravity of mini-Earth.

gravity.webm

If this enhancement will not be used often, can it be worked around with a few lines of script?

No, you would have to either reimplement the entire gravity system, which is unacceptable, or make all objects RigidBody(2D/3D) nodes, but I would really prefer to use CharacterBody(2D/3D) since they are designed for characters.

As pointed out below, it is actually possible to work around this using a physics API that I did not know about before.

Is there a reason why this should be core and not an add-on in the asset library?

Godot's dynamic body gravity calculation is already in core, we just need to make it available for all bodies and expose it.

@letoast
Copy link

letoast commented Oct 24, 2023

I came across this PR while looking for the same solution. It seemed strange to me that the controller's gravity wasn't calculated in the same way as other rigid bodies. Anyway is there a reason why nobody has reviewed this yet? What's the usual time it takes to merge PRs into master?

@aaronfranke
Copy link
Member Author

@letoast PRs often take years, this is only 2 weeks old, so that's why it has not been reviewed yet (not much time has passed). As with the play Godot is named after, contributing to Godot requires a lot of waiting and patience. But if you want to help this get prioritized, you can share it with your friends, on social media, etc, and they can click the 👍 button.

@letoast
Copy link

letoast commented Oct 24, 2023

@letoast PRs often take years, this is only 2 weeks old, so that's why it has not been reviewed yet (not much time has passed). As with the play Godot is named after, contributing to Godot requires a lot of waiting and patience. But if you want to help this get prioritized, you can share it with your friends, on social media, etc, and they can click the 👍 button.

Ahhh, sorry for the annoyance then. I'm more used to web projects where PRs get through relatively fast. Here's to hoping this gets noticed 🤞

@Lazy-Rabbit-2001
Copy link

Lazy-Rabbit-2001 commented Nov 3, 2023

For CharacterBody2D, actually, we can do it through:

@onready var rid = get_rid()

func _physics_process(delta):
        direct_state = PhysicsServer2D.body_get_direct_state(rid)
        gravity = direct_state.total_gravity
        velocity += gravity * delta

which will also takes effect in an Area2D with gravity effect mode on
But anyways, an excellent proposal and implementation for faster access to the operation :D

@voidexp
Copy link

voidexp commented Nov 8, 2023

The solution proposed by @Lazy-Rabbit-2001 is simple enough, is sufficient for the task and is a positive answer to the question "If this enhancement will not be used often, can it be worked around with a few lines of script?".

The property total_gravity is readily accessible in the direct state, and exposes the result of an internal computation, not the mechanics of the computation itself, as this PR would do. Also, the PR puts a very strong constraint/requirement on the physics servers' API and how they must compute gravity. What if a server/extension computes gravity for all the bodies on a GPGPU at once and not per-body, where multiple sources of gravity apply (like in the N-body problem, for instance)? Whatever computation it takes, they can write the result to the total_gravity attribute which can be accessed in scripts too, but compute_gravity() methods are specific to GodotBody2D/3D default physics engine and are an implementation detail and IMO must remain such.

@aaronfranke
Copy link
Member Author

aaronfranke commented Nov 8, 2023

@Lazy-Rabbit-2001 @voidexp Thanks! I did not realize that API was available. I opened godotengine/godot#84640 instead, which will provide this as a helper method on physics bodies, simplifying the API, and yes I tested it and it works the exact same as the other PR.

@akien-mga akien-mga modified the milestones: 4.x, 4.3 Feb 5, 2024
@akien-mga
Copy link
Member

Implemented by godotengine/godot#84640.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants