<!doctype html><html lang="en" data-theme="night"><head><meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" href="data:,">
<title>docs/003-axis-separated-collision.md</title>
<link rel="stylesheet" href="../grug.css">
<link rel="stylesheet" href="../art/art.css">
<script>try { document.documentElement.dataset.theme = JSON.parse(localStorage.getItem('grug.theme')) === 'day' ? 'day' : 'night'; } catch {}</script>
</head><body class="source-viewer">
<p class="back"><a href="../index.html">back to the school</a></p>
<h1>docs/003-axis-separated-collision.md</h1>
<pre class="src">
<a id="L1"></a><span class="ln">   1</span>  # ADR 003: resolve the two axes in separate passes
<a id="L2"></a><span class="ln">   2</span>  
<a id="L3"></a><span class="ln">   3</span>  Status: accepted
<a id="L4"></a><span class="ln">   4</span>  
<a id="L5"></a><span class="ln">   5</span>  ## Decision
<a id="L6"></a><span class="ln">   6</span>  
<a id="L7"></a><span class="ln">   7</span>  `step` calls `resolve_paddle_hits` after `move_ball_x` and `resolve_wall_hits` after `move_ball_y`. Each pass changes only the coordinate it owns. A paddle overlap reverses `vx` and pushes the ball clear in x. A wall overlap reverses `vy` and clamps y.
<a id="L8"></a><span class="ln">   8</span>  
<a id="L9"></a><span class="ln">   9</span>  ## Reason
<a id="L10"></a><span class="ln">  10</span>  
<a id="L11"></a><span class="ln">  11</span>  A ball that hits a corner can overlap a paddle and a wall in the same step. Resolving one combined rectangle test would pick a winner by code order, and the loser would stay overlapped until the next step. The two passes answer two different questions: did the ball cross a paddle face, and did it cross a wall. Each answer has one obvious correction.
<a id="L12"></a><span class="ln">  12</span>  
<a id="L13"></a><span class="ln">  13</span>  ## Consequences
<a id="L14"></a><span class="ln">  14</span>  
<a id="L15"></a><span class="ln">  15</span>  Collision code stays a few short procedures with no corner cases to enumerate. A reader can add a wall or move a paddle without reworking the other pass. The ball can still leave through the goal line; `score_if_out` handles that after both passes.
<a id="L16"></a><span class="ln">  16</span>  
</pre></body></html>