SceneKit - 凹面碰撞盒

SceneKit – Concave collision box

我正在导入一个 DAE 格式的对象,我想将它用作碰撞对象,但它是凹面的,但我在代码中实现它时遇到了问题:

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)   
}

无法使包含此内容的代码行正常工作SCNPhysicsShapeTypeConcavePolyhedron,如代码中所述。

一个 "concave" 物理体,根据 SCNPhysicsShapeTypeConcavePolyhedron,在某种意义上必须仍然是 "solid",并且该碰撞类型的隐含行为是将其他物体保持在 "solid"形体。因此,即使您将其形状类型设置为凹形,您的立方体仍具有 "solid" 内部。

(凹形类型 所做的 为您做的是让您制作一个实体形状 non-convex — 例如,一个立方体,其中一个面向内弯成碗状。)

如果你想将其他物体限制在一个 box-shaped 体积内,你需要创建墙:也就是说,放置 几个 物理物体 around 您要包含的体积。这是一个类似这样的快速实用函数:

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
}

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