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

标签 ios swift xcode scenekit

嘿,下面我有一个正常的球体,我创建它只是为了测试我的游戏场景/世界是否有物理。所以我只是把球放在场景/世界中,它是完美的。它受重力影响。因此,我尝试对 .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(类型:.Dynamic,... sphere1.physicsBody.affectedByGravity = true sphere1.physicsBody.friction = 0 sphere1.physicsBody.restitution = 1 sphere1.physicsBody.angularDamping = 1

关于ios - 将简单物理应用于 SceneKit XCODE SWIFT 中的 .scn 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38648718/

相关文章:

ios - 错误信息 : Linker command failed with exit code 1

ios - 单独的用户类 [解析]

ios - onFormat string For NSPredicate

ios - 从 Jenkins 中获取 CFBundleVersion 以将其用作变量,例如 ${APP_VERSION}

ios - 如何在 iOs 中获取 Google Place Details 值

当从自动更正栏中选择一个单词时,不会调用 ios7 文本字段 shouldchangecharactersinrange

IOS Dev - 如何使那些空的表格单元格不可见?

ios - 使用Swift将一些Class类型的Arraylist转换成JSON格式

ios - Alamofire:支持上传包含文本和文件部分的多部分表单吗?

ios - 为 UITabBar 中的选定选项卡设置色调颜色