CollisionObject.java 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. package me.hammerle.supersnuvi.util;
  2. import static me.hammerle.supersnuvi.util.CollisionBox.createScaledTileBox;
  3. public abstract class CollisionObject
  4. {
  5. public final static CollisionObject NULL_BOX = new CollisionBox(0.0f, 0.0f, 0.0f, 0.0f);
  6. public final static CollisionObject DEFAULT_TILE_BOX = createScaledTileBox(0.0f, 0.0f, 1.0f, 1.0f);
  7. public enum Type
  8. {
  9. LINE, BOX
  10. }
  11. public abstract Type getType();
  12. public abstract CollisionObject copy();
  13. public abstract void save();
  14. public abstract CollisionObject reset();
  15. public abstract float getWidth();
  16. public abstract float getHeight();
  17. public abstract float getMinX();
  18. public abstract float getMaxX();
  19. public abstract float getMinY();
  20. public abstract float getMaxY();
  21. public abstract CollisionObject expand(float x, float y);
  22. public final CollisionObject offset(float x, float y)
  23. {
  24. offsetX(x);
  25. offsetY(y);
  26. return this;
  27. }
  28. public abstract CollisionObject offsetX(float x);
  29. public abstract CollisionObject offsetY(float y);
  30. public abstract boolean isColliding(CollisionObject cb);
  31. public final boolean mayCollide(CollisionObject cb)
  32. {
  33. // box intersect test, even for lines, prevents failures
  34. float minX1 = Math.min(cb.getMinX(), cb.getMaxX());
  35. float minY1 = Math.min(cb.getMinY(), cb.getMaxY());
  36. float maxX1 = Math.max(cb.getMinX(), cb.getMaxX());
  37. float maxY1 = Math.max(cb.getMinY(), cb.getMaxY());
  38. float minX2 = Math.min(getMinX(), getMaxX());
  39. float minY2 = Math.min(getMinY(), getMaxY());
  40. float maxX2 = Math.max(getMinX(), getMaxX());
  41. float maxY2 = Math.max(getMinY(), getMaxY());
  42. return maxX1 > minX2 && maxX2 > minX1 && maxY1 > minY2 && maxY2 > minY1;
  43. }
  44. }