# Getting an angle from a curve and a straight line

• Dec 23rd 2011, 12:01 AM
drak68
Getting an angle from a curve and a straight line
Ok, first things first. I'm a programmer, I'm actually working on a flash game and I'm always having a bad time trying to understand maths.

What I'm trying to create here is an archer autonomously firing on a target using the right angle. The problem is, I can't manage to get that angle. I've tried tons of stuff to no avail so I shall put this into a simple drawing.

Attachment 23128

I have two points, A and B. I have the distance, which is X as well as the curve, that forms a circle, this circle's diameter being Y. Now what I lack is, the angle between the line and the curve at point A (?).

Also, note that I'm not really good at maths, if at all. I may not understand complex terms and such.

Well this is it. Thanks to whoever solves this or tries solving it!

• Dec 23rd 2011, 02:04 AM
CaptainBlack
Re: Getting an angle from a curve and a straight line
Quote:

Originally Posted by drak68
Ok, first things first. I'm a programmer, I'm actually working on a flash game and I'm always having a bad time trying to understand maths.

What I'm trying to create here is an archer autonomously firing on a target using the right angle. The problem is, I can't manage to get that angle. I've tried tons of stuff to no avail so I shall put this into a simple drawing.

Attachment 23128

I have two points, A and B. I have the distance, which is X as well as the curve, that forms a circle, this circle's diameter being Y. Now what I lack is, the angle between the line and the curve at point A (?).

Also, note that I'm not really good at maths, if at all. I may not understand complex terms and such.

Well this is it. Thanks to whoever solves this or tries solving it!

Why are you using a circular arc for the trajectory?

CB
• Dec 23rd 2011, 04:52 AM
HallsofIvy
Re: Getting an angle from a curve and a straight line
Unless you have, for some reason, decided you must use a semi-circular trajectory for your arrow, this is not so much a math problem as a physics problem. Physically, the trajectory, neglecting air resistance, is a parabola.

Neglecting air resistance, the only force on the arrow is gravity, straight downward. That means the arrow has a downward acceleration of -g (g= 9.81 approximately) and 0 horizontal accelration: $\displaystyle a_x=\frac{dv_x}{dt}= 0$ and $\displaystyle a_y= \frac{dv_y}{dt}= -g$ so $\displaystyle v_x= v_0cos(\theta)$ and $\displaystyle v_y= -gt+ v_0sin(\theta)$ where $\displaystyle v_0$ is the initial speed of the arrow and $\displaystyle \theta$ is the angle.

Now, $\displaystyle v_x= \frac{dx}{dt}= v_0cos(\theta)$ and $\displaystyle v_y= \frac{dy}{dt}= -gt+ v_0sin(\theta)$ so $\displaystyle x= v_0cos(\theta)t$ and $\displaystyle y= -\frac{g}{2}t^2+ v_0sin(\theta)t$ where I have taken the initial position of the arrow, the position of the archer, to be (0, 0). If you want other coordinates, add them.

To determine the angle, $\displaystyle \theta$, you need to know two things in advance- the initial velocity of the arrow, $\displaystyle v_0$, and the distance from the archer to the target, X. Solve both $\displaystyle x= v_0cos(\theta)t= X$ and $\displaystyle y= -gt^2+ v_0sin(\theta)t= 0$ for t. That's easy: from the first equation, $\displaystyle t= \frac{X}{v_0cos(\theta)}$ and from the second, $\displaystyle t(-gt+ v_0sin(\theta))= 0$ so either t= 0 (the arrow was fired from height 0 so we get the initial time as a solution) or $\displaystyle t= \frac{v_0sin(\theta)}{g}$. Since you want the arrow to be at (X, 0) for one t, those must be the same: $\displaystyle \frac{X}{v_0cos(\theta)}= \frac{v_0sin(\theta)}{g}$ which is the same as $\displaystyle sin(\theta)cos(\theta)= \frac{gX}{v_0^2}$.

You can simplify that using the trig identity $\displaystyle sin(2\theta)= 2 sin(\theta)cos(\theta)$ so that $\displaystyle sin(\theta)cos(\theta)= \frac{1}{2}sin(2\theta)= \frac{gX}{v_0^2}$. That is, $\displaystyle \theta= \frac{1}{2}arcsin\left(\frac{gX}{2v_0^2}\right)$

Note that since sine cannot be larger than 1, if X is too large or $\displaystyle v_0$ is not large enough, this may be impossible- X is "out of range". Also, it will typically happen that there are two angles, between 0 and 90 degrees, that satisfy that equation.

If you really want to use a circular trajectory, you can do this:
Let r be the radius of the circle (so that Y= 2r). Drawing radii from the center of the circle to the endpoints of X, we have an isosceles triangle with two sides of length r and one of length X. Applying the cosine rule to that triangle, The angle at the center of the circle, $\displaystyle \theta$, satisfies $\displaystyle X^2= 2r^2(1- cos(\theta))$ so that $\displaystyle \theta= arccos(1- \frac{X^2}{2r^2})$. Writing the two "base" angles as $\displaystyle \phi$, we have, of course, $\displaystyle 2\phi+ \theta= 2\pi$ so that $\displaystyle \phi= \pi- \frac{\theta}{2}$. The angle the circle makes with line X is the complement of that: $\displaystyle \frac{\pi}{2}- \phi= \frac{\theta}{2}- \frac{\pi}{2}= \frac{1}{2}arccos(1- \frac{X^2}{2r^2})- \frac{\pi}{2}$.

That is in radians, of course.
• Dec 23rd 2011, 05:02 AM
ILikeSerena
Re: Getting an angle from a curve and a straight line
The (parabola) trajectory is documented in wikipedia:

The angle is documented as:
$\displaystyle \theta = {1\over 2}\arcsin({gX \over v_o^2})$
where $\displaystyle g=9.81 {m \over s^2}$ and $\displaystyle v_o$ is the initial speed.
• Dec 23rd 2011, 08:52 AM
drak68
Re: Getting an angle from a curve and a straight line
I've been using a circle because the arrow DOES makes a circle in the game. There is no gravity applied to it or anything, just a constant rotation and acceleration towards its current angle, which makes circles. I know its clearly not the best way to simulate projectiles but I felt like messing with gravity wasn't really a good idea, or just really time consuming for just a small part of the game.

As for http://latex.codecogs.com/png.latex?...0v_o%5E2%7D%29, it didn't work, probably because I'm not using gravity at all, or flash is just doing wierd stuff when trying to make complex equations.
• Dec 23rd 2011, 09:01 AM
ILikeSerena
Re: Getting an angle from a curve and a straight line
All right.

The angle that you ask for is:
$\displaystyle \theta = 90^o - \arccos({X \over Y})$
• Dec 23rd 2011, 10:24 AM
drak68
Re: Getting an angle from a curve and a straight line
Wow. That actually worked. Thanks a lot I've been having headaches over this for weeks!