0
我試圖在一個飛機上有5個球的世界中模擬重力。我遇到的第一個問題是,球穿過地板是因爲它們具有動態的身體。但是如果我使用運動機構,他們不會受到力量的影響。所以我不得不使用一個動態的物體,禁用重力並改用重力場。場景從COLLADA文件加載。這就是我設置的東西:重力場不起作用,物體浮在真空中
self.scene = SCNScene(named: "art.scnassets/Balls.dae")!
for i in 1...5 {
let sphere = scene.rootNode.childNode(withName: "Sphere\(i)", recursively: true)!
let shape = SCNPhysicsShape(geometry: sphere.geometry!, options: nil)
let body = SCNPhysicsBody(type: .dynamic, shape: shape)
body.mass = 0.2
body.isAffectedByGravity = false
body.categoryBitMask = BallCategory
body.collisionBitMask = PlaneCategory | BallCategory | GravityFieldCategory
sphere.physicsBody = body
balls.append(sphere)
}
self.plane = self.scene.rootNode.childNode(withName: "Plane", recursively: true)
let shape = SCNPhysicsShape(geometry: self.plane.geometry!, options: nil)
let body = SCNPhysicsBody(type: .static, shape: shape)
body.categoryBitMask = PlaneCategory
body.collisionBitMask = BallCategory
self.plane.physicsBody = body
// Create a gravity field
let gravityField = SCNPhysicsField.linearGravity()
gravityField.categoryBitMask = GravityFieldCategory
gravityField.direction = SCNVector3Make(0.0, -1.0, 0.0)
gravityField.isActive = true
gravityField.strength = 3.0
let gravityNode = SCNNode()
gravityNode.physicsField = gravityField
self.scene.rootNode.addChildNode(gravityNode)
然後,當用戶觸摸屏幕時,隨機球向上移動,它應該跌下來:
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
let index = Int(arc4random_uniform(5))
let ball = self.ball(atIndex: index)
let moveUp = SCNAction.move(by: SCNVector3Make(0.0, 2.0, 0.0), duration: 1.5)
ball.runAction(moveUp)
}
但不降下來,它只是停在那裏,漂浮在真空中。重力場不起作用。這是發生了什麼事情:https://youtu.be/egMe4lgPZY0