- Home /
Parabolic Movement Equation Problem?
Alrighty I have the following problem. I've been moding in Warcraft3 Map Editor for quite a long time and decided to start unity and here are parabolic equations that I've used in W3 and that worked flawlessly:
When both targets inital Y heigths are 0 (requires that both targets are grounded aka their y coordinate is 0):
float Parabola(float h, float d, float x)
{
return (4 * h / d) * (d - x) * (x / d);
}
And the function that takes the initial heights that are different ( for example y0 = 5 and y1 = 0)
float ParabolaInitial(float y0, float y1, float h, float d, float x)
{
float A = (2 * (y0 + y1) - 4 * h) / (d * d);
float B = (y1 - y0 - A * d * d) / d;
return A * x * x + B * x + y0;
}
where x is the localized coordinate towards target, d is the total distance between targets, h is the max height, and y0,y1 respectively height coordinates of start and end target.
This works but gives me the parabola path that is colored red, while I remember that in W3 i was getting green colored parabola path. Red look great but green is the thing that will totaly look incredible and I'm not sure where I'm making a mistake. Here is the picture: 
Does anyone has any other parabolic movement codes?
Thank you in advance.
EDIT:
Oh I forgot to state that the problem is that my target is moving, since I am using this formula to make some of the missiles for ranged units have an arc.
Answer by Omberone · Jun 30, 2014 at 10:24 AM
Have you had a look at http://en.wikipedia.org/wiki/Trajectory_of_a_projectile ?
You need either
a fixed time it will take for it to hit the ground, or
an initial angle of attack of the projectile, or
a fixed muzzle velocity
to be able to calculate the parabola accurately. As you say there are two different (four actually, but we won't bother with the two going below the ground) solutions to the equation, one that takes less time, and one that takes the longer time - with a higher arc. You seem like you know your way around math, so that wiki article should come in handy =) Should you find it difficult to implement it, come back here and let us know.
Thank you for answer Omberone.
Problem with my parabola is following:
I don't have fixed distance, just starting distance since when unit attack the projectile is throwed and it should follow the target aka its path is dynamically changed based on targeted unit. I also don't have the predefined time at which projectile should reach target, only projectiles speed (aka distance traveled per time interval). There are two problems I see already and thats following: somehow my parabola is caluclated on hypotenuse of a triangle which is y0 y1 O (where O being the vertex bellow y0). I did in calculation drop the y0=0 and then got the distance so i get the distance between origin points not the absolute distance between mass centres of object (them being y0 and y1).
Another problem is that parabola keeps descending aka since i pass fixed distance when it reaches zero of function and target is still not reached it continues to go bellow the ground. I have fixed this problem with simple if statment condition.
I've found another formula using the angle at which you shoot projectile so I'll try using it and see what happens, but by now i get it that for full effect i need some kind of formula that dynamically calculates the height of parabola while the target moves.
Anyway thanks again, ill update if i find out something.
Could you perhaps explain what it is you mean by "dynamically calculate the height"? You should be able to get the x,z coordinate from a target at any point in time. Do you mean that you want to do a movement prediction?
Yeah I apologize, english is not my native language so I tend to be incomprehensible sometimes.
Yeah I know I can calcualte x and z at any given time problem is when if parabola is in XY coordinate system, what if x is changing over time which represents the unit moving, thats my main problem. $$anonymous$$y solution is to make it calcualte height based on the start distance then if target is not reached it just travels at that last height. Didn't test this knew formula I am currently busy with something else, as soon as I check it I'll post answer. Thanks anyway
maybe you have seen these, but any help?
http://answers.unity3d.com/questions/377334/how-to-move-object-parabolic-projectile-path.html
Well I've seen them, but they are physics based solutions, and I need hard coding solution like the top 2 equations. Not to be mislead, both equations work, I get parabolas in both cases, they are just "positioned" wrong, I need to see how can I get around that.
Your answer