* 1 if the level has been won.
*/
private int detectCollisions() {
Bounds boundsLevel;
final BoundingBox bounds = (BoundingBox) _character.getBounds();
final BoundingBox prevBounds = (BoundingBox) _character.getPrevBounds();
// Get the character's corners
final Vector3f[] corners = new Vector3f[8];
for (int i = 0; i < corners.length; i++) {
corners[i] = new Vector3f();
}
bounds.getCorners(corners, 0);
final float charTop = corners[0].y;
final float charBottom = corners[1].y;
final float charLeft = corners[0].x;
final float charRight = corners[2].x;
prevBounds.getCorners(corners, 0);
final float prevCharTop = corners[0].y;
final float prevCharBottom = corners[1].y;
final float prevCharLeft = corners[0].x;
final float prevCharRight = corners[2].x;
final Vector3f characterCenter =
new Vector3f((charLeft + charRight) / 2.0f,
(charTop + charBottom) / 2.0f, 0.0f);
// Die if you fell off the level
if (characterCenter.y < -20.0f) {
return -1;
}
// Detect collision with the level end object
boundsLevel = _levelGoal.getBounds();
if (bounds.intersects(boundsLevel)) {
_character.addMovementRestriction(SpriteGameCharacter.MOVE_DOWN
| SpriteGameCharacter.MOVE_UP
| SpriteGameCharacter.MOVE_LEFT
| SpriteGameCharacter.MOVE_RIGHT);
return 1;
}
// Detect collisions with obstacles
int count = _enemies.size();
for (int i = 0; i < count; i++) {
boundsLevel = ((Sprite) _enemies.elementAt(i)).getBounds();
if (bounds.intersects(boundsLevel)) {
_character.addMovementRestriction(SpriteGameCharacter.MOVE_DOWN
| SpriteGameCharacter.MOVE_UP
| SpriteGameCharacter.MOVE_LEFT
| SpriteGameCharacter.MOVE_RIGHT);
return -1;
}
}
_character.clearMovementRestrictions();
// Detect collisions with blocks and calculate the character's movement
// restrictions
BoundingBox tileBounds;
count = _tiles.size();
Sprite tile;
for (int i = 0; i < count; i++) {
tile = (Sprite) _tiles.elementAt(i);
tileBounds = (BoundingBox) tile.getBounds();
if (bounds.intersects(tileBounds)) {
// Calculate the center point of the block's bounding box
final Vector3f min = new Vector3f();
final Vector3f max = new Vector3f();
tileBounds.getMin(min);
tileBounds.getMax(max);
final Vector3f blockCenter = new Vector3f(min);
blockCenter.add(max);
blockCenter.scale(0.5f);
// Get character scale