So, this might be a SceneKit/iOS problem -- but it might instead be a Blender problem. This question presupposes the former.
Let's say I open a fresh project in Blender (nothing fancy) and export the default cube it creates as a dae file. I then import that dae file into my Xcode SceneKit project.
This particular SceneKit project consists of a scene in which a small ball rolls around on the top surface of a cube.
The problem: If I use SceneKit's built-in SCNBox shape for the cube (with a static, convexHull physics body), the ball rolls smoothly, as desired. But if I use the model exported from Blender, the ball bounces along the cube's surface as though it were covered in small, invisible bumps.
What I've tried:
- I tried increasing the ball's mass from 2.0 to 100.0 just to see if that would affect the outcome.
- I tried applying some smoothing modifiers to the model in Blender in hopes that it would be free of imperfections.
- I tried converting the
daefile to SceneKit's ownscnfile. - I tried configuring the
SCNPhysicsShape'soptionsparameter in various ways, including the use of.scale,.type,.collisionMargin, and.keepAsCompound. Example:
let shape = SCNPhysicsShape(node: cube, options: [.scale: cube.scale, .collisionMargin: 0.1])
- I tried changing the
frictionandrestitutionof both the cube and ball. Less friction/restitution did not solve the problem, nor did more of it. - I tried disabling continuous collision detection by removing the ball's
continuousCollisionDetectionThreshold.
I'm not sure how the following code will help diagnose this problem, but here it is anyway:
//Create the cube's physics body:
let shape = SCNPhysicsShape(node: cube)
cube.physicsBody = SCNPhysicsBody(type: .static, shape: shape)
cube.physicsBody?.categoryBitMask = CollisionTypes.floor.rawValue
cube.physicsBody?.collisionBitMask = CollisionTypes.dynamicObjects.rawValue
cube.physicsBody?.contactTestBitMask = CollisionTypes.nothing.rawValue
cube.physicsBody?.isAffectedByGravity = false
cube.physicsBody?.allowsResting = false
cube.physicsBody?.restitution = 0.5
cube.physicsBody?.friction = 0.75
//Create the ball's physics body:
let ballGeo = SCNSphere(radius: 0.5)
ballNode?.physicsBody = SCNPhysicsBody(
type: .dynamic,
shape: SCNPhysicsShape(
geometry: ballGeo
)
)
ballNode?.physicsBody?.categoryBitMask = CollisionTypes.dynamicObjects.rawValue
ballNode?.physicsBody?.contactTestBitMask = CollisionTypes.staticObjects.rawValue
ballNode?.physicsBody?.collisionBitMask = CollisionTypes.staticObjects.rawValue | CollisionTypes.floor.rawValue
ballNode?.physicsBody?.allowsResting = false
ballNode?.physicsBody?.continuousCollisionDetectionThreshold = 0.9 //Tried it with and without this property.
ballNode?.physicsBody?.restitution = 0.5
ballNode?.physicsBody?.charge = 4.0
ballNode?.physicsBody?.friction = 0.2
ballNode?.physicsBody?.rollingFriction = 1.0
ballNode?.physicsBody?.angularDamping = 0.4
ballNode?.physicsBody?.mass = 2.0
Question: Why might this problem be occurring? Is it a SceneKit problem, as this question presupposes -- or is it a Blender problem about which I will need to post a question on another forum?
Thanks for your help.
UPDATE: Since writing this post, I've been unable to reproduce this problem when using Blender's default cube. I'm not sure how I got it wrong on that point, but I digress.
I have been able to reproduce the problem when using a slightly more complex shape created in Blender, however. And in doing so, here's what I learned:
If I enable the visualization of physics bodies via scnView.debugOptions = .showPhysicsShapes, I can actually see the features that are causing the ball to bounce. Visually, they appear as red lines that define the shape of the physics body. So, whenever the ball rolls over these red lines, it bounces.
I tried removing any unnecessary vertices from the model in Blender, but that only helped a tiny bit. Here's a screenshot of the model in Blender that shows the wireframe. As you can see, there isn't much going on outside the holes. The surface outside the holes looks like it should be fine, but in SceneKit, it's a mess of physics body...lines...or whatever they are, spidering out in weird directions.
So, I've learned a lot about what's going on -- and it does seem like it's a SceneKit issue to me -- but I still don't know how to solve the problem.
