help with this simple collision

I want to make one of the games where your constantly falling, not necessarily in processing, I was just messing around with the basic mechanics, theres a coin class and a player class. How do I make it so when the player hits the coin something happens, like right now I just want the coin to be replaced by a different color with destroy(). thanks for any help! :)

Heres the code

Player player; 
Coin coin;  

void setup() { 
  size(500,500); 
  background(0); 
  player = new Player(width/2,0); 
  coin = new Coin(); 
}

void draw() { 
  background(0);
  player.display();
  player.update(); 

  if (keyPressed) { 
    if (key == 'a' || key == 'A') {
     player.location.x -= 1; 
    }
    else if (key == 'd' || key == 'D') { 
      player.location.x += 1; 
    }

  }

  coin.update(); 
  coin.display(); 
 // coin.destroy(); // 

} 


class Player {
  float gravity; 
  float speed; 
  PVector location; 

  Player(float x, float y) { 
    location = new PVector(); 
    location.x = x; 
    location.y = y; 
    gravity = 0.28; 
    speed = 1; 
  } 

  void update() { 
    location.y = location.y + gravity;     
    if (location.y > height) { 
      location.y = 0; 
    }

  } 
  void display(){
    fill(#F02727);
    rect(location.x, location.y, 15, 15); 

  }   

} 

class Coin { 
  float cx;
  float cy; 
  boolean got;

  Coin() { 
    got = false; 
    cx = 100;
    cy = 200; 
  } 
  void update(){
  if(player.location.x == cx && player.location.y == cy) {
    got = true; 
  }
  }
  void display(){
    fill(#F5E3E3);
    ellipse(cx,cy,5,5);
  }

  void destroy() { 

    if(got = true){
    fill(#F21111); 
    ellipse(cx,cy,5,5); 
    }
  }


}
Tagged:

Answers

  • Answer ✓

    This should get you started

    Player player;
    Coin coin; 
    
    void setup() {
      size(500, 500);
      background(0);
      player = new Player(width/2, 0);
      coin = new Coin();
    }
    
    void draw() {
      background(0);
      player.display();
      player.update();
    
      if (keyPressed) {
        if (key == 'a' || key == 'A') {
          player.location.x -= 1;
        }
        else if (key == 'd' || key == 'D') {
          player.location.x += 1;
        }
      }
    
      coin.update();
      coin.display();
      coin.destroy(); //
    }
    
    
    class Player {
      float gravity;
      float speed;
      PVector location;
    
      Player(float x, float y) {
        location = new PVector();
        location.x = x;
        location.y = y;
        gravity = 0.28;
        speed = 1;
      }
    
      void update() {
        location.y = location.y + gravity;    
        if (location.y > height) {
          location.y = 0;
        }
      }
    
      void display() {
        fill(#F02727);
        rect(location.x, location.y, 15, 15);
      }
    }
    
    class Coin {
      float cx;
      float cy;
      boolean got;
    
      Coin() {
        got = false;
        cx = 100;
        cy = 200;
      }
      void update() {
        if (cx > player.location.x && cx < player.location.x + 15 && cy > player.location.y && cy < player.location.y + 15) {
          got = true;
        }
      }
      void display() {
        fill(#F5E3E3);
        ellipse(cx, cy, 5, 5);
      }
    
      void destroy() {
    
        if (got == true) {
          println("HIT");
          fill(#F21111);
          ellipse(cx, cy, 5, 5);
        }
      }
    }
    
  • edited October 2013 Answer ✓

    Here's my own take on it: ;))

    /** 
     * Catch Coin (v2.05)
     * by  ReedJones (2013/Oct)
     * mod GoToLoop
     * 
     * forum.processing.org/two/discussion/319/
     * help-with-this-simple-collision
     */
    
    Player player;
    Coin coin;
    
    void setup() {
      size(500, 500);
      frameRate(60);
      smooth();
      rectMode(CORNER);
    
      player = new Player(width >> 1, 0);
      coin = new Coin();
    }
    
    void draw() {
      background(0);
    
      player.script();
      coin.script();
    }
    
    class Player {
      final static float GRAV = .5, SPD = 3.5;
      final static short DIM  = 15;
    
      final PVector loc;
    
      Player(float x, float y) {
        loc = new PVector(x, y);
      }
    
      void script() {
        checkKey();
        update();
        display();
      }
    
      void update() {
        if ((loc.y += GRAV) > height) {
          loc.y = -DIM;
          coin.respawn();
        }
      }
    
      void display() {
        fill(#F02727);
        rect(loc.x, loc.y, DIM, DIM);
      }
    
      void checkKey() {
        if (!keyPressed)   return;
    
        if      (key == 'a' | key == 'A')   loc.x -= SPD;
        else if (key == 'd' | key == 'D')   loc.x += SPD;
        else    return;
    
        loc.x = constrain(loc.x, 0, width - DIM);
      }
    }
    
    class Coin {
      final static byte DIM = 8;
    
      short cx, cy;
      boolean got;
    
      Coin() {
        respawn();
      }
    
      void respawn() {    
        cx = (short) random(width  >> 5, width  - DIM*3);
        cy = (short) random(height >> 1, height - DIM*3);
    
        got = false;
      }
    
      void script() {
        update();
        display();
      }
    
      void update() {
        if (hasHit())   got = true;
      }
    
      void display() {
        fill(got? #F21111 : #F5E3E3);
        ellipse(cx, cy, DIM, DIM);
      }
    
      boolean hasHit() {
        final PVector p = player.loc;
    
        return cx > p.x & cx < p.x + Player.DIM & 
          cy > p.y & cy < p.y + Player.DIM;
      }
    }
    
  • Hey thanks guys! good tips

    Hey gotoloop could you explain this to me :S

    boolean hasHit() {
        final PVector p = player.loc;
    
        return cx > p.x & cx < p.x + player.DIM & 
          cy > p.y & cy < p.y + player.DIM;
      }
    }
    

    and thanks its better how you shortened it to player.script(); coin.script();

    also the gravity as static float :)

  • edited October 2013

    Well, let's try. 1st off, I'm considering Coin as 1D shape and Player as 2D shape and p is player.loc.

    • Expression cx > p.x-> checking whether coin's x coordinate > player's left side location (comes after).
    • Expression cx < p.x + Player.DIM-> whether coin's x coordinate < player's right side location (comes before).
    • Same applies for cy against p.y. But for top & bottom sides! 8-X
    • And if everything above is true, it means we got a hit (coin is inside player's area)! >-)

    You see, for 2D shapes, we need to check their whole area. Just a merely coordinate pair won't cut it! [..]

  • cool thanks :) so I was wondering about using return, when you say return above it basically means return true? is it the same as saying

    if( cx > p.x && cx < p.x + player.DIM && cy > p.y && cy < p.y + player.DIM;) { 
           hasHit = true; }
    

    I was looking at the reference for return, don't get how to use it :-?

  • edited October 2013

    That whole expression evaluates as either true or false. Then its result is returned.
    You can see for yourself hasHit() has been declared to return a boolean value anyways! <):)

    Keyword returnforces a method to quit immediately. Either w/ some value or not.
    Here's returnreference -> processing.org/reference/return.html

    P.S.: Since DIM is a static field, we can also access it using its own class name. Like -> Player.DIM; and Coin.DIM;.
    DIM is after dimension word. But it's also the shape's diameter or both its width & height! ;)

Sign In or Register to comment.