Cos and sin to point North

Hi All,

I’ve forgotten how to do this Sin/Cos zero always point east, I want them to point north for a function I’m building.

public static bool CastLine(float degrees, Vector2 start, float length)
  {
   float mRadians = (degrees * Mathf.Deg2Rad);

   // rotate the mRadians angle clockwise by 90 degrees
   // (this makes 0 point NORTH)
   mRadians += (float)(Math.PI / 2.0F);

   float mX = start.x + (length * (float)(Math.Cos(mRadians)));
   float mY = start.y + (length * (float)(Math.Sin(mRadians)));

   Vector2 mFinish = new Vector2(mX, mY);


   Debug.DrawLine(start, mFinish, Color.blue, 1F, false);

   RaycastHit2D mHit = Physics2D.Linecast(start, mFinish);
 

   return true;
  }

but it does not always point at the correct direction I’m trying to accomplish a compass in effect,

Utilitles.CastLine(90, new Vector2(0F, 0F), 1F);

I thought all I had to do was do this

mRadians += (float)(Math.PI / 2.0F);

to work correctly an help would be nice.

You need to define north first off. You’re casting your line “north”? Is this a birdseye view 2D game where north is ‘up’?

What is your coordinate space? In which direction does +x point? What direction does +y point? I can assume you’re using unity 2d’s directions where +y is visually up, and +x is visually right… but who knows… you may have adjusted your camera.

These will all impact your rotations. The x = Cos(theta), y = Sin(theta), algorithm will rotate a vector from +x towards +y by an amount of theta. This is why theta = 0 points in the direction of +x, you’ve rotated 0.

Lastly, what do you mean “but it does not always point at the correct direction”? What direction does it point? What ‘degrees’ values are you passing in that don’t result the way you expect? How about you give us an example… “when I pass in 45 degrees the line points in insert direction, when I expect it to point insert another direction”.

Hi lordofduct,

The start vector is the centre point of the GameObject, I will adjust this later to make sure it’s on the edge when I cast the line, but for now its like that.

the following is the directions it creates.

Degrees / what direction is should be / What direction is or correct.

  • 0 = North (this points in the correct direction)
  • 45 = North East (This points to North West direction)
  • 90 = East (This points to West direction)
  • 135 = South East (This points to South West direction))
  • 180 = South (this points in the correct direction)
  • 225 = South West (This points to South East direction)
  • 270 = West (This points to East direction)
  • 315 = North West (This points to North East direction)

Yes this is for 2D, I used my android joystick example code, I created but there I had a start and an end point then I worked the angle but this time I know the angle I just want the endpoint calculated.

You still haven’t defined north. But I’m going to assume by “north” you mean +y.

Now note, your expected results vs your results. You EXPECT to travel clockwise around the compass, but the results are traveling counter-clockwise.

If north is +y, and east is +x. And I told you the algorithm of x = Cos(theta), y = Sin(theta) results in a rotational direction from +x towards +y… that’d be a COUNTER-CLOCKWISE rotation. You expect the opposite direction.

Now, to resolve your problem. Instead of adjusting your angle, adjust your algorithm. We need a definition for calculating a vector that records 0 degrees pointing “north”, and 90 degrees pointing “east” (up and right respectively).

Sine is defined from 0-360 as going from 0 → 1 → 0 → -1 → 0 on a 90 degree interval

And Cosine from 0-360 as going from 1 → 0 → -1 → 0 → 1 on a 90 degree interval

We expect our y value to go from 1 → 0 → -1 and back as we rotate clockwise. This happens to be exactly what cosine does.

And we expect our x value to go from 0 → 1 → 0 → -1 → 0 as we rotate clockwise. This happens to be exactly what sine does.

SO:

y = Cosine(theta)
x = Sine(theta)

No need to add the 90 degrees to the angle or anything:

///<summary>
/// degrees is measured as an angle off Y+ (north) rotating clockwise towards X+ (East)
///</summary>
public static bool CastLine(float degrees, Vector2 start, float length)
{
    float mRadians = (degrees * Mathf.Deg2Rad);

    //I'm going to use UnityEngine.Mathf instead, it defaults to 'float' rather than 'double'
    float mY = start.y + (length * Mathf.Cos(mRadians));
    float mX = start.x + (length * Mathf.Sin(mRadians));

    Vector2 mFinish = new Vector2(mX, mY);


    Debug.DrawLine(start, mFinish, Color.blue, 1F, false);

    RaycastHit2D mHit = Physics2D.Linecast(start, mFinish);

    //why return true? it always returns true... what's the point?
    return true;
}

Keep in mind that rotation is just a measure of the angle between two lines. When you talk about rotating an object, there’s more information… the direction of rotation, and off of what you’re rotating. It’s a multi-dimensional operation, yet you represent it in a 1-dimensional value. So thusly you need to include a definition of ‘no-rotation’, as well as a ‘direction’ of rotation.

