3

Say that I have a plasma gun:

enter image description here

It's easy to compute the trajectory of the plasma ray starting from the gun.

However, another ray may come from afar: enter image description here

As everybody knows, plasma rays are deviated when they collide. In fact, they are always deviated 90°, away from the other ray:

enter image description here

Space is discreet (like a chess board). Each tile may contain either a plasma gun, a plasma ray or nothing. Rays can go either straight or diagonally. An important precision: rays are "instant", they do not take any time to propagate.

I need to compute a "solution" trajectory for all rays that satisfies all constraints.

I can easily compute a ray propagation step by step, but if a collision is detected with other ray, it seems that I will need to "backtrack" that ray up to the point of collision, to then restart propagating it correctly.

Any suggestion of an algorithm to do this? Maybe lists are not really indicated for this, and I should use graphs (nodes being collisions)?

I would be also glad if you point me to some class of similar algorithms/research... Thanks!

EDIT: Note that collisions can have cascading consequences. For example in this situation: enter image description here

If I fire the red plasma gun, This is what I should get:

enter image description here

The green trajectory was affected by the new collision between red and blue. New collisions can also appear:

enter image description here

Last but not least, paradoxes can appear:

enter image description here

cdupont
  • 131
  • 3

1 Answers1

3

One possible approach is to use the Bentley-Ottman algorithm (or similar algorithm) to find all intersections between line segments.

More specifically: for each plasma gun, add a directed line starting at the plasma gun and proceeding in a straight line to infinity. The algorithm lets you find all pairs of intersections between the lines in the data structure. We're going to enumerate intersections in order of the increasing distance from the start to the intersection. More specifically, we'll repeat the following until there are no intersections remaining:

  • Find the intersection point that has the minimum (smallest) possible distance from start of line to the intersection point. Call this intersection point $I$.

  • $I$ is the intersection of two directed line segments, one starting at (let's call it) $G$ and going through $I$ and one starting at (let's call it) $H$ and going through $I$.

  • Delete those two lines from the data structure.

  • Output line segments $GI$ and $HI$.

  • Add to the data structure two lines starting at $I$, representing the reflected directions, and proceeding to infinity.

Repeat until there are no intersections remaining among the lines in the data structure, and then output all lines remaining in the data structure.

Then the solution trajectory will be the line segments that have been output.

The core idea here is to start with the closest collision first, and proceed outward, as each collision will have cascading effects. (Thanks to @cdupoint for this insight.)

If you implement the Bentley-Ottmann algorithm with appropriate persistent data structures, it should be possible to make this run fast, with each operation (adding or deleting lines from the data structure, finding the nearest intersection) all running quickly ($O(\log n)$ or $O(\log^2 n)$ time).

In your specific situation, you might be able to simplify or speed up the data structure and algorithm by specializing ti to advantage of the fact that all lines will be horizontal, vertical, or diagonal at a 45 degree angle.

D.W.
  • 167,959
  • 22
  • 232
  • 500