C# XNA角度不';不匹配
我有一个精灵射击子弹,但是子弹离开的角度比精灵旋转少90度。我尝试添加Math.Pi/2,但它不起作用。精灵开始面朝上,精灵旋转的代码如下:C# XNA角度不';不匹配,c#,xna,C#,Xna,我有一个精灵射击子弹,但是子弹离开的角度比精灵旋转少90度。我尝试添加Math.Pi/2,但它不起作用。精灵开始面朝上,精灵旋转的代码如下: Vector2 direction = mousePosition - position; direction.Normalize(); rotation = (float)Math.Atan2((double)direction.Y, (double)direction.X) + ((1f * (float)Math.PI) /
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);
}
public void UpdateBullets()
{
foreach(子弹中的子弹)
{
bullet.position+=bullet.velocity;
if(矢量2.距离(子弹位置,位置)>500)
bullet.isVisible=false;
}
对于(int i=0;i
解决方案是替换:
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;
你能描述一下症状吗?比如说,它朝着正确的方向移动,但却朝着另一个方向?它朝着正确的方向移动,但却朝着错误的方向移动。比如说,如果我试着射击到精灵的顶端,子弹会移动到他的右边,但朝着顶端。我很害怕,我没有直接的答案给你,但我知道如何解决它。在这一行:
newBullet.velocity=newvector2((float)Math.Cos(rotation),(float)Math.Sin(rotation))*5f代码>尝试交换调用以获取X和Y值。如果这不起作用,试着否定其中一个。这是反复试验,但结果,至少在我找到它时,对我来说,没有任何意义。仅供参考:也包含答案。