Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Unity Affine Transform

Discussion in 'Scripting' started by criistii22, Jun 9, 2011.

  1. criistii22

    criistii22

    Joined:
    Jan 18, 2011
    Posts:
    52
    Is there any way to apply affine transforms with unity classes. I didn't manage to find anything in the documentation.

    What I need to do:
    I am using GuiManager2, and I need a "minimap". I will need textures that orbit (rotate) in 2D space around a center point. Since i'll have 20+ objects that need this, I am trying to figure out the most performance effective way to determine the Vector2 for each object.

    In a previous objective c project I used this function:

    Code (csharp):
    1. + (void)rotatePoint:(VBasePoint*)point :(CGPoint)origin :(CGFloat)angle {
    2.     CGAffineTransform translate = CGAffineTransformMakeTranslation(origin.x,origin.y);
    3.     CGAffineTransform rotate = CGAffineTransformMakeRotation(angle);
    4.    
    5.     CGAffineTransform concat = CGAffineTransformConcat(CGAffineTransformConcat( CGAffineTransformInvert(translate), rotate), translate);
    6.    
    7.     CGPoint newPoint = CGPointApplyAffineTransform(*point.location, concat);
    8.     point->x = newPoint.x;
    9.     point->y = newPoint.y;
    10. }
     
  2. Antitheory

    Antitheory

    Joined:
    Nov 14, 2010
    Posts:
    549
    As far as I know Unity doesn't have a built-in tool for creating affine matrices. You could use a 4x4 matrix for this if you want. Better yet you could just use Vector3s and confine their rotation to the plane you are working in.

    Vector3 p;
    p = Quaternion.EulerAngles(0, 0, 45) * p;

    This will rotate the vector P by 45 degrees in the Z axis. So if your center of rotation is at (10,10,0) and you want to rotate point (5,5,0) about this pivot, you would define P as the vector created from (point - pivot). And after you rotate the vector P you get your new point by doing (P + pivot)
     
  3. criistii22

    criistii22

    Joined:
    Jan 18, 2011
    Posts:
    52
    Hey.
    Thanks for the info, it does work. In case anyone else is interested, here is a code snippet from what I am working on. The only change is that I used the 'Euler' function, since 'EulerAngles' is depricted:
    Code (csharp):
    1. private void ManageMonsterIcons()
    2.     {
    3.         float angle = 360f / monsterIcons.Count;
    4.         Vector3 center = new Vector3(Screen.width * 0.5f, 0, Screen.height * 0.5f);
    5.         Vector3 offset = center - new Vector3(0f, 0f, -100f);
    6.         Vector3 dif = offset - center;
    7.        
    8.         for (int i=0; i<monsterIcons.Count; i++)
    9.         {
    10.             GUIQuadObj icon = (GUIQuadObj)monsterIcons[i];
    11.            
    12.             float myAngle = angle * i;
    13.            
    14.             Vector3 p = Quaternion.Euler(0f, myAngle, 0f) * dif;
    15.             p += center;
    16.            
    17.             icon.Location = new Vector2(p.x, p.z);
    18.             icon.Visible = true;
    19.         }
    20.     }
     
  4. Karsnen_2

    Karsnen_2

    Joined:
    Nov 28, 2011
    Posts:
    89
    Hi,

    If I have a vector3 (say A) based off another vector (say B), is it possible to find A if I happen to change B. I hope it is possible through affine transformation but I am not sure as of how to apply them.