1
\$\begingroup\$

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

my plan image

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.

\$\endgroup\$
  • 2
    \$\begingroup\$ 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 \$\endgroup\$ – DMGregory Feb 17 '18 at 13:17
  • \$\begingroup\$ @DMGregory can you please explain this post in little bit easy way and also how i can use this in my situation. \$\endgroup\$ – Gammer Feb 17 '18 at 15:04
  • \$\begingroup\$ And i also want all possible directions where i can shoot \$\endgroup\$ – Gammer Feb 17 '18 at 15:05
  • \$\begingroup\$ @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. \$\endgroup\$ – Engineer Feb 18 '18 at 4:01
  • \$\begingroup\$ @ArcaneEngineer Politely asking for more information is not out of line. I'm here to help. :) \$\endgroup\$ – DMGregory Feb 18 '18 at 4:49
3
\$\begingroup\$

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...

Animated gif showing trajectory planning across a tiled reflected universe

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.

\$\endgroup\$
  • \$\begingroup\$ 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.. \$\endgroup\$ – Gammer Feb 18 '18 at 5:22
  • \$\begingroup\$ 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.. \$\endgroup\$ – Gammer Feb 18 '18 at 5:25
  • \$\begingroup\$ 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. \$\endgroup\$ – DMGregory Feb 18 '18 at 5:29
  • \$\begingroup\$ Yes!!!!!!...thanks for helping me and pointing in right direction... Last 10 days i was trying to get idea... Thank you so much again... \$\endgroup\$ – Gammer Feb 18 '18 at 5:33
  • 1
    \$\begingroup\$ If you want to ask a new question, use the "Ask Question" button at the top of the site. ;) \$\endgroup\$ – DMGregory Jul 29 '18 at 23:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.