【问题标题】:XNA Angles that don't matchXNA 角度不匹配
【发布时间】:2014-06-02 02:31:43
【问题描述】:

我有一个精灵射击子弹,但子弹离开的角度比精灵旋转少 90 度。我尝试添加 Math.Pi/2 但它不起作用。精灵开始朝上,精灵旋转的代码是这样的:

Vector2 direction = mousePosition - position;
direction.Normalize();
rotation = (float)Math.Atan2((double)direction.Y,
            (double)direction.X) + ((1f * (float)Math.PI) / 2);

子弹射击和更新如下:

public void UpdateBullets()
        {
            foreach (Bullets bullet in bullets)
            {
                bullet.position += bullet.velocity;
                if (Vector2.Distance(bullet.position, position) > 500)
                    bullet.isVisible = false;
            }
            for (int i = 0; i < bullets.Count; i++)
            {
                if (!bullets[i].isVisible)
                {
                    bullets.RemoveAt(i);
                    i--;
                }
            }
        }



public void Shoot()
        {
            Bullets newBullet = new Bullets(Content.Load<Texture2D>("Sprites/bala"));
            newBullet.velocity = new Vector2((float)Math.Cos(rotation), (float)Math.Sin(rotation)) * 5f;
            newBullet.position = position + newBullet.velocity * 5;
            newBullet.isVisible = true;

            if (bullets.Count() < 20)
                bullets.Add(newBullet);
        }

【问题讨论】:

  • 您能描述一下症状吗?比如,它朝正确的方向移动,但面向另一个方向?
  • 它面向正确的方向,但在错误的方向移动。就像我尝试向精灵顶部射击一样,子弹会向右移动但面向顶部。
  • 我很害怕,我没有直接给你答案,但我知道如何解决它。在这一行:newBullet.velocity = new Vector2((float)Math.Cos(rotation), (float)Math.Sin(rotation)) * 5f; 尝试交换调用以获取 X 和 Y 值。如果这不起作用,请尝试否定其中一个。这是反复试验,但结果,至少在我发现它时对我来说,没有任何意义。

标签: c# xna


【解决方案1】:

解决方案是替换:

    newBullet.velocity = new Vector2((float)Math.Cos(rotation),
 (float)Math.Sin(rotation)) * 5f;

与:

    newBullet.velocity = new Vector2((float)Math.Sin(rotation),
-(float)Math.Cos(rotation)) * 5f;

【讨论】:

猜你喜欢
  • 2023-03-03
  • 1970-01-01
  • 2020-09-09
  • 2021-02-09
  • 1970-01-01
  • 2017-06-29
  • 2016-12-09
  • 2019-04-05
  • 1970-01-01
相关资源
最近更新 更多