Search code examples
c#direction

Determine new position given rotation and required distance


I have an object that I acquire a position and rotation from in a two-dimensional space.

I need to advance that objects' position 12 meters total between both X and Y in the same direction its' rotated being provided only ways to change the X and Y positions.

This is a (bad) example of what I've tried and failed at.

if (direction <= 45)
{
    float nx = Convert.ToSingle(direction * .13333333);
    float ny = 12 - nx;
} else if (direction <= 90)
{
    float ny = Convert.ToSingle((90 - direction) * .133333333);
    float nx = 12 - ny;
} else if (direction <= 135)
{
    float ny = Convert.ToSingle((135 - direction) * -.133333333);
    float nx = -12 - ny;
} else if (direction <= 180)
{
    float nx = Convert.ToSingle((180 - direction) * -.133333333);
    float ny = -12 - nx;
}

Am I even using the correct formula or methodology to acquire the desired result? I have reason to believe I need Cos and Tan, but have zilch idea as to how or when to use them.


Solution

  • You have to think of the vector as a triangle and then solve for it. The answer is not too complicated. https://www.mathsisfun.com/algebra/trig-finding-side-right-triangle.html

        //convert degrees into radians for the .NET trig functions
        direction *= Math.PI / 180;
    
        float nx = (float)(12 * Math.Cos(direction));
        float ny = (float)(12 * Math.Sin(direction));