Is there a way to detect when an SKPhysicsBody stops bouncing?

I was looking to find a way to find out if my SKSpriteNode with an SKPhysicsBody is in motion.

I create a box, raise it above the bar, then set the bar and box masks to collide. The bar is pinned, but would love to know when the box stops bouncing.

Answered by Claude31 in 684521022

A way would be to observe velocity. When it is zero, object is at rest. You need to detect it is really at rest and not just bouncing, by reading velocity at 2 close time points.

There is even a simpler way: use isResting property.

This property is automatically set to true by the physics simulation when it determines that the body is at rest. This means that the body is at rest on another body in the system. Resting bodies do not participate in the physics simulation until an impulse is applied to the object or another object collides with it. This improves the performance of the physics simulation. If all bodies in the world are resting, the entire simulation is at rest, reducing the number of calculations that are performed by the physics world.

You define the equation parameters for movement. Can't you compute when it stops bouncing ? I agree that may be complicated. But maybe some estimate ?

Accepted Answer

A way would be to observe velocity. When it is zero, object is at rest. You need to detect it is really at rest and not just bouncing, by reading velocity at 2 close time points.

There is even a simpler way: use isResting property.

This property is automatically set to true by the physics simulation when it determines that the body is at rest. This means that the body is at rest on another body in the system. Resting bodies do not participate in the physics simulation until an impulse is applied to the object or another object collides with it. This improves the performance of the physics simulation. If all bodies in the world are resting, the entire simulation is at rest, reducing the number of calculations that are performed by the physics world.

Is there a way to detect when an SKPhysicsBody stops bouncing?
 
 
Q