Skip to main content
1 of 5

How velocity impacts on CCSprite in Cocos2d Chipmunk?

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?

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: https://www.dropbox.com/s/n46vak6qy5wu4x4/jumpyBall_15_jul_152.gif?dl=0

I am using Sprite Builder and I made this demo with gravity = (0, -300). The distance between pipes is 149. So, when the demo starts, my birdy falls onto the platform and the following method is executed on collision of birdy and platform.

Note

distanceObst = 149

__player = birdy

dummyPos = the starting X position of very first pipe

hero = __player

platform = pipe

func ccPhysicsCollisionPreSolve(pair: CCPhysicsCollisionPair!, hero: Player!, platform: Obstacle!) -> Bool {
        if (dummyPos < 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("--- Velocity \(hero.physicsBody.velocity.y) and distance \(ccpSub(hero.position, ccp(dummyPos, 0)))")
        dummyPos = hero.position.x
        
        return true
    }

First check the console for this code execution

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)

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: https://www.dropbox.com/s/v07lfah35iojitz/jumpyBall_15_jul_153.gif?dl=0

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.

If you want to check my update method,

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)
}

Here in update, I just follow the '__player' so it can stay on screen.

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.

Is there any formula so I can calculate velocity and set it to travel 149 distance exactly?

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 velocity (80, 283) and gravity (0, -300)?

These answers will be tremendous help to me. Thanks guys.