How to setup circular game bounds?
How to setup circular game bounds?
How to setup circular game bounds?
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:
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:
William
- public class CircularBounds extends AbstractBounds {
- private final Circle bounds = //;
- private final Gjk detector = new Gjk();
- public boolean isOutside(Collidable<?> collidable) {
- int n = collidable.getFixtureCount();
- for (int i = 0; i < n; i++) {
- Fixture f = collidable.getFixture(i);
- if (detector.detect(this.transform, this.bounds, collidable.getTransform(), f.getShape())) {
- // the circle bounds overlaps one of the fixtures so we can return false
- return false;
- }
- }
- // the bounds didn't overlap any fixture of the collidable, so it must be outside the bounds
- return true;
- }
- }
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:
- public class CircularBounds extends AbstractBounds {
- private final Circle bounds = //;
- public boolean isOutside(Collidable<?> collidable) {
- double collidableRadius = collidable.getRotationDiscRadius();
- double boundsRadius = bounds.getRadius();
- double totalRadius = collidableRadius + boundsRadius;
- // get the centers in world space
- Vector2 boundsCenter = this.transform.getTransformed(bounds.getCenter());
- Vector2 collidableCenter = collidable.getWorldCenter();
- return boundsCenter.distanceSquared(collidableCenter) > (totalRadius * totalRadius);
- }
- }
William
Return to “General Discussion”
Who is online
Users browsing this forum: No registered users and 1 guest