How to setup circular game bounds?

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

How to setup circular game bounds?

Postby asantoso » Wed Sep 20, 2017 2:43 am

How to setup circular game bounds?

William
Site Admin
Posts: 345
Joined: Sat Feb 06, 2010 10:23 pm

Re: How to setup circular game bounds?

Postby William » Wed Sep 20, 2017 8:24 am

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:

Code: [Select all] [Expand/Collapse] [Download] (Untitled.java)
  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:

Code: [Select all] [Expand/Collapse] [Download] (Untitled.java)
  1. public class CircularBounds extends AbstractBounds {
  2.   private final Circle bounds = //;
  3.   public boolean isOutside(Collidable<?> collidable) {
  4.     double collidableRadius = collidable.getRotationDiscRadius();
  5.     double boundsRadius = bounds.getRadius();
  6.     double totalRadius = collidableRadius + boundsRadius;
  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


Return to “General Discussion”

Who is online

Users browsing this forum: No registered users and 2 guests