I am creating a game using java.awt
and was wondering how to do advanced collision detection with Area
s. In my game each entity has a collider made with an Area
. By advanced collision detection, I mean that I do not want a collision between two areas to result in an entity stopping, rather I want the collision to result in the entity sliding across. Entities have double
values of magnitude and direction and a Area of their collision shape. The collision shapes should be able to be any shape (not just rectangles or circles). The output of the program should be a vector that when combined with the entity can result in that entity not colliding.
// Basic collision without sliding
public static boolean testCollsion(Area a, Area b) {
Area c = (Area) a.clone();
c.intersect(b);
return !c.isEmpty();
}
// Advanced sliding collision
// Where b is stationary and a should change
public static void collide(Collidable a, Collidable b) {
Area areaA = new Area(a.getCollider().shape);
Area areaB = new Area(b.getCollider().shape);
Area c = (Area) areaA.clone();
c.intersect(areaB);
if (!c.isEmpty()) {
double directionA = a.getDirection();
double directionB = b.getDirection();
double magnitudeA = a.getMagnitude();
double magnitudeB = b.getMagnitude();
// Calculate collision vector
double directionC = ?;
double magnitudeC = ?;
}
}
After the collision the entity should be moved out of the area of the thing that it hit while preserving its velocity and direction, and allowing it to slide across the area. This should be done by another vector to cancel out the entity's movement towards the collision.