我写了一个小程序来尝试解决这个问题,并得到了 135 分的答案。
https://stackoverflow.com/a/37523818/1430420
编辑:这是任何人都可以检查我的数学是否错误的程序
//
// GameScene.swift
// gravityTest
//
// Created by Steve Ives on 30/05/2016.
// Copyright (c) 2016 Steve Ives. All rights reserved.
//
import SpriteKit
class GameScene: SKScene {
// var elapsedTime: CFTimeInterval = 0.0
var timeOfLastUpdate: CFTimeInterval = 0.0
var updateCount = 0
let sprite = SKSpriteNode(imageNamed:"Spaceship")
override func didMove(to view: SKView) {
/* Setup your scene here */
// physicsWorld.gravity = CGVectorMake(-9.8, 0)
let location = CGPoint(x: (scene?.frame.midX)!, y: (scene?.frame.midY)!)
sprite.xScale = 0.5
sprite.yScale = 0.5
sprite.physicsBody = SKPhysicsBody(rectangleOf: sprite.size)
sprite.physicsBody?.isDynamic = true
sprite.position = location
self.addChild(sprite)
}
override func update(_ currentTime: CFTimeInterval) {
/* Called before each frame is rendered */
updateCount += 1
if (currentTime - timeOfLastUpdate) < 1 {return}
// if (timeOfLastUpdate - currentTime > 1.0) {
print("\(updateCount) - Time : \(currentTime) \(currentTime-timeOfLastUpdate) Location is : \(sprite.position) Velocity is : \(sprite.physicsBody!.velocity)")
// }
timeOfLastUpdate = currentTime
}
}
我使用游乐场来计算我的距离。我已经快150了
我们知道距离是 d = (gt^2)/2,其中 g 是重力,t 是时间。
d = (9.8m/t^2)(t^2)/2 d = (9.8m)/2 d = 4.9m
1秒内,我们将行驶4.9m
在我们的例子中,我们旅行了 765.9298706054688 点。
765.9298706054688/4.9 为我们提供了大约 156.312218491。
理想情况下,我们会行驶 735 个点,不确定我的代码中的什么可能会导致 30 个点的差异,我怀疑这与 SKAction 有关
//: A SpriteKit based Playground
import PlaygroundSupport
import SpriteKit
class GameScene: SKScene {
private var testNode = SKSpriteNode(color: .red, size: CGSize(width: 20, height: 20))
private var flag = false
private var time : CGFloat = 0
private var beginTest = false
override func didMove(to view: SKView) {
}
@objc static override var supportsSecureCoding: Bool {
// SKNode conforms to NSSecureCoding, so any subclass going
// through the decoding process must support secure coding
get {
return true
}
}
func touchDown(atPoint pos : CGPoint) {
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
testNode = SKSpriteNode(color: .red, size: CGSize(width: 20, height: 20))
let physicsBody = SKPhysicsBody(rectangleOf: testNode.size)
physicsBody.angularDamping = 0
physicsBody.linearDamping = 0
physicsBody.friction = 0
testNode.physicsBody = physicsBody
let action = SKAction.customAction(withDuration: 2)
{
node,elapsedTime in
if elapsedTime >= 1 && !self.flag && self.time == 0{
self.flag = true
self.time = elapsedTime
}
}
testNode.run(action)
beginTest = true
}
override func update(_ currentTime: TimeInterval) {
if beginTest{
beginTest = false
addChild(testNode)
}
}
override func didFinishUpdate() {
}
override func didApplyConstraints() {
if flag{
let position = CGPoint(x:testNode.position.x,y:-testNode.position.y)
print("Node traveled \(position.y) points over \(time) seconds with a velocity of \(testNode.physicsBody!.velocity)")
let pointsPerMeter = -testNode.physicsBody!.velocity.dy/9.8
print("Node traveled \(pointsPerMeter) points per meter over \(time) seconds with a velocity of \(testNode.physicsBody!.velocity)")
flag = false
testNode.removeFromParent()
time = 0
}
}
}
// Load the SKScene from 'GameScene.sks'
let sceneView = SKView(frame: CGRect(x:0 , y:0, width: 640, height: 480))
if let scene = GameScene(fileNamed: "GameScene") {
// Set the scale mode to scale to fit the window
scene.scaleMode = .aspectFill
// Present the scene
sceneView.presentScene(scene)
}
PlaygroundSupport.PlaygroundPage.current.liveView = sceneView
节点在 1.0 秒内移动了 765.9298706054688 个点,速度为 (0.0, -1494.4998779296875)
节点在 1.0 秒内每米移动 152.49998754384566 点,速度为 (0.0, -1494.4998779296875)
给任何人和我自己的答案
在苹果 SKSpriteKit 中没有什么可以真正定义像素,这是我的问题中的用词不当。另一方面,一个点是明确定义的。
每个人可能已经知道这一点,但是如果您将 SKPhysicsBody 附加到节点并分配以下属性:
seg.physicsBody?.isDynamic = true
seg.physicsBody?.affectedByGravity = false
seg.physicsBody?.allowsRotation = false
seg.physicsBody?.usesPreciseCollisionDetection = true
seg.physicsBody?.mass = 0
seg.physicsBody?.restitution = 1
seg.physicsBody?.linearDamping = 0
然后应用 200 的速度 dy 分量会导致对象以每秒 200 点的速度移动。这对我来说,移除所有外力后,物理世界将转化为 1m/s = 1point/s。