Help with Intersecting Functions of Game Elements

Hi All,

I am playing around with building an incredibly simple two-person competitive game. Here’s the concept:

One player uses the mouse, EI the ‘spaceship’ that shoots a ‘light beam’ when the mouse is clicked. The other player is an ‘alien’ which toggles back and forth using the Left and Right arrow keys. The point is to hit the alien three times before the timer is up, a classic “beat the clock” game.

How would I go about the interaction triggering the 'life" to decrement when the light beam and the alien intersect? The light beam is a simple rect() drawn in Processing, and the alien is a jpg file.

Can anyone help me troubleshoot coding this intersection function using a boolean statement? Again, my goal is to code beating the clock, if life > 0 before the timer reaches 0 the alien wins and the game ends, if life = 0 before the timer reaches 0, the spaceship wins and the game ends.

Thanks for your feedback!


// images
PImage planet; //https://forums.frontier.co.uk/threads/frontier-is-really-capable-to-truly-change-the-face-of-the-planets-in-2018.384087/page-6
PImage alien; 
PImage spaceship; 

//Global Variable Objects
int x = 10; //alien x
int beamWidth = 15;
int beamHeight = 250;

//timer variables
int begin; 
int duration = 60;
int time = 60;

//life variables
int life = 3;

//true false statement      ** NEED HELP SETTING UP CORRECT BOOLEAN PARAMETERS
boolean touching = false;
 
void setup() {
 size (600, 400);
 planet = loadImage("planet.jpg");
 alien = loadImage("alien.jpg");
 spaceship = loadImage("spaceship.JPG");
}

void draw() {
  background(0);
  imageMode(CENTER);
  image(planet, 90,0, 1200, 800);
  image(alien, x, height/1.3, 50, 80);
  image(spaceship, mouseX, 50, 70, 40);
  
  //timer counts down 60 seconds
  if (time > 0){  
    time = duration - (millis() - begin)/1000;
    text(time, 550, height - 25);
    fill(255);
    textSize(30);
  }
  
  // life decrements if beam and alien intersect
    if (life > 0) {
   text(life, 50, height - 25);
   fill(255);
   textSize(30);
   }
//    if (touching) {   **** NEED TO CREATE CORRECT BOOLEAN FOR INTERACTION
//      life = life - 1;
//    }
}

//Keyboard interaction - move alien left and right with arrow keys
void keyPressed() {
  if (key == CODED) {
    if (keyCode == RIGHT) {
      x = x + 20;
    } else if (keyCode == LEFT) {
      x = x - 20;
    } 
    // Check horizontal edges - alien runs off right side of window and shows up on left
    if (x > width || x < 0) {
      x = 0;
    }
  }
}

//Mouse interaction - light beam displays when mouse is pressed
void mousePressed () {
  if (mousePressed) {
  //draw beam rectangle
  rect(mouseX, 60, beamWidth, beamHeight);   
  }
}

planet spaceship

1 Like

Just use the dist() command to detect a collision

see reference

Hi, I had previously tried the dist() command like this:

// life decrements if beam and alien intersect
    if (life > 0) {
   text(life, 50, height - 25);
   fill(255);
   textSize(30);
   // if the distance of the X Y points of the alien and the X Y points of the beam is less or equal to the beam-width, life decremenents 
    if (dist(x, height/1.3, mouseX, 60) <= beamWidth) { 
      life = life - 1;
    }
    // if time equals 0 and life is greater than 0, the alien wins
   if (time == 0 && life > 0) {  
      text("Alien Wins", width/2, 200);
      }
   // if life equals 0 and the time is greater than 0, spaceship wins
      if (time > 0 && life == 0) {
        text("Spaceship Wins", width/2, 200);
      } 
    }

Unfortunately it didn’t work, so I reached out here to troubleshoot. What am I missing?
Thanks for your help!

1 Like

did you test this?

I mean I don’t have your entire code but…

Questions:

That’s the alien position?
x, height/1.3

That’s the beam position?
mouseX, 60
I doubt it.

And is height/1.3 == 60 ?

Because you have 2 constants in your formula:

