Collision between object moved by keys and random object

edited August 2018 in Questions about Code

Hello, I'm having problems with this collision thingy between the 2 rectangles, I know I'm supposed to use dist(), It worked when my object was controlled by mouse, but I can't make it work now with keys, any help would be appreciated! :)

Ship myShip;
int a=1;
int b=100;
int c=200;
int d=300;
int e=400;
int f=500;
int z=10;
int j=160;
boolean [] keys = new boolean[128];


void setup() {
  size(500, 600); 
  smooth();
  myShip = new Ship();
}
void gameOver() {
    textAlign(CENTER);
    text("GAME OVER", width / 2, height / 2);

    }

void draw() {
  background(0);
  a=a+3;
  b=b+3;
  c=c+3;
  d=d+3;
  e=e+3;
  f=f+3;
  z=z+4;
  rect(j,z+3,25,25);
  rect(270,a+3,5,30);
  rect(270,b,5,30);
  rect(270,c,5,30);
  rect(270,d,5,30);
  rect(270,e,5,30);
  rect(270,f,5,30);
  rect(80,0,5,1500);
  if (a>=600)
  a=-10;
  if (b>=600)
  b=-10;
  if (c>=600)
  c=-10;
  if (d>=600)
  d=-10;
  if (e>=600)
  e=-10;
  if (f>=600)
  f=-10;
  myShip.run();
}

void keyPressed() {
  keys[key] = true;
}

void keyReleased() {
  keys[key] = false;
}


class Ship {  
  PVector pos, vel;
  float rotation;


  Ship() {
    pos = new PVector(160, height/2);
    vel = new PVector(5, 5);
    rotation = 0;
  }

  void run() {    //generic function container
    display();
    move();
  }


  void display() {
    pos.x=constrain(pos.x, 25, width);
    pos.y=constrain(pos.y, 25, height);
    pushMatrix();
    translate(pos.x, pos.y);
    rect(0, 0, 50, 50);
    popMatrix();
   if (pos.x>=260) 
  noLoop();
  if (pos.x<=60) 
  noLoop();
  if (pos.x>=260)
  gameOver();
    if (pos.x<=60)
  gameOver();
  if (dist(pos.x,pos.y,50,a+1)<25)
  noLoop();


  }


  void move() {
    if (keys['a']) //move left 
      pos.x -= vel.x;
    if (keys['d']) //move right
      pos.x += vel.x;
    if (keys['w']) //move up
      pos.y -= vel.y;
    if (keys['s']) //move down
      pos.y += vel.y;

  }

}

Answers

  • Answer ✓
    • there is a new forum - see processing main page, link to forum

    • use ctrl-t in processing to get auto-format (indents)

    • write no functions between setup() and draw() functions. Instead, write those below draw().

    • your question:

    are we talking about this line:

    if (dist(pos.x,pos.y,50,a+1)<25) ?

    If so, a is just one of the 5 middle lines.

    instead use the obstacle position j, z

    entire sketch:

    Ship myShip;
    
    int a=1;
    int b=100;
    int c=200;
    int d=300;
    int e=400;
    int f=500;
    int z=10;
    int j=160;
    
    boolean [] keys = new boolean[128];
    
    
    void setup() {
      size(500, 600); 
      smooth();
      myShip = new Ship();
    }
    
    void draw() {
      background(0);
    
      a=a+3;
      b=b+3;
      c=c+3;
      d=d+3;
      e=e+3;
      f=f+3;
      z=z+4;
    
      // obstacle green 
      fill(0, 255, 0); 
      rect(j, z+3, 25, 25);
    
      // middle lines !!!
      fill(255); 
      rect(270, a+3, 5, 30);
      rect(270, b, 5, 30);
      rect(270, c, 5, 30);
      rect(270, d, 5, 30);
      rect(270, e, 5, 30);
      rect(270, f, 5, 30);
      rect(80, 0, 5, 1500);
    
      if (a>=600)
        a=-10;
      if (b>=600)
        b=-10;
      if (c>=600)
        c=-10;
      if (d>=600)
        d=-10;
      if (e>=600)
        e=-10;
      if (f>=600)
        f=-10;
    
      myShip.run();
    }
    
    void gameOver() {
      textAlign(CENTER);
      text("GAME OVER", width / 2, height / 2);
    }
    
    void keyPressed() {
      keys[key] = true;
    }
    
    void keyReleased() {
      keys[key] = false;
    }
    
    // ============================================
    
    class Ship {  
    
      PVector pos, vel;
      float rotation;
    
      Ship() { // constr 
        pos = new PVector(160, height/2);
        vel = new PVector(5, 5);
        rotation = 0;
      }
    
      void run() {    // generic function container
        display();
        move();
      }
    
      void display() {
        fill(255, 0, 0); 
        pos.x=constrain(pos.x, 25, width);
        pos.y=constrain(pos.y, 25, height);
        pushMatrix();
        translate(pos.x, pos.y);
        rect(0, 0, 50, 50);
        popMatrix();
        if (pos.x>=260) 
          noLoop();
        if (pos.x<=60) 
          noLoop();
        if (pos.x>=260)
          gameOver();
        if (pos.x<=60)
          gameOver();
    
        // obstacle 
        if (dist(pos.x, pos.y, j, z+2)<25)
          noLoop();
      }
    
      void move() {
        if (keys['a']) //move left 
          pos.x -= vel.x;
        if (keys['d']) //move right
          pos.x += vel.x;
        if (keys['w']) //move up
          pos.y -= vel.y;
        if (keys['s']) //move down
          pos.y += vel.y;
      }
      //
    }//class
    //
    

    Regards, Chrisir ;-)

  • Yes, exactly that line, thanks a lot Chrisir!

  • edited August 2018

    Instead of noLoop() you could have a lives counter and say lives=lives-1; in case of a collision

    Starting with int lives=3; we would see game over after 3 collisions

  • edited August 2018

    You could need more luck when it comes to naming variables.

    better use streetLine1, streetLine2... instead of a,b,... or use an array. You might need a bit longer to type but you can read your code faster and find errors later on (like the collision problem you just had which was in fact a naming issue).

    Also say obstacleX and obstacleY instead of j,z

Sign In or Register to comment.