Why does this trig calculation only give results in one quadrant?

I asked this question somewhere else (another website) and nobody seems to want to even look at it…

But anyway the code I’m about to post is supposed to compute an angle in the opposite quadrant of the input point. It should then take the Cosine and Sine of the angle multiplied by a radius CameraOffset variable to produce a point in the opposite quadrant of the original point…

The code:

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);

the movementLerp Vector3 is to be assigned to a gameobject called LerpTarget, something the camera localposition will be Lerped to. This is just to create the effect of fluidity in my flight control scheme, if that makes any sense. :stuck_out_tongue:

I just don’t understand why without regards to what the input is the results are always in one quadrant instead of rotating around the origin opposite my input point.

What does this sentence even mean?

What input point?

Quadrant relative to what origin?

Angle of what? (note an angle at its simplest is the measure between 2 rays with a shared vertex… 1 point is not enough information to presume 2 rays or lines)

Why should it do that?

You’re saying you should perform action a and b to get a result, and a and b aren’t giving me that result. What if actions a and b AREN’T the actions you’re supposed to do. What is the result you’re attempting to get? That information would better help us in figuring out what you’re even attempting to do.

So that’s the result you want?

Well no… it doesn’t make sense. It reminds of my days working in kitchens when we’d send the newb down to the grocer to get us “powdered water” and “wall stretchers”.

Vector2 chainDirection = new Vector2 (relativeMInput.x, relativeMInput.y); //Get opposite direction
float rad  = Vector2.Angle (new Vector2 (relativeMInput.x, 0f), chainDirection)

I would like to point out that there’s a fatal issue with this code right here in regards to ‘opposite quadrants’.

Vector2.Angle returns the measure of the angle between two vectors… better in the simple definition of an angle, it’s 2 rays whose vertex is 0,0,0 and each pointed in the directions of the two vectors.

Thing is, you chose 1 of the rays to point in the same direction along the x-axis as the other. If the vector points northeast, you’re measuring off of east. But when you put in a northwest vector, you measure it off of west.

This means the angle returned will always be acute (less than 90), rather than obtuse. It can’t not be acute.

So how did you expect to get into the ‘opposite quadrant’ with this acute angle?

@lordofduct A little bit about me, something I should have mentioned but is probably pretty obvious now. I don’t have much education in the way of calculus, other than what little I’ve read in books and picked up.

Regarding the confusion I caused, I’ve created an illustration of what this input point, the origin, and the opposite quadrant I am talking about are:

If those roman numerals are wrong, I apologize, I never got that down in school and I don’t see much use for learning it today, though it should be pretty easy. Just thought I would put it in proper notation.

This illustration is important. Its the screen in my game. The “input point” is the mouse pointer, and I want to create a Vector (ignore the dimension for a second) exactly opposite it on this graph. I’m trying to use the angle from the mouse position to the origin (screen center). That angle should be opposite the mouse pointer, so I had a simple multiplication for the chainDirection Vector2 (the mouse position relative to the origin) by negative one, assuming that would flip the signs of the coordinate values to create an oppositely pointing angle. I didn’t do too much hard thinking on that part… and neglected to leave that out of the code I posted.

I understand the concept of acute angles, and I know the Vector2.Angle function always returns an acute angle. I had code in place to account for that, but I removed it because I was confused.

Why I am doing all of this: to create the effect of fluidity in flight when playing this game, I have a few game objects which control the camera position and orientation. When one of these moves or changes orientation, a few functions are called to Lerp the camera’s local position to those gameobjects. So when I turn towards the right by moving the mouse to the right (the craft follows the mouse’s direction continuously), the ship should lag behind the camera as the whole thing turns right.

So when I’m turning right, the ship is moving LEFT relative to the camera and the users viewscreen.
Does that make sense?

If you’re this far, on with the explanation: the angle will be given to the Cos and Sin functions to get the ratio, which would be multiplied by the radius I supply, to find a new position for the LerpTarget at the opposite angle of the mouse pointer (as in the opposite screen quadrant). These two values will take up the x and y values of the Vector3 MovementLerp, and a third value for the Z component value in this Vector3.

All of it should create a smooth visual effect of the camera kind of looking ahead as it turns, and the ship gradually catching up.

Man if you have trouble with this, I don’t think you or anyone else would understand my previous code… it was very arcane and difficult to understand OR explain.

The code with changes to accomodate the acute angle returned by the Vector2.Angle foo, and the sign flip from a negative one multiplication on the chainDir:

    Vector2 chainDirection = new Vector2 (relativeMInput.x, relativeMInput.y)* -1; //Get opposite direction by flipping value signs
        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;
        if (relativeMInput.y < 0f) { //If the mouse is below the X axis...
            rad *= -1; //Make the sign of the angle negative so we can get the right angle
                }

        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);

So you want to find a vector that goes from the origin towards green point? And all you have is the position of the blue point?

That’s actually pretty easy and requires no trig at all.

var mousePoint = *; //get the mouse point
var origin = *; //whatever your origin is if it's not 0,0
var v = mousePoint - origin; //this is the vector pointing from origin to blue point
var oppositeV = -v; //this is the vector pointing from origin to the green point

Straight forward.

Origin I assume will be the middle of the screen. So it’s just the <screenwidth / 2f, screenheight / 2f>

1 Like

Grab a physics text and read the chapter on Vector math. Seriously, you could have solved yourself hours of messing about. Basic trig is also useful.

Calculus hardly comes into game dev. You are typically better off with linear algebra.

@lordofduct Oh my gosh, haha, how come I always make things so damn complicated? Haha I don’t understand why I didn’t think of that in the first place!

@Kiwasi Sounds like good advice. Thanks man!