# algorithm for Shooting objects that bounce off the wall

I'm trying to make a 2D game where my Hero is standing in a 2D plan and surrounded by enemies(highest 3) that throwing balls. and my hero have a shield in hands and and will stop the balls...

well the problem is i'm not able to found the direction on which enemy shoot the ball to reach to the Hero... i want to make enemy little bit smarter so they don't just shoot straight

as in picture the ball can bounce of the wall same as light bounce off wall, the indecent angle and refractive angle will be same(sorry image angles not perfect. i draw them in paint) so i know that slop of this path line will be same in whole path just interchange in negative and positive.

now i want to find the directions in which i can shoot like 2 coordinates of x,y or something where i can shoot and the ball reaches to the hero.

i not want just one direction... i want the all possible directions where my enemy can shoot and that hit to the hero

and also the ball will be gone after 4-5 times bounce

there is an android game "Stupid zombies" that have exactly this kind system in game

any algorithm and solution to archive this....i'm working in java so later i can port my game to android easily

i also tried below link but my ball hitting more then 2 times and i'm not able to find solution https://softwareengineering.stackexchange.com/questions/290628/algorithm-for-calculating-a-bullet-path-to-a-target-with-max-2-ricochets

please edit if you found anything not properly asked ...i tried to make as simple as possible, but its my first time here.

• If the main reflector is the rectangular perimeter of the playing field, you can mirror your playfield across its edges, and aim at one of the reflected copies of the target, as described in this answer Feb 17, 2018 at 13:17
• @DMGregory can you please explain this post in little bit easy way and also how i can use this in my situation. Feb 17, 2018 at 15:04
• And i also want all possible directions where i can shoot Feb 17, 2018 at 15:05
• @Gammer Do not beg for further information on this site. Go and apply your brain to the problem using the info you've been graciously given by DMGregory. Show that you have a can-do attitude. Feb 18, 2018 at 4:01
• @ArcaneEngineer Politely asking for more information is not out of line. I'm here to help. :) Feb 18, 2018 at 4:49

A cool trick you can use for this kind of planning is to imagine what your world would look like if all 4 walls were mirrors.

Like the infinite hall of mirrors effect, you'd see infinite copies of your source and target object, tiling the infinite 2D plane...

The cool thing is that a straight line from your source object aiming at any of these mirrored copies of your target will map to a ricochet trajectory that will hit the original.

So, you can iterate over each adjacent mirrored copy of your play field out to some radius, and aim at the reflection of your target in that copy.

To be safe, you should test this line against copies of your source & target object in each cell the line passes through, to make sure you don't pick a trajectory that bounces back through yourself.

• Thank you so much... It's really helpful.. Actually i just learned coordinate system few months ago... I think coordinate system will be in my subject when I'll be in high school.. Feb 18, 2018 at 5:22
• Just one more thing if you don't mind... How many copies i should create.. Well i can figure out this.. But if you can just hint me then it will be good.. Feb 18, 2018 at 5:25
• How many shots do you want to fire? Make that many copies. When you're looking at the tiled grid of reflected universes, each time you cross the grid lines of the boundary walls, that's another bounce. So there's 1 shot with 0 bounces. Then 4 shots with 1 bounce each, arranged around it like a +. Then 8 shots with 2 bounces, arranged around those in a diamond shape. Then 12 shots with 3 bounces, in a larger diamond outside that. On and on, in expanding diamonds according to the Manhattan Metric. Feb 18, 2018 at 5:29
• Yes!!!!!!...thanks for helping me and pointing in right direction... Last 10 days i was trying to get idea... Thank you so much again... Feb 18, 2018 at 5:33
• If you want to ask a new question, use the "Ask Question" button at the top of the site. ;) Jul 29, 2018 at 23:57