Java-Gaming.org Hi !
 Featured games (90) games approved by the League of Dukes Games in Showcase (753) Games in Android Showcase (228) games submitted by our members Games in WIP (842) 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 3179 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: 485
Exp: 7 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

JGO Kernel

Medals: 517

 « Reply #2 - Posted 2014-08-12 05:32:19 »

Aside:  that's overkill on comparisons (clamp).
theagentd
 « 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

JGO Kernel

Medals: 517

 « 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: 485
Exp: 7 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

JGO Kernel

Medals: 517

 « 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

 ivj94 (584 views) 2018-03-24 14:47:39 ivj94 (48 views) 2018-03-24 14:46:31 ivj94 (382 views) 2018-03-24 14:43:53 Solater (62 views) 2018-03-17 05:04:08 nelsongames (109 views) 2018-03-05 17:56:34 Gornova (159 views) 2018-03-02 22:15:33 buddyBro (703 views) 2018-02-28 16:59:18 buddyBro (92 views) 2018-02-28 16:45:17 xxMrPHDxx (493 views) 2017-12-31 17:17:51 xxMrPHDxx (733 views) 2017-12-31 17:15:51
 Java Gaming Resourcesby philfrei2017-12-05 19:38:37Java Gaming Resourcesby philfrei2017-12-05 19:37:39Java Gaming Resourcesby philfrei2017-12-05 19:36:10Java Gaming Resourcesby philfrei2017-12-05 19:33:10List of Learning Resourcesby elect2017-03-13 14:05:44List of Learning Resourcesby elect2017-03-13 14:04:45SF/X Librariesby philfrei2017-03-02 08:45:19SF/X Librariesby philfrei2017-03-02 08:44:05
 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