Missile Collision Detection

Hi everyone, I’m unsure on how to code collision detection in processing. I have watched tutorials on YouTube however they mainly show collision detection of changing colour rather than making the object disappear once it hits. Also, I would like to know how to do it without using classes. This is my code so far.

int x = 30;
int y = 60;
float gunX = x+20;
float gunY = y+20;
float circleY;
float circleX;
float circleRadius;
float circleDiameter;
boolean bulletFlies=false;
float bulletX;
float bulletY;

void setup() {
  size(500, 500);
  circleY = random(25, 480);
  circleX = 450;
  circleRadius = 80;
  circleDiameter = circleRadius * 2;
}

void draw() {
  background(0);
  fill(255, 56, 99);
  ellipse(x, y, 60, 60);
  ellipse(bulletX, bulletY, 6, 6);
  gunY = y;

  fill(255);
  circle(circleX, circleY, circleDiameter);
  circleX=circleX-0.5;
  
  bullet();
}

void keyPressed() {
  if (key == 's') y = y + 8;
  else if (key == 'w') y = y - 8;
  else if (key == ' ') {
    if (!bulletFlies) {
      bulletFlies=true;
      bulletX = gunX+14;
      bulletY = gunY+4;
    }
  }
}

void bullet() {
  if (bulletFlies) {
    //check the bullet hasn't left the screen
    if (bulletX > width) {
      bulletFlies = false;
      return;
    }
    bulletX = bulletX+5;
    //calculate the distance between the bullet and the circle
    float dx = bulletX - circleX;
    float dy = bulletY - circleY;
    float dist = sqrt(dx * dx + dy * dy);
    if (dist < circleRadius) {
      bulletFlies = false;
    }
  }
}int x = 30;
int y = 60;
float gunX = x+30;
float gunY = y+30;
float circleY;
float circleX;
boolean bulletFlies=false;

float bulletX = 30;
float bulletY = 30;

//--------------------------------------------------------------------

void setup() {
  size(500, 500);
  circleY = random(20,460);
  circleX = 450;
}//func 

void draw() {
background(0);
 
  // player / cannon 
  fill(255, 56, 99);
  ellipse(x, y, 60, 60);
  
  fill(255);
circle(circleX,circleY, 80);
circleX=circleX-0.5;

  // GUN 
  fill(255, 56, 99); // RED 
  noStroke();
  gunX = x+12;
  gunY = y;
  rect(gunX, gunY, 14, 16);

  // bullet
  bullet();
 
}//func 

//--------------------------------------------------------------------

void keyPressed() {
  if (key == 's') y = y + 8;
  else if (key == 'w') y = y - 8;
  else if (key == ' ') {
    if (bulletFlies)
      return; 
    bulletFlies=true;
    bulletX = gunX+14;
    bulletY = gunY+4;
  }//else if
}//func 

void bullet() {  
  if (bulletFlies) {
    fill(255); // WHITE 
    ellipse(bulletX, bulletY, 6, 6);
    bulletX = bulletX+4; // speed
    // bullet stops
    if (bulletX>width+12) { 
      bulletFlies=false;
    }
  }
  }

Not even sure what to say…

the forum has a pretty decent collision detection section. Please learn to make use of the search function it is very helpful and as you can imagine a lot of others like you have asked similar questions.

Also homework code is a touchy one as we understand you are new and may have trouble understanding some concept or idea, or how to implement some code, but we cannot just give you the answer, this would defeat the point of the homework excercise, but would also make you even more reliant on others to finish your projects which ultimately will only hurt you in the long run.

Again this is a very basic topic and has been discussed many times, please use the forum search and also google search, you’d be surprised how many sites cover this topic. How far you get will depend on your willingness to learn code and read resources you find online, then once you can identify something which you dont understand, and provide a valid reason, providing that it is within our breadth of expertise I’m sure someone will be willing to help.

also you have a lot of duplicate functions here. Which ones are we supposed to be helping with?

furthermore processing already has a built in distance function

The function "dist()" expects parameters like: "dist(float, float, float, float)"

finally methods can return bools

ie

boolean collision(){
   return dist(x1,y1,x2,y2)<10;
}

this will auto return true if the statement is true and false if the statement is false;

remember you can pass parameters to your function os the previous function could further be extended to

boolean collision(float a,float b,float c,float d){
   return dist(a,b,c,d)<10;
}

or even

boolean collision(PVector a,PVector b){
   return dist(a.x,a.y,b.x,b.y)<10;
}

this can be used in code ie

if(collision(p1,p2))doSomething();
//where doSomething() is the name of the function you want to call

or

if(collision(p1,p2)){
   someCode
}

this should get you in the right direction

Hi @NewCoder1 – this is an almost identical copy of a post several days ago from @MarkShaker. Both posts say “this is my code.”

I’m trying to figure out:

  1. if you stole this post
  2. if you are a bot / fake account
  3. if you and @MarkShaker are the same person, with two accounts.
  4. …?

Please respond.

1 Like