"Find angle required to hit coordinate (x, y)"?

Started by
9 comments, last by SirMt 5 years, 5 months ago

While I was searching the topic "find the angle required to shoot a projectile towards a known position with a predetermined initial velocity", I came across this equation:

c14d095c7d480ca398e25c01a53ed7be.png(https://en.wikipedia.org/wiki/Projectile_motion#Angle_required_to_hit_coordinate_.28x.2Cy.29)
I'd like to try to understand this equation a bit before put it in code.

Does this equation has a name that I can Google? I poked at some Physics videos on Khan Academy with no luck

 

-------EDIT: Conclusion-------

Good resources on trying to understand this formula: 

https://blog.forrestthewoods.com/solving-ballistic-trajectories-b0165523348c
 

However, if you have a lot of trouble following the above tutorials, because you didn't study math and not a real programmer like me, then my advice is to just accept the formula and don't question it too much...
The formula translates into something like this: Angle = atan(((v*v) + sqrt(v*v*v*v) – g*(g*x*x+2*y*v*v))) / (g*x) so just sub in the values and get the result you need

yeah I was trying to brush up on HS maths and took a lot of notes, have a look if you're a hardstuck noob like me and really need some help
 

 

 

 

image.png

Advertisement

Something like this?  https://stackoverflow.com/questions/31064234/find-the-angle-between-two-vectors-from-an-arbitrary-origin

Please say you're using vectors as that is what they're really for :)

"Those who would give up essential liberty to purchase a little temporary safety deserve neither liberty nor safety." --Benjamin Franklin

They way I normally do this is by first figuring out at what time the impact will happen. If we had no gravity, the position of the projectile would satisfy

x^2+y^2 = v^2t^2

Since we have gravity, we have to modify this a bit:

x^2+(y+(1/2)gt^2)^2 = v^2t^2

That is a quadratic equation in t^2. Solve it, and if there are any positive solutions, compute the corresponding value for t. Once you have that, you can figure the angle by looking just at the horizontal coordinate.

x = v*cos(alpha)*t => alpha = arccos(x/(vt))

This seems much more manageable to me.

 

4 hours ago, alvaro said:

 

Hey Alvaro, can you help me to understand the "(1/2)gt^2)^2" part in the second formula? So I understand that this is using pythagoras theorem and it is trying to account for the gravity, but how does it work, where does the (1/2)gt^2 comes from? Thank you!

Nvm nvm, I'll watch some videos on Quadratic formula first before I bother you again. 

Just now, SirMt said:

Hey Alvaro, can you help me to understand the "(1/2)gt^2)^2" part in the second formula? So I understand that this is using pythagoras theorem and it is trying to account for the gravity, but how does it work, where does the (1/2)gt^2 comes from? Thank you!

Two ways to think about it:

(1) Imagine the points of space that can be reached in time t, first with no gravity. It's simply a circle of radius v*t. Imagine that circle being animated over time. If you now introduce gravity, you can think of it as acting on the center of the circle, bringing it down as it grows. The formula (1/2)gt^2 is just the position under constant acceleration.

(2) If you use a frame of reference in free fall, there is no gravity. Using that frame of reference corresponds to the change of variable y' = y + (1/2)gt^2, and in these (x,y') coordinates you are back to the situation without gravity.

 

In"(1/2)gt^2)^2" where does "1/2" and "^2" come from?

This is something you normally learn in school. Look up "constant acceleration" on the web.

The formula you wrote describes the velocity under constant acceleration. The "1/2" and the "^2" come from integrating that formula to get the position under constant acceleration (the integral of x from 0 to t is t^2/2).

Acceleration --(integrate)--> Velocity --(integrate)--> Position

Acceleration <--(differentiate)-- Velocity <--(differentiate)-- Position

If you know nothing about calculus, ignore everything in this post except the first line. Or go learn some calculus. :)

 

Hey Alvaro, I read up on freefall and now understand the second equation.

So... I went back and tried to solve the problem from the very beginning, one step at a time, and I'm pretty much stuck on step 1 right off the bat... :L (see image)

Even when assuming there is no gravity, I don't know y and t. Oh God... 

question.jpg

First of all, use coordinates where (x1,y1) = (0,0): Just define (x,y):=(x2-x1,y2-y1) and you are now shooting from the origin.

Even if the "time for arrow to fly" doesn't matter, figuring it out is the easiest way to solve the problem. Imagine that the hit happens at time t. At that time, you know that

x^2 + (y+5*t^2)^2 = 400*t^2

Expanding that you get

25*t^2 + (10*y-400)*t^2 + x^2 + y^2 = 0

Plug in the values of x and y, and you get a quadratic equation where the variable is t^2. Solve it. In general there will be two solutions. Pick one of them. Take the square root to get the time t (the one you didn't care about). Now you know that the movement in the horizontal direction is linear, so x = 20 * cos(alpha) * t, and from there you can easily compute cos(alpha). Finally take the arccos and you have solved your problem.

Try to do this step by step and let me know where you got stuck.

Oh, and please don't edit your posts, or this conversation will be ruined for anyone that might encounter it in the future.

 

Hey Alvaro thank you for all the help!The maths is way over my head so I decided to have a go at just implementing the code -_-" this is in Unity C#:


    void SetLaunchAngle(float gravity, float velocity)
    {
        /* Based on this formula: 
         Angle = atan(((v*v) +- sqrt((v*v*v*v) – g*(g*x*x+2*y*v*v)) / (g*x))     
         */

        float x = mouseWorldPos.x - myPos.x;
        float y = mouseWorldPos.y - myPos.y; 
        float v = velocity;
        float g = gravity;

        float root = v*v*v*v - g * (g* (x * x) + 2 * y * (v* v));
        root = Mathf.Sqrt(root);

        float angle = Mathf.Atan2(v*v - root, g*x) * Mathf.Rad2Deg;

        PointerPivot.eulerAngles = new Vector3(0, 0, angle);
    }
 

It's working alright

This topic is closed to new replies.

Advertisement