Check Asteroid death help?

Hello guys, How can I write this checkAsteroidDeath() for both of these classes? Thanks so much

void drawAsteroids() {
  for (int i=0; i<asteroids.size(); i++) {
    asteroids.get(i).draw();
  }
}


void drawBullets() {
  for (int i=0; i<bullets.size(); i++) {
    bullets.get(i).draw();
}
}



void checkAsteroidDeath() {
  // for each asteroid
  // check to see if it is touching any lasers
  // if it is touching any lasers, 
  // remove() the laser
  // remove() the asteroid
  // add +10 to the score
}


public class Bullet {

  float x;
  float y;
  float speed;
  int size = 2;
  float direction;

  Bullet(float x, float y, float d) {


    this.x = x;
    this.y = y;

    direction = d;
    speed = 6;
  }

  float getX() {
    return x;
  }

  float getY() {
    return y;
  }


  public void updatePosition() {

    x = x + (speed*cos(direction));
    y = y + (speed*sin(direction));
  }

  public void draw() {

    updatePosition();

    fill(200, 100, 0);
    ellipse(x, y, size, size);
  }
}

public class Asteroid {

  float x;
  float y;
  float speed;
  float direction;
  int size;

  Asteroid( ) {

    x = random(width);
    y = random(height);


    speed = 1.1;
    direction = random(-1, 1);
    size = 20;
  }

  void draw() {

    updatePosition();


    fill(0, 0, 255); 
    ellipse(x, y, size, size);
  }

  void updatePosition() {

    x = x + (speed*cos(direction));
    y = y + (speed*sin(direction));
  }

  float getX() {
    return x;
  }

  float getY() {  
    return y;
  }
}

