# Using trigonometry to predict future position

Intro

I'm currently creating an AI for a robot whose aim is to shoot another robot.

All I want to do is to be able to calculate at what angle to shoot my bullet, so that it hits my enemy, with the assumption that the enemy continues moving at the same bearing and velocity.

Variables

Here are the variables that are known

• Bullet speed is: $bulletSpeed$ m/s
• $x$ and $y$ coordinates of both mine $(x_1, y_1)$ and the enemy robot $(x_2, y_2)$
• The angle between me and the enemy: $a^\circ$
• The heading of the enemy robot: $b^\circ$
• The distance between me and the enemy robot: $d$ metres
• The velocity of the robot: $v$ m/s

[Note: this may be more information than needed. Also all angles range from 0 to 360, where North is $0^\circ$ and East is $90^\circ$ and so on]

Diagram

Here's a diagram to help illustrate the problem, where the arrows show what direction each robot is facing and $travel$ is the distance of the robot has travelled and $bullet$ is the distance the bullet has travelled in $t$ time:

Objective

I wish to find the angle: $\theta$ to shoot my bullet such that it hits the robot and in the minimum amount of time.

The answer should be a formula such that $\theta$ is the subject, so I can substitute the $bulletSpeed$, $a^\circ$, $b^\circ$, $x_1$, $y_1$, $x_2$ and/or $y_2$ to get an answer for $\theta$. i.e.:

$$\theta = ...$$

What I got so far

Here's what i have got so far: $$bullet = bulletSpeed * t$$ $$travel = v * t$$

As you can see its not much.

Please help, as I really need to get this section of my AI done by the end of today and really struggling.

Please also explain your solution so I can understand it. Many Thanks

## 1 Answer

Name the point where you robot shoots from $P$, the point of departure of the enemy robot $Q$, and the point where the bullet hits the enemy $R$. In triangle $PQR$ we then have $$\angle Q=90^o+(180^o-90^o-a)+b=180^o+b-a$$ and from the law of sines we know that $$\frac{q}{\sin Q}=\frac{p}{\sin\theta}\iff \frac{\sin\theta}{\sin Q}=\frac pq=\frac{enemyspeed\times t}{bulletspeed\times t}$$ and if you look close enough, $\theta$ is the only unknown variable in this expression as $t$ cancels out.

• I'm a bit confused how you got angle B. Could you explain that bit for me. – Yahya Uddin May 4 '15 at 11:48
• @YahyaUddin: In your figure, you can see the two dashed lines from $x_2$ and $y_2$ meeting at a right angle at the point $B$. To one side of that angle is obviously angle $b$. The angle below the right angle is actually equal to $a$, since the line $AB$ connects parallel lines, the $x_1$ dashed line and the $x_2$ dashed line. My English is a bit poor, but it is about transversals between parallel lines. – String May 4 '15 at 12:18
• @YahyaUddin: Have you considered my comment yet? – String May 4 '15 at 13:11
• When you are using the lowercase 'a' and 'b' in this context, I'm assuming you are using the 'a' and 'b' angles in my diagram. I understand how you got the 'b' and the right angle, but isn't the angle below the right angle: "180 - 90 - a". Thats because the sum of the interior angles adds up to 180. A right angle triangle can be formed through: A, B and the intersection of line $y_2$ and $x_1$ – Yahya Uddin May 4 '15 at 13:56
• @YahyaUddin: Probably because the fraction with bulletspeed and enemyspeed should be upside down from what I first wrote. I am simply committing every possible mistake here. Let me know when you find the next one! – String May 4 '15 at 17:49