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

A design problem that i could not solve (Switching from oop)

Discussion in 'Entity Component System' started by Lordinarius, Sep 24, 2018.

  1. Lordinarius

    Lordinarius

    Joined:
    Sep 3, 2012
    Posts:
    94
    Hello everyone. I am trying to build a physic engine for Unity ECS. I have tried ECS before (Ashley , aka java port of Ash). But it did not have restrictions like Unity's has. So i stucked at some point.

    So we know that reference types are not allowed inside a IJob or IJobParallelFor. In a phycis engine we have shapes to test collisions so i seperated Shape and actual shape

    Shape > it holds a variable to connetcted body (Entity)
    CircleShape > Has radius (float)
    BoxShape > Has halfsize (float2)

    So since CircleShape and BoxShape components are value types and not comming from a common parent class or interfaces (They are both IComponentData but it is not related) iterating trough them and comparing with eachother is hard. You have to hardcode all combinations (Circle > Circle , Box > Circle , Box > Box) etc. When there is only two type of colliders. Thats not a big deal. But when you try add a new type of Shape. its really pain. With oop approach it is really easy to do that though. Just add them an interface ( IProvideShape) and impelement for each type thats all, adding new shape type is much more easy.

    So what do i miss here ?. I bet someone did solve this problem. Is there any approach that may solve it ? Any opinions are welcomed.

    Thanks.

    Code (CSharp):
    1.         struct AllGroup
    2.         {
    3.             [ReadOnly] public ComponentDataArray<Shape> shapes;
    4.             public readonly int Length;
    5.         }
    6.      
    7.         struct BoxGroup
    8.         {
    9.             [ReadOnly] public ComponentDataArray<Shape> shapes;
    10.             [ReadOnly] public ComponentDataArray<BoxShape> boxshapes;
    11.             public readonly int Length;
    12.         }
    13.      
    14.         struct CircleGroup
    15.         {
    16.             [ReadOnly] public ComponentDataArray<Shape> shapes;
    17.             [ReadOnly] public ComponentDataArray<CircleShape> circleshapes;
    18.             public readonly int Length;
    19.         }
    20.  
    21.         [BurstCompile]
    22.         struct TestJob : IJobParallelFor
    23.         {
    24.             public AllGroup allGroup;
    25.             public BoxGroup boxGroup;
    26.             public CircleGroup circleGroup;
    27.             private int boxIndex;
    28.             private int circleIndex;
    29.          
    30.             public void Execute(int index)
    31.             {
    32.                 var shape = allGroup.shapes[index];
    33.                 //Iterating over different types is so hard
    34.                 .
    35.                 .
    36.             }
     
  2. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    ArchetypeChunk lets you seperate queries from access and also allows for optional components that way. Check out TransformSystem.cs for example for heavy usage of that API.
     
  3. Vacummus

    Vacummus

    Joined:
    Dec 18, 2013
    Posts:
    191
    I am interested in how this is done. Where can we find the TransformSystem.cs?
     
  4. recursive

    recursive

    Joined:
    Jul 12, 2012
    Posts:
    669
    Earlier than 2018.3 beta, packages can be found in "%APPDATA%\Local\Unity\cache\packages\packages.unity.com\" and you'll want to look at com.unity.entities@PACKAGE_VERSION. I believe on other platforms it's also somewhere under the user library but it's in a similar location for Mac OS X and Linux.

    For 2018.3 a project's packages are cached locally in PROJECT/Library/PackageCache for package obtained via the package manager, and custom/non-package manager packages go in PROJECT/Packages (you need to do this for the New Input Library currently).

    In 2018.3, TransformSystem.cs is under PROJECT/Library/PackageCache/com.unity.entities@VERSION/Unity.Transforms/TransformSystem.cs.

    I know that the project folder in the Editor will also show package contents, and Rider has gotten the ability to browse packages as read-only files in the latest versions. I believe VS may also have this in newer versions / versions of the plugin but I'm not sure as I've been using Rider for the last few months.
     
    Vacummus likes this.
  5. Vacummus

    Vacummus

    Joined:
    Dec 18, 2013
    Posts:
    191
    What!! This is awesome. Thank you for providing that info.
     
    recursive likes this.