TheProgrammer
TheProgrammer

Reputation: 1344

Shoot bullet at a 45 degree angle

I am trying to shoot a bullet at a 45 degree angle. However, it keeps shooting straight.

float armCos = (float)Math.Cos(0.0f - MathHelper.PiOver2);
float armSin = (float)Math.Sin(0.0f - MathHelper.PiOver2);

bullet.position = new Vector2(
                        arm.position.X + 42 * armCos,
                        arm.position.Y + 42 * armSin);

Upvotes: 1

Views: 440

Answers (1)

Davor Mlinaric
Davor Mlinaric

Reputation: 2017

You can use this function that returns vector. Use is on your init bullet function and store into some variable and use it to update your bullet position.

public static Vector2 Vector2FromAngle(double angle, bool normalize = true)
{
    Vector2 vector = new Vector2((float)Math.Cos(angle), (float)Math.Sin(angle));
    if (vector != Vector2.Zero && normalize)
        vector.Normalize();
    return vector;
}

Upvotes: 1

Related Questions