how to get trajectory of an object1 when we swipe an object2?
I am writing an game app in iPad using cocos2d. And the game is in landscape mode. It have a sprite gun that shoots, and the sprite is the middle (512,10).
The targets appear along the x-axis. By swiping on the sprite gun I have to generate a trajectory of the bullet according to the angle I have swiped. So, I have initial and final coordinates of touch of gun. And the angle. How can I get开发者_如何学Go the trajectory ?Thank You.
Assuming the ground is flat, no air resistance, and the bullet is fired at coordinates (0, 0), the formula for height as a function of distance travelled along the ground is as follows:
a = launch angle
v = launch speed
x = distance travelled along the ground
y = distance above the ground
g = acceleration due to gravity.
y(x) = (x * tan(a)) - ( ( (g / ( cos(a) * cos(a) ) ) / (2 * v * v) ) * (x * x) )
Check what units your maths/trigonometry library uses for angles (degrees or radians)
So, assuming the bullet is moving in +ve x direction, plot (0, y(0)), (1, y(1)), (2, y(2)) etc. until y(x) is < 0, meaning that the bullet has hit the ground.
(Don't forget to add 512 to x, and 10 to y when plotting, to match the start point at your gun sprite position).
Here endeth the maths lesson. Over to you on the iPad code.
If you want to get really fancy, the Wikipedia Trajectory page is fairly thorough.
精彩评论