简体   繁体   English

以45度角射击子弹

[英]Shoot bullet at a 45 degree angle

I am trying to shoot a bullet at a 45 degree angle. 我试图以45度角射击子弹。 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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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