17

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.

https://i.stack.imgur.com/fC9Od.png

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

Thanks!

Jim
  • 343
  • 1
  • 3
  • 6
  • likely side effect of physics simulation inaccuracies. Try with allowsRotation off. If that doesn't help you may have to live with this behavior. – CodeSmile Jul 01 '14 at 07:26
  • Thanks for the thought. I did try setting allowsRotation on and off. The objects still tend to stick to the edges rather than bounce when going slowly. I posted a quick video showing this in action here: [link](http://youtu.be/TFNBvsSs6Ak) – Jim Jul 01 '14 at 21:45
  • 1
    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 collision – dragoneye Sep 17 '14 at 11:12
  • @dragoneye but this sounds like a workaround - actually I do the same with impulse on every edge collision... but without friction and linear damping it should bounce infinitely. Jim did you find a solution? – Jurik May 26 '15 at 09:10
  • I assume nothing relevant is going on in `-update:` or `-didSimulatePhysics` etc.? – CloakedEddy May 26 '15 at 13:14
  • 1
    I believe this is a duplicate of http://stackoverflow.com/questions/27671391/spritekit-physics-in-swift-ball-slides-against-wall-instead-of-reflecting/29447887#29447887 – Skyler Lauren May 27 '15 at 03:48
  • 1
    Yes, that's correct. When bounty runs off, I'll flag this as duplicate. And damn - it's really a bug. – Jurik May 27 '15 at 07:49
  • 1
    @Jurik yeah it is frustrating and I hope Apple fixes this. – Skyler Lauren May 27 '15 at 11:22
  • A potential alternative answer to this [here](http://stackoverflow.com/a/32171386/613220) – Barjavel Aug 23 '15 at 20:51

3 Answers3

3

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

Community
  • 1
  • 1
Mazyod
  • 22,319
  • 10
  • 92
  • 157
0

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.

Community
  • 1
  • 1
Juguang
  • 621
  • 2
  • 5
  • 15
  • 2
    I don't think this solves the original issue. I've noticed several times that objects don't bounce if their speed is too low. – sabiland Mar 09 '15 at 08:59
  • even my edge restitution = 1, in slow speed ball is not bouncing from edge and going on sliding, is there some solution on SpriteKit? – Sunrise17 Jul 14 '18 at 21:21
0

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

rswayz
  • 1,122
  • 2
  • 10
  • 21