*/
private BlockFaceCollision getCollisionBlockFace(Ball ball) {
float firstTime = Float.POSITIVE_INFINITY;
BlockFace firstFace = null;
Vector2f ballPos = ball.getPosition();
Vector2f ballVel = ball.getVelocity();
// left faces
if (ballVel.x > EPS) {
for (BlockFace face : leftBlockFaces) {
Vector2f tlCornerPos = face.getFirstCorner();
Vector2f blCornerPos = face.getSecondCorner();
if (ballPos.x < tlCornerPos.x) {
float time = (tlCornerPos.x - ballPos.x - ball.getRadius()) / ballVel.x;
if (time < firstTime) {
float newY = ballPos.y + time * ballVel.y;
if (newY >= blCornerPos.y && newY <= tlCornerPos.y) {
firstTime = time;
firstFace = face;
}
}
}
}
}
// right faces
if (ballVel.x < -EPS) {
for (BlockFace face : rightBlockFaces) {
Vector2f trCornerPos = face.getFirstCorner();
Vector2f brCornerPos = face.getSecondCorner();
if (ballPos.x > trCornerPos.x) {
float time = (trCornerPos.x - ballPos.x + ball.getRadius()) / ballVel.x;
if (time < firstTime) {
float newY = ballPos.y + time * ballVel.y;
if (newY >= brCornerPos.y && newY <= trCornerPos.y) {
firstTime = time;
firstFace = face;
}
}
}
}
}
// top faces
if (ballVel.y < -EPS) {
for (BlockFace face : topBlockFaces) {
Vector2f tlCornerPos = face.getFirstCorner();
Vector2f trCornerPos = face.getSecondCorner();
if (ballPos.y > tlCornerPos.y) {
float time = (tlCornerPos.y - ballPos.y + ball.getRadius()) / ballVel.y;
if (time < firstTime) {
float newX = ballPos.x + time * ballVel.x;
if (newX >= tlCornerPos.x && newX <= trCornerPos.x) {
firstTime = time;
firstFace = face;
}
}
}
}
}
// bottom faces
if (ballVel.y > EPS) {
for (BlockFace face : bottomBlockFaces) {
Vector2f blCornerPos = face.getFirstCorner();
Vector2f brCornerPos = face.getSecondCorner();
if (ballPos.y < blCornerPos.y) {
float time = (blCornerPos.y - ballPos.y - ball.getRadius()) / ballVel.y;
if (time < firstTime) {
float newX = ballPos.x + time * ballVel.x;
if (newX >= blCornerPos.x && newX <= brCornerPos.x) {
firstTime = time;
firstFace = face;
}
}
}
}
}
if (firstTime < Float.POSITIVE_INFINITY) {
Vector2f pos = new Vector2f(ballPos.x + firstTime * ballVel.x, ballPos.y + firstTime * ballVel.y);
Block block = firstFace.getBlockAt(pos);
return new BlockFaceCollision(firstTime, ball, block, firstFace.getFacePlace());
} else
return null;
}