SceneKit – Concave collision box

Deadly 提交于 2021-01-29 03:09:58

问题


Im importing an object in DAE format and I'm wanting to use it as a collision object however it is concave, but I'm having trouble implementing this in code:

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

Cant get the line of code to work which has this in it SCNPhysicsShapeTypeConcavePolyhedron as commented in the code.


回答1:


A "concave" physics body, per SCNPhysicsShapeTypeConcavePolyhedron, must still be "solid" in some sense, and the implied behavior for that collision type is to keep other bodies outside the "solid" form of the body. As such, your cube still has a "solid" interior even if you set its shape type to concave.

(What the concave shape type does do for you is let you make a solid shape that is non-convex — for example, a cube where one of the faces bows inward to produce a bowl shape.)

If you want to confine other bodies to a box-shaped volume, you'll need to create walls: that is, put a few physics bodies around the volume you want to enclose. Here's a quick utility function for something like that:

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
}

This creates a node hierarchy containing visible walls as separate nodes, but you could easily modify it to create invisible walls and/or a single node with a compound physics body. (See SCNPhysicsShape.)



来源:https://stackoverflow.com/questions/34943200/scenekit-concave-collision-box

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!