4
votes

I am an experienced iPhone dev, but new to Cocos2D and Box2D, and I fully admit I need a physics refresher. What I am looking to build is a preview trajectory (an arc indicating a projectiles flight path based on input variables - similar to what is seen in many Angry Birds style games).

I would like to do something a bit different though, in which I set the start point (projectiles start location on screen) and end point (target location). What changes though would be the height of the parabola based on a users input. So in this sense, I'm not using parabolic path to preview where the target will land, but the angle in which the projectile hits the target (again, the parabola always terminates at the target).

I have not seen anything online that quite meets my needs. Most of the code available has more to do with showing the user where the projectile will land. I would like to determine the angle (vector), and impulse to apply to a body to match different parabolic amplitudes, but always end at the same location. A picture is worth a thousand words, so I have linked to a home made pic to try and help explain (SO wont let me embed the image).

Example image

Any help would be appreciated.

2

2 Answers

1
votes

Let's represent this in mathematical model... Angle of parabola's direction is input variable, parabola crosses x-axis in 0 and target point, height will be calculated.

Parabola will look so: y = x*(a-x)*b, where a defines distance to target point, b is some value that (with a) affects start angle and height. We can calculate angle through atan (x/y) at origin center. And we know that tangent represents derivative of parabola. Derivative is y' = a*b - 2*x*b, and in origin center y=0 and x=0, and we get [derivative in origin center] = a*b. a is predefined as distance to target point, so angle is affected by varying b: angle = atan(a*b).

At this point we have parabola equation y = x*(a-x)*b, predefined a=[distance to target point]*4 and angle equation angle=atan(a*b), where b is input value. For example:

  • distance to point is 10 -> a=40
  • user inputs 45* -> tan(45*) = 1, b = tan(45*)/a -> b=1/40
  • place these values into the equation y = x*(a-x)*b: y = x*(40-x)/40 and check...

As for maximum height, it's calculated from equation a*x^2+b*x+c = 0 with condition that х=-b/2а. I think, it should not be hard to calculate by yourself :)

0
votes

Try checking Bezier curves. You can define the path you need using them. You can use them with or without cocos 2d.