1 Attachment(s)

Calculating an Angle for Artillery

Hey guys this is my first post and I would like to start of by saying Hello.

So Hello :)

Anyways, I'm currently programming a 2D game and am having a few difficulties on working out an equation, or finding an equation that is. I'll give you some details of what it is.

Basically, I'm building an AI that fires its artillery gun at a target in the distance. But I need to find out how mathematically wise it would do this. There are 3 key elements to it. The first being the distance between the AI and its target, the second being the gravity that is implemented in the game and the angle in which it needs to find to hit the target. If I know the equations ill be able to implement it within the code.

Does anybody on here know how you would work out the angle in order for the AI to hit it's target?

Thanks for any help :D

Attachment 22104

Re: Calculating an Angle for Artillery

Going down the route of parametric equations would be the way I'd go about it.

$\displaystyle x(t) = t\cos(\theta) - D$ (where D is drag, if your game has no air resistance this is 0)

$\displaystyle y(t) =t\sin(\theta) - mg$ (mg being the object's weight)

Your horizontal displacement is x(t) and the vertical displacement is y(t). I suspect the AI would know/guess x(t), the gravity is up to you as a programmer and depends on the mass of the shell.

To find the angle you can isolate $\displaystyle t\sin(\theta) \text{ and } t\cos(\theta)$

$\displaystyle t\sin(\theta) = y(t) + mg \text{..... [eq1]}$

$\displaystyle t\cos(\theta) = x(t) + D \text{...... [eq2]}$

You can then divide eq1 by eq2

$\displaystyle \dfrac{eq1}{eq2} \Leftrightarrow \dfrac{t\sin(\theta)}{t\cos(\theta)} = \dfrac{y(t)+mg}{x(t)+D} \Longleftrightarrow \tan(\theta) = \dfrac{y(t)+mg}{x(t)+D}$

$\displaystyle \theta = \arctan \left( \dfrac{y(t)+mg}{x(t)+D} \right)$

I'm not sure how correct that is going to be though :\

Re: Calculating an Angle for Artillery

Thanks Man,

I think this is exactly what I needed :)

Cheers