D Yamamoto
D Yamamoto

Reputation: 49

Velocity at an angle

I'm trying to find the velocity at an angle.

To do this I'm trying to use a triangle, but I'm running into road blocks.

This is the code that moves the object

Velocity = new Vector3((float)Math.Cos(MathC.AngleToPoint(EnemyObject.transform.position, PlayerPosition)),
              (float)Math.Sin(MathC.AngleToPoint(EnemyObject.transform.position, PlayerPosition)), 0);

This is the AngleToPoint method

public static double AngleToPoint(Vector3 startingPoint, Vector3 endPoint)
{
    double hypotenuse = LineLength(startingPoint, endPoint);
    double adjacent = LineLength(startingPoint, new Vector3(endPoint.x, startingPoint.y, 0));
    double opposite = LineLength(endPoint,  new Vector3(endPoint.x, startingPoint.y, 0));

    double angle = adjacent / hypotenuse;


    return Math.Acos(DegreeToRadian(angle));

}

And this is the LineLength method

public static float LineLength(Vector2 point1, Vector2 point2)
{
    return Math.Abs((float)Math.Sqrt(Math.Pow((point2.x - point1.x), 2)+Math.Pow((point2.y - point1.y),2)));

}

I am getting a lot of NaN errors, and the movement does not behave how I want it to.

Upvotes: 2

Views: 1260

Answers (1)

duplode
duplode

Reputation: 34378

The solution below was originally posted by D Yamamoto, the question author.


I have solved the problem. I'm not sure I explained myself well, so allow me to attempt to do that again. I am not a physicist so excuse me if I get some of these terms wrong.

My goal was to have an object move at a constant speed regardless of orientation. So you can assume you are a person holding a gun in the direction north (0, 1) and then rotate 45 degrees and fire your gun. The speed of the gun is some scalar quantity, such as 50 units per second. I wanted to know what X and Y values of a vector I needed to have that movement, in any direction. So Velocity (0, 50) rotated 45 degrees to the right.

I know that Velocity.X = SpeedCos(Angle) and Velocity.Y = SpeedSin(Angle), but I needed to find the value of Angle.

To do this, my line of thinking was to make a right triangle based off the starting position and the destination, then find Angle using trig.

public static double AngleToPoint(Vector3 startingPoint, Vector3 endPoint)
{
    double hypotenuse = LineLength(startingPoint, endPoint);
    double adjacent = LineLength(startingPoint, new Vector3(endPoint.x, startingPoint.y, 0));
    double opposite = LineLength(endPoint,  new Vector3(endPoint.x, startingPoint.y, 0));

    double angle = adjacent / hypotenuse;


    return Math.Acos(DegreeToRadian(angle));

}

This code was making a right triangle. The reason opposite is there even though it's not used is because I attempted multiple ways to try to accomplish this, but kept encountering errors. After making a right triangle, it takes the adj and hypo to obtain cos(adj/hypo), which I then call Acos on to give me the angle in radians. This didn't work however, it did not properly return my angle and created a lot of NaN errors.

After searching I found this gamedev.se Q&A which explains atan2, and includes this picture:

https://i.sstatic.net/xQiWG.png

It made me realize that if I just translated the line startingPoint, endPoint to startingPoint = 0 I could call atan2 and be returned my desired angle. This is the code that accomplishes that.

public static double GetAngleToPoint(Vector3 startingPoint, Vector3 endPoint)
{
    endPoint -= startingPoint;
    return Math.Atan2(endPoint.y, endPoint.x);
}

Velocity = new Vector3(Speed * (float)Math.Cos(MathC.GetAngleToPoint(StartingPosition, PlayerPosition)),
              Speed * (float)Math.Sin(MathC.GetAngleToPoint(StartingPosition, PlayerPosition)), 0);

Upvotes: 1

Related Questions