Search Unity

Help Wanted Updating physics world whilst timeScale / fixedDeltaTime is 0

Discussion in 'DOTS Physics' started by doompr, May 11, 2021.

  1. doompr

    doompr

    Joined:
    Feb 27, 2015
    Posts:
    11
    Hey everyone!

    So I've got a scene with various dynamic objects and static objects with colliders. At runtime you can select objects with raycasts and move them using custom tools, only the static objects can be moved.

    I got a time control option which sets the timeScale + fixedDeltaTime to 0 to stop all dynamic objects from moving, but obviously when the time is stopped all the systems stop too. This results into being able to move the static objects once (using the tools), but their colliders remain at their old position. So trying to select them with the raycasts only works if you click at their original position.

    This same issue persists when I instantiate an entity with a collider, the entity is being made and visible in the entity debugger window, but the collision isnt build.

    Is there a way to manually update the physics world for example everytime I move an object?
    I've tried calling World.GetOrCreateSystem<BuildPhysicsWorld>().Update() but that didn't do the trick.

    Thanks in advance!
     
  2. steveeHavok

    steveeHavok

    Joined:
    Mar 19, 2019
    Posts:
    480
    The colliders should be moved but if you are firing a raycast through the PhysicsWorld then it sounds like you need to update the broadphase e.g.

    Code (CSharp):
    1. myPhysicsWorld.CollisionWorld.BuildBroadphase(ref myPhysicsWorld, TimeStep, Gravity);
    The SingleThreadPhysics test calls this directly.
     
  3. doompr

    doompr

    Joined:
    Feb 27, 2015
    Posts:
    11
    Hmm that didn't seem to have any effect unfortunately.
    This is what i'm using at the moment, with the raycast being fired after this. (In a normal Monobehavior script, fired when clicking)

    Code (CSharp):
    1.  
    2. var physicsWorldSystem = World.DefaultGameObjectInjectionWorld.GetExistingSystem<Unity.Physics.Systems.BuildPhysicsWorld>();
    3.  
    4. physicsWorldSystem.PhysicsWorld.CollisionWorld.BuildBroadphase(ref physicsWorldSystem.PhysicsWorld, Time.deltaTime, Physics.gravity);
    Firing the raycast in the original position lets me select it, but directly clicking on the visual after moving it doesn't. I'm using the EntityManager in a normal Monobehavior script to move the collider entities. (They're attached to a normal GameObject which acts as the visual)

    So 'm using
    Code (CSharp):
    1. manager.SetComponentData(entity, new Translation { Value = (new value which i've omitted for it's size)});
    This part is being called properly, and the Translation values move in the Entity Debugger window, but it could be that perhaps?

    Edit: added picture of broadphase debug & collider debug
     

    Attached Files:

    Last edited: May 12, 2021
  4. milos85miki

    milos85miki

    Joined:
    Nov 29, 2019
    Posts:
    197
    Setting Translation is the right thing to do, but it's not enough. In addition to ECS components (Translation), you need to update the PhysicsWorld. That's a part of what BuildPhysicsWorld.OnUpdate() does, but you can also build the PhysicsWorld on your own, as shown in "Build the world" section of SingleThreadedPhysicsSystem.OnUpdate(). Then you can build the broadphase and expect casts to hit new collider positions.
     
    doompr likes this.
  5. doompr

    doompr

    Joined:
    Feb 27, 2015
    Posts:
    11
    Thanks! I did try the BuildPhysicsWorld.OnUpdate() but without building the broadphase, and later with @steveeHavok 's post just the broadphase but never together.

    Just tested it and it seems to work as expected :)! Thanks again
     
    steveeHavok likes this.
unityunity