I don't know that much about calculus, even less about Radians and polar coordinate systems. I need to find a point on the cartesian coordinate graph for my unity3d project. I know that the Mathf library with C# (or is it just Unity3d?) takes Radians as arguments, so I used the Mathf.Deg2Rad after finding my angle, then put that value into the right trig functions.

The problem is the results of these functions only give values in one quadrant. I need them rotate around the origin, with the mouse pointer (the details about the mouse are irrelevant in this question).

My code:

relativeMInput is a Vector2 containing the mouse position relative to the center of screen.

Vector2 chainDirection = new Vector2 (relativeMInput.x, relativeMInput.y); //Get opposite direction
        float rad  = Vector2.Angle (new Vector2 (relativeMInput.x, 0f), chainDirection) * Mathf.Deg2Rad; //Compute angle and convert to Radians because C# AND MANY OTHER language's math library foo's take radian arguments!
        float dirCos, dirSin = 0f;

        print ("RADIANS between center screen and mouse pointer is: " + rad+ " direction is:" + chainDirection );

        //Assuming these angle calculations are right...
        dirCos = Mathf.Cos (rad);
        dirSin = Mathf.Sin (rad);
        print ("DirCos =  " +  dirCos + "Sin = " +  dirSin);
        movementLerp = new Vector3 (cameraOffset  * dirSin, cameraOffset * dirCos, ZCamOffset);
        print ("MovementLerp = " + movementLerp);

movementLerp is a Vector3, which will be assigned to a gameobject's local position to LERP the camera to. CameraOffset and ZCamOffset specify the radius the camera will lerp to and how far back or forward the camera is.

Recommended Answers

All 3 Replies

I don't know what unity3d is, but if you have difficulties converting between radians and degrees, this C# might help:

 public const double cToDegrees = 180.0 / Math.PI;
        public const double cToRadians = Math.PI / 180.0;

        static public double ToRadians(double degrees)
        {
            return degrees * cToRadians;
        }

        static public double ToDegrees(double radians)
        {
            return radians * cToDegrees;
        }

        public static Tuple<int, int, double> DecimalDegreesToAngle(double degrees)
        {
            int deg = (int)Math.Truncate(degrees);
            double minutes = (degrees - Math.Truncate(degrees)) * 60.0;
            int min = (int)Math.Truncate(minutes);
            double sec = (minutes - Math.Truncate(minutes)) * 60.0;
            Tuple<int, int, double> tuple = new Tuple<int, int, double>(deg, min, sec);
            return tuple;
        }

Nevermind, you guys are helpful but I asked this question somewhere else and they provided a helluva lot simpler solution for what I was working on... thank you though! I didn't even need to use the trig functions!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.