Search Unity

Question How to use PhysicsWorld.ApplyLinearImpulse inside of a job

Discussion in 'Physics for ECS' started by Occuros, Dec 16, 2022.

  1. Occuros

    Occuros

    Joined:
    Sep 4, 2018
    Posts:
    300
    The physics world has an ApplyLinearImpulse method.

    But how can we pass a RW physicsworld singleton to a job, in a way that it can be used to apply the Impulse?

    A simple example on how to use PhysicsWorld.ApplyLinearImpulse inside of an IJobEntity inside of an ISystem would be appreciated.

    The following didn't work:

    Code (CSharp):
    1.  public partial struct ExplosivesSystem : ISystem
    2.     {
    3.         [BurstCompile]
    4.         public void OnCreate(ref SystemState state)
    5.         {
    6.         }
    7.  
    8.         [BurstCompile]
    9.         public void OnDestroy(ref SystemState state)
    10.         {
    11.         }
    12.  
    13.         [BurstCompile]
    14.         public void OnUpdate(ref SystemState state)
    15.         {
    16.             var physicsWorld = SystemAPI.GetSingletonRW<PhysicsWorldSingleton>().ValueRW.PhysicsWorld;
    17.  
    18.             var job = new ExplosivesJob()
    19.             {
    20.                 PhysicsWorld = physicsWorld,
    21.             };
    22.            
    23.             job.Schedule();
    24.         }
    25.  
    26.         [BurstCompile]
    27.         [WithAll(typeof(Simulate), typeof(ExplosiveComponent))]
    28.         private unsafe partial struct ExplosivesJob: IJobEntity
    29.         {
    30.             public PhysicsWorld PhysicsWorld;
    31.             public void Execute(Entity entity,
    32.                                 in DynamicBuffer<StatefulTriggerEvent> triggerEvents
    33.             )
    34.             {
    35.                 foreach (var triggerEvent in triggerEvents)
    36.                 {
    37.                     var rbIndex = triggerEvent.EntityA == entity
    38.                         ? triggerEvent.BodyIndexB
    39.                         : triggerEvent.BodyIndexA;
    40.                     if (triggerEvent.State == StatefulEventState.Enter)
    41.                     {
    42.                         PhysicsWorld.ApplyLinearImpulse(rbIndex, math.up() * 10000);
    43.                     }
    44.                 }
    45.             }
    46.         }
     
  2. Spy-Master

    Spy-Master

    Joined:
    Aug 4, 2022
    Posts:
    635
    https://docs.unity3d.com/Packages/c...fication.html#directly-modifying-physicsworld
     
  3. Occuros

    Occuros

    Joined:
    Sep 4, 2018
    Posts:
    300
  4. Spy-Master

    Spy-Master

    Joined:
    Aug 4, 2022
    Posts:
    635
    Shouldn't matter either way.
    The extension methods for PhysicsWorld will operate on copies of the main structs, true, but the mutated state is all in PhysicsWorld.DynamicsWorld.m_MotionVelocities, a NativeArray that you would still be writing to correctly. If there aren't state changes, that would suggest at least these scenarios
    -There's some trivial incorrect system order where the changes are being made but being overwritten.
    -The job or some required state for the job is incorrect, such that the job is not running, or perhaps the events system is currently bugged.
    -Changing MotionVelocities could be completely bugged. Testable by trying to call the extension methods somewhere else.