Collision with Abstract Entity

edited December 2017 in Questions about Code

I made an L shape, was wondering if I could some how make a collision with it. I used vertexes to make the shape for example.

beginShape();
vertex(20, 20);
vertex(100, 20);
vertex(100, 35);
vertex(35, 35);
vertex(35, 100);
vertex(20, 100);
vertex(20, 20);
endShape();

This is the method I used up above to make the L shape. Was wondering if I could make a simple collision with it, like this easy game for example.

float bullX = 20;
float bullY = 165;
float targX = 500;
float targY = 150;
float spd = 7;
color targColour;
int countX = 0;
int countY = 0;
int dirCount = 0;
int dir = 1;
void setup () {
  size (800, 400);
  targColour = color (random (100, 255), random (100, 255), random (100, 255));
}
void draw () {
  background (0);
  countX++;
  countY++;
  dirCount++;
  fill (#FF0303);
  rect (bullX, bullY, 20, 5);
  if (countY == 10) {
    countY = 0;
    targY = targY + random (5, 8) * dir;
  }
  if (dirCount > 130) {
    dir = dir * -1;
    dirCount = 0;
  }
  if (targY < 0) targY = 0;
  if (targY > height - 13) targY = height - 13;
  bullX = bullX + spd;

  if (bullX + 20  >= targX && bullX + 20 <= targX + 50 && bullY <= targY + 40 && bullY + 5 >= targY) {
    spd = 0;
    targColour = color (random (100, 255), random (100, 255), random (100, 255));
  }
  fill ( targColour);
  rect (targX, targY, 50, 40);
  if (spd == 0 || bullX > width) {
    bullX = 0;
    spd = 7;
  }
}

void keyPressed() {
  if (key == CODED) { 
    if (keyCode == UP) {
      if (bullY > 0) {
        bullY = bullY - 5;
      }
    }
    if (keyCode == DOWN) {
      if (bullY < height - 5) {
        bullY = bullY + 5;
      }
    }
  }
}

Thank you in advance for the help!

Answers

Sign In or Register to comment.