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

Time:05-30

I have created a mountain landscape in Blender and imported it into my Xcode project.

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

I would like to drive the SCNVehicle on the landscape as if it were the floor of the scene (landscapeMountains.scn).

The vehicle falls through the landscape!

I have tried the following to solve the issue, but without success:

  1. Different combinations of static, kinetic and dynamic physics bodies.
  2. Using different collision margins such as 0.01
  3. Using category and collision masks (see image)

Does anyone know how to use a scn object as a floor using SceneKit and Xcode?

category & collision bit masks

CodePudding user response:

Replace your entire "// add mountain" code block (line 185 to 191 in GameViewController.swift) with this:

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

In addition: Your Mountain Geometry is very large (file is over 50MB in size for some geometry). I recommend you to reduce this, to avoid memory issues when your game grows. Also: try to avoid texture sizes bigger than 1024x1024. Use textures sizes from a power of 2 if possible - like 128px, 256px, 512px, 1024px squared.

Have fun with your project.

  • Related