Search Unity

Question Runtime creation of CustomPhysicsSystemGroup

Discussion in 'Physics for ECS' started by Thygrrr, Jan 6, 2023.

  1. Thygrrr

    Thygrrr

    Joined:
    Sep 23, 2013
    Posts:
    700
    I'm porting my previous, customized GroupIndex / Tag based multi-world to the new Entities 1.0 physics worlds, but it seems the split into worlds happens... at compile time?

    How can I instantiate and register a new CustomPhysicsSystemGroup at runtime?

    Or is there another way? I need to differentiate between around 100 physics worlds, as the minimum. Most are very sparse, often with all bodies at sleep.

    Some need to be created on the fly; I can work out a pooling / index assignment system but I wonder what some good approaches would be, other than creating a file with 100 descentants of CustomPhysicsSystemGroup to get this going.

    Edit: The challenge appears to be the baffling behaviour of AddSystemManaged to disallow multiple additions of the same system. I can also not construct different instances because CreateSystem does not allow parameters to be passed into the constructor.
    upload_2023-1-6_13-39-13.png

    Following Unity tradition of selectively making random fields likely relevant for typical extension cases inaccessibe to implementors, of course CustomPhysicsSystemGroupBase.m_WorldIndex is private even though it would be perfectly fine to write to it during a descendant class object's construction.

    upload_2023-1-6_13-41-36.png
     
    Last edited: Jan 6, 2023
  2. elliotc-unity

    elliotc-unity

    Unity Technologies

    Joined:
    Nov 5, 2015
    Posts:
    230
    I don't completely understand the usage here, but it sounds to me like you want to process groups of entities together, grouped by a particular index value. This is a perfect job for shared components; you would make a query or a job for a particular value of a shared component that represented which physics world you were talking about, and do that separately for each value. Shared components also make sure that entities with distinct values of a given shared component are grouped into separate chunks, which seems convenient for this.

    (Moreover, if you're using Unity.Physics, this is already how entities are grouped into particular worlds, via the
    PhysicsWorldIndex
    shared component.)

    Does that sound promising?

    In general, having per-instance data on a system instance itself is not recommended, nor is having multiple instances of the same system in a single world, which is why we changed the API this way. The happy path is to have one of each system, which processes ALLL the relevant data in a given world in one shot. This reduces system overhead, too, and works better with ISystem, which can also be bursted.
     
  3. Thygrrr

    Thygrrr

    Joined:
    Sep 23, 2013
    Posts:
    700
    Yup, this is exactly what I am trying to achive!

    According to the docs, I need to manually instantiate my own Physics systems for each PhysicsWorldIndex beyond index zero I wish to simulate.

    The only way I have found to do this is via AddSystemManaged<T>, with T being a CustomPhysicsSystemGroup inheritor. This unfortunately doesn't allow you to use a class more than once, and due it to being a genreric, it's not trivially obvious how to create these CustomPhysicsSystemGroup inheritors programmatically.

    I cannot do this, for instance:
    Code (CSharp):
    1.             for (uint i = 1; i < 100; i++)
    2.             {
    3.                 World.DefaultGameObjectInjectionWorld.AddSystemManaged(new CustomPhysicsSystemGroup(i));
    4.             }
    5.  
    (this does not compile because the constructor of CPSG is protected)

    nor can I do this:
    Code (CSharp):
    1.             for (uint i = 1; i < 100; i++)
    2.             {
    3.                 World.DefaultGameObjectInjectionWorld.AddSystemManaged(new MyCPSG(i));
    4.             }
    5.  
    (this fails at runtime, because AddSystemManaged will reject any system type that has an instance already added)

    Before I delve deep into reflection to create the types at runtime, or to spend days reading up on Roslyn and how that plays with Burst, I wanted to ask here if I am seeing things incorrectly.


    To elaborate on my use case:
    I have a floating origin space system that partitions the world into physics "bubbles", each with their (local) origin. Examples: The surface of a moon, or the vicinity of a space station, or a debris field in deep space, etc.

    On any entities not in the current bubble (indeed! perfect use case for a SharedComponent filter) the player interacts with, I add "DisableRendering", therefore they are still simulated, just invisible. I merely arrange my camera stack accordingly to make it look like the player is somewhere millions of miles away; and it works like a charm.

    There are a couple hundred of these worlds (up to a thousand, but most of them rather sparse or completely static - it's a space game, and one of the longer term goals would be to also tick update these distant worlds a bit less frequently, if ever necessary). Either way, the hard minimum of PhysicsWorldIndices needed is around a hundred, the dream is maybe a thousand, or arbitrary creation and removal of these bubbles as needed.

    What I had before entities 1.0:
    I used to solve this with a hack to CollisionFilters prior to Entities 0.50: I changed the code so groupIndex requires equality for any collisions to take place, and then resolution is exactly the same as if groupIndex is 0.

    Unfortunately these are not a IComponentData/ISharedComponentData, but instead, and incomprehensibly, a field inside the collider blobs (see below for other problems this causes), and I don't feel running tens of thousands of "force unique" physics shapes is the right answer, but I don't have a better one. I know this probably interferes with some of the BVHs, but I figured I could mitigate this by staggering the local origins so not every bubble has a mesh collidr right at 0,0,0.

    Tangent: Other problems CollisionFilter as part of colliders causes is that it makes changing collision behaviour of entities rather tedious, e.g. a rocket being launched from a space ship no longer can temporarily change its CollisionFilter but needs to switch to an entirely different collider copy in order to "arm" itself after leaving the space it shares with the ship's collider. I can't be the only one who's run into this one, maybe I am looking at it wrong?
     
    Last edited: Mar 3, 2023
  4. Thygrrr

    Thygrrr

    Joined:
    Sep 23, 2013
    Posts:
    700
    I mean, surely this isn't really the way multiple physics worlds / layers are intended to be declared and instantiated? Thanks btw, python! :p

    (this creates a large number of clashing sync points... this can't be the way, each system takes longer than the single index 0 system currently holding ALL the objects with ease. it also makes PhysicsWorldSingleton pretty useless and thus works very poorly with raycasts, where formerly I would merely need to change my interaction - e.g. mouseover - to raycast using a different groupIndex)

    I think I will go back to hacking collision filters unless you can maybe suggest a fundamentally alternative approach, I'm really curious. :)

    Code (CSharp):
    1. using Unity.Entities;
    2. using Unity.Physics.Systems;
    3.  
    4. namespace Jovian.Systems
    5. {
    6.     [UpdateInGroup(typeof(FixedStepSimulationSystemGroup))]
    7.     public partial class PhysicsBubbleSystem1 : CustomPhysicsSystemGroup
    8.     {
    9.         public PhysicsBubbleSystem1() : base(1, false)
    10.         {
    11.         }
    12.     }
    13.  
    14.  
    15.     [UpdateInGroup(typeof(FixedStepSimulationSystemGroup))]
    16.     public partial class PhysicsBubbleSystem2 : CustomPhysicsSystemGroup
    17.     {
    18.         public PhysicsBubbleSystem2() : base(2, false)
    19.         {
    20.         }
    21.     }
    22.  
    23.  
    24.     [UpdateInGroup(typeof(FixedStepSimulationSystemGroup))]
    25.     public partial class PhysicsBubbleSystem3 : CustomPhysicsSystemGroup
    26.     {
    27.         public PhysicsBubbleSystem3() : base(3, false)
    28.         {
    29.         }
    30.     }
    31.  
    32.  
    33.     [UpdateInGroup(typeof(FixedStepSimulationSystemGroup))]
    34.     public partial class PhysicsBubbleSystem4 : CustomPhysicsSystemGroup
    35.     {
    36.         public PhysicsBubbleSystem4() : base(4, false)
    37.         {
    38.         }
    39.     }
    40.  
    41.  
    42.     [UpdateInGroup(typeof(FixedStepSimulationSystemGroup))]
    43.     public partial class PhysicsBubbleSystem5 : CustomPhysicsSystemGroup
    44.     {
    45.         public PhysicsBubbleSystem5() : base(5, false)
    46.         {
    47.         }
    48.     }
    49.  
    50.  
    51.     [UpdateInGroup(typeof(FixedStepSimulationSystemGroup))]
    52.     public partial class PhysicsBubbleSystem6 : CustomPhysicsSystemGroup
    53.     {
    54.         public PhysicsBubbleSystem6() : base(6, false)
    55.         {
    56.         }
    57.     }
    58.  
    59.  
    60.     [UpdateInGroup(typeof(FixedStepSimulationSystemGroup))]
    61.     public partial class PhysicsBubbleSystem7 : CustomPhysicsSystemGroup
    62.     {
    63.         public PhysicsBubbleSystem7() : base(7, false)
    64.         {
    65.         }
    66.     }
    67.  
    68.  
    69.     [UpdateInGroup(typeof(FixedStepSimulationSystemGroup))]
    70.     public partial class PhysicsBubbleSystem8 : CustomPhysicsSystemGroup
    71.     {
    72.         public PhysicsBubbleSystem8() : base(8, false)
    73.         {
    74.         }
    75.     }
    76.  
    77.  
    78.     [UpdateInGroup(typeof(FixedStepSimulationSystemGroup))]
    79.     public partial class PhysicsBubbleSystem9 : CustomPhysicsSystemGroup
    80.     {
    81.         public PhysicsBubbleSystem9() : base(9, false)
    82.         {
    83.         }
    84.     }
    85.  
    86.  
    87.     [UpdateInGroup(typeof(FixedStepSimulationSystemGroup))]
    88.     public partial class PhysicsBubbleSystem10 : CustomPhysicsSystemGroup
    89.     {
    90.         public PhysicsBubbleSystem10() : base(10, false)
    91.         {
    92.         }
    93.     }
    94.  
    95.  
    96.     [UpdateInGroup(typeof(FixedStepSimulationSystemGroup))]
    97.     public partial class PhysicsBubbleSystem11 : CustomPhysicsSystemGroup
    98.     {
    99.         public PhysicsBubbleSystem11() : base(11, false)
    100.         {
    101.         }
    102.     }
    103.  
    104.  
    105.     [UpdateInGroup(typeof(FixedStepSimulationSystemGroup))]
    106.     public partial class PhysicsBubbleSystem12 : CustomPhysicsSystemGroup
    107.     {
    108.         public PhysicsBubbleSystem12() : base(12, false)
    109.         {
    110.         }
    111.     }
    112.  
    113.  
    114.     [UpdateInGroup(typeof(FixedStepSimulationSystemGroup))]
    115.     public partial class PhysicsBubbleSystem13 : CustomPhysicsSystemGroup
    116.     {
    117.         public PhysicsBubbleSystem13() : base(13, false)
    118.         {
    119.         }
    120.     }
    121.  
    122.  
    123.     [UpdateInGroup(typeof(FixedStepSimulationSystemGroup))]
    124.     public partial class PhysicsBubbleSystem14 : CustomPhysicsSystemGroup
    125.     {
    126.         public PhysicsBubbleSystem14() : base(14, false)
    127.         {
    128.         }
    129.     }
    130.  
    131.  
    132.     [UpdateInGroup(typeof(FixedStepSimulationSystemGroup))]
    133.     public partial class PhysicsBubbleSystem15 : CustomPhysicsSystemGroup
    134.     {
    135.         public PhysicsBubbleSystem15() : base(15, false)
    136.         {
    137.         }
    138.     }
    139.  
    140.  
    141.     [UpdateInGroup(typeof(FixedStepSimulationSystemGroup))]
    142.     public partial class PhysicsBubbleSystem16 : CustomPhysicsSystemGroup
    143.     {
    144.         public PhysicsBubbleSystem16() : base(16, false)
    145.         {
    146.         }
    147.     }
    148.  
    149.  
    150.     [UpdateInGroup(typeof(FixedStepSimulationSystemGroup))]
    151.     public partial class PhysicsBubbleSystem17 : CustomPhysicsSystemGroup
    152.     {
    153.         public PhysicsBubbleSystem17() : base(17, false)
    154.         {
    155.         }
    156.     }
    157.  
    158.  
    159.     [UpdateInGroup(typeof(FixedStepSimulationSystemGroup))]
    160.     public partial class PhysicsBubbleSystem18 : CustomPhysicsSystemGroup
    161.     {
    162.         public PhysicsBubbleSystem18() : base(18, false)
    163.         {
    164.         }
    165.     }
    166.  
    167.  
    168.     [UpdateInGroup(typeof(FixedStepSimulationSystemGroup))]
    169.     public partial class PhysicsBubbleSystem19 : CustomPhysicsSystemGroup
    170.     {
    171.         public PhysicsBubbleSystem19() : base(19, false)
    172.         {
    173.         }
    174.     }
    175.  
    176.  
    177.     [UpdateInGroup(typeof(FixedStepSimulationSystemGroup))]
    178.     public partial class PhysicsBubbleSystem20 : CustomPhysicsSystemGroup
    179.     {
    180.         public PhysicsBubbleSystem20() : base(20, false)
    181.         {
    182.         }
    183.     }
    184.  
    185.  
    186.     [UpdateInGroup(typeof(FixedStepSimulationSystemGroup))]
    187.     public partial class PhysicsBubbleSystem21 : CustomPhysicsSystemGroup
    188.     {
    189.         public PhysicsBubbleSystem21() : base(21, false)
    190.         {
    191.         }
    192.     }
    193.  
    194.  
    195.     [UpdateInGroup(typeof(FixedStepSimulationSystemGroup))]
    196.     public partial class PhysicsBubbleSystem22 : CustomPhysicsSystemGroup
    197.     {
    198.         public PhysicsBubbleSystem22() : base(22, false)
    199.         {
    200.         }
    201.     }
    202.  
    203.  
    204.     [UpdateInGroup(typeof(FixedStepSimulationSystemGroup))]
    205.     public partial class PhysicsBubbleSystem23 : CustomPhysicsSystemGroup
    206.     {
    207.         public PhysicsBubbleSystem23() : base(23, false)
    208.         {
    209.         }
    210.     }
    211.  
    212.  
    213.     [UpdateInGroup(typeof(FixedStepSimulationSystemGroup))]
    214.     public partial class PhysicsBubbleSystem24 : CustomPhysicsSystemGroup
    215.     {
    216.         public PhysicsBubbleSystem24() : base(24, false)
    217.         {
    218.         }
    219.     }
    220.  
    221.  
    222.     [UpdateInGroup(typeof(FixedStepSimulationSystemGroup))]
    223.     public partial class PhysicsBubbleSystem25 : CustomPhysicsSystemGroup
    224.     {
    225.         public PhysicsBubbleSystem25() : base(25, false)
    226.         {
    227.         }
    228.     }
    229.  
    230.  
    231.     [UpdateInGroup(typeof(FixedStepSimulationSystemGroup))]
    232.     public partial class PhysicsBubbleSystem26 : CustomPhysicsSystemGroup
    233.     {
    234.         public PhysicsBubbleSystem26() : base(26, false)
    235.         {
    236.         }
    237.     }
    238.  
    239.  
    240.     [UpdateInGroup(typeof(FixedStepSimulationSystemGroup))]
    241.     public partial class PhysicsBubbleSystem27 : CustomPhysicsSystemGroup
    242.     {
    243.         public PhysicsBubbleSystem27() : base(27, false)
    244.         {
    245.         }
    246.     }
    247.  
    248.  
    249.     [UpdateInGroup(typeof(FixedStepSimulationSystemGroup))]
    250.     public partial class PhysicsBubbleSystem28 : CustomPhysicsSystemGroup
    251.     {
    252.         public PhysicsBubbleSystem28() : base(28, false)
    253.         {
    254.         }
    255.     }
    256.  
    257.  
    258.     [UpdateInGroup(typeof(FixedStepSimulationSystemGroup))]
    259.     public partial class PhysicsBubbleSystem29 : CustomPhysicsSystemGroup
    260.     {
    261.         public PhysicsBubbleSystem29() : base(29, false)
    262.         {
    263.         }
    264.     }
    265.  
    266.  
    267.     [UpdateInGroup(typeof(FixedStepSimulationSystemGroup))]
    268.     public partial class PhysicsBubbleSystem30 : CustomPhysicsSystemGroup
    269.     {
    270.         public PhysicsBubbleSystem30() : base(30, false)
    271.         {
    272.         }
    273.     }
    274.  
    275.  
    276.     [UpdateInGroup(typeof(FixedStepSimulationSystemGroup))]
    277.     public partial class PhysicsBubbleSystem31 : CustomPhysicsSystemGroup
    278.     {
    279.         public PhysicsBubbleSystem31() : base(31, false)
    280.         {
    281.         }
    282.     }
    283.  
    284.  
    285.     [UpdateInGroup(typeof(FixedStepSimulationSystemGroup))]
    286.     public partial class PhysicsBubbleSystem32 : CustomPhysicsSystemGroup
    287.     {
    288.         public PhysicsBubbleSystem32() : base(32, false)
    289.         {
    290.         }
    291.     }
    292.  
    293.  
    294.     [UpdateInGroup(typeof(FixedStepSimulationSystemGroup))]
    295.     public partial class PhysicsBubbleSystem33 : CustomPhysicsSystemGroup
    296.     {
    297.         public PhysicsBubbleSystem33() : base(33, false)
    298.         {
    299.         }
    300.     }
    301.  
    302.  
    303.     [UpdateInGroup(typeof(FixedStepSimulationSystemGroup))]
    304.     public partial class PhysicsBubbleSystem34 : CustomPhysicsSystemGroup
    305.     {
    306.         public PhysicsBubbleSystem34() : base(34, false)
    307.         {
    308.         }
    309.     }
    310.  
    311.  
    312.     [UpdateInGroup(typeof(FixedStepSimulationSystemGroup))]
    313.     public partial class PhysicsBubbleSystem35 : CustomPhysicsSystemGroup
    314.     {
    315.         public PhysicsBubbleSystem35() : base(35, false)
    316.         {
    317.         }
    318.     }
    319.  
    320.  
    321.     [UpdateInGroup(typeof(FixedStepSimulationSystemGroup))]
    322.     public partial class PhysicsBubbleSystem36 : CustomPhysicsSystemGroup
    323.     {
    324.         public PhysicsBubbleSystem36() : base(36, false)
    325.         {
    326.         }
    327.     }
    328.  
    329.  
    330.     [UpdateInGroup(typeof(FixedStepSimulationSystemGroup))]
    331.     public partial class PhysicsBubbleSystem37 : CustomPhysicsSystemGroup
    332.     {
    333.         public PhysicsBubbleSystem37() : base(37, false)
    334.         {
    335.         }
    336.     }
    337.  
    338.  
    339.     [UpdateInGroup(typeof(FixedStepSimulationSystemGroup))]
    340.     public partial class PhysicsBubbleSystem38 : CustomPhysicsSystemGroup
    341.     {
    342.         public PhysicsBubbleSystem38() : base(38, false)
    343.         {
    344.         }
    345.     }
    346.  
    347.  
    348.     [UpdateInGroup(typeof(FixedStepSimulationSystemGroup))]
    349.     public partial class PhysicsBubbleSystem39 : CustomPhysicsSystemGroup
    350.     {
    351.         public PhysicsBubbleSystem39() : base(39, false)
    352.         {
    353.         }
    354.     }
    355.  
    356.  
    357.     [UpdateInGroup(typeof(FixedStepSimulationSystemGroup))]
    358.     public partial class PhysicsBubbleSystem40 : CustomPhysicsSystemGroup
    359.     {
    360.         public PhysicsBubbleSystem40() : base(40, false)
    361.         {
    362.         }
    363.     }
    364.  
    365.  
    366.     [UpdateInGroup(typeof(FixedStepSimulationSystemGroup))]
    367.     public partial class PhysicsBubbleSystem41 : CustomPhysicsSystemGroup
    368.     {
    369.         public PhysicsBubbleSystem41() : base(41, false)
    370.         {
    371.         }
    372.     }
    373.  
    374.  
    375.     [UpdateInGroup(typeof(FixedStepSimulationSystemGroup))]
    376.     public partial class PhysicsBubbleSystem42 : CustomPhysicsSystemGroup
    377.     {
    378.         public PhysicsBubbleSystem42() : base(42, false)
    379.         {
    380.         }
    381.     }
    382.  
    383.  
    384.     [UpdateInGroup(typeof(FixedStepSimulationSystemGroup))]
    385.     public partial class PhysicsBubbleSystem43 : CustomPhysicsSystemGroup
    386.     {
    387.         public PhysicsBubbleSystem43() : base(43, false)
    388.         {
    389.         }
    390.     }
    391.  
    392.  
    393.     [UpdateInGroup(typeof(FixedStepSimulationSystemGroup))]
    394.     public partial class PhysicsBubbleSystem44 : CustomPhysicsSystemGroup
    395.     {
    396.         public PhysicsBubbleSystem44() : base(44, false)
    397.         {
    398.         }
    399.     }
    400.  
    401.  
    402.     [UpdateInGroup(typeof(FixedStepSimulationSystemGroup))]
    403.     public partial class PhysicsBubbleSystem45 : CustomPhysicsSystemGroup
    404.     {
    405.         public PhysicsBubbleSystem45() : base(45, false)
    406.         {
    407.         }
    408.     }
    409.  
    410.  
    411.     [UpdateInGroup(typeof(FixedStepSimulationSystemGroup))]
    412.     public partial class PhysicsBubbleSystem46 : CustomPhysicsSystemGroup
    413.     {
    414.         public PhysicsBubbleSystem46() : base(46, false)
    415.         {
    416.         }
    417.     }
    418.  
    419.  
    420.     [UpdateInGroup(typeof(FixedStepSimulationSystemGroup))]
    421.     public partial class PhysicsBubbleSystem47 : CustomPhysicsSystemGroup
    422.     {
    423.         public PhysicsBubbleSystem47() : base(47, false)
    424.         {
    425.         }
    426.     }
    427.  
    428.  
    429.     [UpdateInGroup(typeof(FixedStepSimulationSystemGroup))]
    430.     public partial class PhysicsBubbleSystem48 : CustomPhysicsSystemGroup
    431.     {
    432.         public PhysicsBubbleSystem48() : base(48, false)
    433.         {
    434.         }
    435.     }
    436.  
    437.  
    438.     [UpdateInGroup(typeof(FixedStepSimulationSystemGroup))]
    439.     public partial class PhysicsBubbleSystem49 : CustomPhysicsSystemGroup
    440.     {
    441.         public PhysicsBubbleSystem49() : base(49, false)
    442.         {
    443.         }
    444.     }
    445.  
    446.  
    447.     [UpdateInGroup(typeof(FixedStepSimulationSystemGroup))]
    448.     public partial class PhysicsBubbleSystem50 : CustomPhysicsSystemGroup
    449.     {
    450.         public PhysicsBubbleSystem50() : base(50, false)
    451.         {
    452.         }
    453.     }
    454.  
    455.  
    456.     [UpdateInGroup(typeof(FixedStepSimulationSystemGroup))]
    457.     public partial class PhysicsBubbleSystem51 : CustomPhysicsSystemGroup
    458.     {
    459.         public PhysicsBubbleSystem51() : base(51, false)
    460.         {
    461.         }
    462.     }
    463.  
    464.  
    465.     [UpdateInGroup(typeof(FixedStepSimulationSystemGroup))]
    466.     public partial class PhysicsBubbleSystem52 : CustomPhysicsSystemGroup
    467.     {
    468.         public PhysicsBubbleSystem52() : base(52, false)
    469.         {
    470.         }
    471.     }
    472.  
    473.  
    474.     [UpdateInGroup(typeof(FixedStepSimulationSystemGroup))]
    475.     public partial class PhysicsBubbleSystem53 : CustomPhysicsSystemGroup
    476.     {
    477.         public PhysicsBubbleSystem53() : base(53, false)
    478.         {
    479.         }
    480.     }
    481.  
    482.  
    483.     [UpdateInGroup(typeof(FixedStepSimulationSystemGroup))]
    484.     public partial class PhysicsBubbleSystem54 : CustomPhysicsSystemGroup
    485.     {
    486.         public PhysicsBubbleSystem54() : base(54, false)
    487.         {
    488.         }
    489.     }
    490.  
    491.  
    492.     [UpdateInGroup(typeof(FixedStepSimulationSystemGroup))]
    493.     public partial class PhysicsBubbleSystem55 : CustomPhysicsSystemGroup
    494.     {
    495.         public PhysicsBubbleSystem55() : base(55, false)
    496.         {
    497.         }
    498.     }
    499.  
    500.  
    501.     [UpdateInGroup(typeof(FixedStepSimulationSystemGroup))]
    502.     public partial class PhysicsBubbleSystem56 : CustomPhysicsSystemGroup
    503.     {
    504.         public PhysicsBubbleSystem56() : base(56, false)
    505.         {
    506.         }
    507.     }
    508.  
    509.  
    510.     [UpdateInGroup(typeof(FixedStepSimulationSystemGroup))]
    511.     public partial class PhysicsBubbleSystem57 : CustomPhysicsSystemGroup
    512.     {
    513.         public PhysicsBubbleSystem57() : base(57, false)
    514.         {
    515.         }
    516.     }
    517.  
    518.  
    519.     [UpdateInGroup(typeof(FixedStepSimulationSystemGroup))]
    520.     public partial class PhysicsBubbleSystem58 : CustomPhysicsSystemGroup
    521.     {
    522.         public PhysicsBubbleSystem58() : base(58, false)
    523.         {
    524.         }
    525.     }
    526.  
    527.  
    528.     [UpdateInGroup(typeof(FixedStepSimulationSystemGroup))]
    529.     public partial class PhysicsBubbleSystem59 : CustomPhysicsSystemGroup
    530.     {
    531.         public PhysicsBubbleSystem59() : base(59, false)
    532.         {
    533.         }
    534.     }
    535.  
    536.  
    537.     [UpdateInGroup(typeof(FixedStepSimulationSystemGroup))]
    538.     public partial class PhysicsBubbleSystem60 : CustomPhysicsSystemGroup
    539.     {
    540.         public PhysicsBubbleSystem60() : base(60, false)
    541.         {
    542.         }
    543.     }
    544.  
    545.  
    546.     [UpdateInGroup(typeof(FixedStepSimulationSystemGroup))]
    547.     public partial class PhysicsBubbleSystem61 : CustomPhysicsSystemGroup
    548.     {
    549.         public PhysicsBubbleSystem61() : base(61, false)
    550.         {
    551.         }
    552.     }
    553.  
    554.  
    555.     [UpdateInGroup(typeof(FixedStepSimulationSystemGroup))]
    556.     public partial class PhysicsBubbleSystem62 : CustomPhysicsSystemGroup
    557.     {
    558.         public PhysicsBubbleSystem62() : base(62, false)
    559.         {
    560.         }
    561.     }
    562.  
    563.  
    564.     [UpdateInGroup(typeof(FixedStepSimulationSystemGroup))]
    565.     public partial class PhysicsBubbleSystem63 : CustomPhysicsSystemGroup
    566.     {
    567.         public PhysicsBubbleSystem63() : base(63, false)
    568.         {
    569.         }
    570.     }
    571.  
    572.  
    573.     [UpdateInGroup(typeof(FixedStepSimulationSystemGroup))]
    574.     public partial class PhysicsBubbleSystem64 : CustomPhysicsSystemGroup
    575.     {
    576.         public PhysicsBubbleSystem64() : base(64, false)
    577.         {
    578.         }
    579.     }
    580.  
    581.  
    582.     [UpdateInGroup(typeof(FixedStepSimulationSystemGroup))]
    583.     public partial class PhysicsBubbleSystem65 : CustomPhysicsSystemGroup
    584.     {
    585.         public PhysicsBubbleSystem65() : base(65, false)
    586.         {
    587.         }
    588.     }
    589.  
    590.  
    591.     [UpdateInGroup(typeof(FixedStepSimulationSystemGroup))]
    592.     public partial class PhysicsBubbleSystem66 : CustomPhysicsSystemGroup
    593.     {
    594.         public PhysicsBubbleSystem66() : base(66, false)
    595.         {
    596.         }
    597.     }
    598.  
    599.  
    600.     [UpdateInGroup(typeof(FixedStepSimulationSystemGroup))]
    601.     public partial class PhysicsBubbleSystem67 : CustomPhysicsSystemGroup
    602.     {
    603.         public PhysicsBubbleSystem67() : base(67, false)
    604.         {
    605.         }
    606.     }
    607.  
    608.  
    609.     [UpdateInGroup(typeof(FixedStepSimulationSystemGroup))]
    610.     public partial class PhysicsBubbleSystem68 : CustomPhysicsSystemGroup
    611.     {
    612.         public PhysicsBubbleSystem68() : base(68, false)
    613.         {
    614.         }
    615.     }
    616.  
    617.  
    618.     [UpdateInGroup(typeof(FixedStepSimulationSystemGroup))]
    619.     public partial class PhysicsBubbleSystem69 : CustomPhysicsSystemGroup
    620.     {
    621.         public PhysicsBubbleSystem69() : base(69, false)
    622.         {
    623.         }
    624.     }
    625.  
    626.  
    627.     [UpdateInGroup(typeof(FixedStepSimulationSystemGroup))]
    628.     public partial class PhysicsBubbleSystem70 : CustomPhysicsSystemGroup
    629.     {
    630.         public PhysicsBubbleSystem70() : base(70, false)
    631.         {
    632.         }
    633.     }
    634.  
    635.  
    636.     [UpdateInGroup(typeof(FixedStepSimulationSystemGroup))]
    637.     public partial class PhysicsBubbleSystem71 : CustomPhysicsSystemGroup
    638.     {
    639.         public PhysicsBubbleSystem71() : base(71, false)
    640.         {
    641.         }
    642.     }
    643.  
    644.  
    645.     [UpdateInGroup(typeof(FixedStepSimulationSystemGroup))]
    646.     public partial class PhysicsBubbleSystem72 : CustomPhysicsSystemGroup
    647.     {
    648.         public PhysicsBubbleSystem72() : base(72, false)
    649.         {
    650.         }
    651.     }
    652.  
    653.  
    654.     [UpdateInGroup(typeof(FixedStepSimulationSystemGroup))]
    655.     public partial class PhysicsBubbleSystem73 : CustomPhysicsSystemGroup
    656.     {
    657.         public PhysicsBubbleSystem73() : base(73, false)
    658.         {
    659.         }
    660.     }
    661.  
    662.  
    663.     [UpdateInGroup(typeof(FixedStepSimulationSystemGroup))]
    664.     public partial class PhysicsBubbleSystem74 : CustomPhysicsSystemGroup
    665.     {
    666.         public PhysicsBubbleSystem74() : base(74, false)
    667.         {
    668.         }
    669.     }
    670.  
    671.  
    672.     [UpdateInGroup(typeof(FixedStepSimulationSystemGroup))]
    673.     public partial class PhysicsBubbleSystem75 : CustomPhysicsSystemGroup
    674.     {
    675.         public PhysicsBubbleSystem75() : base(75, false)
    676.         {
    677.         }
    678.     }
    679.  
    680.  
    681.     [UpdateInGroup(typeof(FixedStepSimulationSystemGroup))]
    682.     public partial class PhysicsBubbleSystem76 : CustomPhysicsSystemGroup
    683.     {
    684.         public PhysicsBubbleSystem76() : base(76, false)
    685.         {
    686.         }
    687.     }
    688.  
    689.  
    690.     [UpdateInGroup(typeof(FixedStepSimulationSystemGroup))]
    691.     public partial class PhysicsBubbleSystem77 : CustomPhysicsSystemGroup
    692.     {
    693.         public PhysicsBubbleSystem77() : base(77, false)
    694.         {
    695.         }
    696.     }
    697.  
    698.  
    699.     [UpdateInGroup(typeof(FixedStepSimulationSystemGroup))]
    700.     public partial class PhysicsBubbleSystem78 : CustomPhysicsSystemGroup
    701.     {
    702.         public PhysicsBubbleSystem78() : base(78, false)
    703.         {
    704.         }
    705.     }
    706.  
    707.  
    708.     [UpdateInGroup(typeof(FixedStepSimulationSystemGroup))]
    709.     public partial class PhysicsBubbleSystem79 : CustomPhysicsSystemGroup
    710.     {
    711.         public PhysicsBubbleSystem79() : base(79, false)
    712.         {
    713.         }
    714.     }
    715.  
    716.  
    717.     [UpdateInGroup(typeof(FixedStepSimulationSystemGroup))]
    718.     public partial class PhysicsBubbleSystem80 : CustomPhysicsSystemGroup
    719.     {
    720.         public PhysicsBubbleSystem80() : base(80, false)
    721.         {
    722.         }
    723.     }
    724.  
    725.  
    726.     [UpdateInGroup(typeof(FixedStepSimulationSystemGroup))]
    727.     public partial class PhysicsBubbleSystem81 : CustomPhysicsSystemGroup
    728.     {
    729.         public PhysicsBubbleSystem81() : base(81, false)
    730.         {
    731.         }
    732.     }
    733.  
    734.  
    735.     [UpdateInGroup(typeof(FixedStepSimulationSystemGroup))]
    736.     public partial class PhysicsBubbleSystem82 : CustomPhysicsSystemGroup
    737.     {
    738.         public PhysicsBubbleSystem82() : base(82, false)
    739.         {
    740.         }
    741.     }
    742.  
    743.  
    744.     [UpdateInGroup(typeof(FixedStepSimulationSystemGroup))]
    745.     public partial class PhysicsBubbleSystem83 : CustomPhysicsSystemGroup
    746.     {
    747.         public PhysicsBubbleSystem83() : base(83, false)
    748.         {
    749.         }
    750.     }
    751.  
    752.  
    753.     [UpdateInGroup(typeof(FixedStepSimulationSystemGroup))]
    754.     public partial class PhysicsBubbleSystem84 : CustomPhysicsSystemGroup
    755.     {
    756.         public PhysicsBubbleSystem84() : base(84, false)
    757.         {
    758.         }
    759.     }
    760.  
    761.  
    762.     [UpdateInGroup(typeof(FixedStepSimulationSystemGroup))]
    763.     public partial class PhysicsBubbleSystem85 : CustomPhysicsSystemGroup
    764.     {
    765.         public PhysicsBubbleSystem85() : base(85, false)
    766.         {
    767.         }
    768.     }
    769.  
    770.  
    771.     [UpdateInGroup(typeof(FixedStepSimulationSystemGroup))]
    772.     public partial class PhysicsBubbleSystem86 : CustomPhysicsSystemGroup
    773.     {
    774.         public PhysicsBubbleSystem86() : base(86, false)
    775.         {
    776.         }
    777.     }
    778.  
    779.  
    780.  
    781.     [UpdateInGroup(typeof(FixedStepSimulationSystemGroup))]
    782.     public partial class PhysicsBubbleSystem86 : CustomPhysicsSystemGroup
    783.     {
    784.         public PhysicsBubbleSystem86() : base(86, false)
    785.         {
    786.         }
    787.     }
    788.  
    789.  
    790.     [UpdateInGroup(typeof(FixedStepSimulationSystemGroup))]
    791.     public partial class PhysicsBubbleSystem87 : CustomPhysicsSystemGroup
    792.     {
    793.         public PhysicsBubbleSystem87() : base(87, false)
    794.         {
    795.         }
    796.     }
    797.  
    798.  
    799.     [UpdateInGroup(typeof(FixedStepSimulationSystemGroup))]
    800.     public partial class PhysicsBubbleSystem88 : CustomPhysicsSystemGroup
    801.     {
    802.         public PhysicsBubbleSystem88() : base(88, false)
    803.         {
    804.         }
    805.     }
    806.  
    807.  
    808.     [UpdateInGroup(typeof(FixedStepSimulationSystemGroup))]
    809.     public partial class PhysicsBubbleSystem89 : CustomPhysicsSystemGroup
    810.     {
    811.         public PhysicsBubbleSystem89() : base(89, false)
    812.         {
    813.         }
    814.     }
    815.  
    816.  
    817.     [UpdateInGroup(typeof(FixedStepSimulationSystemGroup))]
    818.     public partial class PhysicsBubbleSystem90 : CustomPhysicsSystemGroup
    819.     {
    820.         public PhysicsBubbleSystem90() : base(90, false)
    821.         {
    822.         }
    823.     }
    824.  
    825.  
    826.     [UpdateInGroup(typeof(FixedStepSimulationSystemGroup))]
    827.     public partial class PhysicsBubbleSystem91 : CustomPhysicsSystemGroup
    828.     {
    829.         public PhysicsBubbleSystem91() : base(91, false)
    830.         {
    831.         }
    832.     }
    833.  
    834.  
    835.     [UpdateInGroup(typeof(FixedStepSimulationSystemGroup))]
    836.     public partial class PhysicsBubbleSystem92 : CustomPhysicsSystemGroup
    837.     {
    838.         public PhysicsBubbleSystem92() : base(92, false)
    839.         {
    840.         }
    841.     }
    842.  
    843.  
    844.     [UpdateInGroup(typeof(FixedStepSimulationSystemGroup))]
    845.     public partial class PhysicsBubbleSystem93 : CustomPhysicsSystemGroup
    846.     {
    847.         public PhysicsBubbleSystem93() : base(93, false)
    848.         {
    849.         }
    850.     }
    851.  
    852.  
    853.     [UpdateInGroup(typeof(FixedStepSimulationSystemGroup))]
    854.     public partial class PhysicsBubbleSystem94 : CustomPhysicsSystemGroup
    855.     {
    856.         public PhysicsBubbleSystem94() : base(94, false)
    857.         {
    858.         }
    859.     }
    860.  
    861.  
    862.     [UpdateInGroup(typeof(FixedStepSimulationSystemGroup))]
    863.     public partial class PhysicsBubbleSystem95 : CustomPhysicsSystemGroup
    864.     {
    865.         public PhysicsBubbleSystem95() : base(95, false)
    866.         {
    867.         }
    868.     }
    869.  
    870.  
    871.     [UpdateInGroup(typeof(FixedStepSimulationSystemGroup))]
    872.     public partial class PhysicsBubbleSystem96 : CustomPhysicsSystemGroup
    873.     {
    874.         public PhysicsBubbleSystem96() : base(96, false)
    875.         {
    876.         }
    877.     }
    878.  
    879.  
    880.     [UpdateInGroup(typeof(FixedStepSimulationSystemGroup))]
    881.     public partial class PhysicsBubbleSystem97 : CustomPhysicsSystemGroup
    882.     {
    883.         public PhysicsBubbleSystem97() : base(97, false)
    884.         {
    885.         }
    886.     }
    887.  
    888.  
    889.     [UpdateInGroup(typeof(FixedStepSimulationSystemGroup))]
    890.     public partial class PhysicsBubbleSystem98 : CustomPhysicsSystemGroup
    891.     {
    892.         public PhysicsBubbleSystem98() : base(98, false)
    893.         {
    894.         }
    895.     }
    896.  
    897.  
    898.     [UpdateInGroup(typeof(FixedStepSimulationSystemGroup))]
    899.     public partial class PhysicsBubbleSystem99 : CustomPhysicsSystemGroup
    900.     {
    901.         public PhysicsBubbleSystem99() : base(99, false)
    902.         {
    903.         }
    904.     }
    905.  
    906. }
    907.  
     
    Last edited: Mar 3, 2023
  5. Thygrrr

    Thygrrr

    Joined:
    Sep 23, 2013
    Posts:
    700
    That is my old and current approach to multiple physics "worlds", patching com.unity.physics.CollisionFilter. This works extremely well with raycasts, not so well with moving entities from one world to another (due to CF and groupIndices being baked into colliders, I can see how CF make sense, but groupindex really does not. they really ought to be IComponentData)

    Code (CSharp):
    1.  
    2. //Patched CollisionFilter by me
    3.         public static bool IsCollisionEnabled(CollisionFilter filterA, CollisionFilter filterB)
    4.         {
    5.             if (filterA.GroupIndex >= 0 && filterA.GroupIndex == filterB.GroupIndex)
    6.             {
    7.                 return
    8.                     (filterA.BelongsTo & filterB.CollidesWith) != 0 &&
    9.                     (filterB.BelongsTo & filterA.CollidesWith) != 0;
    10.             }
    11.             return false;
    12.         }
    original code was (I still strain to see any scenario where this original GroupIndex implementation can expect a good amount of use, it appears pretty far removed from actual situations I have encountered in game dev that weren't already covered by Categories). I know this is the Box2D way of doing things, but I think that should be reconsidered.
    Code (CSharp):
    1. //Original CollisionFilter for discussion
    2.         public static bool IsCollisionEnabled(CollisionFilter filterA, CollisionFilter filterB)
    3.         {
    4.             if (filterA.GroupIndex > 0 && filterA.GroupIndex == filterB.GroupIndex)
    5.             {
    6.                 return true;
    7.             }
    8.             if (filterA.GroupIndex < 0 && filterA.GroupIndex == filterB.GroupIndex)
    9.             {
    10.                 return false;
    11.             }
    12.             return
    13.                 (filterA.BelongsTo & filterB.CollidesWith) != 0 &&
    14.                 (filterB.BelongsTo & filterA.CollidesWith) != 0;
    15.         }
    My approach has some issues with the BVH in Unity.Physics, which clobbers the IndexGroup to 0 because it creates so many collider unions and has no idea how to treat them (neither my scheme, nor Unity's/Box2D's own scheme).

    In an ideal world, I'd find a way to reject collisions for IndexGroup 0 (which is "null" in my context), but I can live with it as-is for now.

    I'm considering a system instead where I add a layer field in CollisionFilter and BVH creates some sort of separate tree per layer. Not sure, this is out of scope for me given how much of a moving target Entities 1.0 still appears to be.

    Second hack idea might just be more bits; like a 128 bit int or so could constitute a working layer which those BVH unions could play well with. Seems wasteful, especially in light of the ridiculous requirement of unique colliders.

    Third alternative I want to try would be to encode the world index in the top 8-16 bits of the BelongsTo/CollidesWith masks, and hack the filter code accordingly. That bitmask would still get clobbered by the BVH build step (but not to zero, but to 11...1111), so at least there could be some optimizations to be gained. Possibly with a rolling bit pattern logic or something, IDK, out of scope right now.

    Anyway - I had extremely high hopes for PhysicsWorldIndex, which promises to do all that without open heart surgery on Unity.Physics; but apparently that has too many limitations due to the problem described in OP, and the massive, inexplicable slowdowns experienced beyond ~30 systems, regardless how sparse, dormant, or empty the World is.
     
    Last edited: Mar 6, 2023
  6. Thygrrr

    Thygrrr

    Joined:
    Sep 23, 2013
    Posts:
    700