Skip to main content
Commonmark migration
Source Link

Note

 

distanceObst = 149

 

__player = birdy

 

dummyPos = the starting X position of very first pipe

 

hero = __player

 

platform = pipe

Note

 

distanceObst = 149

 

__player = birdy

 

dummyPos = the starting X position of very first pipe

 

hero = __player

 

platform = pipe

Note

distanceObst = 149

__player = birdy

dummyPos = the starting X position of very first pipe

hero = __player

platform = pipe

added physics calculations
Source Link

*********** 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 :(.

*********** 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 :(.

Rollback to Revision 1
Source Link
Vaillancourt
  • 16.4k
  • 17
  • 55
  • 61

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: First example Ihttps://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.

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

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: First example 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.

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: second example

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.

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

Integrated the images in the question.
Source Link
Vaillancourt
  • 16.4k
  • 17
  • 55
  • 61
Loading
Source Link
Loading