SceneKit - 凹面碰撞框

时间:2016-01-22 09:29:00

标签: ios swift scenekit

我以DAE格式导入一个对象并且我想将它用作碰撞对象,但它是凹的,但我在代码中实现它时遇到了麻烦:

dict

无法按照代码中的注释获取代码行func addCollisionBox() { let collisionBoxNode = SCNNode() let collisionBox = importedCollisionBox.rootNode.childNodeWithName("pCube2", recursively: false) collisionBoxNode.addChildNode(collisionBox!) collisionBoxNode.position = SCNVector3Make(0, 0, 0) collisionBoxNode.scale = SCNVector3Make(30, 20, 50) //collisionBoxNode.physicsBody = SCNPhysicsBody.staticBody() collisionBoxNode.physicsBody = SCNPhysicsBody.bodyWithType(SCNPhysicsShapeTypeConcavePolyhedron, shape: collisionBox) // This line errors collisionBoxNode.physicsBody?.restitution = 0.8 collisionBoxNode.name = "collisionBox" theScene.rootNode.addChildNode(collisionBoxNode) }

1 个答案:

答案 0 :(得分:1)

每个SCNPhysicsShapeTypeConcavePolyhedron的“凹”物理体在某种意义上仍然必须是“实体”,并且该碰撞类型的隐含行为是将其他物体保持在身体的“实体”形式之外。因此,即使将其形状类型设置为凹形,您的立方体仍然具有“实心”内部。

为你做的凹形形状是什么让你做出一个非凸的实体形状 - 例如,一个面向内弯曲以产生碗状的立方体。)

如果你想将其他物体限制在盒形体积中,你需要创建墙壁:即,将几个物理体放在卷上你想要附上。这是一个类似的快速实用函数:

func wallsForBox(box: SCNBox, thickness: CGFloat) -> SCNNode {

    func physicsWall(width: CGFloat, height: CGFloat, length: CGFloat) -> SCNNode {
        let node = SCNNode(geometry: SCNBox(width: width, height: height, length: length, chamferRadius: 0))
        node.physicsBody = .staticBody()
        return node
    }

    let parent = SCNNode()

    let leftWall = physicsWall(thickness, height: box.height, length: box.length)
    leftWall.position.x = -box.width / 2
    parent.addChildNode(leftWall)

    let rightWall = physicsWall(thickness, height: box.height, length: box.length)
    rightWall.position.x = box.width / 2
    parent.addChildNode(rightWall)

    let frontWall = physicsWall(box.width, height: box.height, length: thickness)
    frontWall.position.z = box.length / 2
    parent.addChildNode(frontWall)

    let backWall = physicsWall(box.width, height: box.height, length: thickness)
    backWall.position.z = -box.length / 2
    parent.addChildNode(backWall)

    let topWall = physicsWall(box.width, height: thickness, length: box.length)
    topWall.position.y = box.height / 2
    parent.addChildNode(topWall)

    let bottomWall = physicsWall(box.width, height: thickness, length: box.length)
    bottomWall.position.y = -box.height / 2
    parent.addChildNode(bottomWall)

    return parent
}

这会创建一个包含可见墙作为单独节点的节点层次结构,但您可以轻松修改它以创建不可见的墙和/或具有复合物理主体的单个节点。 (见SCNPhysicsShape。)