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 Rotating 2D square in 3D space around the Y axis

Discussion in 'Scripting' started by give_me_pen, Jul 30, 2023.

  1. give_me_pen

    give_me_pen

    Joined:
    Oct 18, 2021
    Posts:
    24
    The core of what I'm attempting to do is create a 2D square (points across X and Y axis), and rotate it around the Y axis. So only their X and Z values should be changing, their Y value should be stationary.

    Here's my attempt:

    Code (CSharp):
    1.     private void CalculateSquarePosition(Vector3 pos, Vector3 rot)
    2.     {
    3.         // Set Dimensions
    4.         float squareX = squareDimensions.x / 2;
    5.         float squareY = squareDimensions.y / 2;
    6.  
    7.         //calculate position
    8.         Vector3 cornerPos1 = new Vector3(pos.x + squareX, pos.y + squareY, pos.z);
    9.         Vector3 cornerPos2 = new Vector3(pos.x - squareX, pos.y + squareY, pos.z);
    10.         Vector3 cornerPos3 = new Vector3(pos.x + squareX, pos.y - squareY, pos.z);
    11.         Vector3 cornerPos4 = new Vector3(pos.x - squareX, pos.y - squareY, pos.z);
    12.  
    13.         //calculate rotation
    14.         Vector3 cornerRot1 = CalculateRotation(cornerPos1, rot);
    15.         Vector3 cornerRot2 = CalculateRotation(cornerPos2, rot);
    16.         Vector3 cornerRot3 = CalculateRotation(cornerPos3, rot);
    17.         Vector3 cornerRot4 = CalculateRotation(cornerPos4, rot);
    18.  
    19.         //final position (this is just a place holder, but don't i need to do something here?)
    20.         Vector3 corner1 = cornerRot1;
    21.         Vector3 corner2 = cornerRot2;
    22.         Vector3 corner3 = cornerRot3;
    23.         Vector3 corner4 = cornerRot4;
    24.  
    25.  
    26.         Debug.DrawLine(corner2, corner1, Color.white);
    27.         Debug.DrawLine(corner1, corner3, Color.white);
    28.         Debug.DrawLine(corner3, corner4, Color.white);
    29.         Debug.DrawLine(corner4, corner2, Color.white);
    30.     }
    31.  
    32.     private Vector3 CalculateRotation(Vector3 position, Vector3 rotation)
    33.     {
    34.         //2d translate rotation = ((x * cos(rot) - y * sin(rot), (x * sin(rot) - y *  cos(rot))
    35.  
    36.         float rot = rotation.y;
    37.         float posX = position.x;
    38.         float posZ = position.z;
    39.  
    40.         Vector3 rotatedPosition = new Vector3(posX * Mathf.Cos(rot) - posZ * Mathf.Sin(rot), rot, posX * Mathf.Sin(rot) - posZ * Mathf.Cos(rot));
    41.  
    42.         return rotatedPosition;
    43.     }

    I honestly have no clue what I'm doing wrong, but I'm pretty sure it's something in here
     
  2. zulo3d

    zulo3d

    Joined:
    Feb 18, 2023
    Posts:
    510
    Code (CSharp):
    1.         private void CalculateSquarePosition(Vector3 pos, Vector3 rot)
    2.         {
    3.             // Set Dimensions
    4.             float squareX = squareDimensions.x / 2;
    5.             float squareY = squareDimensions.y / 2;
    6.    
    7.             //calculate position
    8.             Vector3 corner1 = new Vector3(pos.x + squareX, pos.y + squareY, pos.z);
    9.             Vector3 corner2 = new Vector3(pos.x + squareX, pos.y - squareY, pos.z);
    10.             Vector3 corner3 = new Vector3(pos.x - squareX, pos.y - squareY, pos.z);
    11.             Vector3 corner4 = new Vector3(pos.x - squareX, pos.y + squareY, pos.z);
    12.    
    13.             //calculate rotation
    14.             corner1 = CalculateRotation(corner1, rot);
    15.             corner2 = CalculateRotation(corner2, rot);
    16.             corner3 = CalculateRotation(corner3, rot);
    17.             corner4 = CalculateRotation(corner4, rot);
    18.    
    19.             Debug.DrawLine(corner1, corner2, Color.white);
    20.             Debug.DrawLine(corner2, corner3, Color.white);
    21.             Debug.DrawLine(corner3, corner4, Color.white);
    22.             Debug.DrawLine(corner4, corner1, Color.white);
    23.         }
    24.    
    25.         private Vector3 CalculateRotation(Vector3 position, Vector3 rotation)
    26.         {
    27.             Vector3 rotatedPosition = Quaternion.Euler(rotation)*position;
    28.             return rotatedPosition;
    29.         }
     
  3. give_me_pen

    give_me_pen

    Joined:
    Oct 18, 2021
    Posts:
    24

    This worked flawlessly, all I had to do afterwards was make the Vector3 entered into the "pos" parameter a local position, and then move it to its global position at that "final position" spot (I don't want it following the position of the GO its attached to)


    I greatly appreciate the help, and if it wouldn't be a bother, could you provide a link to something that would help me better understand quaternion rotations? I've tried looking myself, however I'm never able to wrap my head around any of it
     
  4. zulo3d

    zulo3d

    Joined:
    Feb 18, 2023
    Posts:
    510
    Don't worry about understanding quaternions. Many people use cars everyday and yet they don't have a clue on how they work. They just know how to use them.

    Well just know that if you multiply a vector by a quaternion then the vector will be rotated.

    Personally I went from initially finding quaternions very puzzling to eventually understanding how they worked. But then I later realized I didn't really understand how they worked.

    Maybe one day I'll understand how they work again. But then maybe I'll only think I know how they work...

    I'm now at the stage where I just use them and abuse them. If a thing rotates when I want it to rotate then I'm happy..
     
  5. give_me_pen

    give_me_pen

    Joined:
    Oct 18, 2021
    Posts:
    24

    Thank you for replying, I truly appreciate the advice and expect to keep it in mind going forward. I suppose it makes sense considering that's how I've come to understand other concepts while learning unity. Exposure seems to allow for better understanding than memorizing rules, I often find
     
  6. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,468
    To be clear though, this is nothing to do with physics, it's just using Transforms so I'll move this thread to scripting. Physics is when you use the physics systems!