6
votes

I'm trying to create an application in which the user stacks different geometric shapes. In a .scn file, which is loaded inside the ARSCNView, I insert a static plane and then at each tap of the user, the app inserts a dynamic SCNNode.

The first node is being inserted a few inches above the plane, to replicate a falling object. And then, each other node is being dropped on top of another.

This is the main idea of the application; the problem appears after adding 3 or 4 nodes, they appear to slide of each other, almost jiggle, and the whole structure collapses.

This is my node I'm inserting:

let dimension: CGFloat = 0.075
let cube = SCNBox(width: dimension, height: dimension, length: dimension, chamferRadius: 0.0)
let node = SCNNode(geometry: cube)

node.physicsBody = SCNPhysicsBody(type: SCNPhysicsBodyType.dynamic, shape: nil)
node.physicsBody?.mass = 2.0

node.physicsBody?.friction = 1.0
node.physicsBody?.restitution = 0.01

node.physicsBody?.damping = 0.0
node.physicsBody?.angularDamping = 0.0

node.physicsBody?.rollingFriction = 1.0

node.physicsBody?.allowsResting = true

let insertionYOffset = 0.3
node.position = SCNVector3(hitResult.worldCoordinates.x, hitResult.worldCoordinates.y + Float(insertionYOffset), hitResult.worldCoordinates.z)

I've tried to play with the values and these are the best ones, but they aren't enough to create a stable structure of blocks.

As a requirement, I need to keep the blocks dynamic, they need to be affected by gravity, wind, etc.

2
You can try tweaking the velocityFactor as well. It's a SCNVector3. May affect the forces you want too much, but worth a try.theMikeSwan
The dimensions of these blocks are very small. What happens when you make the blocks a bit larger, say: 1.0?JaredH

2 Answers

1
votes

The problem most likely has something to do with a factor called Dynamic which will allow it to continuously move or may have to do with the objects hitting each other to fix that problem all you have to do is change the collision mask to two different numbers.

1
votes

I see two points that may be the flaws of your simulation:

  • Your objects are stacked. When two objects collides there's a small amount of error added in order to keep the physics in a stable phase and prevent weird behaviors. So when you stack objects you add error amounts and the simulation becomes inaccurate. I suggest making objects static over time or depending of the position, or reduce gravity by a bit.
  • Most of the physics engines works better with objects sized with the unit size (most of the cases, it's 1 like in Box2D or PhysX). Objects that are very small or very big tend to act less natural. 0.075 sounds a bit small for a simulation. I don't see a obvious solution, maybe look for a way to change the reference size.