How to handle forces that depend on velocity? (RigidBody2D)

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By genete
:warning: Old Version Published before Godot 3 was released.

Hi!
I am planning to design a game where the movement of object controlled by the user is governed by external forces. Those forces depend on current geometry of the object (user controlled) and also depend on current object velocity.

My question is:
Continuous variable applied forces over a RigidBody2D should be included in _fixed_process() or should I use _integrate_forces()?

I guess that simply calculating the new forces on each _fixed_process() and reapplying them to the object would be enough. Still not understanding properly how to use _integrate_forces() and when it is needed.

:bust_in_silhouette: Reply From: mateusak

_integrate_forces() is called as much as _fixed_process(). I use _integrate_forces() whenever I can over it, as it’s basically the same but can change the Rigidbody2D’s velocity every frame. So, if you will call set_linear_velocity() or set_angular_velocity() every frame, you should use _integrate_forces().

So are you saying we actually don’t use _fixed_pricess() but rather use _integrate_forces()?

So I put all my control logic etc etc into _integrate_forces()?

Robster | 2017-02-24 04:14

:bust_in_silhouette: Reply From: ericdl

From the documentation:

The best way to interact with a RigidBody2D is during the force integration callback. In this very moment, the physics engine
synchronizes state with the scene and allows full modification of the internal parameters (otherwise, as it may be running in a thread, changes will not take place until next frame).

And using _integrate_forces(state) also gives you access to the stateobject, which I like to use as a contact monitor because it gives colliding body information, contact position, contact angle, etc.