Search Unity

Is there a better way to set Translation Rotation

Discussion in 'Entity Component System' started by RoughSpaghetti3211, Nov 14, 2020.

  1. RoughSpaghetti3211

    RoughSpaghetti3211

    Joined:
    Aug 11, 2015
    Posts:
    1,708
    Im doing a simple orbit cam but im not sure this is the best way to set the position and rotations. Any thoughts would be appreciated

    Code (CSharp):
    1.  
    2. Entities.ForEach((ref Translation translation, ref Rotation rotation, in LocalToWorld localToWorld) =>
    3. {
    4.     var p = translation.Value;
    5.     var pv = new float3(0, 0, 0);
    6.     var ax = new float3(0, 1, 0);
    7.  
    8.     float4x4 m = RotateAround(localToWorld, pv, ax, deltaTime);
    9.     var ltw = new LocalToWorld {Value = m};
    10.     translation.Value = ltw.Position;
    11.     rotation.Value = ltw.Rotation;
    12.  
    13. }).Schedule();
    14.  
    15.  
     
    apkdev likes this.
  2. JakHussain

    JakHussain

    Joined:
    Oct 20, 2016
    Posts:
    318
    I think if you simply replace the local to world value then the super complex transform systems will figure out the new translation and rotation values at the end of the frame for you.
     
  3. JakHussain

    JakHussain

    Joined:
    Oct 20, 2016
    Posts:
    318
    And by complex I just mean there's tonnes of systems which resolve for every single way you can affect a transform system but they all centre around solving for the new local to world value and updating everything according to that.

    There's a section about it all in the entities documentation.
     
  4. RoughSpaghetti3211

    RoughSpaghetti3211

    Joined:
    Aug 11, 2015
    Posts:
    1,708
    Initially, i tried to set it like below but it not work until i set the translation.Value & rotation.Value.. So either my float4x4 need something else like a scale or im not hitting one of that translation sys

    Code (CSharp):
    1.  
    2. Entities
    3.     .WithAll<TagMainCameraComponent>()
    4.     .ForEach((ref Translation translation, ref Rotation rotation, ref LocalToWorld localToWorld) =>
    5.     {
    6.         float4x4 m = RotateAround(localToWorld, pv, ax, dt);
    7.        
    8.         //works
    9.         // var ltw = new LocalToWorld {Value = m};
    10.         // translation.Value = ltw.Position;
    11.         // rotation.Value = ltw.Rotation;
    12.  
    13.         // does not work
    14.         localToWorld = new LocalToWorld {Value = m};
    15.     }).Schedule();
    16.  
    Code (CSharp):
    1.  
    2. public static float4x4 RotateAround(LocalToWorld localToWorld, float3 center, float3 axis, float angle)
    3. {
    4.     // ..
    5.     float3 pos =  // .. get post
    6.     quaternion rot = // ...get rot
    7.     return new float4x4(rot, pos);
    8. }
    9.  
     
  5. skiplist

    skiplist

    Joined:
    Nov 9, 2014
    Posts:
    46
  6. charleshendry

    charleshendry

    Joined:
    Jan 7, 2018
    Posts:
    97
    I also set localToWorld directly for static entities to avoid unnecessary transform system updates. I set it like this:
    new LocalToWorld { Value = float4x4.TRS(...) }