title: Projectile Motion in Cocos2d iphone
body: <p>I want to throw a ball that has a projectile motion. I have a monkey on centre of screen and onTouchBegin I am taking the starting point of the touch and onTouchEnded I am taking the ending points. From the starting and ending points I am taking the angle value between them. Like 30 degrees, 45 or 90 degree.
  This is my code by which I have calculated angle of start to endpoint</p>
  
  <pre><code>    float angleRadians = atan2(startTouchPoint.x - touchPoint.x, startTouchPoint.y -    touchPoint.y);
      float angleDegrees = CC_RADIANS_TO_DEGREES(angleRadians);
      float cocosAngle = -1 * angleDegrees;
  </code></pre>
  
  <p>Now i am using Projectile motion formula to throw ball with angle i have calculated from above formula .</p>
  
  <p><strong>inside init method</strong></p>
  
  <pre><code>        gravity = 9.8; // metres per second square
          X = 0;
          Y = 0;
          V0 = 50; // meters per second -- elevation
          VX0 = V0 * cos(angle); // meters per second
          VY0 = V0 * sin(angle); // meters per second
          gameTime = 0;
  </code></pre>
  
  <p><strong>and onTouchEnded i have called fire method which will throw ball</strong> .</p>
  
  <pre><code>-(void)fire:(ccTime) dt
  {
  
      CCLOG(@"Angle 1: %.2f",angle);
      gameTime += dt*6;
  
      // x = v0 * t * cos(angle)
      X = (V0 * gameTime * cos(angle))/2+120;
  
      // y = v0 * t * sin(angle) - 0.5 * g * t^2
      Y = (V0 * gameTime * sin(angle) - 0.5 * gravity * pow(gameTime, 2))/2+255;
  
      if (Y &gt; 50)
  
      {
          sprite_webfire.position = ccp(X,Y);
          flag = true;
      }
  
      else
      {
  
          //angleValue += 15;
          angleValue = angle;
          angle = [self DegreesToRadians:angleValue];
          gravity = 9.8; // metres per second square
          X = 0;
          Y = 0;
          V0 = 50; // meters per second -- elevation
          VX0 = V0 * cos(angle); // meters per second
          VY0 = V0 * sin(angle); // meters per second
          gameTime = 0;
  
          // [self pauseSchedulerAndActions];
      }
  
      if (Y &lt; 50)
  
      {
          [self unschedule:@selector(fire:)];        
      }
  
      NSLog(@"ball (%lf,%lf), dt = %lf angle value %d", X, Y, dt,angleValue);
  }
  </code></pre>
  
  <p>this code is working . by this code i can throw ball in projectile motion but i cant throw it where i want to. i cant throw wrt to given angle from start to end point.</p>
  
  <p>i can throw it like red mark but i want to throw it blue mark with swipe . but its not throwing like i am swiping screen.</p>
  
  <p><img src="https://i.stack.imgur.com/l1rls.png" alt="enter image description here"></p>