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