Check if Objects are overlapping

I have two calsses where the first one makes rectangles and the other one a ball. How can I check if they are overlapping ? If there where two balls I could use dist() and the radius but now that I have a rectangle and a circle I can’t think of something.

http://JeffreyThompson.org/collision-detection/circle-rect.php

2 Likes

If you don’t care or need a precise collision check, you can simply treat the squares as if they were circles too, like I did in this sketch below: :grinning:

http://Studio.ProcessingTogether.com/sp/pad/export/ro.9qPrLGrYGkr2o

2 Likes

I guess this would do for now but I think it is better to make it perfect form the neginning ( it is about a game) Thanks a lot :P. How can I close this topic ?

Hey I actually got a problem after all. I implemted the code in the link in my program but the thing is that the ball that is moving may as well teleport into the object and thus the function is not returning true.

I’ve just knew about that link; but never tested it yet! :upside_down_face:
Better post your attempt so others can help you too. :sunglasses:

1 Like

Post it where ? aven’t I lready created a topic (isn’t thin considered postind xD, thanks btw)

You can post your code right here or somewhere else, like, for example, GitHub’s gist: :cowboy_hat_face:

1 Like

Haha thanks a lot again… it is funny when such a simple matter irl takes so much time to think in programming.

Ok so last thing. It works while teleporting. but there is the problem: I have a line that divides the screen in two halfs. upper and lower one. Objects appear in both sides, on the line and the ball that runs must dodge them. The only thing the user does is click and the ball changes side. So to make things easy I have translated at (0,height/2). So the ball won’t stop to obstacles with a negative y.
Any Ideas ?

I’m confused: Is that still circle/rect check algorithm? :dizzy_face:
In general, it’s very hard to solve code bugs only by describing them. :no_mouth:

Here you are (your help is much appreciated

class Runner{
  PVector pos = new PVector();
  
  Runner(float x,float y){
    pos.x=x;
    pos.y=y;
  }
  
  void show(){
   fill(255,0,0);
   stroke(0,0,255);
   ellipse(pos.x,pos.y,size,size); 
   //b = loadImage("ball.png");
   //image(b,pos.x-size/2,pos.y-size/2,size,size);
  }
  
  void gravity(){
   if (pos.y != size/2+2 && pos.y>0)  pos.y--;
   else if (pos.y != -size/2-2 && pos.y<0)  pos.y++;
   
  }
  
  void invert(){
    pos.y*=-1; 
  }
}


class Obstacles{
  PVector pos = new PVector();
  PVector dim = new PVector();

  
  Obstacles(float x, float h, float w){
    pos.x=x;
    pos.y=0;
    dim.x=w;
    dim.y=h;
  }
  void drawRect(){
    stroke(0);
    fill(0);
    rect(pos.x,pos.y,dim.x,dim.y);
  }

  float sideInit(){
   side = random(0,1);
   if(side>0.5)  return 1;
   return -1;
  }
}





Runner ball;
//PImage b;
Obstacles obj = new Obstacles(0,0,0);
int size=50;
float offx=0;
float side;

void setup(){
 size(640,640);  
 translate(-offx,height/2); 
 //ball = new Runner(size/2+width/6.4,size/2);
 ball = new Runner(size/2+width/6.4,-100);
 side = obj.sideInit();
 obj = new Obstacles(width,side*random(50,90),random(size/2,100));
 background(255);
 strokeWeight(4);
 frameRate(120);
}

void draw(){
 translate(-offx,height/2);
 background(255);   
 obj.drawRect();
 ball.gravity();
 ball.show();
 stroke(0);
 line(0,0,width+offx,0);
 ball.pos.x+=4;
 offx+=4;
 boolean hit = circleRect(ball.pos.x,ball.pos.y,size/2, obj.pos.x,obj.pos.y,obj.dim.x,obj.dim.y);
 if (hit) noLoop();
 if (obj.pos.x<offx) {side=obj.sideInit();obj = new Obstacles(width+offx,side*random(50,90),random(size/2,100));}
}

void mousePressed(){
 ball.invert(); 
}

void  keyPressed(){
 if(keyCode==32) ball.pos.y = -200;
}


boolean circleRect(float cx, float cy, float radius, float rx, float ry, float rw, float rh) {

  float testX = cx;
  float testY = cy;

  if (cx < rx)         testX = rx;     
  else if (cx > rx+rw) testX = rx+rw;   
  if (cy < ry)         testY = ry;      
  else if (cy > ry+rh) testY = ry+rh; 

  float distX = cx-testX;
  float distY = cy-testY;
  float distance = sqrt( (distX*distX) + (distY*distY) );

  if (distance <= radius) return true;
    
  return false;
}

thanks @GoToLoop. can you please post a summary or explanation of a link when you include one?