【问题标题】:SceneKit – Concave collision boxSceneKit - 凹面碰撞盒
【发布时间】:2018-09-05 17:02:38
【问题描述】:

我正在导入 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 的代码行正常工作。

【问题讨论】:

  • 请输入代码?如前所述,您的问题非常模糊,很难看出您在问什么。
  • 对不起,我还没有写任何代码,但今晚会尝试一些。想象一个盖子打开的麦片盒。我希望动态对象在内部与此框发生碰撞。如果我使用 SCNBox 创建一个盒子,在这个盒子内初始化的动态对象会在模拟过程中立即弹出到外面。我知道基于对象法线的碰撞是如何工作的,但我对 SceneKit 不太熟悉。
  • 我刚刚更新了我的原始帖子,谢谢。
  • 我建议你把它打开,写一个答案,然后接受那个答案。

标签: ios swift scenekit


【解决方案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。)

【讨论】:

  • 我最初做了一些与此非常相似的事情,但没有你的代码那么优雅。我也预见到这种方法可能更容易用当前视图尺寸控制每个平面的宽度和高度。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-09
  • 2019-07-16
  • 2018-04-09
  • 2021-02-01
  • 1970-01-01
相关资源
最近更新 更多