1
\$\begingroup\$

I've tried to understand the Maths behind AddForce, as to reproduce it using rigidbody.velocity +=.

Based on the formulas provided by PhysX documentation for AddForce, we have:

eFORCE            parameter has unit of mass * distance/ time^2, i.e. a force
eIMPULSE          parameter has unit of mass * distance /time
eVELOCITY_CHANGE  parameter has unit of distance / time, i.e. the effect is mass independent: a velocity change.
eACCELERATION     parameter has unit of distance/ time^2, i.e. an acceleration. It gets treated just like a force except the mass is not divided out before integration.

I tried to convert these formulas above to a rigidbody.velocity += syntax, but I am unable to get the Maths right.

Here is what I got working:

float force = 3f;
public void FixedUpdate() {
  // Same as r.AddForce(force, ForceMode.Force)
  Vector2 distance = (force / r.mass) * Time.fixedDeltaTime;
  r.velocity += distance;
} 
float force = 3f;
public void FixedUpdate() {
  // Same as r.AddForce(force, ForceMode.Impulse)
  Vector2 distance = (force / r.mass);
  r.velocity += distance;
} 

I tested both of them, by superposing two rigidbodies, one using AddForce, while the other used r.velocity+=. So far, it seem my convertion is correct as they move exactly the same.

As for the Maths I am not getting right, here is what I came with:

eFORCE = mass * distance/ time^2
eFORCE / mass = distance/ time^2
(eFORCE / mass) * time^2 = distance
distance = (eFORCE / mass) * time^2

Same as ?

Vector2 distance = (force / r.mass) * Time.fixedDeltaTime;

That woud mean time^2 is equal to Time.fixedDeltaTime.

Then let's do the same for ForceMode.Impulse:

eIMPULSE = mass * distance /time
eIMPULSE / mass = distance / time
(eIMPULSE / mass) * time = distance

Meaning time is Mathf.Sqrt(Time.fixedDeltaTime) ? Well, as you guessed, on the terrain this doesn't check out.

I like to get the Maths right, how do I get to the result c# code I have shown (that seem correct) starting from the PhysX documentation ?

\$\endgroup\$
2
  • \$\begingroup\$ Why are you taking the square root instead of squaring? time^2 is equivalent to time*time, so moving that to the delta version, wouldn't it be Time.fixedDeltaTime * Time.fixedDeltaTime? \$\endgroup\$ Commented Dec 24, 2021 at 1:31
  • \$\begingroup\$ @Pikalek This is part of the thing bothering me. time^2 should be Time.fixedDeltaTime * Time.fixedDeltaTime for ForceMode.Force, but the formula that is working when testing is distance = (force / r.mass) * Time.fixedDeltaTime, not distance = (force / r.mass) * Time.fixedDeltaTime * Time.fixedDeltaTime. I made the assumption time^2 is somehow equivalent to Time.fixedDeltaTime. But this assumption is wrong when testing that on ForceMode.Impulse. \$\endgroup\$ Commented Dec 24, 2021 at 11:11

1 Answer 1

1
\$\begingroup\$

The confusion you are having is that the PhysX documentation is telling you the units of the values, not the formulas. Forces are given in Newtons whose units are \$kg\cdot m/s^2\$. That is, 1N is the force it takes to accelerate a 1kg mass by 1 m per second each second.

\$\endgroup\$

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.