0

While I was messing with spritekit, I noticed that even the restitution is set to 1.0 (with linearDamping and friction both 0), if the node's velocity is small, it will not bounce. E.g. in the code below, I create an edge on the left, and have a ball hit the edge. I noticed that in the x direction, whenever the velocity is larger than 150, the ball will bounce; otherwise it'll just stick to the wall and stop moving. So I am wondering, is there a way to change this threshold so that the ball can bounce even with a low speed? Thanks!

self.physicsWorld.gravity = CGVectorMake(0, 0);
SKNode *leftEdge = [[SKNode alloc] init];
leftEdge.physicsBody = [SKPhysicsBody bodyWithEdgeFromPoint:CGPointZero toPoint:CGPointMake(0.0, self.size.height)];
leftEdge.position = CGPointZero;
[self addChild:leftEdge];

SKShapeNode *ball = [SKShapeNode shapeNodeWithCircleOfRadius:30];
ball.position = CGPointMake(self.size.width * 0.3, self.size.height * 0.5);
ball.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:30];
ball.physicsBody.velocity = CGVectorMake(-150, 0.0);//not bounce
ball.physicsBody.restitution = 1.0;
ball.physicsBody.friction = 0.0;
ball.physicsBody.linearDamping = 0.0;
[self addChild:ball];
CodeSmile
  • 64,284
  • 20
  • 132
  • 217
Jay Wang
  • 21
  • 5
  • I think you have to implement custom implementation for threshold velocity, extend your SKShapeNode class add their your property and create your custom implementation. – Tirth Jan 21 '15 at 06:49
  • Easiest solution is probably to use the didBeginContact: method to assign extra velocity to your node once it touches the wall. – sangony Jan 21 '15 at 16:53
  • Possible duplicate of [SpriteKit physics in Swift - Ball slides against wall instead of reflecting](http://stackoverflow.com/questions/27671391/spritekit-physics-in-swift-ball-slides-against-wall-instead-of-reflecting) – bummi Feb 03 '16 at 06:41

1 Answers1

0

When two bodies collide both of their restitution and friction (as well as many other) properties are taken into account. So give the wall a restitution of 1 and a friction of 0 for an elastic collision.