0
\$\begingroup\$

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.

*********** Update - 16/08/15 *****************

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.

let Pi = CGFloat(M_PI)
let degreesToRadians = CGFloat(Pi / 180)
let radiansToDegrees = CGFloat(180 / Pi)

    func d2R(angle: CGFloat) -> CGFloat {
        return angle * degreesToRadians
    }
    
    func updateSpeed() {
        // t = (2*V*Sin(a))/g -> 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
    }

Now preSolve collision will be

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

But my result is not matching with what I expect.

Guys please answer or at lest hint as I am exhausted with this and unable to find any proper solution :(.

\$\endgroup\$
2
  • \$\begingroup\$ I am using "v0 = x/t - ½at" and get velocity (80, 279) and distance 148.8 in 1.8667 seconds. But the result remains same, consecutively distance gets reduced. :( \$\endgroup\$ Commented Aug 13, 2015 at 18:50
  • \$\begingroup\$ Now, lets remove X velocity and use scroll speed of 80 in update method. So, each frame X will be updated by 80 pixels * delta time. But still result remains same. Sometimes, distance gets 147.454556... or sometimes it gets 149.0023045. :( \$\endgroup\$ Commented Aug 13, 2015 at 18:50

0

You must log in to answer this question.

Browse other questions tagged .