Finding an angle, probably with trigonometry?
Posted 02 January 2012 - 04:41 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?
Follow me on Twitter!
Posted 02 January 2012 - 05:10 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
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
Posted 02 January 2012 - 09:30 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 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:
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 θ:
Posted 02 January 2012 - 10:03 PM
if abs(sen2a)>1 exit;
Edited by peter henry, 02 January 2012 - 10:08 PM.
Please change my nick to Pedro Henrique. It's my name.
Posted 03 January 2012 - 12:35 AM
Δ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.
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.
I've even run into college professors that don't know that this equation can be solved
Posted 03 January 2012 - 01:17 AM
if abs(sen2a)>1 exit;
In this other example, the vspeed is calculed by hspeed. The target can be mobile.
Edited by peter henry, 03 January 2012 - 01:18 AM.
Please change my nick to Pedro Henrique. It's my name.
Posted 23 February 2012 - 04:15 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²
Posted 16 April 2012 - 06:18 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.
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).
A = Sin-1(xg/u2)/2.
Edited by ga05as, 16 April 2012 - 06:23 PM.
Posted 17 April 2012 - 09:44 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
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
but if you do this, you will want to make sure your sprites are situated correctly.
Edited by dmahurdle, 17 April 2012 - 09:46 PM.
0 user(s) are reading this topic
0 members, 0 guests, 0 anonymous users