Calculating a bounce angle to hit a target

Like in the game Tanks! for the Wii, I wanted to replicate how the Green tank AI finds the correct spot to shoot at to get a good ricochet hit on the player. Demonstrated down below:

I found a bunch of math formulas to actually calculate a bounce angle, but how would I find which plane to actually shoot at? I can calculate where to aim based on a plane/block to ricochet off of, however finding the plane to base it off of in the first place is a difficulty I’m facing. I was wondering if anyone could take a crack at what they’d do in this situation or any tips on what I should do to sort the “best shot” scenario to aim around an object.

4 Likes

If it helps at all, this is basically how I’d calculate the angle. Which isn’t the problem here. I was wondering since like in original post, the map will have multiple blocks in it, how could I find which plane to utilize as the optimal ricochet plane?

(Just realizing this after posting, but the point B’ would be reflected to be the same distance that point B is from the wall, so the point here is a little off, but the idea is basically there.)

2 Likes

I found my solution.

https://gyazo.com/917f922f82e799968ceaea4c80131069

1 Like

Could you possibly show how you did this?

Are you talking about how I calculated the correct angle to hit a target at? Or how I made the bullets hit bank off walls? Both I can say really easily.

Banking off of walls is a like, two or three lines. I cast a ray out and when I detect an object to bank off of, I calculate the distance between both objects. If close enough it runs these two lines to calculate the new normal (direction) to move said object to.

local reflect = (currentNormal - (2 * currentNormal:Dot(norm) * norm))
currentNormal = reflect

currentNormal is the directional value in the direction of the bank off of an object.

As for calculating angles to hit a target? It’s a really bad method and it only worked for this game because there’s not much code running at the same time, I casted a ray 360 degrees around the tank and used the reflection calculation to calculate if it would bank and hit a target. I did this ray 3 times until it found a hit. If it didn’t connect with any player, then it wouldn’t count as a hit and it’d get a new angle to attempt. It sounds clunky but it caused the green tanks to be a menace and hit nasty shots.

3 Likes

Thanks for sharing, but sadly this doesn’t help me, as I need to be able to calculate it with a projectile that has an arc. Specifically I need to be able to bank a basketball off of the backboard with perfection.

That’d be someone similar to what I’ve done, all you have to do is account for gravitational fall in your equation. There are a lot of dev forum posts that talk about mapping projectile movements. I think it was EgoMoose or someone else who wrote an entire community tutorial on how it’d be done and including bounces, arcs, etc.