将简单物理应用于 SceneKit 中的 .scn 对象 XCODE SWIFT

Applying simple Physics to a .scn object in SceneKit XCODE SWIFT

嘿,下面我创建了一个法线球体,只是为了测试我的游戏 scene/world 是否具有物理特性。所以我只是把球放在 scene/world 中,它是完美的。它受重力影响。因此,我尝试对 .scn 文件执行完全相同的操作。我给它的物理原理与掉落的测试球体对重力的作用相同。但男人没有动。重力设置为 -9.8 以模拟常规重力 代码:

  //----Test-Circle-here--------------------

    var sphere1: SCNNode!
    let sphereGeometry = SCNSphere(radius: 10.5)
    let sphereMaterial = SCNMaterial()
    let collisionCapsuleRadius = CGFloat(0.4 - 0.4) * 0.4
    let collisionCapsuleHeight = CGFloat(0.4 - 0.4)
    sphereMaterial.diffuse.contents = UIColor.greenColor()
    sphereGeometry.materials = [sphereMaterial]
    sphere1 = SCNNode(geometry: sphereGeometry)
    sphere1.position = SCNVector3(x: 1.0, y: 0.05, z: 0.05)

    //----Giving it a physics---------

    sphere1.physicsBody?.affectedByGravity = true
    sphere1.physicsBody?.friction = 0
    sphere1.physicsBody?.restitution = 1 //bounceness of the object
    sphere1.physicsBody?.angularDamping = 1 // rotationess
    sphere1.physicsBody = SCNPhysicsBody(type: .Dynamic, shape:SCNPhysicsShape(geometry: SCNCapsule(capRadius: collisionCapsuleRadius, height: collisionCapsuleHeight), options:nil))
    scnView.scene!.rootNode.addChildNode(sphere1)

下面的人无论如何都留在原地

     class Character {

let node = SCNNode()
init() {

    let GuyScene = SCNScene(named: "art.scnassets/FoxMan2.scn")
    let characterTopLevelNode: SCNNode = GuyScene!.rootNode.childNodeWithName("Guy", recursively: true)!
    let collisionCapsuleRadius = CGFloat(0.4 - 0.4) * 0.4
    let collisionCapsuleHeight = CGFloat(0.4 - 0.4)
    characterTopLevelNode.position = SCNVector3(x: 10.0, y: 0.0, z: 0.0)

    //----Giveing it a physics---------

    characterTopLevelNode.physicsBody?.affectedByGravity = true
    characterTopLevelNode.physicsBody?.friction = 0
    characterTopLevelNode.physicsBody?.restitution = 1 //bounceness of the object
    characterTopLevelNode.physicsBody?.angularDamping = 1 // rotationess
    characterTopLevelNode.physicsBody = SCNPhysicsBody(type: .Dynamic, shape:SCNPhysicsShape(geometry: SCNCapsule(capRadius: collisionCapsuleRadius, height: collisionCapsuleHeight), options:nil))
    node.addChildNode(characterTopLevelNode)

 }
}

enter image description here

enter image description here

检查以确保您的 characterTopLevelNode 实际上是您认为的节点(名称不匹配很常见)。在这些情况下,这通常是问题所在。您似乎将 characterTopLevelNode 添加为 node 的子项,但从未将 node 添加为正在显示的场景的子项。

还有一点,在创建物理体之前不要在物理体上设置选项。

例如: sphere1.physicsBody = SCNPhysicsBody(type: .Dynamic, ... sphere1.physicsBody.affectedByGravity = true sphere1.physicsBody.friction = 0 sphere1.physicsBody.restitution = 1 sphere1.physicsBody.angularDamping = 1