Search Unity

Trouble measuring position differences on a modified co-ordinate system

Discussion in 'Scripting' started by petey, Aug 29, 2011.

  1. petey

    petey

    Joined:
    May 20, 2009
    Posts:
    1,823
    Hi there,
    Just wondering if anyone could help me out here,
    I have this script that is measuring the difference between the position of two objects. It's really simple and it works fine
    Code (csharp):
    1.  (Base.position.y - Top.position.y)



    But I am having trouble when they are on an angle like this. The two objects can't be in a hierarchy but I want to measure the difference in position in this new co-ordinate system.



    any ideas?
    Thanks
    Pete
     
  2. u10

    u10

    Joined:
    Aug 29, 2011
    Posts:
    4
    /* C# */

    Vector3 v3distance;
    float distance;
    v3distance = Base.position - Top.postition;
    distance = v3distance.magnitude; // Length of vector between Base and Top positions

    /* or */

    distance = Vector3.Distance(Top.position, Base.position);
     
    Last edited: Aug 29, 2011
  3. Jessy

    Jessy

    Joined:
    Jun 7, 2007
    Posts:
    7,325
    That's not the same thing as differences in three axes. A 3x3 matrix or quaternion is what I think you're looking for. This may help you, Pete.

    http://aras-p.info/texts/matrices.html

    Although a matrix may be easier to understand, Unity's got several functions in the Quaternion class that will let you construct and use what you need, too. You'll do your same Base.position.y - Top.position.y, and then rotate the resulting vector.
     
    Last edited: Aug 29, 2011
  4. u10

    u10

    Joined:
    Aug 29, 2011
    Posts:
    4
    Ah, yes,

    v3distance = Base.rotation * (Base.postion - Top.position); //get range vector and apply rotation for "Base" object as base of new coord system/
     
  5. petey

    petey

    Joined:
    May 20, 2009
    Posts:
    1,823
    Wow, thanks guys. That's really simple but I never would have thought to do it like that.
     
  6. petey

    petey

    Joined:
    May 20, 2009
    Posts:
    1,823
    Ahh crap i spoke too soon. Weird seemed to be working last night but maybe I was just half asleep??

    With this code
    Code (csharp):
    1. v3distance = Base.rotation * (Base.position - Top.position)
    this position,


    Im getting this, I thought this would only report back a Y distance but there's more going on in X




    Am I doing something obviously wrong?
    Pete
     
  7. Jessy

    Jessy

    Joined:
    Jun 7, 2007
    Posts:
    7,325
    If you wanted to do it that way, you'd need Quaternion.Inverse(Base.rotation). However, only consider doing that if the coordinate system is fixed; otherwise, it will be too much calculation, when you could just use Transform.InverseTransformDirection. It's possible the latter is fastest anyway, due to SIMD.
     
  8. petey

    petey

    Joined:
    May 20, 2009
    Posts:
    1,823
    Hey Jessy,
    The object is going to be rotating around during the game so i had a look at Transform.InverseTransformDirection.

    For the angle in the image above Transform.InverseTransformDirection gives me Vector3(0.0,0.0,1.0). Which seems kinda unexpected I'm not sure how I could use it to get the result I'm after... Sorry to be a pain but I find this stuff a little hard to get my head around...
     
  9. Jessy

    Jessy

    Joined:
    Jun 7, 2007
    Posts:
    7,325
    Code? This works for me:

    Code (csharp):
    1. Base.InverseTransformDirection(Base.position - Top.position)
    Every Transform has a local coordinate system, aka "local space". All Transforms also exist in a common "world space". Transform.InverseTransformDirection rotates vectors from world to local space. (If the Transform has no rotation or translation applied, its local space and world space are the same space.) Vectors are only directions with magnitude, so they can be rotated alone, to get from world to local. Positions, however, need to be both rotated and translated, which is where Transform.InverseTransformPoint comes in.

    3D gets more complicated, but understanding rotations in 2D helped me understand them better in 3D. http://en.wikipedia.org/wiki/Rotation_matrix
     
  10. petey

    petey

    Joined:
    May 20, 2009
    Posts:
    1,823
    Hey Jessy,
    I was using Transform.InverseTransformDirection in a weird way that was wrong for what I was trying to accomplish, that makes heaps more sense, thanks again for the help!