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

GODOT v 3.3 rc 6 (GD Script & C#) RigidBody2D.LinearVelocity grows after execute RigidBody2D.AddCentralForce ones on it #47137

Closed
lukaszbulzacki opened this issue Mar 18, 2021 · 5 comments

Comments

@lukaszbulzacki
Copy link

lukaszbulzacki commented Mar 18, 2021

Godot version:
GODOT 3.3 rc 6

OS/device including version:
Linux, Debian 9

Issue description:
Problem with RigidBody2D simulation.
When RigidBody2D is moved by AddCentralForce (even called once) it LinearVelocity doesn't stop.
It (RigidBody2D.LinearVelocity) will increase constantly.
Could be reproduce with GD Script or C#.

Problem doesn't reproduce when RigidBody2D.ApplyCentralImpulse was used.

Steps to reproduce:
Please Run attached project.
Then look into N2D._Ready from ND2.cs.
Look also logs from Godot editor console where you will see values with linear velocity of ball body.

Minimal reproduction project:
TESTPHY__GODOTv32rc5.zip

@lukaszbulzacki lukaszbulzacki changed the title GODOTv324rc5/C# RigidBody2D.LinearVelocity grows after execute RigidBody2D.AddCentralForce ones on it GODOTv324rc5 (GD Script & C#) RigidBody2D.LinearVelocity grows after execute RigidBody2D.AddCentralForce ones on it Mar 19, 2021
@lukaszbulzacki lukaszbulzacki changed the title GODOTv324rc5 (GD Script & C#) RigidBody2D.LinearVelocity grows after execute RigidBody2D.AddCentralForce ones on it GODOTv33rc6 (GD Script & C#) RigidBody2D.LinearVelocity grows after execute RigidBody2D.AddCentralForce ones on it Mar 19, 2021
@lukaszbulzacki lukaszbulzacki changed the title GODOTv33rc6 (GD Script & C#) RigidBody2D.LinearVelocity grows after execute RigidBody2D.AddCentralForce ones on it GODOT v 3.3 rc 6 (GD Script & C#) RigidBody2D.LinearVelocity grows after execute RigidBody2D.AddCentralForce ones on it Mar 19, 2021
@bluecorn745
Copy link

This is most likely caused by #38646. In the 2D physics engine, using add_central_force or add_force will add a "constant force" which is applied every frame, whereas apply_central_impulse and apply_impulse add impulse only once. This behavior is inconsistent with the 3D engine, and there's an ongoing debate about which is the correct way, so for now you'll probably just want to use the impulse functions to move things around.

@lukaszbulzacki
Copy link
Author

lukaszbulzacki commented Mar 20, 2021

Hmm to be honest I always thought it is like this:

  • use add_* methods in _process/_physics_process - you should use it every simulation step and consider delta with force adjustment
  • use apply_* methods (not constrait from where it is called) - you call it once (there is no need to consider delta in you force)

Thanks for explanation.

@lukaszbulzacki
Copy link
Author

I dig in docs. And from docs it states that using apply_ methods in _process/_physics_process will result in different results where frame rate is variable (not constant).

So my questions is what is good way to adjust RigidBody2D with force every frame I don't want force to be "remember" because I will be adding it every frame?

https://docs.godotengine.org/en/stable/classes/class_rigidbody2d.html#class-rigidbody2d-method-apply-impulse

void apply_impulse ( Vector2 offset, Vector2 impulse )

Applies a positioned impulse to the body. An impulse is time-independent! Applying an
impulse every frame would result in a framerate-dependent force.

@bluecorn745
Copy link

bluecorn745 commented Mar 20, 2021

Yeah, I got the same idea you did out of the docs, so that confused me as well a while back. Fortunately, there is a pretty simple way to get the expected behavior with the add_ methods, just zero out the applied_force/applied_torque properties at the beginning of _physics_process() like this:

applied_force = Vector2();
applied_torque = 0.0f;

(Got that from the comments in PR #38648, its alternative #42850 is also interesting for context).

@madmiraal
Copy link
Contributor

Closing as duplicate of #38646.

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

4 participants