Using trigonometry to predict future position

643 Views Asked by At

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:

diagram

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

There are 1 best solutions below

9
On BEST 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.