Hey, this is a class I made useful for collisions. It’s really efficient as you don’t need a Rectangle for every Entity you have. The entire game collisions can be managed with just 2 rectangles and 2 circles.
It also has a method to check if a rectangle overlaps a circle, really useful.
You can use them for your entities or buttons.
import net.autlos.sgf2.entities.Entity;
import com.badlogic.gdx.math.Circle;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector2;
public class CollisionHelper {
public static final Rectangle rectA = new Rectangle();
public static final Rectangle rectB = new Rectangle();
public static final Circle circA = new Circle();
public static final Circle circB = new Circle();
public static boolean collidesRectangles(Entity entityA, Entity entityB) {
entityA.setBoundingRectangle(rectA);
entityB.setBoundingRectangle(rectB);
return rectA.overlaps(rectB);
}
public static boolean collidesCircles(Entity entityA, Entity entityB) {
entityA.setBoundingCircle(circA);
entityB.setBoundingCircle(circB);
return circA.overlaps(circB);
}
public static boolean collidesCircleRectangle(Entity circleEntity, Entity rectangleEntity){
circleEntity.setBoundingCircle(circA);
rectangleEntity.setBoundingRectangle(rectA);
float circleDistanceX = Math.abs(circA.x - rectA.x - rectA.width/2);
float circleDistanceY = Math.abs(circA.y - rectA.y - rectA.height/2);
if(circleDistanceX > (rectA.width/2 + circA.radius) || circleDistanceY > (rectA.height/2 + circA.radius))
return false;
if(circleDistanceX <= (rectA.width/2) || circleDistanceX <= (rectA.height/2))
return true;
float cornerDistance = (circleDistanceX - rectA.width/2)*(circleDistanceX - rectA.width/2)
+ (circleDistanceY - rectA.height/2)*(circleDistanceY - rectA.height/2);
return (cornerDistance <= (circA.radius * circA.radius));
}
public static boolean isTouchingCircle(Entity circEntity, Vector2 position){
return isTouchingCircle(circEntity, position.x, position.y);
}
public static boolean isTouchingRectangle(Entity rectEntity, Vector2 position){
return isTouchingRectangle(rectEntity, position.x, position.y);
}
/**
* Returns true if the rectangle contains the given coordinates.
* @param rectEntity
* @param x
* @param y
* @return
*/
public static boolean isTouchingRectangle(Entity rectEntity, float x, float y) {
rectEntity.setBoundingRectangle(rectA);
return rectA.contains(x, y);
}
/**
* Returns true if the circle contains the given position
* @param circEntity
* @param x
* @param y
* @return
*/
public static boolean isTouchingCircle(Entity circEntity, float x, float y) {
circEntity.setBoundingCircle(circA);
return circA.contains(x, y);
}
}
You don’t need my Entity class, but you might want to see “setBoundingCircle” and “setBoundingRectangle” methods:
public void setBoundingRectangle(Rectangle boundingRectangle){
boundingRectangle.set(position.x, position.y, width, height);
}
public void setBoundingCircle(Circle boundingCircle){
boundingCircle.set(position.x + width / 2, position.y + height / 2, width/2);
}