How to apply force to a Rigidbody2D bypassing the drag?

TL;DR
How to apply some force to a rigidbody with some random drag value, so that the velocity of the rigidbody is the same, independently of what the drag value is?

So i have some rigidbodies, and a force field that is pushing them in some direction. What i need, is that their speed when they are being pushed to be the same. The problem here is that rigidbodies have different Linear Drag values.
So to compensate for the drag, i must apply different forces to rigidbodies depending on their drag value. But i can’t find any formula where it says how drag is related to velocity of the body.
I have found this thread, and the formula for drag is claimed to be something like this:

float coeff = (1 - Time.deltaTime * rigidbody.drag);

which does not work, since for drag values enough high (which is my case, its 55 for some objects), i get a negative coefficient, which is not correct.
I use it like this:

var dragCoeff = 1f + Time.deltaTime * target.CachedRigidbody2D.drag;//we need to eliminate drag
var power = dir.normalized * Power * Time.deltaTime;
target.CachedRigidbody2D.AddForce(power.ToVector2() / dragCoeff, Mode);

where

  • dir - the direction of the force that will be applied
  • Power - is the power of the force
  • target - the object to which the force will be applied
  • Mode - is the force mode, ForceMode2D.Force is used in this case.

and when the coefficient is negative, the object is being pushed in the wrong direction.
Any solution? how is drag related to velocity?
I need a function that depends on drag that will give me the needed force to apply to the object so that it would compensate for the drag, any hint is appreciated.

1 Like

TL;DR
To bypass the drag when applying some force to a rigidbody, multiply that force by 1 + rigidbody.drag

After some more experimenting, i found out that if i set the gravity acceleration to -10 on the y axis, with drag on the rigidbody equal to 1, the terminal velocity of the rigidbody is 10, at drag 2 it is 5, at drag 4 it is 2.5 and so on. So the relationship between the applied force and drag is inverse proportional and linear.
When i multiply the force i want to apply by the (1+drag), i get a constant speed of the rigidbody in the direction of the force, whatever i set the drag to be.
So if anyone wonders, use it like this:

var dragCoeff = 1 + target.CachedRigidbody2D.drag;//we need to eliminate drag
target.CachedRigidbody2D.AddForce(power * dragCoeff, Mode);

where :

  • target - the object to which the force will be applied
  • Mode - is the force mode, ForceMode2D.Force is used in this case.
1 Like