// 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
final Vector3f s = new Vector3f();
_character._transform.getScale(s);
// Determine which way(s) the character's
// movement should be restricted. We also set the character's
// translation vector
// so that it does not visibly intersect the blocks.
final float tileTop = max.y;
final float tileBottom = min.y;
final float tileLeft = min.x;
final float tileRight = max.x;
final float tileMidX = (tileLeft + tileRight) / 2.0f;
final float tileMidY = (tileBottom + tileTop) / 2.0f;
final Vector3f v = new Vector3f();
// If character's top or bottom is between tile's
// top and bottom, restrict horizontally
if (charTop < tileTop - 0.1f && charTop > tileBottom + 0.1f
|| charBottom > tileBottom + 0.1f