Interception function not working

I can’t figure out why my intercept function is not working… It isn’t being ignored completely, just going off in the wrong spot and consistently, not just when it intersects. No code please.

Basket gold;
Basket pink;
Stars test;
float y;
float x=random(0,width);
float speedy;
void setup(){
  fullScreen();
  gold=new Basket(10,100,mouseX,mouseY, 255,215,0);
  pink=new Basket(10,100,width-100,height-20,255,192,203);
  test=new Stars(y,speedy,random(20,40),x);
}
void draw(){
  background(255);
  test.intercept();
  test.display();
  test.move();
  gold.moveGold();
  gold.display();
  pink.movePink();
  pink.display();
  
}
class Basket{
  float h;
  float w;
  float x;
  float y;
  float r;
  float g;
  float b;
  float score=0;
  Basket(float h1, float w1, float x1, float y1, float r1, float g1,
  float b1){
    h=h1;
    w=w1;
    x=x1;
    y=y1;
    r=r1;
    g=g1;
    b=b1;
  }
  void moveGold(){
    x=mouseX;
    y=constrain(mouseY,height-20,height-10);
  }
  void movePink(){
    if (keyPressed){
      if (key==CODED){
        if (keyCode==RIGHT){
          x=x+5;
        }else if (keyCode==LEFT){
          x=x-5;
        }
      }
    }
  }
  void display(){
    fill(r,g,b);
    rect(x,y,w,h);
  }
}
class Stars {
  float y=0;
  float speedy;
  float r = 30;
  float p;
  Stars(float tempy, float tempSpeed, float tempr, float tempp) {
    y=tempy;
    speedy=tempSpeed;
    r=tempr;
    p=tempp;
  }
  void move() {
    if (y<=height) {
      speedy=random(1, 10);
      y=speedy+y;
    } else {
      clear();
      background(255);
    }
  }
  void display() {
    ellipse(p, y, r, r);
  }
  void intercept() {
    if (y==gold.y && gold.x+gold.w>x && x>gold.x) {
      clear();
      background(255);
      gold.score++;
      print(gold.score);
    } else if (y==pink.y && pink.x+pink.w>x && x>pink.x) {
      clear();
      background(255);
      pink.score++;
      print(pink.score);
    } else {
      background(255);
    }
  }
}

Hi Fey-Lyn,

i guess you’re not too far, a first hint, you can check your condition:

for eg,here is where y is updated:

  speedy=random(1, 10);
      y=speedy+y;

so, at every draw() loop, y increase to some amount, not necessary 1

then on your interception fonction you test y value as:
if (y==gold.y &&....
it means y to be strictly equal to a precise value, do you see the problem here?
i thinks others condition have to be checked too like gold.x+gold.w>x && x>gold.x
pleasure is on solving problems, so i don t give you much but i’m sure you can figure this out (tell me otherwise)
last tip, as you used print() to print the score, you can use print to print your variables
println(y,gold.y ,gold.x+gold.w,x);
and check with those values when do the interception test will trigger

3 Likes

Thank you. That helped somewhat.

1 Like

The variables y and gold.y are both declared as floats. It is not recommended to test for equality between float because of rounding errors. For instance the CPU would consider the numbers 78.141234 and 78.141235 different but if these were screen positions then you would probably say they represent the same position.

3 Likes