Utils.java 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. package me.hammerle.supersnuvi.util;
  2. import me.hammerle.supersnuvi.tiles.Tile;
  3. public class Utils
  4. {
  5. public static float interpolate(float from, float to, float factor)
  6. {
  7. return from + (to - from) * factor;
  8. }
  9. public static int toBlock(float c)
  10. {
  11. return (int) (c / Tile.SIZE);
  12. }
  13. public static float toCoord(int b)
  14. {
  15. return b * Tile.SIZE;
  16. }
  17. public static float getSquaredDistance(float x1, float y1, float x2, float y2)
  18. {
  19. return (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2);
  20. }
  21. public static Face getCollidingFace(
  22. float xMin1, float yMin1, float xMax1, float yMax1,
  23. float xMin2, float yMin2, float xMax2, float yMax2)
  24. {
  25. float diffUp = yMax1 - yMin2;
  26. float diffRight = xMax2 - xMin1;
  27. float diffDown = yMax2 - yMin1;
  28. float diffLeft = xMax1 - xMin2;
  29. float min = diffUp;
  30. Face f = Face.UP;
  31. if(min > diffRight)
  32. {
  33. min = diffRight;
  34. f = Face.RIGHT;
  35. }
  36. if(min > diffDown)
  37. {
  38. min = diffDown;
  39. f = Face.DOWN;
  40. }
  41. if(min > diffLeft)
  42. {
  43. f = Face.LEFT;
  44. }
  45. int c = 0;
  46. c += (min == diffUp) ? 1 : 0;
  47. c += (min == diffRight) ? 1 : 0;
  48. c += (min == diffDown) ? 1 : 0;
  49. c += (min == diffLeft) ? 1 : 0;
  50. if(c >= 2)
  51. {
  52. return Face.NULL;
  53. }
  54. return f;
  55. }
  56. }