SceneKit物理不按预期运行

时间:2017-10-21 14:53:12

标签: swift scenekit

我有4面墙,1个天花板,1个楼层和一个家具。我在房间里拖着我的家具,但它正穿过墙壁。我尝试了我所知道的一切。仍然无法解决问题。接触检测工作正常。如果我不给它家具,我的家具节点不会停留在地板上。物理身体!.damping = 1.0属性。请查看视频:https://youtu.be/1iwq7gvTzA4

enum ABC:Int {
    case furni = 1
    case wall = 4
    case floor = 6
}

private func setup() {
    // create the wall geometry
    let wallGeometry = SCNPlane(width: 2000, height: 300)
    wallGeometry.firstMaterial!.diffuse.contents = "Wallpaper1.png"
    wallGeometry.firstMaterial!.isDoubleSided = false
    wallGeometry.firstMaterial!.locksAmbientWithDiffuse = true

    let wallWithBaseboardNode = SCNNode(geometry: wallGeometry)
    wallWithBaseboardNode.position = SCNVector3Make(0, 150, 0)
    wallWithBaseboardNode.physicsBody = .static()
    wallWithBaseboardNode.physicsBody!.restitution = 1.0
    wallWithBaseboardNode.physicsBody!.contactTestBitMask = ABC.furni.rawValue

    //front
    _wallNode = wallWithBaseboardNode.clone()
    _scene.rootNode.addChildNode(_wallNode)

    //left
    _wallNode = wallWithBaseboardNode.clone()
    _wallNode.position = SCNVector3Make(0, 150, 0)
    _wallNode.rotation = SCNVector4Make(0, 1, 0, .pi/2)
    _scene.rootNode.addChildNode(_wallNode)

    //right
    _wallNode = wallWithBaseboardNode.clone()
    _wallNode.position = SCNVector3Make(roomDimensions[0], 150, 0)
    _wallNode.rotation = SCNVector4Make(0, 1, 0, -.pi/2)
    _scene.rootNode.addChildNode(_wallNode)

    //back
    _wallNode = wallWithBaseboardNode.clone()
    _wallNode.position = SCNVector3Make(0, 150, roomDimensions[1])
    _wallNode.rotation = SCNVector4Make(0, 1, 0, .pi/1)
    _scene.rootNode.addChildNode(_wallNode)

    //ceiling
    var ceilingNode = wallWithBaseboardNode.clone()
    ceilingNode = wallWithBaseboardNode.copy() as! SCNNode
    ceilingNode.geometry = (ceilingNode.geometry!.copy() as! SCNGeometry)
    ceilingNode.geometry!.firstMaterial = SCNMaterial()
    ceilingNode.geometry!.firstMaterial!.diffuse.contents = UIColor.white
    ceilingNode.position = SCNVector3Make(0, Float(wallGeometry.height), 0)
    ceilingNode.scale = SCNVector3Make(10, 10, 10)
    ceilingNode.rotation = SCNVector4Make(1, 0, 0, .pi/2)
    _scene.rootNode.addChildNode(ceilingNode)

    //floor
    let floor = SCNFloor()
    floor.reflectionFalloffEnd = 0
    floor.reflectivity = 0

    _floorNode = SCNNode()
    _floorNode.geometry = floor
    _floorNode.geometry!.firstMaterial!.diffuse.contents = "Wood3.jpg"
    _floorNode.geometry!.firstMaterial!.locksAmbientWithDiffuse = true
    _floorNode.geometry!.firstMaterial!.diffuse.wrapS = .repeat
    _floorNode.geometry!.firstMaterial!.diffuse.wrapT = .repeat
    _floorNode.geometry!.firstMaterial!.diffuse.mipFilter = .nearest
    _floorNode.geometry!.firstMaterial!.isDoubleSided = false

    _floorNode.physicsBody = .static()
    _floorNode.physicsBody!.restitution = 1.0

    //_floorNode.physicsBody!.contactTestBitMask = ABC.furni.rawValue

    _scene.rootNode.addChildNode(_floorNode)
}

这是我家具节点的代码

private func addFurniture() {
    let furnitureScene = SCNScene(named: "a.dae", inDirectory: "assets.scnassets/models/", options: nil)!
    let furniture = furnitureScene.rootNode.childNode(withName: "a", recursively: true)!

    furniture.physicsBody = .dynamic()
    furniture.physicsBody!.restitution = 1.0
    furniture.physicsBody!.damping = 1.0

    furniture.physicsBody!.contactTestBitMask = ABC.wall.rawValue

    _scene.rootNode.addChildNode(furniture)

    scnView.prepare(_scene, shouldAbortBlock: nil)

    _furniture = furniture

    scnView.debugOptions = [.showPhysicsShapes, .showPhysicsFields]
}

1 个答案:

答案 0 :(得分:0)

如果您想手动移动家具(即不使用力),请将体型更改为.kinematic

当您移动节点时,您可以执行contactTest(with:options:),如果您碰到墙壁,它将返回联系人列表。在renderer(_:willRenderScene:atTime:)中,您可以将家具的位置固定到"修复"墙体穿透抵靠墙壁。

您可以在此处查看我的类似问题的解决方案: https://stackoverflow.com/a/46915698/1389597

希望这会有所帮助,我花了几天时间试图解决这个问题:)