16
votes

I've searched this one and I think there must be some parameter to fix this but I haven't found it.

I have a scene in SpriteKit where I want some circles/balls to bounce around and maintain any velocity they have indefinitely. They should bounce off the edges of the scene.

This is working if they are moving fast enough, or hit at a fairly sharp angle, but if they are going slower and coming in close to the plane of the edge, they keep moving (which is good) but they "Stick" to the edges. This sticking is what I don't want. They should rebound even if going very slowly.

To set up the edges, I used:

SKPhysicsBody *borderBody = [SKPhysicsBody bodyWithEdgeLoopFromRect:self.frame];
self.physicsBody = borderBody;
self.physicsBody.friction = 0.0;
self.physicsBody.restitution = 1.0;
self.physicsBody.linerDamping = 0.0;
self.physicsBody.angularDamping = 0.0;
self.physicsBody.affectedByGravity = NO;

And on the circle nodes, I have similar settings, like:

ball.usesPresciseCollisionDetection = YES;
ball.dynamic = YES;
ball.restitution = 1.0;
ball.linearDamping = 0.0;
ball.angularDamping = 0.0;
ball.friction = 0.0;

I have the gravity in my scene at zero. I add an impulse to the nodes and they start bouncing- It seems very close, as things bounce around, but then if there are any that are moving slowly and come in at a shallow angle, they "hug" the edges. I'll try including an illustration below to help visualize.

http://i.imgur.com/Rpr7luY.png

I've tried playing with lots of the PhysicsBody settings, but can't get things to stop sticking.

Thanks!

3
likely side effect of physics simulation inaccuracies. Try with allowsRotation off. If that doesn't help you may have to live with this behavior.LearnCocos2D
set friction to zero friction preventing them bouncing on the edges or you can get contact.collisionImpulse on didBeginContact function and apply and little force or impulse on every edge collisiondragoneye
I believe this is a duplicate of stackoverflow.com/questions/27671391/…Skyler Lauren
Yes, that's correct. When bounty runs off, I'll flag this as duplicate. And damn - it's really a bug.Jurik
@Jurik yeah it is frustrating and I hope Apple fixes this.Skyler Lauren

3 Answers

3
votes

As the guys mentioned, this answer could intuitively be seen as a step in the right direction, but the problem is with the whole SpriteKit physics engine. It is non-deterministic and fractions get lost in the calculation, causing imprecise simulation.

The short answer is to use Box2D instead. The hackish answer is to apply an impulse in the opposite direction.

All details are highlighted in my other answer.

@Jurik Glad I could help :)

0
votes

Wrong initializer. There are 2 kinds of SKPhysicsBody - Volume-based and Edge-based. You are using the exactly wrong kind, which does not participant movement.

Please check out SKPhysicsBody Documentation.

0
votes

The issue is severalfold and it has to do with shortcomings in the physics engine itself, however, I have managed to come up with a solution without requiring any third party software and that produces consistent results.

See my solution here: Accounting for Low Impact Angles/Velocities that Don't Register as Collisions