0

I have a sprite shooting bullets but the angle that the bullets leave is 90 degrees short of the sprite rotation.I tried adding Math.Pi/2 but it doesnt work. The sprite starts facing up and code of the sprite rotation is this:

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

The bullet Shoot and Update are the following:

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);
        }
  • Could you describe the symptom? Like, it moves in the correct direction, but is facing another direction? – bubbinator Jun 01 '14 at 17:42
  • It is facing the right way but moves in the wrong direction. Like if i try to shoot to the sprite top, the bullet moves to his right but facing top. – user3150130 Jun 01 '14 at 17:46
  • 1
    I was afraid of that, I don't have the answer for you directly, but I do know how to solve it. On this line: `newBullet.velocity = new Vector2((float)Math.Cos(rotation), (float)Math.Sin(rotation)) * 5f;` try swapping the calls to get the X and Y values. If that doesn't work, try negating one or the other. It's trial and error, but the result, at least to me when I found it, didn't make any sense. – bubbinator Jun 01 '14 at 17:52

1 Answers1

0

The solution was to replace:

    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;