SceneKit & Swift: 如何使用 SCN 对象作为地板?

SceneKit & Swift: How to use SCN object as floor?

我在 Blender 中创建了山地景观并将其导入到我的 Xcode 项目中。

https://github.com/QBeukelman/Vehicle-Demo.git

我想在风景上驾驶SCNVehicle,就好像它是场景的地板(landscapeMountains.scn)。

车辆从景观中坠落!

我尝试了以下方法来解决问题,但没有成功:

  1. 静态、动态和动态物理体的不同组合。
  2. 使用不同的碰撞余量,例如 0.01
  3. 使用类别和碰撞遮罩(见图)

有谁知道如何使用 SceneKit 和 Xcode 将 scn 对象用作地板?

category & collision bit masks

将整个“// add mountain”代码块(GameViewController.swift 中的第 185 至 191 行)替换为:

func floorPhysBody(type: SCNPhysicsBodyType = .static, shape: SCNGeometry, scale: SCNVector3 = SCNVector3(1.0,1.0,1.0)) -> SCNPhysicsBody {
            
    // Create Physics Body and set Physics Properties
    let body = SCNPhysicsBody(type: type, shape: SCNPhysicsShape(geometry: shape, options: [SCNPhysicsShape.Option.type: SCNPhysicsShape.ShapeType.concavePolyhedron, SCNPhysicsShape.Option.scale: scale]))
        
    // Physics Config
    body.isAffectedByGravity = false
    return body
}
    
// configure Physics Floor
let mountain = scene!.rootNode.childNode(withName: "mountain", recursively: true)!
mountain.physicsBody = floorPhysBody(type: .static, shape: mountain.geometry!)

此外:您的 Mountain Geometry 非常大(某些几何图形的文件大小超过 50MB)。我建议您减少它,以避免在您的游戏增长时出现内存问题。另外:尽量避免纹理尺寸大于 1024x1024。如果可能,使用 2 的幂次方的纹理大小 - 例如 128px、256px、512px、1024px 的平方。

祝您项目愉快。