4
votes

I have a SceneKit game in swift and in it I have a car with a dynamic physics body that is set up like this:

let carScene = SCNScene(named: "art.scnassets/truck.scn")!
    let carNode = carScene.rootNode.childNode(withName: "Cube", recursively: true)!



   let carPhysicsBody = SCNPhysicsBody(type: .dynamic,shape: SCNPhysicsShape(geometry: SCNBox(width: 5.343, height: 12.125, length: 4.373, chamferRadius: 0)))

    carPhysicsBody.mass = 3
    carPhysicsBody.friction = 2
    carPhysicsBody.contactTestBitMask = 1
    carNode.physicsBody = carPhysicsBody
    carNode.position = SCNVector3(x: 0, y: 0, z: 5)
    carNode.physicsBody?.applyForce(SCNVector3(x: 0, y: 50, z: 0), asImpulse: true)

    car = carNode
    floorScene.rootNode.addChildNode(car)

The floor's physics looks like this:

enter image description here

As you can see the car gets launched into the air. Then the gravity in the scene makes it fall, and instead of colliding with the floor, it goes right through it.

The gravity looks like this:

What should I change so it will collide with the floor?

3

3 Answers

3
votes

I've made a sample macOS playground, available at github.com/heckj/scenekit-physics-playground that shows a physics body interacting. I suspect the core of the problem in your local example is that the floor object doesn't actually have an SCNPhysicsBody associated with it. Until I explicitly set it on SCNFloor (as static), the text blob "fell through" as you originally described.

I recommend adding sceneView.debugOptions = [.showPhysicsShapes] to see the relevant debugging shapes. I've made (and pushed) a few updates to the above repo. Using stock SCNFloor to establish geometry for the physics collection made a tiny target (which is why the slight horizontal impulse made it appear to "pass through"). This last update sets the floor geometry to a long, wide, thin box.

1
votes

You want to set the floor to be of the type kinematic, not static. With the car being dynamic and the floor being static, the floor isn't interacting with other objects in any fashion, and you explicitly want a collision.

UPDATE: static vs. kinematic doesn't make a difference for the collision interaction, either work effectively the same way, but having a physics body, and verifying it's size or viewing the interaction with .showPhysicsShapes may answer the underlying question of why they're not interacting.

0
votes

One of posible solutions for u is to set collisionMargin property for your car from 0.0 to probably 0.01, I had the same problem for ball and plane.