我有一个精灵射击子弹,但子弹离开的角度是精灵旋转的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);
}
答案 0 :(得分:0)
解决方案是取代:
newBullet.velocity = new Vector2((float)Math.Cos(rotation),
(float)Math.Sin(rotation)) * 5f;
with:
newBullet.velocity = new Vector2((float)Math.Sin(rotation),
-(float)Math.Cos(rotation)) * 5f;