2016-01-22 44 views
1

Im以.dae格式導入一個對象,我想將它作爲碰撞對象使用,但它是凹形的,但是Im在代碼中執行時遇到了問題。Scenekit凹碰撞盒

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」作爲代碼註釋。

+0

請輸入密碼?如上所述,您的問題非常模糊,很難看到您問的是什麼。 –

+0

對不起,我還沒有寫任何代碼,但今晚會嘗試一些。打開蓋子想想穀物盒子。我想讓動態對象與這個盒子在內部發生碰撞。如果我使用SCNBox創建一個盒子,我的動態對象在盒子內部初始化,在模擬過程中立即彈出到外部。我知道如何基於對象法線進行碰撞工作,但我不熟悉SceneKit。 – JeremyRaven

+0

我剛剛更新了我原來的帖子,謝謝。 – JeremyRaven

回答

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

+0

我做了一些非常類似於最初的東西,但不像代碼那麼優雅。另外我預見,使用當前視圖尺寸,這種方法可能更容易控制每個平面的寬度和高度。 – JeremyRaven