How to create a preview path in cocos2d / box2d when the start and end points are known, but the height is variable?

I am an experienced iPhone developer, but new to Cocos2D and Box2D, and I fully admit that I need to update physics. What I'm going to build is a preliminary trajectory (an arc indicating the trajectory of the projectiles based on the input variables), similar to what is seen in many Angry Birds style games).

I would like to do something a little different, though, in which I set the starting point (the beginning of the launch of shells on the screen) and the ending point (target location). What changes would be the height of the parabola based on user input. Thus, in this sense, I do not use the parabolic path to preview where the target will land, but the angle at which the projectile hits the target (again, the parabola always ends at the target).

I did not see anything on the Internet, which is quite consistent with my needs. Most of the available code is more related to showing the user in which the projectile will fly. I would like to determine the angle (vector) and momentum applied to the body in order to correspond to different parabolic amplitudes, but always end in the same place. A picture is worth a thousand words, so I contacted my home pic to try to help explain (SO won't let me embed the image).

Image example

Any help would be appreciated.

+4
source share
2 answers

Let’s denote this in the mathematical model ... Angle direction of the parabola is an input variable, the parabola crosses the x axis at 0 and the target point , height will be calculated.

The parabola will look like this: y = x*(ax)*b , where a determines the distance to the target point, b is a certain value that (with a ) affects the initial angle and height. We can calculate the angle through atan (x/y) in the center of the origin. And we know that a tangent is a derivative of a parabola. The derivative y' = a*b - 2*x*b , and in the center of the beginning y=0 and x=0 , and we get [derivative in origin center] = a*b . a predetermined as the distance to the target point, so Angle affected by the change in b : angle = atan(a*b) .

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

  • Distance to point 10 β†’ a=40
  • user inputs 45 * β†’ tan (45 *) = 1, b = tan(45*)/a β†’ b=1/40
  • put these values ​​in the equation y = x*(ax)*b : y = x*(40-x)/40 and check ...

As for the maximum height, it is calculated from the equation a*x^2+b*x+c = 0 with the condition that =-b/2 . I think it should not be difficult to calculate on your own :)

+1
source

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

0
source

All Articles