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.

225 lines
7.1 KiB

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