if (dist(x, height/1.3, mouseX, 60) <= beamWidth) {

Try

also did you try

if (dist(x, height/1.3, mouseX, 60) <= 70) {

for a test?

1 Like
// images
PImage planet; //https://forums.frontier.co.uk/threads/frontier-is-really-capable-to-truly-change-the-face-of-the-planets-in-2018.384087/page-6
PImage alien;  //http://clipart-library.com/alien-peace-sign.html
PImage spaceship; //https://github.com/shiffman/LearningProcessing/blob/master/chp07_functions/exercise_07_10_spaceship/exercise_07_10_spaceship.pde

//Global Variable Objects
int x = 10; //alien x 
int beamWidth = 15;
int beamHeight = 250;

//timer variables
int begin; 
int duration = 60;
int time = 60;

//life variables
int life = 3;
 
 
void setup() {
 size (600, 400);
 planet = loadImage("planet.jpg");
 alien = loadImage("alien.jpg");
 spaceship = loadImage("spaceship.JPG");
}

void draw() {
  background(0);
  imageMode(CENTER);
  image(planet, 90,0, 1200, 800);
  image(alien, x, height/1.3, 50, 80);
  image(spaceship, mouseX, 50, 70, 40);
  
  //timer counts down 60 seconds
  if (time > 0){  
    time = duration - (millis() - begin)/1000;
    text(time, 550, height - 25);
    fill(255);
    textSize(30);
  }
  
  // life text is displayed
    if (life > 0) {
   text(life, 50, height - 25);
   fill(255);
   textSize(30);
   // if the distance of the X Y points of the alien and the X Y points of the beam is less or equal to the beam-width, life decrements 
    if (dist(x, height/1.3, mouseX, 60) <= beamWidth) { 
      life = life - 1;
    }
    // if time equals 0 and life is greater than 0, the alien wins
   if (time == 0 && life > 0) {  
      text("Alien Wins", width/2, 200);
      }
   // if life equals 0 and the time is greater than 0, spaceship wins
      if (time > 0 && life == 0) {
        text("Spaceship Wins", width/2, 200);
      } 
    }
}

//Keyboard interaction - move alien left and right with arrow keys
void keyPressed() {
  if (key == CODED) {
    if (keyCode == RIGHT) {
      x = x + 20;
    } else if (keyCode == LEFT) {
      x = x - 20;
    } 
    // Check horizontal edges - alien runs off right side of window and shows up on left
    if (x > width || x < 0) {
      x = 0;
    }
  }
}

//Mouse interaction - beam displays when mouse is pressed
void mousePressed () {
  if (mousePressed) {
  //draw beam rectangle
  rect(mouseX, 60, beamWidth, beamHeight);   
  }
}

Here is my entire code again (my first post included it along with the image files) so anyone could see what I am seeing.

Please see my code as to why you doubt my position of the alien. If there is a better way to position it, suggestions are most welcome. Since the alien is a JPG image, the beam in drawn and only appears when the mouse is pressed, I need help coding the intersection correctly.

1 Like

The distance will never be smaller than the width of your beam, since the beam‘s y position alone is always at least 10*the beam width away. How about you use only the x position, so your beam will Hit the alien if it‘s right beneath it.

And add a bool to check if the beam is Shot or not, Else the Alien will die if the spaceship passes it, even without a beam being Shot.

// images
PImage planet; //https://forums.frontier.co.uk/threads/frontier-is-really-capable-to-truly-change-the-face-of-the-planets-in-2018.384087/page-6
PImage alien;  //http://clipart-library.com/alien-peace-sign.html
PImage spaceship; //https://github.com/shiffman/LearningProcessing/blob/master/chp07_functions/exercise_07_10_spaceship/exercise_07_10_spaceship.pde

//Global Variable Objects
int x = 10; //alien x 
int beamWidth = 15;
int beamHeight = 250;
bool beam = false;

//timer variables
int begin; 
int duration = 60;
int time = 60;

//life variables
int life = 3;
 
 
void setup() {
 size (600, 400);
 planet = loadImage("planet.jpg");
 alien = loadImage("alien.jpg");
 spaceship = loadImage("spaceship.JPG");
}

void draw() {
  background(0);
  imageMode(CENTER);
  image(planet, 90,0, 1200, 800);
  image(alien, x, height/1.3, 50, 80);
  image(spaceship, mouseX, 50, 70, 40);
  
  //timer counts down 60 seconds
  if (time > 0){  
    time = duration - (millis() - begin)/1000;
    text(time, 550, height - 25);
    fill(255);
    textSize(30);
  }
  
  // life text is displayed
    if (life > 0) {
   text(life, 50, height - 25);
   fill(255);
   textSize(30);
   // if the distance of the X Y points of the alien and the X Y points of the beam is less or equal to the beam-width, life decrements 
    if (abs(x - mouseX) <= beamWidth && beam) { 
      life = life - 1;
    }
    // if time equals 0 and life is greater than 0, the alien wins
   if (time == 0 && life > 0) {  
      text("Alien Wins", width/2, 200);
      }
   // if life equals 0 and the time is greater than 0, spaceship wins
      if (time > 0 && life == 0) {
        text("Spaceship Wins", width/2, 200);
      } 
    }
}

//Keyboard interaction - move alien left and right with arrow keys
void keyPressed() {
  if (key == CODED) {
    if (keyCode == RIGHT) {
      x = x + 20;
    } else if (keyCode == LEFT) {
      x = x - 20;
    } 
    // Check horizontal edges - alien runs off right side of window and shows up on left
    if (x > width || x < 0) {
      x = 0;
    }
  }
}

//Mouse interaction - beam displays when mouse is pressed
void mousePressed () {
  if (mousePressed) {
  //draw beam rectangle
  rect(mouseX, 60, beamWidth, beamHeight);
  beam = true;   
  } else {
    beam = false;
  }
}
3 Likes