# Finding an angle, probably with trigonometry?

8 replies to this topic

### #1 frolacosta

frolacosta

GMC Member

• GMC Member
• 154 posts
• Version:GM:Studio

Posted 02 January 2012 - 04:27 PM

I am absolutely stumped on this one, even my brother who is amazing at maths couldn't figure it out, but he did tell me what is needed and that it would probably use trigonometry.

I need a formula to find x depending on the distance of the player from the gun.
X is the angle at which the guns turret is pointed.

The projectile will then arc and land on its target.

Does anyone have the knowledge or the formula to do this?
• 0

@Frolacosta

### #2 TerraFriedSheep

TerraFriedSheep

GMC Member

• GMC Member
• 3234 posts
• Version:GM:Studio

Posted 02 January 2012 - 04:56 PM

You have to break this problem down into two components: the projectiles vertical and horizontal motion. Then using Newton's equations of motion you can deduce the time the projectile will be in the air when fired with an initial velocity of 15.

Considering the vertical motion, the projectiles displacement is 0 (from being fired to hitting, assume these heights are equal).

Therefore using the equation s=ut+0.5at^2 (where s is displacement, u is initial velocity, t is the time and a is the value of your gravity), we can work out the time. Note because gravity is downwards, it will be:

0 = 15*Sin(X) * t - 0.5*a*t^2

This gives the time the projectile is in the air to be:

t = 30*Sin(X) / a

Now we know how long the projectile will be in the air, we can apply this to the horizontal component of the motion.

For horizontal motion, the displacement is d, which should be a known value for you, and there is no acceleration in the horizontal direction:

Since a=0 for horizontal, the equation becomes:

d = 15*Cos(X) * t

Sub in t from the vertical motion to get:

d = 15*Cos(X) * 30*Sin(X) / a
=> d*a/450=Sin(X)*Cos(X)

This equation now only leaves one unknown value, the angle X. However, it is muddled in some horible trig, which gives a number of solutions, but for now it is the best I can come up with, if anyone can spot a mistake or there is infact an easier way with the unknowns used, do say, but I thought I'd post this as a starting point.
• 0

Previously known as Soulsnatcher | I'm on Twitter

### #3 Yourself

Yourself

The Ultimate Pronoun

• GMC Elder
• 7352 posts
• Version:Unknown

Posted 02 January 2012 - 09:16 PM

The trig isn't too horrible, you just have to remember your trig identities:

$\sin( 2 \theta ) = 2 \sin\theta \cos\theta$

Let's redo this without the numbers, so that we can easily apply this to projectiles of different speeds. Let's also work with displacements that aren't absolutely horizontal so that we can shoot uphill or downhill.

$\Delta y = V t \sin\theta - \frac{g}{2} t^2$

Solving for t yields two solutions, naturally. One solution happens before the projectile has reached its peak and the other one happens after. Keep in mind that if the target is below the cannon, the earlier solution can actually be a negative time, so keep an eye out for that (although ultimately keeping both solutions here won't matter).

$t = \frac{V \sin\theta \pm \sqrt{ V^2 \sin^2\theta - 2 g \Delta y} }{g}$

Now, let's go ahead and plug this into the horizontal component:

$\Delta x = V t \cos\theta = \frac{V^2}{g} \left( \sin\theta \pm \sqrt{\sin^2\theta - 2\frac{g \Delta y}{V^2} } \right) \cos\theta$

So now the real tricky part is isolating that sine function, let's see what we can do about that. First I'm going to introduce another variable to save myself some typing, since that V2/g appears in a few places.

$\eta = \frac{V^2}{g}$

$\frac{\Delta x}{\eta} = \left( \sin\theta \pm \sqrt{\sin^2\theta - 2 \frac{y}{\eta}} \right) \cos\theta$

So that actually acts as a normalization parameter for all the distances in this problem, allowing us to work in non-dimensional units. That's convenient, so let's do a change of coordinates to save myself some more typing:

$x' = \frac{\Delta x}{\eta}$
$y' = \frac{\Delta y}{\eta}$

So, now we have this:

$x' = \left( \sin\theta \pm \sqrt{\sin^2\theta - 2 y'} \right) \cos\theta$

Much simpler, but first note that there's technically no (real) solution for y' > sin2θ. Keep this in mind.

