- Home /
Adding more forces vs adding sum of forces
Hello, To be short I need to know if calling rigidbody.AddForce(A) and rigidbody.AddForce(B) in the same FixedUpdate() is the exactly the same as calling once rigidbody.AddForce(A+B) where A and B are the Vector3 forces i need to add, because in my situation it appears to be different. My situation is the following: I have a complex situation with a lot of interacting rigid body, the do not are in contact with each other, they're interaction is ruled by forces that I add by scripting. I usually add for each rigid body each force by rigid body.AddForce(force_to_add), calling this method for each force I needed to add during the fixed time step. Then I changed the code, computing a vector sum of the forces I needed to add and than add only the vector as sum. Surprisingly as the added forces are the same, the results are quite different. The rigid-bodies trajectories are equal in the beginning and than progressively diverge. Could that be that physics performs on the stack of the forces other operation than the simple sum of them? Thanks in advance for every answer.
Answer by Baste · May 21, 2015 at 10:44 AM
This is probably because of the default force mode.
AddForce(force) is short for AddForce(force, ForceMode.Force)
ForceMode.Force will "Add a continuous force to the rigidbody, using its mass". This means that the force is scaled in some way by the rigidbody's mass.
This is to give correct simulations - pushing a heavy thing two seperate times will not give the same result as pushing it once with the sum of the two pushes. Of course, if the two pushes happens at exactly the same time, the sum should really be the force being applied, but it seems like the simulation doesn't wait until the end of the physics frame to combine all of the forces, but just applies them right away in a synchronous manner.
There's two solutions. You've found the first one - adding the forces together. The other one is to use a different ForceMode. Both ForceMode.Acceleration and ForceMode.VelocityChange ignores mass. The first one is somewhat similar to the second one, while the other one is (I believe) just short for adding the input vector directly to the rigidbody's velocity.
Read more about force modes here.
Hi so, you think forces are applied one after another even if they are applied in the same FixedUpdate()? I read that FixedUpdate are performed all before physx computation and updates , so I thought that at each time step first all forces are collected, than they are all applied and new velocity and position are computed. Are you sure that ins$$anonymous$$d could happen that forces are applied one after another even if they are in the same FixedUpdate()? Thanks a lot for the help.
I did the check, and it turns out I'm full of shit. Adding force V once in fixedUpdate does exactly the same as adding force V/50 50 times, regarless of force mode.
So you probably have a bug somewhere.
O$$anonymous$$ thanks, if there is a bug I can't find it, even because forces seem to work just the same in the beginning of the simulation. $$anonymous$$aybe using more rigibodies and running the simulation for long times changes the things.did you try to log position at every time step? It could be that the simulation of one rigidbody gives very slightly different values that in my simulation spread as a butterfly effect...Anyway i'll also recheck for bugs...
Answer by Owen-Reynolds · May 21, 2015 at 03:22 PM
I'm 99% sure that AddForce merely performs a simple Vector3 addition, adding the adjusted input to your velocity. It doesn't check in with the physics system, or move the object slightly. The physics steps run after all your code is done, so physics only "sees" the final result of all the AddForces.
So, YES, two AddForces in a row is the same as one combined AddForce. You probably changed something else in your code. And a small change can sometimes "butterfly effect" into bigger changes.
Yes, as gabMonChair writes, some modes divide by the time or mass. But it's linear -- A/mass+B/mass is the same as (A+B)/mass. But implying the system tracks each push -- pretty sure that's not the case. The only end result of any number of AddForces is your velocity (and angularVelocity, for some cases) is changed.
Answer by gabrielemontecchiari · May 22, 2015 at 12:08 PM
I agree with you that (A+B)/mass is equal to A/mass+B/mass but I don't know if that is the same with floating point. Could that when I add each force calling AddForce are performed different operation than just the sum (A+B)/m ? Maybe also the order in which operation are performed may affect the result because of floating point error. I agree with you about the butterfly effect because it is just what happens: at first is a very little difference and the behavior is very similar, but it gets different set by step, so I thought about a difference due to something like floating point precision.
Sure, different ways of arranging the math can result in different floating point rebounding, but it's going to be so tiny as to be unnoticeable during normal movements. The only time it would matter is if you just barely hit something, and now you just barely miss. For fun, AddForce +/-0.0001, to simulate funny rounding, and see if you notice a difference.