How can I make my bullet class move with my ship and rotate when it rotates?

edited January 2018 in Questions about Code

Hello, I am trying to make an asteroids game for my video game programming class, and I want to make my bullet circle move with my ship so it shoots out of the ship instead of just shooting from the center of the screen. Here is my code, and a link to the the whole program. Google Drive Link Thanks, Mason

import processing.sound.*; //Import the processing sound library into the program
SoundFile shoot, thrust; //Set the shooting sound & the thrust sound
float a1X, a1Y, a2X, a2Y, a3X, a3Y, aD1, aD2, aD3, hbX, hbY, SmvX, SmvY, sAng, sRot, shotMV;
float clickX, clickY;
float bDia = 200;
float sX = 900, sY = 500;
float dbW; //Damage Bar
float bX = sX; //Bullet X = Ship X
float bY = sY - 25; //Bullet Y = Ship Y-25
float speedVar = 0.1; //Speed Variable for the ship to accelerate at
float sDia = 35; //The ships collision circle 
int bW = 4, bH = 4; //Bullet width & height
float bSpd = 10; //Bullet speed
color red = color(255, 0, 0); //Color variable to make the bullet red
PImage ship, a1, a2, a3, sViz; //Image variables
PFont pixel; //Font variable
boolean accel = false; //Boolean for the ship to accelerate or not
boolean shot = false; //Boolean for if the bullet is shot
boolean rules = true; //Boolean for if the rules need to be displayed or not
String ruletext = "Use W to thrust forward\n \nUse A and D keys to\nrotate the ship left and right\n\nPress space to shoot the asteroids\n\n\nClick anywhere to start";
Bullet bullet;

void setup() {
  size(1800, 1000); //Size of the window
  background(0); //Background color
  sX = 900; //Ship X location
  sY = 500; //Ship Y location
  a1X = 1000; //Asteroid1 X location
  a1Y = 100; //Asteroid1 Y location
  a2X = 400; //Asteroid1 X location
  a2Y = 700; //Asteroid1 Y location
  a3X = 1600; //Asteroid1 X location
  a3Y = 800; //Asteroid1 Y location
  aD1 = 100;
  aD2 = 100;
  aD3 = 80;
  hbX = 1750; //Health bar rectangle X location variable
  hbY = 50; //Health bar rectangle Y location variable
  dbW = 0; //Damage bar variable
  SmvX = 0; //The ships X movement variable
  SmvY = 0; //The ships Y movement variable
  shoot = new SoundFile(this, "shoot.mp3"); //Load the shoot soundfile
  thrust = new SoundFile(this, "thrust.mp3"); //Load the thrust soundfile
  ship = loadImage("data/ship.png"); //Load the ship image
  a1 = loadImage("a1.png"); //Load asteroid1 image
  a2 = loadImage("a2.png"); //Load asteroid2 image
  a3 = loadImage("a3.png"); //Load asteroid3 image
  sViz = ship; //The ships visible sprite
  pixel = createFont("pixel.TTF", 24); //Load the font
  textAlign(CENTER, CENTER); //Make the text align centered
}

void draw() {
  if (rules == true) { //If rules is true then display the rules
    fill(255, 0, 0);
    textFont(pixel);
    textSize(52);
    text(ruletext, 900, 500);
    if (mousePressed) {
      rules = false; //When the screen is clicked stop displaying the rules
    }
  }

  if (rules == false) { //When rules are gone, start the game
    fill(0); //Fill the rectangle below with black
    rect(-1, -1, 1801, 1001); //Make a ractangle the size of the screen
    fill(50, 255, 100); //Fill the rectangle below with green
    rect(hbX, hbY, -80, 900, 7); //Make the health bar rectangle
    fill(red); //Fill the rectangle below with red
    rect(1750, 50, -80, dbW, 7); //Make the damage bar rectangle
    stroke(255); //Set the stroke to white

    imageMode(CENTER); //Sets the image to be centered from the center
    pushMatrix();
    translate(sX, sY);
    rotate(radians(sAng));
    image(sViz, 0, 0, 80, 80); //Shows an image on the screen
    popMatrix();

    image(a1, a1X, a1Y, 100, 100);
    image(a2, a2X, a2Y, 100, 100);
    image(a3, a3X, a3Y, 80, 80);

    a1X = a1X-random(25);
    a1Y = a1Y+random(25);
    a2X = a2X-random(25);
    a2Y = a2Y-random(25);
    a3X = a3X-random(25);
    a3Y = a3Y+random(25);

    noStroke();
    noFill();
    ellipse(a1X, a1Y, 100, 100);
    ellipse(a2X, a2Y, 100, 100);
    ellipse(a3X, a3Y, 100, 100);

    if (dist(sX, sY, a1X, a1Y) < sDia/2 + aD1/2) {
      dbW = dbW+10;
    }
    else if (dist(sX, sY, a2X, a2Y) < sDia/2 + aD2/2) {
      dbW = dbW+10;
    }
    else if (dist(sX, sY, a3X, a3Y) < sDia/2 + aD3/2) {
      dbW = dbW+10;
    }

    clickX = mouseX;
    clickY = mouseY;

    if (accel == true) {
      SmvX += cos(radians(sAng)); //If accel is true, then make the image move
      SmvY += sin(radians(sAng));
    }

    sX = sX + SmvX * speedVar;
    sY = sY + SmvY * speedVar;

    sAng = (sAng + sRot);

    if (shot == true) {
      bullet.move();
      bullet.display();
    }

    if (sX < -100) { //Has an image gone off the left edge?
      sX = 1900; //Teleport image to right edge
    } else if (sX > 1900) { //Has an image gone off the right edge?
      sX = 0; //Teleport image to left edge
    }
    if (sY < -100) { //Has an image gone off the top edge?
      sY = 1100; //Teleport image to bottom edge
    } else if (sY > 1100) { //Has an image gone off the bottom edge?
      sY = 0; //Teleport image to top edge
    }

    if (a1X < -100) { //Has an image gone off the left edge?
      a1X = 1900; //Teleport image to right edge
    } else if (a1X > 1900) { //Has an image gone off the right edge?
      a1X = 0; //Teleport image to left edge
    }
    if (a1Y < -100) { //Has an image gone off the top edge?
      a1Y = 1100; //Teleport image to bottom edge
    } else if (a1Y > 1100) { //Has an image gone off the bottom edge?
      a1Y = 0; //Teleport image to top edge
    }

    if (a2X < -100) { //Has an image gone off the left edge?
      a2X = 1900; //Teleport image to right edge
    } else if (a2X > 1900) { //Has an image gone off the right edge?
      a2X = 0; //Teleport image to left edge
    }
    if (a2Y < -100) { //Has an image gone off the top edge?
      a2Y = 1100; //Teleport image to bottom edge
    } else if (a2Y > 1100) { //Has an image gone off the bottom edge?
      a2Y = 0; //Teleport image to top edge
    }

    if (a3X < -100) { //Has an image gone off the left edge?
      a3X = 1900; //Teleport image to right edge
    } else if (a3X > 1900) { //Has an image gone off the right edge?
      a3X = 0; //Teleport image to left edge
    }
    if (a3Y < -100) { //Has an image gone off the top edge?
      a3Y = 1100; //Teleport image to bottom edge
    } else if (a3Y > 1100) { //Has an image gone off the bottom edge?
      a3Y = 0; //Teleport image to top edge
    }

    if (dbW >= 900) { //If the player has taken enough damage, end the game
      fill(0);
      rect(-1, -1, 1801, 1001); //Make a ractangle the size of the screen
      fill(red);
      ellipse(400, 600, bDia, bDia); // Draw the left button.
      ellipse(1400, 600, bDia, bDia); // Draw the right button. 
      text("Click this to\n play again", 400, 350);
      text("Click this to\n exit", 1400, 350);
    }
  }
}

