Sprite Kit physicsBody.resting behavior
If you examine the velocity of a physics body, you'll see that it is indeed moving but at a rate that is not perceivable. That's why the resting property is not set. A more reliable way to check if a SKPhysicsBody is at rest is to test if its linear and angular speeds are nearly zero. Here's an example of how to do that:
func speed(velocity:CGVector) -> Float {
let dx = Float(velocity.dx);
let dy = Float(velocity.dy);
return sqrtf(dx*dx+dy*dy)
}
func angularSpeed(velocity:CGFloat) -> Float {
return abs(Float(velocity))
}
// This is a more reliable test for a physicsBody at "rest"
func nearlyAtRest(node:SKNode) -> Bool {
return (self.speed(node.physicsBody.velocity)<self.verySmallValue
&& self.angularSpeed(node.physicsBody.angularVelocity) < self.verySmallValue)
}
override func update(_ currentTime: TimeInterval) {
/* Enumerate over child nodes with names starting with "circle" */
enumerateChildNodesWithName("circle*") {
node, stop in
if (node.physicsBody.resting) {
println("\(node.name) is resting")
}
if (self.nearlyAtRest(node)) {
println("\(node.name) is nearly resting")
}
}
}
SpriteKit - Detect if sprite still moving/stopped
You can check physics body velocity vector to see if the node is moving in any direction. With something like this you would be probably fine :
if((yournode.physicsBody.velocity.dx == 0.0f) && (yournode.physicsBody.velocity.dy == 0.0f)) {
//do your stuff
}
Also there is a property on node's physics body called resting which indicates whether the object is at rest within the physics simulation. So you could probably do something like this:
if(yourNode.physicsBody.resting ) {
//do your stuff
}
You can read about certain behaviours and recommendations about resting property in this SO answer.
Hope this helps
Sprite Kit Collision without bouncing
Had the same issue just a minute ago. It works with setting the restitution
but you need to set both restitutions. The one of the hero AND the one of the platform (or in my case the scene boundaries).
so:
hero.physicsBody.collisionBitMask = platformCategory
hero.physicsBody.restitution = 0.0
platform.physicsBody.restitution = 0.0
any_other_object_that_should_still_bounce.physicsBody.restitution = 1.0
will do it. All other objects on the screen still bounce on the platform as long as you set their restitution > 0
Apple's Swift Sprite Kit: linearDamping, how is this property used mathematically?
The formula for Box2D is velocity *= 1.0f / (1.0f + time * linearDamping);
Related Topics
How to Pause and Resume Nstimer.Scheduledtimerwithtimeinterval in Swift
When and How to Use @Noreturn Attribute in Swift
Mutable Binding in Swiftui Live Preview
Declaration Is Only Valid at File Scope (Extension)
How to Conform to Nscopying and Implement Copywithzone in Swift 2
Is There an Alternative to Initialize() in MACos Now That Swift Has Deprecated It
How to Remove Spaces from a String in Swift
Can't Understand How Collision Bit Mask Works
How to Change the Associated Values of a Enum
Drag Rotate a Node Around a Fixed Point
How to Create a String from Utf8 in Swift
Can an Enum Contain Another Enum Values in Swift
How to Convert Between Related Types Through a Common Initializer
How to Improve Camera Quality in Arkit
Swift Function with Args... Pass to Another Function with Args