Skip to content
Thomas Cashman edited this page Jul 14, 2019 · 5 revisions

mini2Dx provides several classes for storing collision data depending on how you want to handle collisions.

PointQuadTree

The PointQuadTree and CollisionPoint classes are useful if all your objects are moving freely and their collision area is represented by a point coordinate.

QuadTree<CollisionPoint> collisions = new PointQuadTree<>(4, 2, 0, 0, 640, 320);

// Add some points
collisions.add(new CollisionPoint(1, 1));
collisions.add(new CollisionPoint(4, 4));

// Get all points in an area
List<CollisionPoint> collisionsInArea = new ArrayList<CollisionPoint>();
collisions.getElementsWithinArea(collisionsInArea, new CollisionBox(2, 2, 6, 6));

RegionQuadTree

The RegionQuadTree and CollisionBox classes are useful if all your objects are moving freely and their collision area is represented by a rectangle.

RegionQuadTree<CollisionBox> collisions = new RegionQuadTree<CollisionBox>();
//Add a collision
collisions.add(new CollisionBox(4f, 4f, 2f, 2f));
//Get all collisions in an area
List<CollisionBox> collisionsInArea = new ArrayList<CollisionBox>();
collisions.getElementsWithinRegion(collisionsInArea, new Rectangle(0f, 0f, 2f, 2f));
//collisionsInArea now has a list of collisions intersecting or within the rectangle's area

Connecting to Entities

If you are using an entity-component-system pattern, a good approach for associating a CollisionShape with an entity is to pass the entity ID into the constructor of the CollisionShape (which can be returned via getId()) and then looking up the entity by id when resolving collisions.

Clone this wiki locally