And that’s what the algorithm we set is doing.

So I add it to the comments so anyone using the function understands what the angle ‘degrees’ is representing. With out that, one would probably assume the standard definition which is off +x towards +y.

I created a little vb.net app of what I’m on about, very similar code as here and this works fine.

You can run it if you go in the LineCastApp\LineCastApp\bin\Debug, if you worry about a virus just delete the bin folder and load it in Visual Studio 'm using VS2012.

As I said the function is not finished so it will return true, this however does not

  public static bool CastLine(float degrees, Vector2 start, float length, ModeType mode, List<String> tags)
  {
   float mRadians = (degrees * Mathf.Deg2Rad);

   // rotate the mRadians angle clockwise by 90 degrees
   // (this makes 0 point NORTH)
   mRadians += (float)(Math.PI / 2.0F);

   float mX = start.x + (length * (float)(Math.Cos(mRadians)));
   float mY = start.y + (length * (float)(Math.Sin(mRadians)));

   Vector2 mFinish = new Vector2(mX, mY);

   if (mode == ModeType.Debug)
    Debug.DrawLine(start, mFinish, Color.blue, length, false);

   RaycastHit2D mHit = Physics2D.Linecast(start, mFinish);
    if (mHit != null && mHit.collider != null)
      {
       if (tags != null && tags.Count > 0)
        return tags.OfType<string>().Any(F => F.Contains(mHit.collider.tag));   
       else
        return true;   
      }

   return false;
  }

I wonder if its the Mathf.Deg2Rad constant??

2472771–170300–LineCastApp.zip (77.3 KB)

Yes, Mathf.Deg2Rad is a constant.

And did you at all attempt to use my adjusted algorithm?

have a quick look at 08:00 onwards for a little bit… does that help?

for Zero the vb.net has a radian value of -1.57079637

But the unity function has this 3.92699077439937

no ignore me, they both the same I had 315 in the parameter not zero

Do note, WinForms (which I assume your vb.net project uses) has a COMPLETELY different coordinate system from Unity. Where y+ points down, and x+ points right!

To rotate from x+ to y+ means to rotate clockwise.

Have you TRIED my solution:

///<summary>
/// degrees is measured as an angle off Y+ (north) rotating clockwise towards X+ (East)
///</summary>
public static bool CastLine(float degrees, Vector2 start, float length)
{
    float mRadians = (degrees * Mathf.Deg2Rad);

    //I'm going to use UnityEngine.Mathf instead, it defaults to 'float' rather than 'double'
    float mY = start.y + (length * Mathf.Cos(mRadians));
    float mX = start.x + (length * Mathf.Sin(mRadians));

    Vector2 mFinish = new Vector2(mX, mY);


    Debug.DrawLine(start, mFinish, Color.blue, 1F, false);

    RaycastHit2D mHit = Physics2D.Linecast(start, mFinish);

    //why return true? it always returns true... what's the point?
    return true;
}

Note, I CHANGED your algorithm. I Cos for y, and Sin for x… instead of the other way around like you have. This way you get a clockwise rotation from y+ to x+.

Also:

This is missing information. What radian value? What function? We’re talking about multiple different angles and functions here. The result of Cosine? You do know the result of cosine is not in radians… it’s the length of the adjacent leg of a right triangle with hypotenous of length 1 for an angle with that radian measure input.

Really dude… I REWROTE YOUR CODE FOR YOU!

I’ve posted it twice.

Did you not read? You don’t need to be a mathematician to read. The explanation was there just so you could get an explanation for why my changes were made.

I didn’t see you had swapped them just taken out the 90 degrees rotation.

Are yes now it works I see what you did, so I had to change them (cos/sin around) BECAUSE of unity then??

I do wonder why it was different than I did , I did not spot it, is this because unity say 0 Degrees is north not east??

I think its where Sine or Cosine start from if I’m right??

Thanks

lordofduct

Because the coordinate system in unity is lower-left, y+ up.

VB.Net & WinForms is a coordinate system that is upper-left y+ down.

unity doesn’t say 0 degrees is north.

Unity doesn’t say ANYTHING about north.

North is an arbitrary term in the context of unity (as well as with vb.net & WinForms). You merely defined north as being up on the screen… that’s not actually north. North is the direction from a point in a magnetic field (which are round mind you) towards the respective pole defined as north… most commonly defined as towards the north pole of Earth. There is no NORTH in Unity, there is no NORTH in VB.Net/WinForms.

That’s why I kept telling you to define north!

In which I assume you meant “up”, because you never actually told me. In which case up in unity is +y, and up in VB.Net/WinForms is -y.

points at vid ^^

1 Like