Rotate bullet along with ship?

edited May 2014 in How To...

Hello, I've been having trouble rotating the bullet independently from the ship, the closest I've gotten is rotating along with the Ship

I understand I need to translate the ships angle to the X and Y position of the bullet but I can't seem to figure it out without using Pvectors

Any ideas?

   Ship player;
    Background b;



    void setup() {
      size(500, 500); 
      b = new Background();
      player= new Ship(width/2, height/2, 10);
    }//end setup

    void keyReleased() {
      if (key == 'w' || key == 'W') {
        player.up = false;
      }
      else if (key == 's' || key == 'S') {
        player.down = false;
      }
      else if (key == 'a' || key == 'A') {
        player.left = false;
      }
      else if (key == 'd' || key == 'D') {
        player.right = false;
      }
      else if (key == ' ') {
        player.spacePressed = false;
      }
    }

    void draw() {
      player.update();
      player.move(player);
      b.display();
      player.display();
    }

    class Background{
      //fields
      //constructor
      Background(){
      }
      //methods
      void display(){
        background(0);
      }
    }

    class Bullet {
      //Fields
      float BxPos, ByPos, bulletWidth, bulletHeight, speed;
      boolean isAlive;
      //Constructor
      Bullet() {
        reset(0, 0, 0);
        bulletWidth = 3;
        bulletHeight = 4;
        isAlive = false;
      }

      //Methods
      void reset(float xIn, float yIn, float speedIn) {
        BxPos = xIn;
        ByPos = yIn;
        speed = speedIn;
        isAlive = true;
      }
      void move() {
        if (!isAlive) {
          return;
        }
        ByPos -= speed;
        if (ByPos < -bulletHeight) {
          isAlive = false;
        }
        /*code for line laser
         if ( xPos < -xPos ) {
         xPos = width;
         }
         if ( xPos > width ) {
         xPos = 0;
         }
         if ( yPos < -yPos ) {
         yPos = height;
         }
         if ( yPos > height) {
         yPos = 0;
         }
         **/
      }

      void display() {
        if (!isAlive) {
          return;
        }
        fill(255);
        stroke(255);
        rect(BxPos, ByPos, bulletWidth, bulletHeight);
      }
    }

    class Ship {
      //Fields
      boolean spacePressed = false, up = false, down = false, left = false, right = false;
      float Xv, Yv, force, angle, drag, xPos, yPos, speed;
      int cooldown;
      Bullet[] bullets;
      //Constructor

      Ship(float xIn, float yIn, int maxBullets) {
        xPos = xIn;
        yPos = yIn;
        speed = 0.10f;   //Speed of ship
        drag = 0.99;   //Drag to restrain speed
        angle =0;
        Xv =0;   //Xvelocity
        Yv =0;   //YVelocity
        force = radians(5.0);   //Ship rotation speed
        cooldown = 10; //Bullet Cooldown

        bullets = new Bullet[maxBullets];
        for ( int i=0; i<bullets.length; i++) {
          bullets[i] = new Bullet();
        }
      }//end Ship constructor
      //Methods
      void update() {

        if (cooldown > 0) {
          cooldown--;
        }
        else {
          if (!spacePressed) {
            return;
          }
          for (int i = 0; i < bullets.length; i++) {
            if (!bullets[i].isAlive) {
              bullets[i].reset(xPos - bullets[i].bulletWidth/2, yPos - bullets[i].bulletHeight, 5);
              break;
            }
          }
          cooldown = 15; //Every 15 frames, a new bullet can be shot
        }
      }
      void move(Ship player) {
        Xv *= drag;
        Yv *= drag;
        xPos += Xv;
        yPos += Yv;
        checkInput();

        for (int i = 0; i < bullets.length; i++) {
          bullets[i].move();
        }

        if (up) {

          Xv += cos(angle) * speed; //increase speed while angle direction in Xposition e.g Ship pointing 90degrees propells at max speed while say 45degrees only propells half half speed
          Yv += sin(angle) * speed; //Enabling both Cos and sin maintains equal speed increase across 360 degrees of direction
        }


        if (right) {
          angle += force;
        }
        if (left) {
          angle -= force;
        }
      }//end move function

      void checkInput() {
        if (keyPressed) {
          if (key == 'a') {
            left = true;
          }
          if (key == 'd') {
            right = true;
          }
          if (key == 'w') {
            up = true;
          }
          if (key == 's') {
            down = true;
          }
          if (key == ' ') {
            spacePressed = true;
          }
        }//end KeyPressed
      }//end CheckInput


      void display() {
        strokeWeight(1);
        for (int i = 0; i < bullets.length; i++) {
          bullets[i].display();
        }

        translate(xPos, yPos);
        rotate(angle);


        stroke(255);
        triangle(-30, -20, 15, 0, -30, 20);
        noStroke();
        translate(-xPos, -yPos);


        if (xPos > width + 30)
          xPos = -30;
        else if (xPos < -30)
          xPos= width + 30;
        if (yPos > height + 30)
          yPos = -30;
        else if (yPos < -30)
          yPos = height + 30;
      }//end Display
    }//end Ship Class
Tagged:

Answers

Sign In or Register to comment.