Utils.java 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. public static boolean intersect(float x11, float y11, float x12, float y12, float x21, float y21, float x22, float y22)
  57. {
  58. if(compareFloats(x11, x12))
  59. {
  60. if(compareFloats(x21, x22))
  61. {
  62. return false;
  63. }
  64. else
  65. {
  66. if(!isBetween(x11, x21, x22))
  67. {
  68. return false;
  69. }
  70. float k = (y21 - y22) / (x21 - x22);
  71. float d = y22 - k * x22;
  72. float y = d + x11 * k;
  73. return isBetween(y, y11, y12) && isBetween(y, y21, y22);
  74. }
  75. }
  76. else
  77. {
  78. if(compareFloats(x21, x22))
  79. {
  80. if(!isBetween(x21, x11, x12))
  81. {
  82. return false;
  83. }
  84. float k = (y11 - y12) / (x11 - x12);
  85. float d = y12 - k * x12;
  86. float y = d + x21 * k;
  87. return isBetween(y, y11, y12) && isBetween(y, y21, y22);
  88. }
  89. else
  90. {
  91. float k1 = (y11 - y12) / (x11 - x12);
  92. float k2 = (y21 - y22) / (x21 - x22);
  93. if(compareFloats(k1, k2))
  94. {
  95. return false;
  96. }
  97. float d1 = y12 - k1 * x12;
  98. float d2 = y22 - k2 * x22;
  99. float x = (d1 - d2) / (k2 - k1);
  100. if(!isBetween(x, x11, x12) || !isBetween(x, x21, x22))
  101. {
  102. return false;
  103. }
  104. float y = k1 * x + d1;
  105. return isBetween(y, y11, y12) && isBetween(y, y21, y22);
  106. }
  107. }
  108. }
  109. private static final float ERROR = 1.0f / 512.0f;
  110. private static boolean isBetween(float y, float y1, float y2)
  111. {
  112. float min = Math.min(y1, y2) - ERROR;
  113. float max = Math.max(y1, y2) + ERROR;
  114. return y >= min && y <= max;
  115. }
  116. private static boolean compareFloats(float a, float b)
  117. {
  118. return Math.abs(a - b) < ERROR;
  119. }
  120. }