# Check if Objects are overlapping

**URL:** <https://discourse.processing.org/t/check-if-objects-are-overlapping/720>\
**Category:** Processing\
**Created:** [June 5, 2018, 12:52pm UTC](https://discourse.processing.org/t/check-if-objects-are-overlapping/720 "2018-06-05T12:52:50Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![mikegiannak](https://avatars.discourse-cdn.com/v4/letter/m/96bed5/32.png) [@mikegiannak](https://discourse.processing.org/u/mikegiannak)\
**Post date:** [June 5, 2018, 12:52pm UTC](https://discourse.processing.org/t/check-if-objects-are-overlapping/720/1 "2018-06-05T12:52:50Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [June 5, 2018, 1:03pm UTC](https://discourse.processing.org/t/check-if-objects-are-overlapping/720/2 "2018-06-05T13:03:08Z")

</div>

[http://JeffreyThompson.org/collision-detection/circle-rect.php](http://JeffreyThompson.org/collision-detection/circle-rect.php)

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [June 5, 2018, 1:07pm UTC](https://discourse.processing.org/t/check-if-objects-are-overlapping/720/3 "2018-06-05T13:07:12Z")

</div>

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: 😀

[http://Studio.ProcessingTogether.com/sp/pad/export/ro.9qPrLGrYGkr2o](http://Studio.ProcessingTogether.com/sp/pad/export/ro.9qPrLGrYGkr2o)

---

<div class="post-metadata">

**Author:** ![mikegiannak](https://avatars.discourse-cdn.com/v4/letter/m/96bed5/32.png) [@mikegiannak](https://discourse.processing.org/u/mikegiannak)\
**Post date:** [June 5, 2018, 1:08pm UTC](https://discourse.processing.org/t/check-if-objects-are-overlapping/720/4 "2018-06-05T13:08:44Z")

</div>

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 ?

---

<div class="post-metadata">

**Author:** ![mikegiannak](https://avatars.discourse-cdn.com/v4/letter/m/96bed5/32.png) [@mikegiannak](https://discourse.processing.org/u/mikegiannak)\
**Post date:** [June 5, 2018, 2:28pm UTC](https://discourse.processing.org/t/check-if-objects-are-overlapping/720/5 "2018-06-05T14:28:46Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [June 5, 2018, 2:38pm UTC](https://discourse.processing.org/t/check-if-objects-are-overlapping/720/6 "2018-06-05T14:38:04Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mikegiannak](https://avatars.discourse-cdn.com/v4/letter/m/96bed5/32.png) [@mikegiannak](https://discourse.processing.org/u/mikegiannak)\
**Post date:** [June 5, 2018, 2:39pm UTC](https://discourse.processing.org/t/check-if-objects-are-overlapping/720/7 "2018-06-05T14:39:46Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [June 5, 2018, 2:43pm UTC](https://discourse.processing.org/t/check-if-objects-are-overlapping/720/8 "2018-06-05T14:43:17Z")

</div>

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

> <https://gist.github.com/discover>

---

<div class="post-metadata">

**Author:** ![mikegiannak](https://avatars.discourse-cdn.com/v4/letter/m/96bed5/32.png) [@mikegiannak](https://discourse.processing.org/u/mikegiannak)\
**Post date:** [June 5, 2018, 2:44pm UTC](https://discourse.processing.org/t/check-if-objects-are-overlapping/720/9 "2018-06-05T14:44:41Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mikegiannak](https://avatars.discourse-cdn.com/v4/letter/m/96bed5/32.png) [@mikegiannak](https://discourse.processing.org/u/mikegiannak)\
**Post date:** [June 5, 2018, 3:02pm UTC](https://discourse.processing.org/t/check-if-objects-are-overlapping/720/10 "2018-06-05T15:02:02Z")

</div>

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 ?

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [June 5, 2018, 3:14pm UTC](https://discourse.processing.org/t/check-if-objects-are-overlapping/720/11 "2018-06-05T15:14:20Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mikegiannak](https://avatars.discourse-cdn.com/v4/letter/m/96bed5/32.png) [@mikegiannak](https://discourse.processing.org/u/mikegiannak)\
**Post date:** [June 5, 2018, 6:24pm UTC](https://discourse.processing.org/t/check-if-objects-are-overlapping/720/12 "2018-06-05T18:24:03Z")

</div>

Here you are (your help is much appreciated

```auto
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;
}

```

---

<div class="post-metadata">

**Author:** ![lmccart](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/lmccart/32/12_2.png) [@lmccart](https://discourse.processing.org/u/lmccart)\
**Post date:** [June 6, 2018, 12:49am UTC](https://discourse.processing.org/t/check-if-objects-are-overlapping/720/13 "2018-06-06T00:49:22Z")

</div>

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