我需要对速度变化如何运作有基本的了解?即,当某个速度设定时,CCSprite如何能够在特定高度跳跃?
让我举一个我目前正在工作的例子。我正在准备一个像飞鸟一样的演示,我想让我的鸟落在垂直管道上。所以,就像,我的小鸟从管子跳到另一根烟斗:微笑:检查我放在这里的gif:https://www.dropbox.com/s/n46vak6qy5wu4x4/jumpyBall_15_jul_152.gif?dl=0
我正在使用Sprite Builder,我用 gravity =(0,-300)制作了这个演示版。管道之间的距离为149 。因此,当演示开始时,我的小鸟落在平台上,并且在小鸟和平台碰撞时执行以下方法。
请注意
distanceObst = 149
__ player = birdy
dummyPos =第一个管道的起始X位置
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
}
首先检查控制台是否执行此代码
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)
当我开始弹跳小鸟时,它会从管道中心正常弹跳但是当它在几秒钟后继续弹跳时,小鸟会移位并进入管道的左/右边缘,有时会脱落到地面:open_mouth:检查此GIF:https://www.dropbox.com/s/v07lfah35iojitz/jumpyBall_15_jul_153.gif?dl=0
我观察到X参数中的距离值发生了变化,这在控制台中是可见的。我认为这可能是原因,但这里的价值是计算的,并且不知道为什么它没有达到相同的价值,比如,149。
如果您想查看我的更新方法,
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)
}
在更新中,我只需按照&#39; __播放器&#39; 即可停留在屏幕上。
有人可以给我正确的指导我应该怎么做因为我在这里取的值是常数,如果我想提高速度,我会增加X速度或者可能会降低Y速度,这也会影响管道之间的距离。
是否有任何公式,所以我可以计算速度并将其设置为准确地移动149距离?
我google了很多,发现了一些速度方程,但无法找出它们如何应用于Cocos2d花栗鼠。谁能告诉我Chipmunk如何决定从简单的速度(80,283)和重力(0,-300)反弹的高度,持续时间和距离?
这些答案对我很有帮助。谢谢你们。
*********** 更新 - 2015年8月16日 *****************
我修改了我的代码,而不是采用修正值,我使用一些公式来计算速度,时间,高度和距离。这样我就可以得到我想要的完美答案。
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
}
现在preSolve碰撞将是
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
}
但我的结果与我的预期不符。
伙计们请回答或至少提示,因为我已经筋疲力尽,无法找到任何适当的解决方案:(。