2
votes

I have a SceneKit scene within a parent UIScrollView. I'd like certain objects in the scene to bounce in place when scrollViewWillEndDragging is called. I've been trying to use SceneKit physics to do this with no luck. Here is my current physics set up in my viewDidLoad, with "statue" being the one node I'm trying to animate:

    statue.physicsBody = SCNPhysicsBody(type: .dynamic, shape: nil)
    statue.physicsBody?.isAffectedByGravity = false
    let springnode = SCNNode()
    springnode.position = statue.position
    springnode.physicsField = SCNPhysicsField.spring()
    scene.rootNode.addChildNode(springnode)
    springnode.physicsField?.strength = 1

And here's my scrollViewWillEndDragging:

func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {

    statue.physicsBody?.applyForce(SCNVector3(0, velocity.y, 0), asImpulse: true)

}

Instead of bounce up and down in place until it slows to a stop, the "statue" node bounces up and down at an accelerating rate until it flies offscreen. Any ideas about what's wrong with the implementation?

I'm also unsure if this physics-based solution would work with 10 to 20 more nodes, each with their own physics field holding them in place. Any suggestions would be helpful.

1

1 Answers

0
votes

You can try configure your "bounciness" effect with

statue.physicsBody?.restitution = 0.0