Answers

  • edited March 2014

    Here's a simulation of Zombie Vs. Bullet I had here: 8-X

    /** 
     * Zombie Shooting Simulation (v1.02)
     * by GoToLoop (2014/Jan)
     *
     * forum.processing.org/two/discussion/2435/
     * help-with-collision-detecting-of-2-arrays
     */
    
    class Zombie {
      void script() {
      }
    }
    
    class Bullet {
      boolean checkHit(Zombie z) {
        return random(1) < .01;
      }
    
      boolean isOuttaBounds() {
        return random(1) < .05;
      }
    
      boolean script() {
        return isOuttaBounds();
      }
    }
    
    static final int ZOMBIES = 10, BULLETS = 30, FPS = 5;
    
    final ArrayList<Zombie> zombies = new ArrayList(ZOMBIES);
    final ArrayList<Bullet> bullets = new ArrayList(BULLETS);
    
    void setup() {
      size(600, 400);
      frameRate(FPS);
      smooth(4);
      imageMode(CORNER);
    
      for ( int i = 0; i++ != ZOMBIES; zombies.add(new Zombie()) );
      for ( int i = 0; i++ != BULLETS; bullets.add(new Bullet()) );
    }
    
    void draw() {
      clear();
    
      runScripts();
      checkDeath();
      checkVictory();
    }
    
    void runScripts() {
      for (Zombie z: zombies)  z.script();
    
      for (int b = bullets.size(); b-- != 0;)
        if (bullets.get(b).script()) {
          bullets.remove(b);
          println("A bullet has strayed too far from view!");
        }
    }
    
    void checkDeath() {
      for (int z = zombies.size(); z-- != 0;) {
        final Zombie ghoul = zombies.get(z);
    
        for (int b = bullets.size(); b-- != 0;)
          if (bullets.get(b).checkHit(ghoul)) {
            bullets.remove(b);
            zombies.remove(z);
            println("A zombie has been hit and died for good!");
    
            return;
          }
      }
    }
    
    void checkVictory() {
      final int z = zombies.size(), b = bullets.size();
    
      frame.setTitle("Zombies: #" + z + "\t\tBullets: #" + b);
    
      if (z == 0)  println("\nAll zombies are in Hell now!");
      if (b == 0)  println("\nZombies shall wander the Earth forever!");
    
      if (z * b == 0) {
        println("Zombies: #" + z + "\t Bullets: #" + b);
        exit();
      }
    }
    
  • is there another better way of writing this code?

    http://pastebin.com/jpmGhJTm

  • You're not following the backwards loop remove() rule there!!! :-w
    And the best place to place a checkHit() kinda method is inside a class!

    void checkAsteroidDeath() {
      for (int c = asteroids.size(); c-- != 0;) {
        final Asteroid a = asteroids.get(c);
    
        for (int i = bullets.size(); i-- != 0;)
          if (bullets.get(i).checkHit(a)) {
            bullets.remove(i);
            zombies.remove(c);
            score += 10;
    
            return;
          }
      }
    }
    
  • thanks. What does this part of the code mean?

    if ((sq(aX - bX) + sq(aY-bY)) < 160) {

  • edited March 2014

    Fused my "Zombie Shooting Simulation" into your sketch, and made "Asteroids Vs. Bullets Sim". Check it out: B-)

    http://studio.processingtogether.com/sp/pad/export/ro.9K-kjhuONcJDZ/latest

    /**
     * Asteroids Vs. Bullets Sim (v2.11)
     * by  Miguelito92 (2014/Mar)
     * mod GoToLoop
     *
     * forum.processing.org/two/discussion/3980/check-asteroid-death-help
     *
     * studio.processingtogether.com/sp/pad/export/ro.9K-kjhuONcJDZ/latest
     */
    
    static final int ASTEROIDS = 30, BULLETS = 100, NUM = 10, FPS = 60;
    static final color BG = 0;
    
    ArrayList<Asteroid> asteroids = new ArrayList(ASTEROIDS);
    ArrayList<Bullet> bullets = new ArrayList(BULLETS);
    
    int score;
    
    void setup() {
      size(900, 750, JAVA2D);
      frameRate(FPS);
      smooth(4);
    
      ellipseMode(CENTER);
      strokeWeight(Bullet.DIAM);
      fill(Asteroid.COLOUR);
    
      instantiateSprites();
    }
    
    void draw() {
      background(BG);
    
      runScripts();
      checkDeath();
      checkVictory();
    }
    
    void mousePressed() {
      loop();
    }
    
    void keyPressed() {
      loop();
    }
    
    void restart() {
      instantiateSprites();
      score = 0;
    }
    
    void instantiateSprites() {
      asteroids.clear();
      bullets.clear();
    
      for ( int i = 0; i++ != ASTEROIDS; asteroids.add(new Asteroid()) );
    
      for ( int i = 0; i++ != BULLETS; bullets.add(new Bullet(
      random(width), random(height), random(TWO_PI))) );
    }
    
    void runScripts() {
      noStroke();
      for (int c = asteroids.size(); c-- != 0;)
        if (asteroids.get(c).script()) {
          asteroids.remove(c);
          println("An asteroid has flew too far away!");
        }
    
      stroke(Bullet.COLOUR);
      for (int b = bullets.size(); b-- != 0;)
        if (bullets.get(b).script()) {
          bullets.remove(b);
          println("A bullet has strayed too far from view!");
        }
    }
    
    void checkDeath() {
      for (int c = asteroids.size(); c-- != 0;) {
        final Asteroid a = asteroids.get(c);
    
        for (int b = bullets.size(); b-- != 0;)
          if (bullets.get(b).checkHit(a)) {
            bullets.remove(b);
            asteroids.remove(c);
    
            score += NUM;
            println("An asteroid has been pulverized!");
    
            return;
          }
      }
    }
    
    void checkVictory() {
      final int a = asteroids.size(), b = bullets.size();
    
      if (!online)  frame.setTitle("Score: #" + score
        + "\t\tFPS: #" + round(frameRate));
    
      if (b == 0) {
        println("\nAsteroids left: #" + a + "\tFinal score: #" + score);
        noLoop();
        restart();
      }
    }
    
    class Asteroid {
      static final color COLOUR = #0000FF;
    
      static final int DIAM = 25, RAD = DIAM>>1, RAD_SQ = RAD*RAD;
      static final float SPD = 1.5;
    
      float x = random(DIAM, width  - DIAM);
      float y = random(DIAM, height - DIAM);
    
      final float vx = cos(random(-1., 1.))*SPD;
      final float vy = sin(random(-1., 1.))*SPD;
    
      boolean script() {
        update();
        display();
        return isOuttaBounds();
      }
    
      void update() {
        x += vx;
        y += vy;
      }
    
      void display() {
        ellipse(x, y, DIAM, DIAM);
      }
    
      boolean isOuttaBounds() {
        return x < -RAD | x > width+RAD | y < -RAD | y > height+RAD;
      }
    }
    
    class Bullet {
      static final color COLOUR = #C86400;
    
      static final int DIAM = 4, RAD = DIAM>>1, RAD_SQ = RAD*RAD;
      static final float SPD = 4.0;
    
      float x, y;
      final float vx, vy;
    
      Bullet(float xx, float yy, float dir) {
        x = xx;
        y = yy;
    
        vx = cos(dir)*SPD;
        vy = sin(dir)*SPD;
      }
    
      boolean script() {
        update();
        display();
        return isOuttaBounds();
      }
    
      void update() {
        x += vx;
        y += vy;
      }
    
      void display() {
        point(x, y);
      }
    
      boolean isOuttaBounds() {
        return x < -RAD | x > width+RAD | y < -RAD | y > height+RAD;
      }
    
      boolean checkHit(Asteroid a) {
        return sq(a.x - x) + sq(a.y - y) < Asteroid.RAD_SQ;
      }
    }
    
  • edited March 2014 Answer ✓

    What does this part of the code mean?
    if ((sq(aX - bX) + sq(aY-bY)) < 160) {

    Where did you get that algorithm from? Wasn't that in your PasteBin.com link btW? [..]
    That's pretty much the same as my checkHit() method:
    return sq(a.x - x) + sq(a.y - y) < a.RAD * a.RAD;

    It gets whether 2 objects are intersecting within a circular area! *-:)

  • edited June 2015

    As a bonus, a CoffeeScript version: \m/

    ###
    * Asteroids Vs. Bullets Sim (v1.11)
    * by  GoToLoop (2014/Mar)
    *
    * forum.processing.org/two/discussion/3980/check-asteroid-death-help
    * studio.processingtogether.com/sp/pad/export/ro.9K-kjhuONcJDZ/latest
    ###
    
    ASTEROIDS = 30; BULLETS = 100; NUM = 10; FPS = 60; BG = 0
    asteroids = []; bullets = []
    score = 0
    
    setup: ->
    
        size 900, 750, JAVA2D; frameRate FPS; smooth 4
        ellipseMode CENTER
        strokeWeight Bullet::DIAM; fill Asteroid::COLOUR
    
        do instantiateSprites
    
    
    draw: ->
    
        background BG
        do runScripts; do checkDeath; do checkVictory
    
    
    mousePressed: -> do doLoop
    keyPressed:   -> do doLoop
    
    restart = -> do instantiateSprites; score = 0
    
    instantiateSprites = ->
    
        asteroids.length = bullets.length = 0
        
        asteroids.push new Asteroid  for i in [0...ASTEROIDS] by 1
      
        bullets.push new Bullet(random(width), random(height),
        random(TWO_PI))  for i in [0...BULLETS] by 1
    
        return
    
    
    runScripts = ->
    
        do noStroke
    
        c = asteroids.length; while c-- isnt 0
            if do asteroids[c].script
    
                asteroids.splice c, 1
                println "An asteroid has flew too far away!"
    
        stroke Bullet::COLOUR
    
        b = bullets.length; while b-- isnt 0
            if do bullets[b].script
    
                bullets.splice b, 1
                println "A bullet has strayed too far from view!"
    
        return
    
    
    checkDeath = ->
    
        c = asteroids.length; while c-- isnt 0
            a = asteroids[c]
    
            b = bullets.length; while b-- isnt 0
                if bullets[b].checkHit a
    
                    bullets.splice b, 1; asteroids.splice c, 1
    
                    score += NUM
                    println "An asteroid has been pulverized!"
    
                    return
    
    
    checkVictory = ->
    
        a = asteroids.length; b = bullets.length
    
        frame.setTitle "Score: ##{score}\t\tFPS: " +
            "##{round frameRate}"  unless online
    
        if b is 0
        
            println "\nAsteroids left: ##{a}\tFinal score: ##{score}"
            do noLoop
            do restart
    
    
    class Asteroid
    
        COLOUR: 0xff0000FF; SPD: 1.5; DIAM: 25
        RAD: Asteroid::DIAM>>1; RAD_SQ: Asteroid::RAD * Asteroid::RAD
    
        constructor: ->
    
            @x = random @DIAM, width  - @DIAM
            @y = random @DIAM, height - @DIAM
    
            @vx = @SPD * cos random -1, 1
            @vy = @SPD * sin random -1, 1
    
    
        script:  -> do @update; do @display; do @isOuttaBounds
        update:  -> @x += @vx; @y += @vy
        display: -> ellipse @x, @y, @DIAM, @DIAM
    
        isOuttaBounds: ->
            @x < -@RAD | @x > width+@RAD | @y < -@RAD | @y > height+@RAD
    
    
    class Bullet
    
        COLOUR: 0xffC86400; SPD: 4; DIAM: 4
        RAD: Bullet::DIAM>>1; RAD_SQ: Bullet::RAD * Bullet::RAD
    
        constructor: (@x, @y, dir) ->
            @vx = @SPD * cos dir; @vy = @SPD * sin dir
    
        script:  -> do @update; do @display; do @isOuttaBounds
        update:  -> @x += @vx; @y += @vy
        display: -> point @x, @y
    
        isOuttaBounds: ->
            @x < -@RAD | @x > width+@RAD | @y < -@RAD | @y > height+@RAD
    
        checkHit: (a) ->
            sq(a.x - @x) + sq(a.y - @y) < Asteroid::RAD_SQ
    
Sign In or Register to comment.