Apply force equal to gravity

Godot Version

4.2.1

Question

Hi all. I think this has already been discussed, and I read that physics in Godot is not very accurate. But it seems to me that the engine should cope with the integration of two forces. I created Rigidbody2d and I added a script to it in which

_process(delta):
apply_central_force(Vector2(0.0,-980.0))

It results in accelerated movement in up direction. Even applying (0.0,-980.0/2.0) results in up direction movement. (It starts fall only when I use -980.0/3). Mass is set to 1kg.

Am I doing something wrong?

If you’re really applying it in _process(delta), you’re probably running the game at a framerate higher than the _physics_process(delta) callback.

The correct way to apply a force to negate gravity would be:

_physics_process(delta):
	apply_central_force(-ProjectSettings.get_setting("physics/2d/default_gravity") * delta)

This ensures the force is applied only once per second, and gets the gravity from the project settings itself, automatically syncing it up.

1 Like

Thank you so much. Using physics process solves my problem.
It works, but we shouldn’t use *delta for forces.

Correction: you don’t use delta when applying instant forces. If you’re counteracting gravity for a period of time, you should use delta because gravity uses delta.

I wrote script like that:
func _physics_process(delta):
apply_central_force(Vector2(0,-980))

And all the time the body was motionless

From godot documentation:

Applies a directional force without affecting rotation. A force is time dependent and meant to be applied every physics update.
This is equivalent to using apply_force at the body’s center of mass.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.