Search Unity

Quaternion.Euler behaving not as expected in dots

Discussion in 'Entity Component System' started by Cell-i-Zenit, Jun 12, 2019.

  1. Cell-i-Zenit

    Cell-i-Zenit

    Joined:
    Mar 11, 2016
    Posts:
    290
    Hi,


    I want to rotate my entites to a 45/45/45 Degree angle. I see that quaterion.Euler(x,y,z) expects the value as Radian (which should be 0,785398), but the rotation pretty off. If i rotate a gameobject cube to 45,45,45 in unity it looks as expected.

    Iam not sure if i am in the correct forum or not, but i guess its fine since iam using the new mathematics library.

    Why is quaternion.EulerXYZ(45,45,45) or quaternion.EulerXYZ(0.785398,0.785398,0.785398) not working?
     
  2. eizenhorn

    eizenhorn

    Joined:
    Oct 17, 2016
    Posts:
    2,683
    It works fine. Show your code.
     
  3. JPrzemieniecki

    JPrzemieniecki

    Joined:
    Feb 7, 2013
    Posts:
    33
  4. Cell-i-Zenit

    Cell-i-Zenit

    Joined:
    Mar 11, 2016
    Posts:
    290
    EDIT: looks like i dont know how quaternions work :p Its fine now.. it appears that euler angles 45,45,45, is in quaternion:
    quaternion.EulerXYZ(0.785398f,0,0.785398f);

    Sorry for the late answer!

    here are my attempts:

    //var angle = Quaternion.Euler(45, 45, 45);
    //var angle = quaternion.EulerXYZ(45,45,45);
    var angle = quaternion.EulerXYZ(0.785398f, 0.785398f, 0.785398f);

    Iam not a fan of posting the whole code, but in this case it looks like iam doing something wrong but i cant pinpoint it anywhere... here is the code where iam creating the object.

    PS: this code is nowhere near refactored :p

    Code (CSharp):
    1. using Assets.Others.Utils;
    2. using Assets.Structs;
    3. using Assets.Systems;
    4. using Unity.Collections;
    5. using Unity.Entities;
    6. using Unity.Mathematics;
    7. using Unity.Transforms;
    8. using UnityEngine;
    9.  
    10. namespace Assets.Classes
    11. {
    12.     public class VegetationFirTree : VegetationBase
    13.     {
    14.  
    15.         public override int ObjectCount => 4;
    16.         public float Height { get; set; }
    17.  
    18.         public VegetationFirTree(float2 position, int world)
    19.         {
    20.             Position = position;
    21.             CreateInWorld = world;
    22.             Height = UnityEngine.Random.Range(10, 15);
    23.         }
    24.  
    25.         public override int Create(int parentIndex, NativeArray<Entity> parentEntityArray, int cubeIndex, NativeArray<Entity> cubeArray,
    26.                                    EntityManager manager)
    27.         {
    28.             var parentEntity = parentEntityArray[parentIndex];
    29.             var lerpableComponent = new Lerpable(LerpTime, LerpGoal);
    30.  
    31.             var treeComponent = new FirTree
    32.             {
    33.                 CreatedInWorld = CreateInWorld,
    34.                 Height = Height,
    35.  
    36.                 Trunk = cubeArray[cubeIndex],
    37.                 LeafTop = cubeArray[cubeIndex + 1],
    38.                 LeafMiddle = cubeArray[cubeIndex + 2],
    39.                 LeafBottom = cubeArray[cubeIndex + 3]
    40.             };
    41.  
    42.             var trunkScaleValue = new float3(0.4f, treeComponent.Height, 0.4f);
    43.             var trunkScaleComponent = new NonUniformScale() {Value = trunkScaleValue * LerpTime};
    44.             var trunkPosition = new float3(0, treeComponent.Height * 0.5f, 0);
    45.             var trunkLerpDataComponent = new LerpDataContainer
    46.             {
    47.                 ScaleValue = trunkScaleValue,
    48.                 TranslationValue = trunkPosition
    49.             };
    50.  
    51.             var leafBottomScaleComponent = new Scale {Value = 4 * LerpTime};
    52.             var leafMiddleScaleComponent = new Scale {Value = 3 * LerpTime};
    53.             var leafTopScaleComponent = new Scale {Value = 2 * LerpTime};
    54.  
    55.             var leafBottomPosition = new float3(0, treeComponent.Height * 0.6f + 1, 0);
    56.             var leafMiddlePosition = new float3(0, treeComponent.Height * 0.8f + 1, 0);
    57.             var leafTopPosition = new float3(0, treeComponent.Height + 1, 0);
    58.  
    59.             var leafBottomHeightComponent = new Translation {Value = leafBottomPosition * LerpTime};
    60.             var leafMiddleHeightComponent = new Translation {Value = leafMiddlePosition * LerpTime};
    61.             var leafTopHeightComponent = new Translation {Value = leafTopPosition * LerpTime};
    62.  
    63.             var leafBottomLerpDataComponent = new LerpDataContainer
    64.             {
    65.                 ScaleValue = new float3(4, 4, 4),
    66.                 TranslationValue = leafBottomPosition
    67.             };
    68.             var leafMiddleLerpDataComponent = new LerpDataContainer
    69.             {
    70.                 ScaleValue = new float3(3, 3, 3),
    71.                 TranslationValue = leafMiddlePosition
    72.             };
    73.             var leafTopLerpDataComponent = new LerpDataContainer
    74.             {
    75.                 ScaleValue = new float3(2, 2, 2),
    76.                 TranslationValue = leafTopPosition
    77.             };
    78.  
    79.             float groundHeight;
    80.             if (LerpSystem.IsLerping)
    81.             {
    82.                 groundHeight = DataGenerator.GetHeightBetweenWorlds(Position, WorldManager.Instance.CurrentWorld,
    83.                     WorldManager.Instance.NextWorld, LerpTime) * WorldManager.Instance.MeshScale;
    84.             }
    85.             else
    86.             {
    87.                 groundHeight = DataGenerator.GetHeight(Position, WorldManager.Instance.CurrentWorld) * WorldManager.Instance.MeshScale;
    88.             }
    89.  
    90.             var groundPosition = new float3(Position.x, groundHeight, Position.y);
    91.  
    92.             //trunk
    93.             manager.AddComponentData(treeComponent.Trunk, trunkScaleComponent);
    94.             manager.AddComponentData(treeComponent.Trunk, lerpableComponent);
    95.             manager.AddComponentData(treeComponent.Trunk, trunkLerpDataComponent);
    96.             manager.SetComponentData(treeComponent.Trunk, new Translation {Value = trunkPosition * LerpTime});
    97.             manager.AddComponentData(treeComponent.Trunk, new Parent {Value = parentEntity});
    98.             manager.AddComponentData(treeComponent.Trunk, new LocalToParent());
    99.  
    100.             //var angle = Quaternion.Euler(45, 45, 45);
    101.             //var angle = quaternion.EulerXYZ(45,45,45);
    102.             var angle = quaternion.EulerXYZ(0.785398f, 0.785398f, 0.785398f);
    103.          
    104.             //leafTop
    105.             manager.AddComponentData(treeComponent.LeafTop, leafTopScaleComponent);
    106.             manager.AddComponentData(treeComponent.LeafTop, leafTopLerpDataComponent);
    107.             manager.AddComponentData(treeComponent.LeafTop, lerpableComponent);
    108.             manager.SetComponentData(treeComponent.LeafTop, leafTopHeightComponent);
    109.             manager.AddComponentData(treeComponent.LeafTop, new Parent {Value = parentEntity});
    110.             manager.AddComponentData(treeComponent.LeafTop, new LocalToParent());
    111.             manager.SetComponentData(treeComponent.LeafTop, new Rotation {Value = angle});
    112.  
    113.             //leafBottom
    114.             manager.AddComponentData(treeComponent.LeafBottom, leafBottomScaleComponent);
    115.             manager.AddComponentData(treeComponent.LeafBottom, leafBottomLerpDataComponent);
    116.             manager.AddComponentData(treeComponent.LeafBottom, lerpableComponent);
    117.             manager.SetComponentData(treeComponent.LeafBottom, leafBottomHeightComponent);
    118.             manager.AddComponentData(treeComponent.LeafBottom, new Parent {Value = parentEntity});
    119.             manager.AddComponentData(treeComponent.LeafBottom, new LocalToParent());
    120.             manager.SetComponentData(treeComponent.LeafBottom, new Rotation {Value = angle});
    121.  
    122.             //leafMiddle
    123.             manager.AddComponentData(treeComponent.LeafMiddle, leafMiddleScaleComponent);
    124.             manager.AddComponentData(treeComponent.LeafMiddle, leafMiddleLerpDataComponent);
    125.             manager.AddComponentData(treeComponent.LeafMiddle, lerpableComponent);
    126.             manager.SetComponentData(treeComponent.LeafMiddle, leafMiddleHeightComponent);
    127.             manager.AddComponentData(treeComponent.LeafMiddle, new Parent {Value = parentEntity});
    128.             manager.AddComponentData(treeComponent.LeafMiddle, new LocalToParent());
    129.             manager.SetComponentData(treeComponent.LeafMiddle, new Rotation {Value = angle});
    130.  
    131.             //parent
    132.             manager.AddComponentData(parentEntity, treeComponent);
    133.             manager.AddComponentData(parentEntity, lerpableComponent);
    134.             manager.SetComponentData(parentEntity, new PlanePosition(Position));
    135.             manager.SetComponentData(parentEntity, new Translation {Value = groundPosition});
    136.  
    137.             return ObjectCount;
    138.         }
    139.  
    140.     }
    141. }
     
    Last edited: Jun 15, 2019