void keyPressed() {
  if (key == ' ') {
    if (shot == false) {
      bullet = new Bullet(bX, bY, bSpd, bW, bH, red, red);
      bullet.display();
      shoot.play();
      shot = true;
    }
  } else if (key == 'w') {
    accel = true;
    thrust.play();
  } else if (key == 'a') { //If a is pressed, rotate the image left
    sRot = -3.0;
  } else if (key == 'd') { //If d is pressed, rotate the image left
    sRot = 3.0;
  } else if (key == 'l') { //If d is pressed, rotate the image left
    dbW = dbW+50;
  }
}

void keyReleased() {

  if (key == 'w') { //If s is released, stop thrusting
    accel = false;
  } else if (key == 'a') {  //If a is released, stop rotating the image
    sRot = 0.0;
  } else if (key == 'd') { //If d is released, stop rotating the image
    sRot = 0.0;
  }
}

void mousePressed() {
  if (dist(clickX, clickY, 400, 600) < bDia/2) {
    rules = true;
    dbW = 0;
  }

  //Check if the user clicked the right button.
  else if (dist(clickX, clickY, 1400, 600) < bDia/2) {
    exit();
  }
}


class Bullet {
  color bFill, bStroke;
  int bW = 2, bH = 2;
  float bX, bY, bSpd;

  Bullet(float bX, float bY, float bSpd, int bW, int bH, color bFill, color bStroke) {
    this.bX = bX;
    this.bY = bY;
    this.bSpd = bSpd;
    this.bW = bW;
    this.bH = bH;
    this.bFill = bFill;
    this.bStroke = bStroke;
  }

  void display() {
    fill(this.bFill);
    stroke(this.bStroke);
    ellipse(this.bX, this.bY, this.bW, this.bH);
  }

  void move() {
    this.bY -= this.bSpd;
    if (this.bY < 1) {
      this.bY = 500;
      shot = false;
    }
  }
}

Answers

  • I made a lot of comments, but if you need more just ask what part and I might be able to tell what that part does.

  • Answer ✓
    bullet = new Bullet(bX, bY, bSpd, bW, bH, red, red);
    

    when you create your bullet, use values that make sense for the current position and direction of the ship.

    so those first two should probably be sx and sy

    speed needs to be a vector rather than a scalar though, you need speed AND direction. and this should reflect the speed and direction of the ship when fired.

    we can't run any of your code due to lack of sound and image files.

  • (oops, i didn't see the google drive link)

  • Changing the bullet location worked, but now I need to make it rotate in the same direction as the ship. I tried putting it with the rotation code of the ship, but it didn't work.

  • You need another variable in your bullet class. Speed is not enough, you need speed and direction (or xspeed and yspeed)

  • Ok, but how do I use the variable to make the bullet rotate in the same direction of the ship?

  • How is the direction of the ship defined?

    Is it the movement vector of the ship?

    Then just use these two variables and multiply by 3.2 or so

  • The direction is defined from pressing keys that change the a rotation variable, which then changes the angle of the ship and uses the angle in the rotate functions. Lines 74-78 & 118

  • The bullet also needs a direction. Add that and paste the new code here.

  • Then just use these two variables and multiply by 3.2 or so

  • multiply by 3.2

    this seems a little random at the moment but we'll explain it when you're ready. (basically the bullet needs to move faster than the ship)

Sign In or Register to comment.