$\left( x' - \sin\theta \cos\theta \right)^2 = \left( \sin^2\theta - 2 y' \right) \cos^2\theta$

$x'^2 - 2 x' \sin\theta \cos\theta + \sin^2\theta \cos^2\theta = \sin^2\theta \cos^2\theta - 2 y' \cos^2\theta$

Fortunately those quadratic trig terms drop out, otherwise this would be a lot hairier (it'd end up requiring the solution of a quartic equation and usually in that case it's quicker to get a numerical solution than it is to evaluate the analytical one).

$x'^2 - 2 x' \sin\theta\cos\theta = -2 y' \cos^2\theta$

Up until now we've just been dealing with algebra, this part is where the trig actually comes in. We need a few trig identities:

$2\sin\theta \cos\theta = \sin 2\theta$

$\cos^2\theta = \frac{1 + \cos 2\theta}{2}$

So now we get:

$x'^2 - x' \sin 2\theta = -y' \left(1 + \cos 2\theta \right)$

So let's juggle some things around to solve this.

$x' \sin 2\theta - y' \cos 2\theta = y' + x'^2$

Alright, now there's a little trig trick we can do here that they don't usually teach and I really think they ought to. I've even run into college professors that don't know that this equation can be solved. In fact, the only reason I saw the method for solving this is because of a class that showed how to transform an expression of the form A sinθ + B cosθ into C cos(θ + φ) (I think it was actually an electronics class). It helps to draw a diagram in this case:

Hopefully it's clear from the diagram that the following is true:

$z' \sin\left(2\theta - \phi\right) = x' \sin 2\theta - y' \cos 2\theta$

From the Pythagorean theorem we see that:

$z' = \sqrt{x'^2 + y'^2}$

And some basic trig gets us:

$\phi = \tan^{-1}\left(\frac{y'}{x'}\right)$

So, that finally brings us to:

$\sqrt{x'^2 + y'^2} \sin\left(2\theta - \tan^{-1}\left(\frac{y'}{x'}\right)\right) = y' + x'^2$

And, solving for θ:

$\theta = \frac{1}{2}\left(\sin^{-1}\left(\frac{y' + x'^2}{\sqrt{x'^2 + y'^2}}\right) + \tan^{-1}\left(\frac{y'}{x'}\right)\right)$
• 4

### #4 peter henry

peter henry

GMC Member

• GMC Member
• 80 posts
• Version:GM8

Posted 02 January 2012 - 09:49 PM

My example:

Or:

sen2a=g*dist/sqr(spd);
if abs(sen2a)>1 exit;

obj=instance_create(x,y,obj_projectile);
obj.gravity=1;
obj.direction=ang;
obj.speed=spd;

Edited by peter henry, 02 January 2012 - 09:54 PM.

• 0

Please change my nick to Pedro Henrique. It's my name.

### #5 Tepi

Tepi

GMC Member

• Global Moderators
• 4238 posts
• Version:GM:Studio

Posted 03 January 2012 - 12:21 AM

