Jump to content


Photo

Finding an angle, probably with trigonometry?


  • Please log in to reply
8 replies to this topic

#1 frolacosta

frolacosta

    GMC Member

  • GMC Member
  • 138 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.

Posted Image
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

#2 TerraFriedSheep

TerraFriedSheep

    GMC Member

  • GMC Member
  • 3053 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

#3 Yourself

Yourself

    The Ultimate Pronoun

  • Retired Staff
  • 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:



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 V2/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' > sin2θ. 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:

Posted Image

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

#4 peter henry

peter henry

    GMC Member

  • GMC Member
  • 80 posts
  • Version:GM8

Posted 02 January 2012 - 09:49 PM

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;

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

  • 0

#5 Tepi

Tepi

    GMC Member

  • Global Moderators
  • 4236 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

#6 peter henry

peter henry

    GMC Member

  • GMC Member
  • 80 posts
  • Version:GM8

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.

  • 0

#7 rawta

rawta

    GMC Member

  • GMC Member
  • 55 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
  • 880 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:

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




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users