## How to setup circular game bounds?

Posts that don't fit into other categories.
asantoso
Posts: 14
Joined: Thu Mar 02, 2017 12:18 am

### How to setup circular game bounds?

How to setup circular game bounds?

William
Posts: 378
Joined: Sat Feb 06, 2010 10:23 pm

### Re: How to setup circular game bounds?

One option is extending the AbstractBounds class and implementing the isOutside method. You are given a collidable, which will have a list of fixtures. You would iterate those fixtures and check if they are all outside the circle. This can be done in a number of ways, but the easiest might be to create a Circle shape and Gjk instance to test the shapes. See the following code:

1. public class CircularBounds extends AbstractBounds {
2.   private final Circle bounds = //;
3.   private final Gjk detector = new Gjk();
4.   public boolean isOutside(Collidable<?> collidable) {
5.     int n = collidable.getFixtureCount();
6.     for (int i = 0; i < n; i++) {
7.       Fixture f = collidable.getFixture(i);
8.       if (detector.detect(this.transform, this.bounds, collidable.getTransform(), f.getShape())) {
9.         // the circle bounds overlaps one of the fixtures so we can return false
10.         return false;
11.       }
12.     }
13.     // the bounds didn't overlap any fixture of the collidable, so it must be outside the bounds
14.     return true;
15.   }
16. }

An alternative implementation would be to use the getRotationDiscRadius method and do a simple circle-circle collision test. Not as accurate, but might be faster:

1. public class CircularBounds extends AbstractBounds {
2.   private final Circle bounds = //;
3.   public boolean isOutside(Collidable<?> collidable) {
7.
8.     // get the centers in world space
9.     Vector2 boundsCenter = this.transform.getTransformed(bounds.getCenter());
10.     Vector2 collidableCenter = collidable.getWorldCenter();
11.
12.     return boundsCenter.distanceSquared(collidableCenter) > (totalRadius * totalRadius);
13.   }
14. }

William