You can change your CollisionDetection class to an utility class, with static-only methods and therefore you could access it from anywhere.
As CJ_COIMBRA said, you could create a new collection of Entity object and store them in the CollisionDetection class.
public class CollisionDetection {
private static ArrayList<Entity> entities = new ArrayList<Entity>();
public static boolean collides(Entity e) {
return collidesWith(e) != null;
}
public static Entity collidesWith(Entity e) {
Entity obstacle = null;
for(Entity entity: entities) {
//If its not the same entity
if(entity!= e) {
if(e.getRect().intersects(entity.getRect()) {
obstacle = entity;
}
}
}
return obstacle;
}
//Add obstacle to detect
public static void addEntity(Entity entity) {
entities.add(entity);
}
//Remove obstacle to detect
public static void removeEntity(Entity entity) {
entities.remove(entity);
}
}
You could consider your walls and other obstacles as Entity. With this utility class you can initialize the detection in your init method like this:
CollisionDetection.addEntity(ball); CollisionDetection.addEntity(topWall); etc...
Then in the entities that you need to manage collision (like the ball) in the update method, after updating the position of the ball you can detect collision with any other entities and handle it.
//Detect if the ball (this) collides with any other entities boolean collision = CollisionDetection.collides(this); //Detect with which entity the ball collides //If its not colliding with anything it will return null. //Its your job to ensure that it will not collide with more than one entity at the time //by reacting on collision. Entity obstacle = CollisionDetection.collidesWith(this);

Find content
Male