4 ms·
This is exactly how I implemented almost the exact same thing in the iOS clone of a classic DOS game (Snipes!) I made (shameless plug: https://itunes.apple.com/
by w0utert 12y ago
This is exactly how I implemented almost the exact same thing in the iOS clone of a classic DOS game (Snipes!) I made (shameless plug: https://itunes.apple.com/nl/app/snipes!/id428845764?mt=8 https://itunes.apple.com/nl/app/snipes!/id428845764?mt=8)
The linked article is pretty thorough, but a little complex for the nature of the problem I think. What I did was just calculate all tiles (I call them 'cells') intersected by the displacement vector of the projectiles (balls) moving around the maze, which can be done in constant time if your maze dimensions are fixed. Then I look up the walls of intersected cells, and calculate the closest intersection of the displacement vector with any of these walls. This intersection happens somewhere along the vector between t=0 and t=1, so I translate the projectile position back in time using this value, change its direction (invert either x or y based on the orientation of the wall), then move it along (1 - t) * v_inverted to solve the 'fast projectiles skipping walls' problem (this is basically a very simple case of continuous collision detection).
If you allow a tiny bit of non-determinism in very rare cases, the edge case where a ball exactly hits an corner doesn't need a lot of additional work, you just repeat the intersection test iteratively until there are no more intersecting edges (99 out of 100 cases there will be only 1, and in the remaining case there will be 2, provided that your balls don't move faster than 1 unit cell distance per frame).