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

How would I "rotate" x, y coordinate?

Discussion in 'Scripting' started by Stef_Morojna, Dec 3, 2016.

  1. Stef_Morojna

    Stef_Morojna

    Joined:
    Apr 15, 2015
    Posts:
    289
    Ok so I have a position stored in a double x and y, and I want to rotate it by A.

    Code (CSharp):
    1.  
    2.    double x; // coordinates
    3.    double y;
    4.    double A; // The rotation I want to apply to x,y in radians
    Here I have a visual representation of what I want to do:
    upload_2016-12-3_18-34-51.png

    Btw I know I could rotate it if I used vector3 and quaternion, but I really need the precision of doubles
     
  2. The-Little-Guy

    The-Little-Guy

    Joined:
    Aug 1, 2012
    Posts:
    297
    What you're looking for is the RotateAround method. You may want to add some more logic, as this will rotate forever.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class ExampleClass : MonoBehaviour {
    5.     void Update() {
    6.         transform.RotateAround(Vector3.zero, Vector3.up, 20 * Time.deltaTime);
    7.     }
    8. }
     
    MadPenguin likes this.
  3. Stef_Morojna

    Stef_Morojna

    Joined:
    Apr 15, 2015
    Posts:
    289
    I need to rotate doubles, not floats. Floats simply aren't precise enough for what I need.
     
  4. Sabo

    Sabo

    Joined:
    Nov 7, 2009
    Posts:
    151
    My math is quite rusty, but I think you can do something like this:

    Code (CSharp):
    1. var cos = Math.Cos(angle);
    2. var sin = Math.Sin(angle);
    3. var rotatedX = x * cos - y * sin;
    4. var rotatedY = x * sin + y * cos;
    Note that you need to use System.Math.Cos, not UnityEngine.Mathf.Cos, since the former is using doubles and the latter is using floats. Also, mind the unit of angular measure, radian vs degree. Check the documentation.

    Again, not sure if the math is right, but I think so and you'll find out rather fast I'm sure.
     
  5. Stef_Morojna

    Stef_Morojna

    Joined:
    Apr 15, 2015
    Posts:
    289
    Thanks!