Search Unity

[Solved] Changing Fixed Timestep, Gravity change? Mass, Physics and General Game speed.

Discussion in 'Physics' started by AlanMattano, Aug 24, 2015.

  1. AlanMattano

    AlanMattano

    Joined:
    Aug 22, 2013
    Posts:
    1,501

    IN PHYSICS 2D
    [ running Unity 5.2.0f2 ]

    If you increase the "Fixed Timestep" form 0.02 to 0.01 then the energy of the object will increase considerably.

    Reading other links I made an experiment. I put a ball and let it fall down by gravity at default 0.02 Physics fixed time step.
    Then i change the fixed time step to 0.01 (100fps) then the ball falls much faster (in 2D).
    I was not expecting this effect since the manual talks about physics quality. It did not mention changes in mass or weight . It did not mention that if we change the timestep then we will need to change the gravity , all the masses and the joints, etc.

    Is it a bug? It looks more like a conceptual bug. If i set up gravity at 9.8, I expect that the engine calculates correctly the acceleration, no matter if i change time steps to calculate that info.

    I do not get it. Can you please put more info about ?
     
    Last edited: Aug 25, 2015
  2. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    Sounds more like your code is frame rate dependent. Youll need to multiply force values by FixedTimeStep.
     
  3. AlanMattano

    AlanMattano

    Joined:
    Aug 22, 2013
    Posts:
    1,501
    I made a second check in an empty scene after reading your answer and you were right.
    Looks like a bug in my game or engine. I don't understand how my code affects the unity fixed Update.
    I was using the unity angry birds example: http://unity3d.com/learn/tutorials/.../making-angry-birds-style-game?playlist=17120
    And is made for Unity 4.5 Not for Unity5 I am researching if there is a relation
     
  4. AlanMattano

    AlanMattano

    Joined:
    Aug 22, 2013
    Posts:
    1,501
    Physics inside a collider problem

    The problem is a boundary global box Collider 2D all around the game. If I turn it off, the physics speed of the game works as expected. If all the physics game objects are inside this Collider following the tutorial instructions (making-angry-birds-style-game) the speed of the physics it became frame rate dependent. So strange.
     
  5. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    If you are not multiplying your force values, and you double the speed, you are applying double the force. What is your goal with increasing the fixed time? is it to speed up gameplay?

    forceValue * Time.fixedDeltaTime should make it frame rate independent enough for your needs.
     
  6. AlanMattano

    AlanMattano

    Joined:
    Aug 22, 2013
    Posts:
    1,501
    I increase Fixed time to 0.01 to get 100 frames per second. Since by default is only 0.02 (50 frames per second) and my camera moves in fixed updates because follows the game object with a rigidboady attach to it. I'm researching fluidity. The problem was that passing from the default 0.02 Timestep to 0.01 , the game was much faster. Just using gravity. Looks like the collider in the collider do not work as expected. Now i Import one more time Unity angry-birds tutorial and the default scene given by Unity works well. Yes there is something in my code that affects physics updates as if it is working in normal updates. I'm trying to isolate the bug to understand if is my error or a combination of colliders and code conversion. Clearly the problem is when the 2D physics is working inside a collider boundary.
     
  7. AlanMattano

    AlanMattano

    Joined:
    Aug 22, 2013
    Posts:
    1,501
    The big box collider 2D that is use as boundary was not trigger
    When [ Is Trigger = false ] the physics inside this box collider acts as if it is in update instead of fixed updates and my game was not working properly. Now Is trigger = true and all works fine.
     
    Last edited: Aug 25, 2015
  8. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    when you say you're researching fluidity, do you mean liquids, or making your game smoother?
     
  9. AlanMattano

    AlanMattano

    Joined:
    Aug 22, 2013
    Posts:
    1,501
    Game smoother. My real frame rate was over 120 frames per second and the perception physics was 50 frames per second. So I increase the physics frame rate up to 100 frames per second changing time step. Now my visual frame rate is closer to my physics frame rate.
     
  10. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    ok, so, rather than increasing the physics time step, I would move your camera movement code into Update. Should be able to smooth that out easier and you wont be working the engine as hard.

    Increasing the timestep may make it look like its running better, but tbh, you're just putting a bunch more pressure on the system to perform, which means (especially on low end devices) it will run poorly. I've never had a problem with the default physics step. Increasing it is only advised when you need more accuracy because everything is happening really fast (like car racing games).
     
  11. AlanMattano

    AlanMattano

    Joined:
    Aug 22, 2013
    Posts:
    1,501
    Yes i'm at 350kmh. The game is made for PC. The default option was good using 0.02 and the option Interpolate active. But i want to push it more just in case for having extra margin to take out.
    And then let the player or my system change it via code in the game if the frame rate is poor.
     
  12. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    I see! carry on as you are then ;)
     
  13. AlanMattano

    AlanMattano

    Joined:
    Aug 22, 2013
    Posts:
    1,501
    Thx for the help! Good night
    ps: or good morning
     
  14. Ivgenii

    Ivgenii

    Joined:
    Apr 2, 2017
    Posts:
    1

    You are the best man! I spend 2 days on fixing enemy jittering with rb.AddForce and pixel perfect camera (render texture based). I understand that problem was is not synced movement between enemy and player + camera (because player and camera works good) even if movement code is equal. But you help me fix this in 1 step, just adding fixedDeltaTime in AddForce, when everybody tells that DeltaTime already in it. After fixing all objects on scene move synced because movement not depend on frame rate now and there is no difference and no jitter. Grateful!