0

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.

Edited by Curious Gorge

3
Contributors
3
Replies
11
Views
1 Year
Discussion Span
Last Post by Curious Gorge
Featured Replies
  • 1
    ddanbe 2,525   1 Year Ago

    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 … Read More

1

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;
        }
0

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!

This article has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.