1

SceneKit Is there a way to get notified when dynamicBody is in resting State ? I want to remove dynamicBody when it finished to fall to the ground and stopped moving completely - I presume that I will have quite large amount of those so I would like to use something event based rather than looping through all the bodies and checking their velocities ?

Coldsteel48
  • 3,482
  • 4
  • 26
  • 43

1 Answers1

1

You could use Key-Value Observation on the isResting property. See Is key-value observation (KVO) available in Swift?.

Or you could use the SCNPhysicsContact and SCNPhysicsContactDelegate to detect collisions with the floor, and use that to trigger a check for velocity.

Community
  • 1
  • 1
Hal Mueller
  • 7,019
  • 2
  • 24
  • 42
  • It has some cost. You could measure it to find out how much, exactly. More at issue is that KVO isn't very Swift-y (you didn't mention the language you're using). I don't remember offhand what the sequence is of the SCNPhysicsContact callback, the renderer(_:didSimulatePhysicsAtTime:) callback from the SCNSceneRenderer, and the update of the objects' speeds; you might have to enqueue the node for deletion in one callback and delete it in another. Using the contact delegate feels like better Swift style to me, although I can't articulate precisely why. – Hal Mueller Dec 26 '16 at 07:37
  • I am using - Objective-C, I asked on performance because I am tight on CPU - animations takes almost 13ms + flush and stuff... I have left less than 1.2 ms - honestly Pathetic... – Coldsteel48 Dec 26 '16 at 07:41
  • It's not much work to implement each approach and then measure/compare. Premature optimization being the root of all evil, according to some famous person :-) – Hal Mueller Dec 26 '16 at 07:43
  • I stuck in the mid way to there, will have to handle it first then will try both as you suggest. Guess I better gonna post a question about Animation optimisations. – Coldsteel48 Dec 26 '16 at 07:52