How to make Hit Detection detect more than once
              in 
             Programming Questions 
              •  
              5 months ago    
            
 
           
             Hi there, 
            
I have a strange issue with my hit detection. It does detect, but only once on one of the ellipses (asteroids). I would like to make it detect all the collisions on each ellipse hit.
            
Thank you!
            
            
 
            
           I have a strange issue with my hit detection. It does detect, but only once on one of the ellipses (asteroids). I would like to make it detect all the collisions on each ellipse hit.
Thank you!
- PFont font;
 ArrayList asteroidsList = new ArrayList();
 ArrayList laserList = new ArrayList();
 Asteroids rocks;
 FlyingObjects flying;
 Laser myLaser;
 Spaceship ship;
 Asteroids aster;
 //MAGIC NIMBERS
 int WIDE = 80; //Asteroids radius
 int SHIP_HEIGHT = 70;
 int SHIP_WIDTH = 50;
 int PADDING_TOP = 20;
 int NUM = 2; // 8 asteroids
 void setup() {
 size(1024, 768);
 flying = new FlyingObjects();
 ship = new Spaceship(width/2, height/2, 0, 0);
 
 for (int i = 0; i < NUM; i++) {
 Asteroids newAs = new Asteroids();
 asteroidsList.add(newAs);
 }
 }
 void draw() {
 background(0);
 stroke(255);
 fill(0);
 
 for(int i = 0; i < asteroidsList.size(); i++) {
 aster = (Asteroids) asteroidsList.get(i);
 
 aster.render();
 aster.update();
 }
 for (int i = 0; i < laserList.size(); i++) {
 Laser myLaser = (Laser) laserList.get(i);
 myLaser.render();
 myLaser.update();
 myLaser.collision(aster);
 println(myLaser.hit);
 }
 //BOUNDARIES //////////////////////////////////////////////
 if (ship.pos.x < -SHIP_WIDTH) {
 ship.pos.x = width;
 }
 if (ship.pos.x > width + SHIP_WIDTH) {
 ship.pos.x = 0;
 }
 if (ship.pos.y < -SHIP_HEIGHT) {
 ship.pos.y = height;
 }
 if (ship.pos.y > height + SHIP_HEIGHT) {
 ship.pos.y = 0;
 }
 //END BOUNDARIES //////////////////////////////////////////
 flying.update();
 ship.render();
 ship.update();
 }
 void keyPressed() {
 if (key == CODED) {
 if (keyCode == UP) {
 ship.vel.y = -5;
 }
 if (keyCode == DOWN) {
 ship.vel.y = 5;
 }
 if (keyCode == LEFT) {
 ship.vel.x = -5;
 }
 if (keyCode == RIGHT) {
 ship.vel.x = 5;
 }
 }
 if (key == ' ') {
 Laser currentLaser = new Laser(ship.pos.x, ship.pos.y);
 laserList.add(currentLaser);
 
 }
 }
 void keyReleased() {
 if (key == CODED) {
 if (keyCode == UP) {
 ship.vel.y = 0;
 }
 if (keyCode == DOWN) {
 ship.vel.y = 0;
 }
 if (keyCode == LEFT) {
 ship.vel.x = 0;
 }
 if (keyCode == RIGHT) {
 ship.vel.x = 0;
 }
 }
 }
- class Asteroids {
 int x;
 int y;
 int speed;
 int counter;
 float wide;
 
 color col;
 Asteroids() {
 reset();
 }
 void reset() {
 wide = 80;
 x = int(random (0, width - wide/2));
 y = 0; //begins at 0
 speed = int(random(2, 8));
 counter = 0;
 col = color(0);
 }
 void render() {
 pushMatrix();
 translate(x, y);
 fill(col);
 ellipse(0, 0, wide, wide);
 popMatrix();
 }
 void update() {
 counter++;
 y = speed * counter;
 if (y-wide/2 > height) {
 reset();
 }
 }
 }
- class Laser extends FlyingObjects {
 float yVel, wide;
 Boolean hit;
 
 Laser(float x, float y) {
 pos = new PVector(x, y);
 yVel = 0;
 wide = 10;
 
 hit = false;
 }
 void render() {
 fill(255,0,0);
 ellipse(pos.x, pos.y, wide, wide);
 pos.y += yVel;
 }
 void update() {
 yVel = -3;
 }
 
 void collision(Asteroids a) {
 if(dist(pos.x, pos.y, a.x, a.y) < wide/2 + a.wide/2) {
 hit = true;
 a.col = color(255,0,0);
 } else {
 hit = false;
 }
 }
 }
- class FlyingObjects {
 PVector pos;
 PVector vel;
 FlyingObjects() {
 
 }
 void update(){
 
 }
 }
- class Spaceship extends FlyingObjects {
 Spaceship(float x, float y, float dx, float dy) {
 pos = new PVector(x, y);
 vel = new PVector(dx, dy);
 }
 void render() {
 pushMatrix();
 translate(pos.x, pos.y);
 scale(0.6);
 stroke(0, 255, 255);
 fill(75);
 beginShape();
 //right side
 vertex(0, 0);
 vertex(50, 45);
 vertex(40, 70);
 vertex(20, 45);
 vertex(10, 60);
 vertex(0, 40);
 //left side
 vertex(0, 40);
 vertex(-10, 60);
 vertex(-20, 45);
 vertex(-40, 70);
 vertex(-50, 45);
 vertex(0, 0);
 endShape();
 //glass window
 fill(24, 59, 147);
 beginShape();
 vertex(0, 1);
 vertex(15, 30);
 vertex(0, 40);
 vertex(0, 40);
 vertex(-15, 30);
 vertex(0, 1);
 endShape();
 popMatrix();
 }
 
 
 void update(){
 pos.x += vel.x;
 pos.y += vel.y;
 }
 }
 
              
              1  
            
 
            
 
 
          