Java-Gaming.org Hi !
 Featured games (84) games approved by the League of Dukes Games in Showcase (604) Games in Android Showcase (171) games submitted by our members Games in WIP (653) 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 1299 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: 419
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: 526
Projects: 3
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: 419
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.

 SHC (26 views) 2015-08-01 03:58:20 Jesse (19 views) 2015-07-29 04:35:27 Riven (40 views) 2015-07-27 16:38:00 Riven (21 views) 2015-07-27 15:35:20 Riven (24 views) 2015-07-27 12:26:13 Riven (14 views) 2015-07-27 12:23:39 BurntPizza (36 views) 2015-07-25 00:14:37 BurntPizza (46 views) 2015-07-24 22:06:39 BurntPizza (30 views) 2015-07-24 06:06:53 NoxInc (37 views) 2015-07-22 22:16:53
 theagentd 52x wessles 48x basil_ 35x KaiHH 26x orangepascal 25x ags1 21x Riven 19x mooman219 17x bornander 14x KudoDEV 13x CelestialCreator 11x princec 11x Jesse 11x klaus 10x pquiring 9x chrislo27 8x
 List of Learning Resourcesby gouessej2015-07-09 11:29:36How Do I Expand My Game?by bashfrog2015-06-14 11:34:43List of Learning Resources2015-05-31 05:37:30Intersection Methodsby Roquen2015-05-29 08:19:33List of Learning Resources2015-05-05 10:20:32How 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:21
 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