You can avoid trigonometry by using vx2 + vy2 = V2 for the initial velocity (vx, vy). There you'd have:
Δx = vx t, and vy = ±√(V2 - vx2), therefore vy = ±√(V2 - (Δx/t)2), (which looks pretty relativistic, not fully a coincidence); giving us:
Δy + ½gt2 = vy t = ±√(V2 - (Δx/t)2) t, thus
(Δy)2 + gt2 + g2t4/4 = (V2 - (Δx/t)2) t2 = V2t2 - (Δx)2; now solving the quartic equation of t (note: it's a quadratic equation for t2):
t = ±2√[ V2-g ±√( (V2-g)2 - g2((Δx)2 + (Δy)2) ) ]/ g.
Now simply vx = Δx / t and vy = ±√(V2 - vx2) (calculate t, use it for vx and then calculate vy with vx). Now the angle in question can be calculated as point_direction(0,0,vx,vy).

This is all written in my trajectory_set script.

I've even run into college professors that don't know that this equation can be solved

Really? If it seems easier you could even use cos2 2x + sin2 2x = 1 to make it an equation for cosine or sine, squaring out the root and solving the quadratic that results.
• 0

----------------

Tepi's mathematical problems will be reuploaded on demand.

### #6 peter henry

peter henry

GMC Member

• GMC Member
• 80 posts
• Version:GM8

Posted 03 January 2012 - 01:03 AM

My example:

Or:

sen2a=g*dist/sqr(spd);
if abs(sen2a)>1 exit;

obj=instance_create(x,y,obj_projectile);
obj.gravity=1;
obj.direction=ang;
obj.speed=spd;

In this other example, the vspeed is calculed by hspeed. The target can be mobile.
http://dl.dropbox.co...speed_movel.gmk

Other examples:
http://gmc.yoyogames...howtopic=524518

Edited by peter henry, 03 January 2012 - 01:04 AM.

• 0

Please change my nick to Pedro Henrique. It's my name.

### #7 rawta

rawta

GMC Member

• GMC Member
• 56 posts

Posted 23 February 2012 - 04:01 AM

An equation I use is d=(sin2x*v²)/g
where d is the horizontal distance it travels. x is the angle of the trajectory. V is the velocity. G is gravity which is 9.8 m/s²
• 0

### #8 ga05as

ga05as

GMC Member

• GMC Member
• 881 posts

Posted 16 April 2012 - 06:04 PM

If the ball is projected with a speed of u, making an angle A with the horizontal, in a gravitational field of strength g ....

Then the only force on the ball will be gravity...
This will cause the ball to have an acceleration of g vertically downwards, We will take upwards as positive so the acceleration is -g.

Acceleration is the rate of change of velocity...
So by integrating the acceleration with respect to time, t , we will get a value for the vertical componant of velocity Vy:

Vy = -gt + c. (Eq 1)

The vertical component of the initial velocity will be uSin(A), by simple trig...
So when t = 0, Vy = uSin(A).

Therefore, plugging those values into (Eq 1), we get:
c = uSin(A)..

So the general equation for Vy is:
Vy = -gt + uSin(A). (Eq 2)

Now velocity is the rate of change of displacement...
So by integrating the vertical component of velocity (Eq 2) with respect to time, we will get a value for the vertical displacement y:

y = (-gt2)/2 + utSin(A) + k. (Eq 3)

When t = 0, y = 0, i.e at ground level, so subbing these values into (Eq 3) gives us:
k = 0.

So the general equation for y is:
y = (-gt2)/2 + utSin(A). (Eq 4)

Now we want the to find out how long it will take for the ball to reach the ground, so we set y = 0...
This will give us two values for t, one will be at the initial point of projection (i.e. t = 0) and the other will be the time we want.

So:
0 = (-gt2)/2 + utSin(A).

We can factorize out a t and get:
0 = t( (-gt)/2 + uSin(A) )

This tells us either t = 0 or:
(-gt)/2 + uSin(A) = 0

Solving this gives us:
t = 2uSin(A)/g. (Eq 5)

Now we know how long it will take for the ball to reach the floor again...

Because we know there is no forces acting horizontally, the ball will move with a constant horizontal component of velocity,
uCos(A)... (simple trig again)
This means that we can use speed = distance / time, to calculate the horizontal distance that will be traveled, x.

uCos(A) = x/t

So x =
utCos(A) (Eq 6)

We know the time that will have elapsed before the ball hits the floor from (Eq 5)...
So subbing t from (Eq 5) into (Eq 6) gives us:

x = 2u2Sin(A)Cos(A)/g. (Eq 7)

All we have to do now is solve this equation for A, and that is your angle...

and as Sin(2A) = 2Sin(A)Cos(A).

x = u2Sin(2A)/g.
So, Sin(2A) = xg/u2.
And, 2A = Sin-1(xg/u2).

Therefore:

A = Sin-1(xg/u2)/2.

Edited by ga05as, 16 April 2012 - 06:09 PM.

• 0

### #9 dmahurdle

dmahurdle

GMC Member

• New Member
• 3 posts
• Version:GM8

Posted 17 April 2012 - 09:30 PM

Um... Why don't you just use the rage equation from basic physics? Here:

R=the range of the object
V=the speed of the object as it exits the barrel.

(.5(arcsin(g*R)/(V^2))) is the first angle of attack. This will be in radians. To get it in degrees, just type

Now, there are always two options for angles, since you can go above or below 45 degrees. the second one would be

These only work if your are aiming right of course. To get aiming left angle, you'll need to do something like