Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (498)
Games in Android Showcase (116)
games submitted by our members
Games in WIP (563)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Rectangle to Circle collision test? (2D)  (Read 890 times)
0 Members and 1 Guest are viewing this topic.
Offline JonnyGingerKid

Senior Newbie


Exp: 1 year



« 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!
Offline BurntPizza
« 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.
Offline Roquen
« Reply #2 - Posted 2014-08-12 05:32:19 »

Aside:  that's overkill on comparisons (clamp).
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline 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 center
float 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 circle
float dx = closestX - circle.x;
float dy = closestY - circly.y;
float distanceSqrd = dx*dx + dy*dy;

//test distance against radius
if(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.
Offline 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;
}


Symmetry is your friend.
Offline BurntPizza
« 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.
Offline 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  
 
 

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

radar3301 (12 views)
2014-09-21 23:33:17

BurntPizza (30 views)
2014-09-21 02:42:18

BurntPizza (22 views)
2014-09-21 01:30:30

moogie (20 views)
2014-09-21 00:26:15

UprightPath (28 views)
2014-09-20 20:14:06

BurntPizza (32 views)
2014-09-19 03:14:18

Dwinin (48 views)
2014-09-12 09:08:26

Norakomi (74 views)
2014-09-10 13:57:51

TehJavaDev (102 views)
2014-09-10 06:39:09

Tekkerue (50 views)
2014-09-09 02:24:56
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
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
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!