SKPhysicsBody
has a boolean resting
property that the physics world turns on when deemed at rest - that might be what you're looking for. Not sure that will work though in your case since you can't really set the threshold when it triggers.
Checking when SKPhysicsBody has stopped moving?
-
29-06-2022 - |
Question
Is there anyway of calculating when an SKPhysicsBody
comes to rest (or rather its velocity gets very small) so that you can remove it from the physics simulation. I have tried watching the velocity but there are two issues. (1) on any given bounce there is a point at the apex where the object has little velocity, (2) when the SKPhysicsBody
has visibly finished bouncing the velocity.dy still shows a pretty large number (i.e. 30+) even though for all intents the body is now at rest. Just curious if there is anything available I might have missed for checking when a body is no longer moving?
Solution
OTHER TIPS
It sounds like you want the resting
property of SKPhysicsBody
.
From the linked documentation,
This property is automatically set to YES 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, then the entire simulation is at rest, reducing the number of calculations that are performed by the physics world.