Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SpriteKit Nodes Sticking to Edges of Scene / Not Bouncing

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!

like image 450
Jim Avatar asked Jul 01 '14 00:07

Jim


1 Answers

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 :)

like image 72
Mazyod Avatar answered Oct 08 '22 04:10

Mazyod