简体   繁体   中英

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);

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM