Search Unity

[Transform] The right way to set global rotation and position

Discussion in 'Data Oriented Technology Stack' started by dzamani, Sep 13, 2018.

  1. dzamani

    dzamani

    Joined:
    Feb 25, 2014
    Posts:
    89
    Hi,

    I have some code that output world positions and rotations. I'm working int a pure ECS context (with parenting transforms), so I'm at the last step and I see that LocalToWorld matrix can't be set because it's overwritten by TransformSystem at each update (also, I was fooled into thinking that Position and Rotation are world which is incorrect when there is a parent and it's very confusing).

    So I have access to :
    - Current localToWorld matrix
    - New localToWorld matrix
    - Parent localToWorld matrix

    My solution is this :

    Code (CSharp):
    1. float4x4 newLocalMatrix = math.mul(math.inverse(parentMatrix), newWorldMatrix);
    This code only works if there is no holes in the hierarchy I'm changing, for example if I have this :
    A -> B - > C - >D
    If I update only A, B and D the math would be wrong because I would not have updated C matrices.

    Is there a plan to handle world positioning ?
    Should we call the Update of TransformSystem manually when we need everything to be recalculated ?

    Thanks!
     
  2. julian-moschuering

    julian-moschuering

    Joined:
    Apr 15, 2014
    Posts:
    329
    As I understand it you want something like Transform.SetParent keeping the current world position.

    Having a 'KeepWorldPosition' in the attach event component would be great.
     
  3. dzamani

    dzamani

    Joined:
    Feb 25, 2014
    Posts:
    89
    Not really, I'm moving Entities based on world positions but these entities have parents / children. For now we have Position and Rotation, which are local if there is a parent, what I want is a SetWorldPosition / Rotation that would be more robust than my solution.
     
  4. julian-moschuering

    julian-moschuering

    Joined:
    Apr 15, 2014
    Posts:
    329
    Why would you want to attach something if the transform should be independent?
     
  5. dzamani

    dzamani

    Joined:
    Feb 25, 2014
    Posts:
    89
    My use case is an IK algorithm working with world positions / rotations, so I'm working on rigs which have transform hierarchies. Some bones could be skipped. So for example I move the wrist of an arm, the hand should follow because it's a child of the wrist.
     
  6. julian-moschuering

    julian-moschuering

    Joined:
    Apr 15, 2014
    Posts:
    329
    Hmm, okay, makes sense.
    I would probably update the hierrachy manually using a list of Bones sorted breadth first, as the problem is limited to specific subhierarchies.
     
  7. julian-moschuering

    julian-moschuering

    Joined:
    Apr 15, 2014
    Posts:
    329
    Or, wait... I think you only have to remove Position/Rotation/Scale from the ones you control for yourself, then LocalToWorld shouldn't be touched by TransformSystem and the children still having these components still move correctly... I think.
     
    Last edited: Sep 13, 2018
  8. dzamani

    dzamani

    Joined:
    Feb 25, 2014
    Posts:
    89
    That's a nice idea but doesn't the TransformSystem needs the parents to have Positions/Rotations components ?
    I will need to think a lot to see if nothing breaks with this idea and I feel like it could give a little performance boost.
    Thanks, that's a good alternative if it works but I still think that a SetWorldPosition would be useful.