Search Unity

Unity approach to existing Monobehaviour functions

Discussion in 'Entity Component System' started by JooleanLogic, Jan 10, 2019.

  1. JooleanLogic

    JooleanLogic

    Joined:
    Mar 1, 2018
    Posts:
    447
    Just wondering if Unity has some idea on how they're going to implement existing MonoBehaviour functions once converted to ecs.
    E.g. How do you think the Rigidbody2D AddForce, AddRelativeForce and AddTorque behaviours might be implemented?

    Is it likely these will be separate components you add directly to the entity, event entities, or perhaps just stick all this data on a heavy Rigidbody2D component and poll it every frame?
    I know it's a long way off but I'm implementing hybrid approach atm and just checking if you have any future insight on your approach. :)

    Code (CSharp):
    1. // Example event entity
    2. public struct RigidBody2DRelativeForceEvent : IComponentData
    3. {
    4.     public float2 relativeForce;
    5.     public ForceMode2D forceMode;
    6.     Entity entity;
    7. }
    8.  
    9.  
    10. // Example separate components. The processing systems would zero these values after forces applied
    11. public struct RigidBody2DRelativeForce : IComponentData
    12. {
    13.     public float2 value;
    14.     public void AddRelativeForce(float2 force) => value += force;
    15. }
    16.  
    17. public struct RigidBody2DRelativeImpulse : IComponentData
    18. {
    19.     public float2 value;
    20.     public void AddRelativeImpulse(float2 impulse) => value += impulse;
    21. }
    22.  
    23.  
    24. // Example Heavy Rigidbody2D Component
    25. public struct RigidBody2D : IComponentData
    26. {
    27.    public float mass;
    28.    public float linearDrag;
    29.    public float gravityScale;
    30.    public float2 relativeForce {get; private set;}
    31.    ...
    32.    public void AddForce(float2 force, ForceMode2d forceMode){...}
    33.    public void AddRelativeForce(float2 force, ForceMode2d forceMode){...}
    34.    public void AddTorque(float torque, ForceMode2d forceMode){...}
    35.    ...
    36. }
     
  2. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    Unity plans on writing ECS versions for each of the classic components that matter. This would obviously mean remaking the entirety of Physx as ECS though :)

    Think that's what they're doing.
     
  3. JooleanLogic

    JooleanLogic

    Joined:
    Mar 1, 2018
    Posts:
    447
    Thanks hippo, I was more wondering if they have any idea on what ecs technique (I listed 3 in my first post) they might use when they do implement such systems.

    I used Rigidbody2D and its AddForce, AddRelativeForce methods as an example but all Unity's current Monobehaviour components have a bunch of one off behaviours that are applied via methods.

    For such ubiquitous components like sprites and rigid bodies, there are various perf/usability tradeoffs to whether you use events, polling or adding components.
    I'm curious as to what kind of approach they're likely to recommend/use for their own widely used components but I realise it might still be too early for such feedback.
     
  4. JooleanLogic

    JooleanLogic

    Joined:
    Mar 1, 2018
    Posts:
    447
    I just realised my example with Rigidbody is misguided as I didn't understand that such forces are accumulated into a single vector/torque which are always in use. Me bad math.

    I was creating my own rigidbody components for pure ecs usage and then transferring that data to mono Rigidbody2D at end of frame but this is a like trying to stick a square into a round hole.
    So I'm guessing current Unity components are likely to just be single heavy components in the end with static functions acting on them to replace existing Monobehaviour methods.