# Finding an angle, probably with trigonometry?

### #1

Posted 02 January 2012 - 04:27 PM

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?

### #2

Posted 02 January 2012 - 04:56 PM

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.

Previously known as Soulsnatcher | I'm on Twitter

Survivor | Space Survivor | Survivor Reborn | Fatal Tournament | Blob Balls

Letterboxes | Off The Rails! | Fatal Tournament 2: World Tour | List of my Jam Games

### #3

Posted 02 January 2012 - 09:16 PM

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.

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).

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

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 V

^{2}/g appears in a few places.

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:

So, now we have this:

Much simpler, but first note that there's technically no (real) solution for y' > sin

^{2}θ. Keep this in mind.

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).

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:

So now we get:

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

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:

From the Pythagorean theorem we see that:

And some basic trig gets us:

So, that finally brings us to:

And, solving for θ:

### #4

Posted 02 January 2012 - 09:49 PM

http://dl.dropbox.co..._velocidade.gmk

Or:

sen2a=g*dist/sqr(spd);

if abs(sen2a)>1 exit;

ang=radtodeg(arcsin(sen2a))/2;

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.**

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

### #5

Posted 03 January 2012 - 12:21 AM

_{x}

^{2}+ v

_{y}

^{2}= V

^{2}for the initial velocity (v

_{x}, v

_{y}). There you'd have:

Δx = v

_{x}t, and v

_{y}= ±√(V

^{2}- v

_{x}

^{2}), therefore v

_{y}= ±√(V

^{2}- (Δx/t)

^{2}), (which looks pretty relativistic, not fully a coincidence); giving us:

Δy + ½gt

^{2}= v

_{y}t = ±√(V

^{2}- (Δx/t)

^{2}) t, thus

(Δy)

^{2}+ gt

^{2}+ g

^{2}t

^{4}/4 = (V

^{2}- (Δx/t)

^{2}) t

^{2}= V

^{2}t

^{2}- (Δx)

^{2}; now solving the quartic equation of t (note: it's a quadratic equation for t

^{2}):

t = ±2√[ V

^{2}-g ±√( (V

^{2}-g)

^{2}- g

^{2}((Δx)

^{2}+ (Δy)

^{2}) ) ]/ g.

Now simply v

_{x}= Δx / t and v

_{y}= ±√(V

^{2}- v

_{x}

^{2}) (calculate t, use it for v

_{x}and then calculate v

_{y}with v

_{x}). Now the angle in question can be calculated as point_direction(0,0,v

_{x},v

_{y}).

This is all written in my trajectory_set script.

Really? If it seems easier you could even use cosI've even run into college professors that don't know that this equation can be solved

^{2}2x + sin

^{2}2x = 1 to make it an equation for cosine or sine, squaring out the root and solving the quadratic that results.

### #6

Posted 03 January 2012 - 01:03 AM

My example:

http://dl.dropbox.co..._velocidade.gmk

Or:

sen2a=g*dist/sqr(spd);

if abs(sen2a)>1 exit;

ang=radtodeg(arcsin(sen2a))/2;

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.**

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

### #7

Posted 23 February 2012 - 04:01 AM

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²

### #8

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 V_{y}:

V_{y} = -gt + c. (Eq 1)

The vertical component of the initial velocity will be uSin(A), by simple trig...

So when t = 0, V_{y} = uSin(A).

Therefore, plugging those values into (Eq 1), we get:

c = uSin(A)..

So the general equation for V_{y} is:

V_{y} = -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 = (-gt^{2})/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 = (-gt^{2})/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 = (-gt^{2})/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 = 2u^{2}Sin(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 = u^{2}Sin(2A)/g.

So, Sin(2A) = xg/u^{2}.

And, 2A = Sin^{-1}(xg/u^{2}).

Therefore:

A = Sin^{-1}(xg/u^{2})/2.

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

### #9

Posted 17 April 2012 - 09:30 PM

g=your gravity for the projectile

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

degtorad((.5(arcsin(g*R)/(V^2))))

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

90-(degtorad((.5(arcsin(g*R)/(V^2))))

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

180-(degtorad((.5(arcsin(g*R)/(V^2)))).

but if you do this, you will want to make sure your sprites are situated correctly.

**Edited by dmahurdle, 17 April 2012 - 09:32 PM.**

#### 0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users