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.

217 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 readonly Tile Tile;
  72. public AABB(Vector2 position, Vector2 halfSize) : this(position, halfSize, null) {
  73. }
  74. public AABB(Vector2 position, Vector2 halfSize, Tile tile) {
  75. Position = position;
  76. HalfSize = halfSize;
  77. Tile = tile;
  78. }
  79. public float Top {
  80. get { return Position.Y - HalfSize.Y; }
  81. }
  82. public float Bottom {
  83. get { return Position.Y + HalfSize.Y; }
  84. }
  85. public float Left {
  86. get { return Position.X - HalfSize.X; }
  87. }
  88. public float Right {
  89. get { return Position.X + HalfSize.X; }
  90. }
  91. public Vector2 TopLeft {
  92. get { return new Vector2(Left, Top); }
  93. }
  94. public Vector2 TopRight {
  95. get { return new Vector2(Right, Top); }
  96. }
  97. public Vector2 BottomLeft {
  98. get { return new Vector2(Left, Bottom); }
  99. }
  100. public Vector2 BottomRight {
  101. get { return new Vector2(Right, Bottom); }
  102. }
  103. public Hit? Intersect(AABB box) {
  104. float dx = box.Position.X - Position.X;
  105. float px = box.HalfSize.X + HalfSize.X - Math.Abs(dx);
  106. if (px <= 0) {
  107. return null;
  108. }
  109. float dy = box.Position.Y - Position.Y;
  110. float py = box.HalfSize.Y + HalfSize.Y - Math.Abs(dy);
  111. if (py <= 0) {
  112. return null;
  113. }
  114. if (px < py) {
  115. int sign = Math.Sign(dx);
  116. Vector2 delta = new Vector2(px * sign, 0);
  117. Vector2 normal = new Vector2(sign, 0);
  118. Vector2 hitPos = new Vector2(Position.X + HalfSize.X * sign, box.Position.Y);
  119. return new Hit(box, hitPos, delta, normal);
  120. } else {
  121. int sign = Math.Sign(dy);
  122. Vector2 delta = new Vector2(0, py * sign);
  123. Vector2 normal = new Vector2(0, sign);
  124. Vector2 hitPos = new Vector2(box.Position.X, Position.Y + HalfSize.Y * sign);
  125. return new Hit(this, hitPos, delta, normal);
  126. }
  127. }
  128. public Hit? IntersectSegment(Vector2 pos, Vector2 delta) {
  129. return IntersectSegment(pos, delta, Vector2.Zero);
  130. }
  131. public Hit? IntersectSegment(Vector2 pos, Vector2 delta, Vector2 padding) {
  132. float scaleX = 1.0f / delta.X;
  133. float scaleY = 1.0f / delta.Y;
  134. int signX = Math.Sign(scaleX);
  135. int signY = Math.Sign(scaleY);
  136. float nearTimeX = (Position.X - signX * (HalfSize.X + padding.X) - pos.X) * scaleX;
  137. float nearTimeY = (Position.Y - signY * (HalfSize.Y + padding.Y) - pos.Y) * scaleY;
  138. float farTimeX = (Position.X + signX * (HalfSize.X + padding.X) - pos.X) * scaleX;
  139. float farTimeY = (Position.Y + signY * (HalfSize.Y + padding.Y) - pos.Y) * scaleY;
  140. if (nearTimeX > farTimeY || nearTimeY > farTimeX) {
  141. return null;
  142. }
  143. float nearTime = Math.Max(nearTimeX, nearTimeY);
  144. float farTime = Math.Min(farTimeX, farTimeY);
  145. if (nearTime >= 1 || farTime <= 0) {
  146. return null;
  147. }
  148. // If we've gotten this far, a collision is happening. If the near time is greater than zero,
  149. // the segment starts outside and is entering the box. Otherwise, the segment starts inside
  150. // the box, so we set the hit time to zero.
  151. float hitTime = Math.Max(0, nearTime);
  152. Vector2 normal = nearTimeX > nearTimeY ?
  153. new Vector2(-signX, 0) :
  154. new Vector2(0, -signY);
  155. Vector2 hitDelta = Vector2.Multiply(delta, -(1.0f - hitTime));
  156. Vector2 hitPos = Vector2.Add(pos, Vector2.Multiply(delta, hitTime));
  157. return new Hit(this, hitPos, hitDelta, normal, hitTime);
  158. }
  159. public Sweep Sweep(AABB box, Vector2 delta) {
  160. // Fast-path case if the other box is static.
  161. if (delta.X == 0 && delta.Y == 0) {
  162. Hit? staticHit = Intersect(box);
  163. // TODO: I don't understand the original source here, but I think this is correct.
  164. return new Sweep(staticHit, box.Position, staticHit?.Time ?? 1);
  165. }
  166. Hit? maybeHit = IntersectSegment(box.Position, delta, box.HalfSize);
  167. if (maybeHit == null) {
  168. return new Sweep(null, Vector2.Add(box.Position, delta), 1);
  169. }
  170. Hit hit = (Hit) maybeHit;
  171. Vector2 hitPos = Vector2.Add(box.Position, Vector2.Multiply(delta, hit.Time));
  172. // TODO: why is this calculation made, and then thrown away?
  173. // Vector2 direction = Vector2.Normalize(delta);
  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. }