Search code examples
c#vectorxna

Getting a vector from an angle and length?


I'm working on a monster's sight(cone-shaped) and need a way to draw it. The vector I get from the angle/distance will be centered around a position(the monster's position).

  • Using C#/Xna, how can I get a vector if I have an angle and distance(length from position)?

The answer helped lead me in the right direction. Code now looks like:

Vector2 vector = new Vector2((float)Math.Cos(angle) * distance + position.X, (float)Math.Sin(angle) * distance + position.Y);

Solution

  • You'd use some trigonometry.

    Like so, assuming that angle is your angle, in radians:

    new Vector2((float)Math.Cos(angle), (float)Math.Sin(angle));
    

    However, if you want an angle in degrees, then you'd use MathHelper.ToRadians, like this:

    float angleInRadians = MathHelper.ToRadians(angle);
    new Vector2((float)Math.Cos(angleInRadians), (float)Math.Sin(angleInRadians));