Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Multiple physic simulations running in different threads (Bullet or Physx)?

Discussion in 'Physics' started by mauriciosaro, Jun 14, 2018.

  1. mauriciosaro

    mauriciosaro

    Joined:
    Feb 20, 2018
    Posts:
    5
    Out team is using Unity as a simulation for path planning of robot arms. This mainly requires collision checking and other relevant information such as shortest distances between colliders. We also require to be able to access the simulation from various different threads that work at different speeds. For example, arm hardware will be constantly updating the simulation with it's current position every 4-8ms, while sensors might be changing object positions every 8-20 ms from a different thread and the planner querying for collisions in a third thread.

    After much research into the new changes and the new job system added to Unity, I still don't think what we need can be done with Unity and it's default Physx integration.

    Our current solution is to integrate the Bullet physics engine through BulletSharp in Unity with a little help from this package: https://assetstore.unity.com/packages/tools/physics/bullet-physics-for-unity-62991

    We then create a main Bullet world which is in synch with Unity and various copied worlds which are used by the different threads. This require that we move objects in the Bullet simulation from other threads and then synch with Unity in the main thread.

    While the solution works, it requires a major change in the normal Unity workflow which causes the loss of some of the features from Unity.

    I am curious if there is a better or more straightforward approach, or if someone has some experience doing something similar with Unity.

    Thanks in advance
     
  2. snacktime

    snacktime

    Joined:
    Apr 15, 2013
    Posts:
    3,356
    I think you might be jumping ahead of yourself here. Like you don't actually give end reasons why you need what you say you do. You say you want to update some things on different intervals but not why. Like if you just want things to move at different speeds, you don't need any of what you are talking about.

    My guess is what might work well is kinematic controllers you move manually combined with using say ComputePenetration to do simple depenetration. That would likely give you the most fine grained control over everything.
     
  3. mauriciosaro

    mauriciosaro

    Joined:
    Feb 20, 2018
    Posts:
    5
    @snacktime

    we are using this to control robot hardware in the real world. To do this we have multiple parts which need their own simulation world.

    On one side, the arm controller (the controller which actually moves the robot arm in the real world) needs to constantly update Unity at a very fast rate (~4ms) and perform hypotheticals in it's own copy of the simulation.
    At the same time the planner will be running hypotheticals in it's own thread and simulation to compute a plan
    At the same time the AI image detection system will be updating the positions of colliders in it's own thread

    So multithreading and multiple physics world is a main requirement here.

    As an update though: I've fully integrated Bullet and created a WorldManager to manage multiple simulation worlds and synch between them. Also added a BulletTransform which replaces the Unity Transform component. This way everything happens in the Bullet world and Unity is just a renderer. We can even select which of the Bullet worlds we've created we want to render (mostly for debugging purposes) Or separate them into different layers and use culling to render multiple worlds at once using split screen.

    All logic can be done in different threads without any problems and the Main thread is just updating the Unity transforms according to the current Bullet World to be rendered.