Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question What is DOTS equivalent of UnityEngine TransformPoint, TransformDirection and TransformVector?

Discussion in 'Entity Component System' started by Antypodish, Oct 5, 2022.

  1. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,574
    I am looking for DOTS based equivalent of
    UnityEngine.Tansform
    Code (CSharp):
    1. InverseTransformDirection(direction)
    and
    Code (CSharp):
    1. TransformDirection(direction)
    Or custom functions of these two.
    Any thoughts?
     
  2. ThomasEgan

    ThomasEgan

    Joined:
    Dec 17, 2013
    Posts:
    20
    I haven't used them myself yet but it looks like TransformAspect.TransformDirectionWorldToLocal() and TransformAspect.TransformDirectionLocalToWorld() respectively should do what you want
     
    Antypodish likes this.
  3. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    3,292
    There's math.transform() for TransformPoint equivalent, as for the TransformDirection, I think its more like this:
    https://forum.unity.com/threads/transform-inversetransformdirection-for-ecs.652543/#post-7448927

    Overall, its a matrix manipulation, so if you want, you can just take matrix and perform math.mul on it.
    And if something is inverse, you'd just take inverse of that matrix and perform the same operation.
     
    tmonestudio and Antypodish like this.
  4. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,574
    I will need to check these later and compare with UnityEngine transform. Thx.

    That what I suspects so. But unsure, if I got right matrix manipulation. I was able to find many equations for UnityEngine. Vector3 for example, but it is much harder to find for Unity Engine.Transform functions.

    I am converting some old OOP code.
     
    xVergilx likes this.
  5. Karearea

    Karearea

    Joined:
    Sep 3, 2012
    Posts:
    386
    I've done this in a few places, looking through here's an example of both ways.


    Code (CSharp):
    1. //Get airflow localised values, clamp z value. This avoids physics-breaking spins when tailsliding.
    2. float3 localAirflow = matrix4x4.inverse.MultiplyVector(relativeAirflow);
    3.  
    4. localAirflow.z = math.clamp(localAirflow.z, s.maxPositiveVelocity_ms, s.maxNegativeVelocity_ms); //TODO Make Variable
    5.  
    6. //Transform airflow back to world values
    7. relativeAirflow = matrix4x4.MultiplyVector(localAirflow);
    I got the matrix4x4 via:

    Code (CSharp):
    1. //Get localToWorld matrix
    2. Matrix4x4 matrix4x4 = Matrix4x4.identity;
    3. matrix4x4 = localToWorld.Value;
     
    Antypodish likes this.
  6. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,574
    @Karearea, this is interesting. Seems getting results. Yet it operates on the Unity Engine math libraries using Matrix4x4, instead float4x4.

    Did you hit any burst limitations to that?
     
  7. Karearea

    Karearea

    Joined:
    Sep 3, 2012
    Posts:
    386
    Well as luck would have it, I think my technique is now broken in Entities 1.0 builds due to burst (ok in the editor). So I'd be interested to know of a solution too. Will try the solution from xVergilx's link when I get a chance.
     
  8. Karearea

    Karearea

    Joined:
    Sep 3, 2012
    Posts:
    386
    From the other thread, these all work well:

    LocalPosition = math.transform(math.inverse(localToWorld.Value), WorldPostion)
    WorldPostion = math.transform(localToWorld.Value, LocalPosition)

    LocalVector = math.rotate(math.inverse(localToWorld.Value), WorldVector)
    WorldVector = math.rotate(localToWorld.Value, LocalVector)
     
    RyanJVR, Elapotp and Antypodish like this.
  9. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,574
    Hey again.
    I would like say hips of thx for the answers.

    And actually I would like to confirm @Karearea solutions, as I validated them on my side.

    Code (CSharp):
    1. // UnityEngine.Transform InverseTransformPoint
    2. localPosition = math.transform(math.inverse(localToWorld.Value), worldPostion)
    Code (CSharp):
    1. // UnityEngine.Transform TransformPoint
    2. worldPostion = math.transform(localToWorld.Value, localPosition)
    Code (CSharp):
    1. // UnityEngine.Transform InverseTransformDirection
    2. localVector = math.rotate(math.inverse(localToWorld.Value), worldDirection)
    Code (CSharp):
    1. // UnityEngine.Transform TransformDirection
    2. worldVector = math.rotate(localToWorld.Value, localDirection)

    My one more question on the matter of transforms, for this to be complementary, what is the DOTS equivalent for
    TransformVector and InverseTransformVector?

    I am testing following and seems getting correct results.
    Code (CSharp):
    1. // UnityEngine.Transform InverseTransformVector
    2. float3 vDiff = targetPosition - refWorldPosition;
    3. float3 worldVector = math.normalize(vDiff);
    4. float3 localVector = math.rotate(math.inverse(refWorldRotation), worldVector);
    Code (CSharp):
    1. // UnityEngine.Transform tnsformVector
    2. float3 vDiff = targetPosition - refLocalPosition;
    3. float3 localVector= math.normalize(vDiff);
    4. float3 worldVector= math.rotate(refLocalRotation, localVector);
     
    Last edited: Nov 9, 2022
    CheMBurN, RyanJVR, nicloay and 3 others like this.