title: How velocity impacts on CCSprite in Cocos2d Chipmunk?
body: <p>I am in need of the basic understanding of how velocity changing works? i.e. how CCSprite is able to jump at particular height when some velocity is set?</p>
  <p>Let me give you an example on which I am working currently. I am preparing a demo like flappy bird, in which I wanted my bird to land on vertical pipes. So, it is like, my birdy jumps from the pipe to another pipe :smile:. Check the gif I place here:  <a href="https://www.dropbox.com/s/n46vak6qy5wu4x4/jumpyBall_15_jul_152.gif?dl=0" rel="nofollow noreferrer">https://www.dropbox.com/s/n46vak6qy5wu4x4/jumpyBall_15_jul_152.gif?dl=0</a></p>
  <p>I am using Sprite Builder and I made this demo with <strong>gravity = (0, -300)</strong>. The <em>distance between pipes is 149</em>. So, when the demo starts, my birdy falls onto the platform and the following method is executed on collision of birdy and platform.</p>
  <blockquote>
  <p>Note</p>
  <p>distanceObst = 149</p>
  <p>__player = birdy</p>
  <p>dummyPos = the starting X position of very first pipe</p>
  <p>hero = __player</p>
  <p>platform = pipe</p>
  </blockquote>
  <pre><code>func ccPhysicsCollisionPreSolve(pair: CCPhysicsCollisionPair!, hero: Player!, platform: Obstacle!) -&gt; Bool {
          if (dummyPos &lt; 120) {    // This is to confirm that velocity X is set only once in lifetime.
              __player.physicsBody.velocity.x = 80
          }
          hero.physicsBody.velocity.y = 283
          
          NSLog(&quot;--- Velocity \(hero.physicsBody.velocity.y) and distance \(ccpSub(hero.position, ccp(dummyPos, 0)))&quot;)
          dummyPos = hero.position.x
          
          return true
      }
  </code></pre>
  <blockquote>
  <p>First check the console for this code execution</p>
  </blockquote>
  <pre><code>2015-08-09 02:34:05.042 tsb[15327:94829] cocos2d: surface size: 1136x640
  2015-08-09 02:34:05.750 tsb[15327:94829] --- Velocity 283.0 and distance (135.0,153.25)
  2015-08-09 02:34:07.617 tsb[15327:94829] --- Velocity 283.0 and distance (149.334320068359,154.448120117188)
  2015-08-09 02:34:09.483 tsb[15327:94829] --- Velocity 283.0 and distance (149.333770751953,155.526428222656)
  2015-08-09 02:34:11.366 tsb[15327:94829] --- Velocity 283.0 and distance (150.664428710938,151.796905517578)
  2015-08-09 02:34:13.233 tsb[15327:94829] --- Velocity 283.0 and distance (149.331237792969,153.140319824219)
  2015-08-09 02:34:15.100 tsb[15327:94829] --- Velocity 283.0 and distance (149.331237792969,154.349395751953)
  2015-08-09 02:34:16.967 tsb[15327:94829] --- Velocity 283.0 and distance (149.33642578125,155.437561035156)
  2015-08-09 02:34:18.850 tsb[15327:94829] --- Velocity 283.0 and distance (150.671020507812,151.716918945312)
  </code></pre>
  <p>It's like when I start bouncing the birdy it bounces properly from centre of the pipe but when it continues bouncing after few seconds the birdy gets displaced and comes to the left/right edge of the pipe and sometimes fall off to the ground :open_mouth:. Check this GIF: <a href="https://www.dropbox.com/s/v07lfah35iojitz/jumpyBall_15_jul_153.gif?dl=0" rel="nofollow noreferrer">https://www.dropbox.com/s/v07lfah35iojitz/jumpyBall_15_jul_153.gif?dl=0</a></p>
  <p>I observed there are changes in distance values in X parameter which is visible in console. I think this might be the cause but the value here is calculated and don't know why it does not come to same value, like, 149.</p>
  <p>If you want to check my <strong>update</strong> method,</p>
  <pre><code>override func update(delta: CCTime) {
          __player.position = ccp(__player.position.x /*+ (scrollSpeed * CGFloat(delta))*/, __player.position.y)
          __physicsWorld.position = ccp(firstObstPos - __player.position.x, __physicsWorld.position.y)
  }
  </code></pre>
  <p>Here in update, I just follow the <em>'__player'</em> so it can stay on screen.</p>
  <p>Can someone give me proper guidance of what should I do because the values I took here are constants and if I want to increase speed, I increase X velocity or might decrease Y velocity which will affect distance between pipes as well.</p>
  <p>Is there any formula so I can calculate velocity and set it to travel 149 distance exactly?</p>
  <p>I googled a lot and found some velocity equations but unable to find out how they can be applied in Cocos2d chipmunk. Can anyone tell me how Chipmunk decides height, duration and distance of bounce from simple <strong>velocity (80, 283) and gravity (0, -300)</strong>?</p>
  <p>These answers will be tremendous help to me. Thanks guys.</p>
  <p>*********** <strong>Update - 16/08/15</strong> *****************</p>
  <p>I have modified my code and now rather than taking fix values I use some formula to calculate velocity, time, height and distance. This way I get perfect answers on what I want.</p>
  <pre><code>let Pi = CGFloat(M_PI)
  let degreesToRadians = CGFloat(Pi / 180)
  let radiansToDegrees = CGFloat(180 / Pi)
  
      func d2R(angle: CGFloat) -&gt; CGFloat {
          return angle * degreesToRadians
      }
      
      func updateSpeed() {
          // t = (2*V*Sin(a))/g -&gt; V = (t*g)/(2*sin(a))
          // h = ((V*V)/2g)*Sin^2(a))
          // D = ((V*V)/g)*sin(2*a)
          
          let angle = d2R(cAngle)
          velocity_y = (delta_t * gravity)/(2*sin(angle))
          maxJumpHeight = ((velocity_y*velocity_y)/(2*gravity))*(sin(angle)*sin(angle))
          //distanceObst = ((velocity_y*velocity_y)/gravity)*sin(2*angle)
          
          // Distance in terms of speed rather than velocity
          distanceObst = delta_t*scrollSpeed
          
          var a = 10      // Debug line
      }
  </code></pre>
  <p>Now preSolve collision will be</p>
  <pre><code>func ccPhysicsCollisionPreSolve(pair: CCPhysicsCollisionPair!, hero: Player!, platform: Obstacle!) -&gt; Bool {
          let diff = ccpSub(hero.position, ccp(dummyPos, 0))
          let distance = distanceObst + (distanceObst - diff.x)
          NSLog(&quot;--- Velocity \(hero.physicsBody.velocity) and \(hero.position) -&gt; \(dummyPos) distance \(diff) and \(distance)&quot;)
          
          if ((dummyPos &gt; firstObstPos) &amp;&amp; (diff.x &gt; 0)) {
              // TODO: Adjustment for the bounce point when distance travelled differs
              //velocity_y = velocityForDistance(distance)
              //delta_t = ((delta_t*diff.x)/distance)
              //updateSpeed()
          }
          hero.physicsBody.velocity.y = velocity_y
          
          dummyPos = hero.position.x
          
          return false
      }
  </code></pre>
  <p>But my result is not matching with what I expect.</p>
  <p>Guys please answer or at lest hint as I am exhausted with this and unable to find any proper solution :(.</p>