BigBug
BigBug

Reputation: 6290

Trying to shoot bullets

So, i'm making my own game of asteroids and i'm a bit stuck. I have a bullet that is fired from the ship when i press the key Z. But the bullet always fires off in the same direction and i'm not sure how to change that? I want it to fire off in the direction which the ship is facing. Also, if i push Z quickly (to fire many bullets) the one bullet appears to keep getting reset. How do i fix these issues? Here is my code:

class SpaceShip
{
    private Vector2 pos; //Our position in 2D.
    private Texture2D tex; //Our texture
    private float speed = 8; //The speed to move.
    private float angleOfRotation = 0;
    Sprite tempSprite;


    public SpaceShip(Vector2 pos, Texture2D tex, Texture2D bulletsToAdd)
    {
        this.pos = pos; //Our starting position
        this.tex = tex; //Our texture
        tempSprite = new Sprite(bulletsToAdd); 
    }



    private void WorldWrap(int worldWidth, int worldHeight)
    {

        if (this.pos.X < 0)
        {
            //If we've past the left side of the window, set on other side of the world.
            this.pos.X = worldWidth;
        }
        if (this.pos.X > worldWidth)
        {
            //If we've past the right side of the window, set on other side of the world.
            this.pos.X = this.tex.Width;
        }
        if (this.pos.Y < 0)
        {
            //If we've passed the top side of the window, set on other side of the world.
            this.pos.Y = worldHeight;
        }
        if (this.pos.Y > worldHeight)
        {
            //If we've passed the bottom side of the window, set on other side of the world.
            this.pos.Y = this.tex.Height;
        }
    }

    public void CreateBullets(Texture2D bulletsToAdd)
    {

        tempSprite.Position = new Vector2((pos.X),(pos.Y));
        tempSprite.Velocity = new Vector2((float)7, 7);
        //tempSprite.Rotation = angleOfRotation;
    }


    public void Update(int WorldWidth, int WorldHeight, Texture2D bulletsToAdd)
    {
        KeyboardState keys = Keyboard.GetState();
        if (keys.IsKeyDown(Keys.A))
        {
            //We want to turn the ship LEFT
            this.angleOfRotation -= 2f;

        }
        if (keys.IsKeyDown(Keys.D))
        {
            //We want to turn the ship RIGHT
            this.angleOfRotation += 2f;

        }
        if (keys.IsKeyDown(Keys.W))
        {
            //We're pressing the up key (W) so go Forwards!
            this.pos.X += (float)(Math.Cos(this.angleOfRotation * Math.PI / 180) * this.speed);
            this.pos.Y += (float)(Math.Sin(this.angleOfRotation * Math.PI / 180) * this.speed);

        }
        if (keys.IsKeyDown(Keys.S))
        {
            //We're pressing the down key (S) so go Backwards!
            this.pos.X -= (float)(Math.Cos(this.angleOfRotation * Math.PI / 180) * this.speed);
            this.pos.Y -= (float)(Math.Sin(this.angleOfRotation * Math.PI / 180) * this.speed);

        }
        if (keys.IsKeyDown(Keys.Right))
        {
            //We're pressing the up key (W) so go Forwards!
            this.pos.X += (float)(Math.Cos(this.angleOfRotation * Math.PI / 180) * this.speed);
            this.pos.Y += (float)(Math.Sin(this.angleOfRotation * Math.PI / 180) * this.speed);

        }
        if (keys.IsKeyDown(Keys.Left))
        {
            //We're pressing the down key (S) so go Backwards!
            this.pos.X -= (float)(Math.Cos(this.angleOfRotation * Math.PI / 180) * this.speed);
            this.pos.Y -= (float)(Math.Sin(this.angleOfRotation * Math.PI / 180) * this.speed);

        }

        if (keys.IsKeyDown(Keys.Up))
        {
            //We want to turn the ship LEFT. Rotate it counter-clockwise
            this.angleOfRotation -= 2f;

        }
        if (keys.IsKeyDown(Keys.Down))
        {
            //We want to turn the ship RIGHT. Rotate it clockwise
            this.angleOfRotation += 2f;

        }
        if (keys.IsKeyDown(Keys.Z))
        {

            CreateBullets(bulletsToAdd); 
        }


        tempSprite.Position += tempSprite.Velocity;

        //check if we need to move the ship.
        WorldWrap(WorldWidth, WorldHeight);

    }

    public void Draw(SpriteBatch batch)
    {
        batch.Draw(this.tex, //Texture to use.
        this.pos, //Position to draw at
        new Rectangle(0, 0, this.tex.Width, this.tex.Height),Color.White, (float)((this.angleOfRotation + 90) * Math.PI / 180), new Vector2(this.tex.Width / 2, this.tex.Height / 2), 1.0f, SpriteEffects.None, 0.0f);

        batch.Draw(tempSprite.Texture, tempSprite.Position, null, Color.White, tempSprite.Rotation, tempSprite.Center, tempSprite.Scale, SpriteEffects.None, 1.0f);
    } //End Draw function

Upvotes: 0

Views: 3829

Answers (1)

Robert Rouhani
Robert Rouhani

Reputation: 14678

First, you only have a single bullet - tempSprite. You're probably going to want to create a separate Bullet class and a List<Bullet> somewhere that you can add bullets to.

All you need for this is a little trigonometry. Right now your velocity vector is a constant <7, 7>. What you want to do is make that vector go in the direction the spaceship is pointing at. So assuming your angle is in radians and not degrees (if they're in degrees you'll want to change them to radians), we can use Math.Sin() and Math.Cos() to get the individual X and Y components of the velocity vector like this:

bulletSpeed = 7;
tempSprite.Velocity = new Vector2((float)Math.Cos(angleOfRotation) * bulletSpeed, (float)Math.Sin(angleOfRotation) * bulletSpeed);

Upvotes: 2

Related Questions