SceneKit - 凹面碰撞盒 [英] SceneKit – Concave collision box

查看:42
本文介绍了SceneKit - 凹面碰撞盒的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在导入一个 DAE 格式的对象,我想将它用作碰撞对象,但是它是凹面的,但是我在代码中无法实现它:

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

无法使代码行工作,其中包含此 SCNPhysicsShapeTypeConcavePolyhedron 如代码中所注释.

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

推荐答案

按照 SCNPhysicsShapeTypeConcavePolyhedron 的凹形"物理体,在某种意义上仍然必须是实心的",以及其隐含的行为碰撞类型是将其他物体保持在物体的固体"形式之外.因此,即使您将其形状类型设置为凹面,您的立方体仍然具有实心"内部.

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
}

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

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

这篇关于SceneKit - 凹面碰撞盒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