Swift: 设置距离的速度
Swift: Set velocity for distance
我正在尝试在 Swift 上制作一个移动平台,为了让玩家与平台一起移动,我需要更改 self.moveByX()
,这更简单, physicsBody?.velocity
(给平台和播放器增加一些摩擦)。
所以,我做了一个这样的函数:
self.runAction(
SKAction.repeatActionForever(
SKAction.sequence([
SKAction.runBlock({
print(self.position.x)
self.physicsBody?.velocity = CGVectorMake(20, 0.0)
}),
SKAction.waitForDuration(3),
SKAction.runBlock({
print(self.position.x)
self.physicsBody?.velocity = CGVectorMake(-20, 0.0)
}),
SKAction.waitForDuration(3)
])
)
)
问题是我预计位置之间的差异恰好是 60 (20 * 3s),但我收到平台 x
位置的日志是这样的:
596.042907714844
544.228393554688
596.048950195312
544.234008789062
596.054565429688
544.116333007812
595.936584472656
544.121887207031
595.942199707031
544.127685546875
595.824584960938
544.009704589844
所以,位置之间的差异在51~52之间
如何通过改变速度使平台移动到特定的精确距离?
编辑:我不知道这是否相关,但我已经像这样设置了平台的物理体:
self.physicsBody = SKPhysicsBody(rectangleOfSize: self.size)
self.physicsBody?.categoryBitMask = PhysicsCategory.Ground
self.physicsBody?.contactTestBitMask = PhysicsCategory.Player
self.physicsBody?.collisionBitMask = PhysicsCategory.Player
self.physicsBody?.allowsRotation = false
self.physicsBody?.affectedByGravity = false
self.physicsBody?.dynamic = true
self.physicsBody?.friction = 1.0
self.physicsBody?.restitution = 0.0
self.physicsBody?.mass = 99999999999
我之前没有使用过 Sprite Kit,但我认为你的问题是 SKPhysicsBody 的 linearDamping 属性 默认设置为 0.1,快速计算 (20*3-20*3 *0.1 = 54) 如果速度每帧都被阻尼,这听起来是正确的。
这是 属性 的文档:
编辑:我想我至少应该检查一下我的答案,所以我写了一个快速(粗略)的程序:
var dampingOccurancesPerSecond = 60.0 //Corresponds to frame rate
var numberOfSeconds = 3.0
var time = 0.0
@IBAction func test(sender: UIButton) {
let result = checkDistanceForVelocity(20, damping: 0.1, distanceTraveled: 0.0)
print(result)
}
func checkDistanceForVelocity(velocity: Double, damping: Double, distanceTraveled: Double) -> Double {
if time < numberOfSeconds {
time += 1/dampingOccurancesPerSecond
let newVelocity = velocity - (velocity * damping/dampingOccurancesPerSecond)
let newDistance = (distanceTraveled + velocity/dampingOccurancesPerSecond)
return checkDistanceForVelocity(newVelocity, damping: damping, distanceTraveled: newDistance)
}
return distanceTraveled
}
您的行进距离的微小变化也可能是由于您的帧速率的微小变化。当 dampingOccurancesPerSecond 从 60 -> 50 变化时,总行进距离从 52.1203109538336 -> 51.8808576268828
希望对您有所帮助!!
我正在尝试在 Swift 上制作一个移动平台,为了让玩家与平台一起移动,我需要更改 self.moveByX()
,这更简单, physicsBody?.velocity
(给平台和播放器增加一些摩擦)。
所以,我做了一个这样的函数:
self.runAction(
SKAction.repeatActionForever(
SKAction.sequence([
SKAction.runBlock({
print(self.position.x)
self.physicsBody?.velocity = CGVectorMake(20, 0.0)
}),
SKAction.waitForDuration(3),
SKAction.runBlock({
print(self.position.x)
self.physicsBody?.velocity = CGVectorMake(-20, 0.0)
}),
SKAction.waitForDuration(3)
])
)
)
问题是我预计位置之间的差异恰好是 60 (20 * 3s),但我收到平台 x
位置的日志是这样的:
596.042907714844
544.228393554688
596.048950195312
544.234008789062
596.054565429688
544.116333007812
595.936584472656
544.121887207031
595.942199707031
544.127685546875
595.824584960938
544.009704589844
所以,位置之间的差异在51~52之间
如何通过改变速度使平台移动到特定的精确距离?
编辑:我不知道这是否相关,但我已经像这样设置了平台的物理体:
self.physicsBody = SKPhysicsBody(rectangleOfSize: self.size)
self.physicsBody?.categoryBitMask = PhysicsCategory.Ground
self.physicsBody?.contactTestBitMask = PhysicsCategory.Player
self.physicsBody?.collisionBitMask = PhysicsCategory.Player
self.physicsBody?.allowsRotation = false
self.physicsBody?.affectedByGravity = false
self.physicsBody?.dynamic = true
self.physicsBody?.friction = 1.0
self.physicsBody?.restitution = 0.0
self.physicsBody?.mass = 99999999999
我之前没有使用过 Sprite Kit,但我认为你的问题是 SKPhysicsBody 的 linearDamping 属性 默认设置为 0.1,快速计算 (20*3-20*3 *0.1 = 54) 如果速度每帧都被阻尼,这听起来是正确的。
这是 属性 的文档:
编辑:我想我至少应该检查一下我的答案,所以我写了一个快速(粗略)的程序:
var dampingOccurancesPerSecond = 60.0 //Corresponds to frame rate
var numberOfSeconds = 3.0
var time = 0.0
@IBAction func test(sender: UIButton) {
let result = checkDistanceForVelocity(20, damping: 0.1, distanceTraveled: 0.0)
print(result)
}
func checkDistanceForVelocity(velocity: Double, damping: Double, distanceTraveled: Double) -> Double {
if time < numberOfSeconds {
time += 1/dampingOccurancesPerSecond
let newVelocity = velocity - (velocity * damping/dampingOccurancesPerSecond)
let newDistance = (distanceTraveled + velocity/dampingOccurancesPerSecond)
return checkDistanceForVelocity(newVelocity, damping: damping, distanceTraveled: newDistance)
}
return distanceTraveled
}
您的行进距离的微小变化也可能是由于您的帧速率的微小变化。当 dampingOccurancesPerSecond 从 60 -> 50 变化时,总行进距离从 52.1203109538336 -> 51.8808576268828
希望对您有所帮助!!