Java-Gaming.org Hi !
 Featured games (84) games approved by the League of Dukes Games in Showcase (564) Games in Android Showcase (151) games submitted by our members Games in WIP (605) games currently in development
 News: Read the Java Gaming Resources, or peek at the official Java tutorials
Pages: [1]
 ignore  |  Print
 Rectangle to Circle collision test? (2D)  (Read 1126 times) 0 Members and 1 Guest are viewing this topic.
JonnyGingerKid

Junior Devvie

Exp: 3 years

 « Posted 2014-08-12 04:55:50 »

Hi everyone. I've recently been adding a simple physics engine to my 2D game engine that I will hopefully be using in my future projects. Right now I have two colliders, a BoundingBox (essentially an AABB) and a BoundingCircle class. Both of these extend my abstract BoundingVolume class. The BoundingBox is defined by an X, Y, Width and Height. The BoundingCircle is defined by an X, Y (the center of the circle) and a radius.

I check collisions between two BoundingBox's like this:
 1  2  3  4  5  6  7  8  9  10 `public boolean intersects(BoundingBox other) {      if(other.getY() > (getY()+getHeight()) || //check top edge         (other.getY()+other.getHeight()) < getY() || //check bottom edge            other.getX() > (getX()+getWidth()) || //check right edge               (other.getX()+other.getWidth()) < getX()){ //check left edge         //NO COLLISION         return false;      }      return true;   }`

And between two BoundingCircle's like this:
 1  2  3  4  5  6  7  8  9 `public boolean intersects(BoundingCircle other) {      //get the distance between the two circle's centers      Vector2f distance = new Vector2f(other.getCenter().getX() - center.getX(), other.getCenter().getY() - center.getY());      if(distance.getLength() <= radius + other.getRadius()){         //COLLISION!         return true;      }      return false;   }`

But how would I check for a collision between the BoundingBox and the BoundingCircle? I've looked into the Separating Axis Theorem and I don't think that it can work for circles as they have an infinite amount of sides. I also don't want to use an open source physics library as I want the learning experience. What is a simple and fast way of checking for a collision between a rectangle and a circle?

P.S. Tell me if you see anything wrong with my way of checking between rectangle-rectangle and circle-circle collisions as well.

Thank you!
BurntPizza

« JGO Bitwise Duke »

Medals: 356
Exp: 6 years

 « Reply #1 - Posted 2014-08-12 05:13:44 »

http://stackoverflow.com/questions/401847/circle-rectangle-collision-detection-intersection

Pick one. I like Cygon's.
Roquen
 « Reply #2 - Posted 2014-08-12 05:32:19 »

Aside:  that's overkill on comparisons (clamp).
theagentd

« JGO Bitwise Duke »

Medals: 409
Projects: 2
Exp: 8 years

 « Reply #3 - Posted 2014-08-12 06:38:01 »

I use the following algorithm:

 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15 `//find closest point inside rectangle from circle centerfloat closestX = clamp(circle.x, rect.x, rect.x + rect.width);float closestY = clamp(circle.y, rect.y, rect.y + rect.height);//find distance from point on rectangle to circlefloat dx = closestX - circle.x;float dy = closestY - circly.y;float distanceSqrd = dx*dx + dy*dy;//test distance against radiusif(distanceSqrd < circle.radius*circle.radius){    //collision detected!!!}else{    //no collision}`

Code for clamp()
 1  2  3  4  5  6  7  8 `public static float clamp(float f, float min, float max){    return f < min ? min : f > max ? max : f;    /*    if(f < min){ return min;}    if(f > max){ return max;}    return f;    */}`

Myomyomyo.
Roquen
 « Reply #4 - Posted 2014-08-12 07:18:08 »

Newbies ignore this.  Do whatever works and move on.

Q: How often do you compare exactly two things for intersections?
A: Statistically never.

Ignoring that: this method performs 3-5 comparisons prior to completion and they (should be) statistically random between true/false (given the above...if they are not you're missing information you should be exploiting) with a minimum dependency chain of 7.  Alternate methods will binary subdivide the problem.  After the first comparison 50% of cases will be decided, 50% of the remaining for the second.  A well chosen ordering can cause the comparisons to be not uniformly random (again on average)...so statistically the result will either true or false depending on construction.

As an example (last code snippet):
http://www.java-gaming.org/topics/vectors-what-s-the-point/24307/msg/225743/view.html#msg225743
convert it to AABB looks like this (assume I didn't screw up):

 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  18 `public boolean intersects(Circle c){  float px = c.x - ref.x; //center.x - delta.x;  float r  = c.radius;    if (px > r)  return false;  float py = c.y - ref.y; //center.y - delta.y;  if (py > r)  return false;  if (px <= 0) return true;  if (py <= 0) return true;    px *= px;  py *= py;    return px+py <= r*r;}`

BurntPizza

« JGO Bitwise Duke »

Medals: 356
Exp: 6 years

 « Reply #5 - Posted 2014-08-12 07:24:06 »

I was debating mentioning a "short-circuiting" algo like you showed, but I figured that a more straightforward, intuitive, what-have-you test might be better for a beginner.
Intuitiveness is subjective as well, however.
Roquen
 « Reply #6 - Posted 2014-08-13 08:32:38 »

That's why I hedged my comment.  Note my version above is broken..I yanked out the abs..see the rotated version and it should be clear.

EDIT: And I should note that the clamped version can be made very reasonable for bulking processing by formulating with min/max assuming the language generates max/max opcodes.
Pages: [1]
 ignore  |  Print

You cannot reply to this message, because it is very, very old.

 theagentd (8 views) 2015-03-27 23:08:20 wxwsk8er (52 views) 2015-03-20 15:39:46 Fairy Tailz (43 views) 2015-03-15 21:52:20 Olo (28 views) 2015-03-13 17:51:59 Olo (30 views) 2015-03-13 17:50:51 Olo (35 views) 2015-03-13 17:50:16 Olo (40 views) 2015-03-13 17:47:07 ClaasJG (30 views) 2015-03-10 11:36:42 ClaasJG (39 views) 2015-03-10 11:33:01 Pippogeek (47 views) 2015-03-05 14:36:23
 LiquidNitrogen 23x BurntPizza 21x KevinWorkman 21x basil_ 20x theagentd 18x Riven 18x EgonOlsen 17x Roquen 13x princec 12x Varkas 10x SHC 9x NegativeZero 9x wessles 7x phu004 7x Slyth2727 7x ClaasJG 7x
 How to: JGO Wikiby Mac702015-02-17 20:56:162D Dynamic Lighting2015-01-01 20:25:42How do I start Java Game Development?by gouessej2014-12-27 19:41:21Resources for WIP gamesby kpars2014-12-18 10:26:14Understanding relations between setOrigin, setScale and setPosition in libGdx2014-10-09 22:35:00Definite guide to supporting multiple device resolutions on Android (2014)2014-10-02 22:36:02List of Learning Resources2014-08-16 10:40:00List of Learning Resources2014-08-05 19:33:27
 java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org