perhaps the title isn't clear so here is an explanation with a picture to better express my question
Consider the following breakout clone game
where the game is rigged to change the ricochet angle according to some parameters
Suppose the ball was to hit one of these blue bricks, finding the angle at which the ball should ricochet to go toward any point below the bricks using trigonometry should be trivial and easy.
My question however, is if we have the following setup
How would I got about finding the optimal angle to ricochet from the collision point (circled in red) to get to a certain location (green dot for example)
The path should ricochet off of multiple points to get there, obviously, I'm just not sure how to go about it.
Any math guru who can help me out? Thanks!
Answer
Depending on how often you can have a "rigged" bounce, an incremental solution might be possible.
For this example, I'll assume that:
- Collisions with the playing field boundary are always normal (angle in = angle out)
- Collisions with blocks can be 100% rigged whenever needed (angle out = arbitrary)
Then each time you collide with a block, check whether you have an unimpeded path to the target point without touching another block (but collisions with the boundaries are permitted). If such a path exists, bounce that way.
A fast way to check this is to mirror your playing field and look for a straight-line path to any of the target point's reflections:
In the example you've given, there's no such path - even the best option hits the green row.
So, as a fallback, we just aim for a block that gets us a better view and let the simulation proceed. When we hit that block, this code runs again, and hopefully finds a path directly to the target. If not, it gets incrementally closer each time.
This way you don't need to account for the erosion of the playing field in your algorithm, since you're only ever looking one block impact ahead.
There are a lot of metrics you could use to select this fallback target. You could...
- ...use the mirroring trick above to identify all blocks reachable from both the source point and the target point, and pick a block from the intersection of those two sets. (Guaranteed to reach your target after hitting this block, if the intersection is non-empty)
- ...select the block visible from the current one that is closest to the target point.
- ...continue bouncing normally (angle in = angle out) waiting for one that permits a boundary-bounce-only path. (Makes rigged bouncing less obvious, but not guaranteed to succeed)
- ...always just bounce toward the reflection of the target point that's closest to lying on the normal bounce vector (angle in = angle out), whether it gets you there or not. This applies a mild bias to all block bounces so it's consistent, and should usually prevent the ball from doing something obviously rigged like reversing course. ;)
No comments:
Post a Comment