A stealth-based 2D platformer where you don't have to kill anyone unless you want to. https://www.semicolin.games
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

215 lines
6.8 KiB

  1. using Microsoft.Xna.Framework;
  2. using System;
  3. // Design largely from https://noonat.github.io/intersect/.
  4. namespace SemiColinGames {
  5. // Math functions that return floats rather than doubles, for convenience.
  6. public static class FMath {
  7. public const float PI = (float) Math.PI;
  8. private readonly static float[] degToRad = new float[360];
  9. static FMath() {
  10. for (int i = 0; i < degToRad.Length; i++) {
  11. degToRad[i] = PI / 180 * i;
  12. }
  13. }
  14. // Converts degrees to radians using a look-up table. Expects the input to be near [0, 360)
  15. // and will loop for potentially a long while if that's not the case.
  16. public static float DegToRad(int degrees) {
  17. while (degrees < 0) {
  18. degrees += 360;
  19. }
  20. while (degrees >= 360) {
  21. degrees -= 360;
  22. }
  23. return degToRad[degrees];
  24. }
  25. public static float Sin(double degrees) {
  26. return (float) Math.Sin(degrees);
  27. }
  28. public static float Cos(double degrees) {
  29. return (float) Math.Cos(degrees);
  30. }
  31. public static T Clamp<T>(T value, T min, T max) where T : IComparable {
  32. if (value.CompareTo(min) == -1) {
  33. return min;
  34. } else if (value.CompareTo(max) == 1) {
  35. return max;
  36. } else {
  37. return value;
  38. }
  39. }
  40. }
  41. public readonly struct Hit {
  42. public readonly AABB Collider;
  43. public readonly Vector2 Position;
  44. public readonly Vector2 Delta;
  45. public readonly Vector2 Normal;
  46. public readonly float Time; // ranges from [0, 1].
  47. public Hit(AABB collider, Vector2 position, Vector2 delta, Vector2 normal) :
  48. this(collider, position, delta, normal, 0.0f) {
  49. }
  50. public Hit(AABB collider, Vector2 position, Vector2 delta, Vector2 normal, float time) {
  51. Collider = collider;
  52. Position = position;
  53. Delta = delta;
  54. Normal = normal;
  55. Time = time;
  56. }
  57. }
  58. public readonly struct Sweep {
  59. public readonly Hit? Hit;
  60. public readonly Vector2 Position;
  61. public readonly float Time;
  62. public Sweep(Hit? hit, Vector2 position, float time) {
  63. Hit = hit;
  64. Position = position;
  65. Time = time;
  66. }
  67. }
  68. public readonly struct AABB {
  69. public readonly Vector2 Position; // centroid
  70. public readonly Vector2 HalfSize;
  71. public AABB(Vector2 position, Vector2 halfSize) {
  72. Position = position;
  73. HalfSize = halfSize;
  74. }
  75. public float Top {
  76. get { return Position.Y - HalfSize.Y; }
  77. }
  78. public float Bottom {
  79. get { return Position.Y + HalfSize.Y; }
  80. }
  81. public float Left {
  82. get { return Position.X - HalfSize.X; }
  83. }
  84. public float Right {
  85. get { return Position.X + HalfSize.X; }
  86. }
  87. public Vector2 TopLeft {
  88. get { return new Vector2(Left, Top); }
  89. }
  90. public Vector2 TopRight {
  91. get { return new Vector2(Right, Top); }
  92. }
  93. public Vector2 BottomLeft {
  94. get { return new Vector2(Left, Bottom); }
  95. }
  96. public Vector2 BottomRight {
  97. get { return new Vector2(Right, Bottom); }
  98. }
  99. public Hit? Intersect(AABB box) {
  100. float dx = box.Position.X - Position.X;
  101. float px = box.HalfSize.X + HalfSize.X - Math.Abs(dx);
  102. if (px <= 0) {
  103. return null;
  104. }
  105. float dy = box.Position.Y - Position.Y;
  106. float py = box.HalfSize.Y + HalfSize.Y - Math.Abs(dy);
  107. if (py <= 0) {
  108. return null;
  109. }
  110. // TODO: which of delta/normal/hitPos do we actually care about?
  111. if (px < py) {
  112. int sign = Math.Sign(dx);
  113. Vector2 delta = new Vector2(px * sign, 0);
  114. Vector2 normal = new Vector2(sign, 0);
  115. Vector2 hitPos = new Vector2(Position.X + HalfSize.X * sign, box.Position.Y);
  116. return new Hit(box, hitPos, delta, normal);
  117. } else {
  118. int sign = Math.Sign(dy);
  119. Vector2 delta = new Vector2(0, py * sign);
  120. Vector2 normal = new Vector2(0, sign);
  121. Vector2 hitPos = new Vector2(box.Position.X, Position.Y + HalfSize.Y * sign);
  122. return new Hit(this, hitPos, delta, normal);
  123. }
  124. }
  125. public Hit? IntersectSegment(Vector2 pos, Vector2 delta) {
  126. return IntersectSegment(pos, delta, Vector2.Zero);
  127. }
  128. public Hit? IntersectSegment(Vector2 pos, Vector2 delta, Vector2 padding) {
  129. float scaleX = 1.0f / delta.X;
  130. float scaleY = 1.0f / delta.Y;
  131. int signX = Math.Sign(scaleX);
  132. int signY = Math.Sign(scaleY);
  133. float nearTimeX = (Position.X - signX * (HalfSize.X + padding.X) - pos.X) * scaleX;
  134. float nearTimeY = (Position.Y - signY * (HalfSize.Y + padding.Y) - pos.Y) * scaleY;
  135. float farTimeX = (Position.X + signX * (HalfSize.X + padding.X) - pos.X) * scaleX;
  136. float farTimeY = (Position.Y + signY * (HalfSize.Y + padding.Y) - pos.Y) * scaleY;
  137. if (nearTimeX > farTimeY || nearTimeY > farTimeX) {
  138. return null;
  139. }
  140. float nearTime = Math.Max(nearTimeX, nearTimeY);
  141. float farTime = Math.Min(farTimeX, farTimeY);
  142. if (nearTime >= 1 || farTime <= 0) {
  143. return null;
  144. }
  145. // If we've gotten this far, a collision is happening. If the near time is greater than zero,
  146. // the segment starts outside and is entering the box. Otherwise, the segment starts inside
  147. // the box, so we set the hit time to zero.
  148. float hitTime = Math.Max(0, nearTime);
  149. Vector2 normal = nearTimeX > nearTimeY ?
  150. new Vector2(-signX, 0) :
  151. new Vector2(0, -signY);
  152. // TODO: replace these with Vector2.Multiply (etc)
  153. Vector2 hitDelta = new Vector2((1.0f - hitTime) * -delta.X, (1.0f - hitTime) * -delta.Y);
  154. Vector2 hitPos = new Vector2(pos.X + delta.X * hitTime, pos.Y + delta.Y * hitTime);
  155. return new Hit(this, hitPos, hitDelta, normal, hitTime);
  156. }
  157. public Sweep Sweep(AABB box, Vector2 delta) {
  158. // fast-path case if the other box is static
  159. if (delta.X == 0 && delta.Y == 0) {
  160. Hit? staticHit = Intersect(box);
  161. // TODO: I don't understand the original source here, but I think this is correct.
  162. return new Sweep(staticHit, box.Position, staticHit?.Time ?? 1);
  163. }
  164. Hit? maybeHit = IntersectSegment(box.Position, delta, box.HalfSize);
  165. if (maybeHit == null) {
  166. return new Sweep(null, Vector2.Add(box.Position, delta), 1);
  167. }
  168. Hit hit = (Hit) maybeHit;
  169. Vector2 hitPos = new Vector2(
  170. box.Position.X + delta.X * hit.Time,
  171. box.Position.Y + delta.Y * hit.Time);
  172. Vector2 direction = Vector2.Normalize(delta);
  173. // TODO: why is this calculation made, and then thrown away?
  174. Vector2 sweepHitPos = new Vector2(
  175. FMath.Clamp(hit.Position.X + direction.X * box.HalfSize.X,
  176. Position.X - HalfSize.X,
  177. Position.X + HalfSize.X),
  178. FMath.Clamp(hit.Position.Y + direction.Y * box.HalfSize.Y,
  179. Position.Y - HalfSize.Y,
  180. Position.Y + HalfSize.Y));
  181. return new Sweep(hit, hitPos, hit.Time);
  182. }
  183. }
  184. }