swift - 车轮沉入地板 - Scenekit(IOS)

标签 swift scenekit

我正在用 scenekit 制作汽车,车轮似乎沉入地板。 我用苹果代码来制作它。当我使用他们的模型时,它工作正常。 但是当我使用我的时却没有。我不明白为什么会这样。这是一张车轮下沉的图片,代码就在它的正下方。 enter image description here enter image description here

@IBAction func AddFloor(_ sender: Any) {
    let floor = SCNNode()
    let floorGeom = SCNFloor()
    floorGeom.firstMaterial?.diffuse.contents = "concrete.png"

    floor.geometry = floorGeom
    let staticBody = SCNPhysicsBody.static()
    floor.physicsBody = staticBody

    floor.position = SCNVector3(0, -10, 0)

    sceneView.scene.rootNode.addChildNode(floor)
}


@IBAction func AddCar(_ sender: Any) {
    let chassisNode = getNode("rccarBody", fromDaePath: "Models.scnassets/rc_car.scn")
    let wheelnode0 = chassisNode
        .childNode(withName: "wheelFrontL", recursively: false)
    let wheelnode1 = chassisNode
        .childNode(withName: "wheelFrontR1", recursively: false)
    let wheelnode2 = chassisNode
        .childNode(withName: "wheelRearL", recursively: false)
    let wheelnode3 = chassisNode
        .childNode(withName: "wheelRearR", recursively: false)


    let body =  SCNPhysicsBody.dynamic()
    body.allowsResting = false
    body.mass = 80
    body.restitution = 0.1
    body.friction = 0.5
    body.rollingFriction = 0
    chassisNode.physicsBody = body
    sceneView.scene.rootNode.addChildNode(chassisNode)



    // add wheels
    let wheel0 = SCNPhysicsVehicleWheel(node: wheelnode0!)
    let wheel1 = SCNPhysicsVehicleWheel(node: wheelnode1!)
    let wheel2 = SCNPhysicsVehicleWheel(node: wheelnode2!)
    let wheel3 = SCNPhysicsVehicleWheel(node: wheelnode3!)
    // set physics
    vehicle = SCNPhysicsVehicle(chassisBody: chassisNode.physicsBody!,
                                wheels: [wheel1, wheel0, wheel3,wheel2 ])
    chassisNode.position = SCNVector3(0,-8,0)

    sceneView.scene.physicsWorld.addBehavior(vehicle)
}

最佳答案

我刚刚在研究一个类似的问题,并意识到添加 SCNFloor 不一定会创建一个良好的几何体来作为 PhysicsBody 进行交互。

我建议添加 sceneView.debugOptions = [.showPhysicsShapes] 这样您就可以看到交互的各种物理对象的边界框。就我而言,我将地板几何体制作成非常宽、长且薄的盒子形状,而不是尝试使用内置的“地板”几何体,后者只创建了一个用于碰撞交互的小立方体。

我在 github.com/heckj/scenekit-physics-playground 的 macOS playground 中有一些示例代码如果您想查看和窃取任何对调试有用的东西。

关于swift - 车轮沉入地板 - Scenekit(IOS),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45753919/

相关文章:

ios - 绘制带阴影的渐变圆

ios - ARKit 面部跟踪 SceneKit 对象移动不正确

xcode - SCNPhysicsWorld错误

ios - 无法使用栏按钮创建导出

ios - 为什么GCD,ObjC和Swift之间的性能差距如此之大

objective-c - 如何将 NSDictionary 转换为 Swift Dictionary<String, NSObject>?

ios - 将值从自定义 View 传递到主视图 Controller - Swift

ios - Toon.shader 找不到 _light 变量

objective-c - Swift - SCNVector4 到 NSValue 的数组?

scenekit - 如何创建 .usdz 动画?