|
@@ -2,11 +2,8 @@ package me.hammerle.supersnuvi.util;
|
|
|
|
|
|
import me.hammerle.supersnuvi.tiles.Tile;
|
|
|
|
|
|
-public class CollisionBox
|
|
|
+public class CollisionBox extends CollisionObject
|
|
|
{
|
|
|
- public final static CollisionBox NULL_BOX = new CollisionBox(0.0f, 0.0f, 0.0f, 0.0f);
|
|
|
- public final static CollisionBox DEFAULT_TILE_BOX = createScaledTileBox(0.0f, 0.0f, 1.0f, 1.0f);
|
|
|
-
|
|
|
private float minX;
|
|
|
private float maxX;
|
|
|
private float minY;
|
|
@@ -29,16 +26,18 @@ public class CollisionBox
|
|
|
cMaxY = maxY;
|
|
|
}
|
|
|
|
|
|
- public static CollisionBox createScaledTileBox(float x1, float y1, float x2, float y2)
|
|
|
+ public static CollisionObject createScaledTileBox(float x1, float y1, float x2, float y2)
|
|
|
{
|
|
|
return new CollisionBox(x1 * Tile.SIZE, y1 * Tile.SIZE, x2 * Tile.SIZE, y2 * Tile.SIZE);
|
|
|
}
|
|
|
|
|
|
- public CollisionBox copy()
|
|
|
+ @Override
|
|
|
+ public CollisionObject copy()
|
|
|
{
|
|
|
return new CollisionBox(cMinX, cMinY, cMaxX, cMaxY);
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
public void save()
|
|
|
{
|
|
|
minX = cMinX;
|
|
@@ -47,7 +46,8 @@ public class CollisionBox
|
|
|
maxY = cMaxY;
|
|
|
}
|
|
|
|
|
|
- public CollisionBox reset()
|
|
|
+ @Override
|
|
|
+ public CollisionObject reset()
|
|
|
{
|
|
|
cMinX = minX;
|
|
|
cMaxX = maxX;
|
|
@@ -56,37 +56,44 @@ public class CollisionBox
|
|
|
return this;
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
public float getWidth()
|
|
|
{
|
|
|
return maxX - minX;
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
public float getHeight()
|
|
|
{
|
|
|
return maxY - minY;
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
public float getMinX()
|
|
|
{
|
|
|
return cMinX;
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
public float getMaxX()
|
|
|
{
|
|
|
return cMaxX;
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
public float getMinY()
|
|
|
{
|
|
|
return cMinY;
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
public float getMaxY()
|
|
|
{
|
|
|
return cMaxY;
|
|
|
}
|
|
|
|
|
|
- public CollisionBox expand(float x, float y)
|
|
|
+ @Override
|
|
|
+ public CollisionObject expand(float x, float y)
|
|
|
{
|
|
|
if(x < 0.0f)
|
|
|
{
|
|
@@ -106,60 +113,42 @@ public class CollisionBox
|
|
|
}
|
|
|
return this;
|
|
|
}
|
|
|
-
|
|
|
- public CollisionBox grow(float x, float y)
|
|
|
- {
|
|
|
- x = Math.abs(x);
|
|
|
- y = Math.abs(y);
|
|
|
- cMinX -= x;
|
|
|
- cMaxX += x;
|
|
|
- cMinY -= y;
|
|
|
- cMaxY += y;
|
|
|
- return this;
|
|
|
- }
|
|
|
-
|
|
|
- public CollisionBox grow(float value)
|
|
|
- {
|
|
|
- return this.grow(value, value);
|
|
|
- }
|
|
|
|
|
|
- public CollisionBox offset(float x, float y)
|
|
|
- {
|
|
|
- cMinX += x;
|
|
|
- cMaxX += x;
|
|
|
- cMinY += y;
|
|
|
- cMaxY += y;
|
|
|
- return this;
|
|
|
- }
|
|
|
-
|
|
|
- public CollisionBox offsetX(float x)
|
|
|
+ @Override
|
|
|
+ public CollisionObject offsetX(float x)
|
|
|
{
|
|
|
cMinX += x;
|
|
|
cMaxX += x;
|
|
|
return this;
|
|
|
}
|
|
|
|
|
|
- public CollisionBox offsetY(float y)
|
|
|
+ @Override
|
|
|
+ public CollisionObject offsetY(float y)
|
|
|
{
|
|
|
cMinY += y;
|
|
|
cMaxY += y;
|
|
|
return this;
|
|
|
}
|
|
|
|
|
|
- public boolean isColliding(float minX, float minY, float maxX, float maxY)
|
|
|
- {
|
|
|
- return cMaxX > minX && maxX > cMinX && cMaxY > minY && maxY > cMinY;
|
|
|
- }
|
|
|
-
|
|
|
- public boolean isColliding(CollisionBox cb)
|
|
|
- {
|
|
|
- return isColliding(cb.cMinX, cb.cMinY, cb.cMaxX, cb.cMaxY);
|
|
|
- }
|
|
|
-
|
|
|
- public CollisionBox intersect(CollisionBox cb)
|
|
|
+ @Override
|
|
|
+ public boolean isColliding(CollisionObject cb)
|
|
|
{
|
|
|
- return new CollisionBox(Math.max(cMinX, cb.cMinX), Math.max(cMinY, cb.cMinY),
|
|
|
- Math.min(cMaxX, cb.cMaxX), Math.min(cMaxY, cb.cMaxY));
|
|
|
+ switch(cb.getType())
|
|
|
+ {
|
|
|
+ case BOX:
|
|
|
+ return cMaxX > cb.getMinX() && cb.getMaxX() > cMinX && cMaxY > cb.getMinY() && cb.getMaxY() > cMinY;
|
|
|
+ case LINE:
|
|
|
+ float x1 = cb.getMinX();
|
|
|
+ float y1 = cb.getMinY();
|
|
|
+ float x2 = cb.getMaxX();
|
|
|
+ float y2 = cb.getMaxY();
|
|
|
+ return CollisionLine.intersect(x1, y1, x2, y2, cMinX, cMinY, cMaxX, cMinY) ||
|
|
|
+ CollisionLine.intersect(x1, y1, x2, y2, cMaxX, cMinY, cMaxX, cMaxY) ||
|
|
|
+ CollisionLine.intersect(x1, y1, x2, y2, cMaxX, cMaxY, cMinX, cMaxY) ||
|
|
|
+ CollisionLine.intersect(x1, y1, x2, y2, cMinX, cMaxY, cMinX, cMinY);
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+
|
|
|
}
|
|
|
|
|
|
@Override
|
|
@@ -176,4 +165,10 @@ public class CollisionBox
|
|
|
sb.append(']');
|
|
|
return sb.toString();
|
|
|
}
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Type getType()
|
|
|
+ {
|
|
|
+ return Type.BOX;
|
|
|
+ }
|
|
|
}
|