Targetting AI

I've got a problem with my targeting AI, essentially, gravity affects the trajectory of the bullet - which means that the CPU opponent misses after a certain distance.

I can obviously put a some sort of linear correction in, but I wondered if anyone could suggest different approaches?

I'm keen not to use sensors - to check whether bullets are hitting the target... but maybe this is the only way
Imho your statement of the problem is not clear:

Do you wish to change what actually happens, or what you AI predicts ?
In the second case, it has nothing do to with sensors or even Box2D at all.
Unless your AI runs some Box2D simulation to "predict" what will happen, but that would be far fetched (or cheating 😮 )
Unless you are simulating wind or air friction, the path of the projectile will be a simple parabola and pretty easy to calculate. But as the previous poster said, it's not clear exactly what you are trying to 'solve' - you want to find an initial angle and speed for the projectile to land at a certain place?
For whatever horizontal velocity you choose to give the projectile, solve the time t it will take for it to get there.
The vertical component of the velocity of the projectile will just be g (gravitational acceleration) multiplied by that t, divided by 2 (since the bullet needs to reach 0 velocity halfway through the trajectory).

Done.
Thanks for the replies (and apologies for not seeing them 'til now, I didn't get an email notifying me...)

@Gnurfos
Apologies, the question was a bit vague, and you are correct that the problem doesn't require box2D, I was just inquiring as the game is in box2D. (As a side note there is a way to 'predict' the path of an object. I've done it before, it requires setting up a duplicate world, and looping thru the world steps.)

@irresistible force
To clarify, I wanted the AI to 'realise' that after a certain distance aiming straight for the target would result in a miss. You are correct that the offset is parabolic, I'm not sure how easy or not that calculation would be, I went with a simplified solution (outlined below.)

@acarlston
In the end I didn't follow this method directly - the AI doesn't need to be perfect it just needs to be ok (i.e. not to miss the target after a certain distance) essentially the AI 'guesses' the amount of offset it needs from the distance to the target.
charlie_says wrote:
(As a side note there is a way to 'predict' the path of an object. I've done it before, it requires setting up a duplicate world, and looping thru the world steps.)


That's only approximate prediction though. If you want a completely accurate prediction, you'll need to make a deep copy of the entire Box2d data including memory pool layouts, cached data, and the like.
Storyyeller wrote:
That's only approximate prediction though. If you want a completely accurate prediction, you'll need to make a deep copy of the entire Box2d data including memory pool layouts, cached data, and the like.


That's what I call "cheating" 🙂
And it probably would not be very efficient.
You'd better go for calculation of the parabola, it's really easy.
@Storyyeller - I only duplicated the static items, so you could show where a ball would bounce. It wasn't terribly efficient I think, but it did work (if you're interested you can see the result here: http://www.agame.com/game/Plink.html)

@Gnurfos - I completely agree about the 'cheating' comment - but in a game where the AI can consistently miss or cheat - I'll pick cheat every time. In the end the calculation (I used) was very simple.

Original thread: https://box2d.org/forum/viewtopic.php?t=5782