Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by gabrielemontecchiari · May 21, 2015 at 10:38 AM · rigidbodyforcerigidbodiesforces

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
1

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image gabrielemontecchiari · May 21, 2015 at 03:06 PM 0
Share

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.

avatar image Baste · May 22, 2015 at 02:16 PM 0
Share

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.

avatar image gabrielemontecchiari · May 22, 2015 at 02:36 PM 0
Share

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...

avatar image
1

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Owen-Reynolds · May 22, 2015 at 02:55 PM 0
Share

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.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Rigidbody rotate around 2 Answers

Add Force without Rotation 2 Answers

Why is it so hard to make a rigidbody jump in the unity engine? 1 Answer

Unexpected Force being applied after adding rigidbody 1 Answer

Unity First person controller force and mass 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges