Hello, i’m using Unity 5 with its integrated Physx engine (version should be 3.1).
In my simulation there are a lot of rigid bodies that interact without collision, only by forces that I add by scripting.
In order to add those forces at each time step I tried two different ways:

• 2 I compute force_sum wich is the sum of all the forces I need to add in that tilme-step and then call once rigidbody.AddForce(sum_force).

I though that the choice of the way 2 instead of 1 wouldn’t change the results at all, but instead the behavior of my rigidbodies are similar (almost equal) in the beginning of the simulation and progressively get different.
I wonder what is the cause of this difference.
I though that Physx would internally sum the forces i added anyway to compute the new velocity and vector-position.
Thanks a lot for the help.

PhysX sums up all the forces applied and only applies them when the next simulate is called. Theoretically, it should make no difference how the forces are summed up but in practice there may be subtle differences between the two approaches.

There could be a couple of reasons for the divergence. The first is numerical differences between adding the forces in script (I’m assuming you’re adding the forces in script) and adding the forces in physx. Even small numerical differences can add up over hundreds of frames and completely diverge if the rigid body has a collision or is attached to a joint. The second reason would be if any call to addForce is made while physx is executing a simulate call. When this happens the forces is cached and only applied at the next simulate. I’m not familiar with the threading model of unity scripting but I can imagine a scenario where some of the calls to addForce are cached for next simulate while some are made early enough to make the current simulate. This would lead to fast divergence.

If possible, I’d recommend adding forces outside physx and calling once per body with the accumulated force and torque. This is almost certainly going to be more optimal than individual calls, especially if the overhead of calling from script is high.

Cheers,

Gordon

Hi Gordon, thanks a lot for your answer. As far as I know (from Unity website) Unity works with only one thread, all scripting are executed before the physics update call in this thread, at each time step. I assume forces are put in a stack during the script execution and this stack is then passed as an input the physics engine.
I thought instead the problem could be in the floating point precision (as unity uses floating points), as changing the order of operation and lead to different results in case of floats.
Can you explain me better how to add forces directly in Physx? I know how to add them only by scripting.
So you think, tell me if I’ve understood correctly, that is better if I add force and torque only once for each rigidbody at each time step adding the sum of forces and torque instead of calling the AddForce method multiple times.
Thanks again .

Gabriele

Hi,

If you have access to a PxRigidDynamic instance then you can just call myDynamicActor->addForce or myDynamicActor->addTorque. I doubt you can do that directly from unity script because it manages the way that users make calls to the physx types that are in turn owned by the unity runtime. The unity forums are likely to give you more expert advice on this.

The order of the sum will certainly make a difference. Imagine you have four forces of values A, -A, B and -B and you want to add them up. In this case, precision is lost if you add them in order (((A + B) - A) - B). The best precision is found if you keep your accumulated sum as close to zero as possible ie (((A - A) + B) - B). This will be especially true if A or B is a large value.

I’m really only speculating that it will be more optimal to add the forces and torques in script and then pass the sum to physx with a single call. I’m also not 100% sure what guarantees there are that unity executes your calls to physx in the order you expect from your script. I also don’t know what precision that is applied to unity script arithmetic. I’m afraid my knowledge of the unity integration is quite limited.

Cheers,

Gordon

Great tips Gordon. I have similar questions.

I have two forces applied to a body at given location.

Can I represent the same situation in this way, so that I have single force and momentum to add angular velocity? Is this correct ?