Search Unity

Unity 2D Physics : Manual Simulation, Simulation Groups & Multiple Worlds

Discussion in '2D' started by MelvMay, Apr 16, 2017.

  1. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    1,972
    I just don't follow what you're saying and it seems to be going in circles. I also have no idea what you mean when you say "nothing goes wrong".

    If Time.deltaTime and Time.fixedDeltaTimeare identical then it'll enter the loop each and every time only once as it checks >=.

    Why not just try this yourself rather than trying to convince me this doesn't work. Also, for the final time; please post on another thread. You've effectively took over this thread for your own purposes.

    Thanks.
     
  2. Deeeds

    Deeeds

    Joined:
    Mar 15, 2018
    Posts:
    739
    Why don't I try it?

    LOVE TO!!!

    But where/how do I set fixedDeltaTime independent of the cycle rate of FixedUpdate() ?

    They seem to be the same thing, but without control of which is first. It seems impossible to know if the FixedUpdate is called before the manual simulation, or after it.
     
  3. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    1,972
    All this information I have provided to you already in the links above showing you how to set the time and what the order is for the MonoBehaviour callbacks etc.
     
  4. any_user

    any_user

    Joined:
    Oct 19, 2008
    Posts:
    343
    Going back to the original topic of this – I'd be very interested in multiple 2d physics worlds.

    In a game we're working on, we're using cavalier perspective. There is a ground plane at which we look from a 45° angle, and there are vertical planes that are aligned to the camera. We use 2d physics for the ground plane. So circles on the ground turn into ellipses etc. Sprites "standing" on the ground are rendered completely flat because they are aligned to the camera (like billboards, but they can still be rotated on the vertical axis).

    There are some scenes, in which we would like to use 2d physics on some of these vertical "sprite-planes" in addition to the normal ground physics. Right now that's very complicated to do, the only easy way is to either use ground physics or sprite-plane-physics for one of these sprites. Or we could simulate it somewhere on the ground plane and then project it to the visible plane, but that seems very cumbersome. For this it would be very useful to have small 2d physics worlds, one for each 2d object that uses physics in local space.

    Is that a use case that would be supported with your approach of having multiple worlds?
     
  5. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    1,972
    This feature will be going into 2018.3 and is pretty much complete already. Your scenario will be possible with the feature.
     
  6. any_user

    any_user

    Joined:
    Oct 19, 2008
    Posts:
    343
    Great to hear, thanks!
     
  7. jollyblade

    jollyblade

    Joined:
    Mar 20, 2014
    Posts:
    5
    Hey Guys,

    It's a really good thread, but I still have some questions about how to organise some things, and why things are so as described here.

    First of all, why is the Simulate called in the Update?
    Why is it not just in the FixedUpdate()? Am I missing some info?

    Like this:
    void FixedUpdate(){
    // apply input, and then
    Physics2D.Simulate(fixedTimeStep);
    // do anything else with the results
    }

    I need this to be able to re apply input, for local prediction, from an earlier state.

    What I do:

    I set the rigidBody properties to the latest available from the server, then I take my input from my buffer, and reapply the remaining. However it does not really work, and I don't understand why.


    What I do:

    void FixedUpdate ()
    {

    reset each networked rigidbody to acked state from server
    // everything else is kinematik atm

    for each input > acked input
    {
    apply Input; // add forces, torque, etc
    simulate(fixedDeltaTime); // do a simulation step for the given input
    }
    }

    But this unfortunately does not work, and I don't really understand why. It seems that either the reset breaks something, or doing multiple simulate calls in one fixed update is basically bad.

    If there are 3 - 5 not acked inputs in the queue, they all have to re simulated and applied onto the last result from the server, meaning that in one fixed frame all the inputs needs to be simulated.

    Or not? what do you think? How is it done properly?

    Thank you very much.
     
  8. chesiq

    chesiq

    Joined:
    Apr 25, 2015
    Posts:
    2
    Hi,
    Thank you for creating this thread!

    I'm currently developing a game a lot like Heat Signature.
    It's in 2D, the view is top down, you are able to control a char that is able to walk around, build and fly spaceships.

    The solution I'm using is a hack ( parenting empty object with player child(with a rigid body) to the ship with a rigid body).

    The problem I'm having is low max usable speed of ships. I can walk on spaceships that are slower than 15 - 20m\s (unity meter per second) and if set higher - player starts to hit lagging behind walls and is able to glitch through the ship walls.

    I need to be able to walk on fast moving ships - like in Heat Signature; as well as use projectile weapons inside fast-moving ships.

    Is there a current possible solution? Maybe making physics deterministic so that walls do not lag behind their visual representations by locking fixed update to the framerate?

    From what I was able to find out games like Star Citizen use multiple physics simulations, I wonder when will be seeing this feature? And if so, will it solve my problems?

    I read about different cheat like approaches like making ships stationary and only moving the world around, but then you are fighting other ships that also have players on them... so I don't want to try out other solutions that I will regret further when implementing multiplayer.

    Thank you in advance.
     
  9. foxnne

    foxnne

    Joined:
    Apr 18, 2016
    Posts:
    59
    This is exactly the problem I'm facing. Basically at the moment, unless I've missed something, the only option is to create your own collision solver using casts or overlaps, which is harder than it seemed given the data available from those casts or overlaps. Doing this, you can call your move method to process input whenever you like.

    However, making it behave in many conditions (circle character controller moving into a small space between two boxes, or similar) is hard to solve in an efficient way. I've been researching Speculative Contacts to try to build something that works but it's difficult to shove that type of system into Unity. If anyone has information on that, I'd love to see it.

    The 3D character controller component does this, i think. At least it works in the same way. It can be used in place at the moment, but that means not using 2D colliders which is annoying.

    I've been battling this for a long time now, each time I try to make a better system but it never quite gets there. I would absolutely love some specific information on if it's currently possible to implement client side prediction using unity physics in some way (simulating multiple inputs in a single frame).

    Hopefully what I read above is that you could either put all currently moving bodies in a seperate world and simulate thoseton demand or something, coming in 2018.3. Still, I hope it's possible to simulate multiple times within a single frame (even just one single collider) .

    Is there any information on whether that will be possible in the near future?
     
  10. rxmarccall

    rxmarccall

    Joined:
    Oct 13, 2011
    Posts:
    301
    @MelvMay
    Will the simulation groups be included in Unity 2018.3?

    I'm working on netcode and am trying to simulate each individual player Rigidbody2d one at a time....
    Currently I'm setting all other player Rigidbody2d's to "isKinematic" and then simulating physics just for that player and then moving on to the next player.

    Is this an appropriate way to simulate only specific Rigidbody2d's at a time?
    I'm running into an issue where the physics simulation seems to run 2x as fast once I have 2 players in the game, so I'm assuming somehow I'm running the physics step 2x as much as needed. For that reason I'm asking about physics groups as I think it'd help me out!

    Thanks
     
    Deeeds likes this.
  11. wongagame

    wongagame

    Joined:
    May 17, 2018
    Posts:
    2
    HI

    my one query i am develop flip poker card animation. i'm used fliping book page project.
    i'm develop poker card animation but no idea so plz help me.
    i'm create fore card animation but card drag and other three card not proper working.

    – reference pokerrr 2 card show animation
    – reference link :


    Plz help me.

    Thank You !!
     
  12. impbox

    impbox

    Joined:
    Mar 31, 2017
    Posts:
    5
    Hi @MelvMay, I see unity 2018.3.0b3 says it includes support for per scene physics and there is now a PhysicsScene2D in the docs, however I can't see any mention of how to set up a new PhysicsScene2D. Is this now supported?

    Thanks!
     
  13. Creepgin

    Creepgin

    Joined:
    Dec 14, 2010
    Posts:
    297
  14. HaTi13

    HaTi13

    Joined:
    Apr 8, 2019
    Posts:
    1
     
  15. impbox

    impbox

    Joined:
    Mar 31, 2017
    Posts:
    5
  16. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    1,972
  17. MrHaribo

    MrHaribo

    Joined:
    Aug 9, 2012
    Posts:
    14
    Hello @MelvMay

    I am working on a 2D Multiplayer Game in Unity. It absolutely needs an authoritative server so i am stuck with implementing server side physics with client-side prediction wich requires some kind of way to rewind and replay different Ridigbody2D individually.

    I Stumbled on the thread https://forum.unity.com/threads/2d-...ion-simulation-groups-multiple-worlds.466501/ about different simulation groups to simulate rigidbodies individually which could greatly help with rewind and replay.

    My question is, how is the progress on simulation groups? Is there an experimental build for Unity 2019 i could use wich has the feature enabled?

    My current approach is to enable/disable all Ridigbodies but one, but this approach seems poor and so look the results. Do you have another idea how i could Simulate Ridigbody2D individually? Is it possible to set the Property Rigidbody2D.simulated to false to simulate rigidbodies individually?

    Thanks for your reply in advance.
     
    foxnne likes this.
  18. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    1,972
    No work has been done on it.

    There's no way to simulate a single Rigidbody2D and it would be pretty expensive to go through a whole simulation step per Rigidbody2D. Maybe you can just control the body using physics queries and move it kinematically. That way you have full control over how it moves and where it ends up.
     
  19. MrHaribo

    MrHaribo

    Joined:
    Aug 9, 2012
    Posts:
    14
    Thanx for your quick reply.

    My game has rather simple physics so i hope it will not be to complicated to resolve collisions manually. I hope that i can rely on Trigger Callbacks for collision detection.
     
    Last edited: May 27, 2019
  20. impbox

    impbox

    Joined:
    Mar 31, 2017
    Posts:
    5
    Thanks for the help so far, I've managed to get multiple physicsScene2D's simulating separately, however for performance I'd like them to run in parallel rather than in sequence. I've enabled "Use Multithreading" in Physics2D Settings. However when attempting to simulate inside a IJobParallelFor

    Code (CSharp):
    1.  
    2. UnityException: IsValid_Internal can only be called from the main thread.
    3. Constructors and field initializers will be executed from the loading thread when loading a scene.
    4. Don't use this function in the constructor or field initializers, instead move initialization code to the Awake or Start function.
    5. UnityEngine.PhysicsScene2D.IsValid_Internal (UnityEngine.PhysicsScene2D physicsScene) <0x14485d04ad0 + 0x00052> in <b8f5f0ad853f4c859c01e6abc87868f0>:0
    6. UnityEngine.PhysicsScene2D.IsValid () (at C:/buildslave/unity/build/Modules/Physics2D/ScriptBindings/Physics2D.bindings.cs:39)
    7. UnityEngine.PhysicsScene2D.Simulate (System.Single step) (at C:/buildslave/unity/build/Modules/Physics2D/ScriptBindings/Physics2D.bindings.cs:59)
    8. GameManager+PhysicsJob.Execute (System.Int32 i) (at Assets/Scripts/GameManager.cs:1167)
    9. Unity.Jobs.IJobParallelForExtensions+ParallelForJobStruct`1[T].Execute (T& jobData, System.IntPtr additionalPtr, System.IntPtr bufferRangePatchData, Unity.Jobs.LowLevel.Unsafe.JobRanges& ranges, System.Int32 jobIndex) (at C:/buildslave/unity/build/Runtime/Jobs/Managed/IJobParallelFor.cs:43)
    10.  
    Is there a way to Simulate multiple PhysicsScene2Ds in parallel?
     
  21. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    1,972
    That means when you call simulate either explicitly or Unity calls it automatically then it goes wide across all cores. It does not allow you to perform multiple simulations in parallel from a C# job. This is mostly because of the main-thread actions it needs to do such as performing script callbacks etc.
     
  22. impbox

    impbox

    Joined:
    Mar 31, 2017
    Posts:
    5
    Thanks. I see, does that mean it's impossible to do parallel 2d physics simulations in Unity as of now, or is there another way to do this?
     
  23. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    1,972
    It can't be done for now and the only way I can think of is to place your different physics objects in separate scenes but separate them spatially and use additive loading to merge them into one big scene which you can simulate using all the cores.
     
  24. impbox

    impbox

    Joined:
    Mar 31, 2017
    Posts:
    5
    Ok! Thanks for confirming!