47

I'm trying to set up some elastic collisions using Sprite Kit. There is an issue with the case of multiple objects resting near each other as I asked in Sprite Kit Physics Collision Issue

I am confused on the timing of the situation for a collision.

I've tried to set dynamic to NO in -didBeginContact: delegate method, calculate final speeds, then in -didEndContact: set dynamic to YES and then set the velocities correctly.

The reason I want it to be dynamic outside of the collision is because I want friction/gravity etc to be available. What is wrong with the order/logic? I looked at -didSimulatePhysics method, but it didn't seem like the way to go.

Community
  • 1
  • 1
Cherr Skees
  • 1,508
  • 2
  • 21
  • 37
  • 2
    How are you applying the speeds/forces you are calculating? – joshd Jan 29 '14 at 05:01
  • I'm doing "setVelocity" – Cherr Skees Feb 05 '14 at 02:55
  • 1
    collision has a float property (collision.collisionImpulse) that can inform you of the severity of an impact. SKPhysicsPins can have elastic properties. May i suggest downloading Matti Räty's MRRopes example from GitHub and playing with the properties on that example? – Wharbio Jul 27 '14 at 19:15
  • 2
    Could you put your code to let us see clearly. May be it's a matter of physics theory and not sprite kit problem. – Guizmoo03 Jan 22 '15 at 18:48
  • I don't understand very well what you want to do but maybe you want to remove some behavior on the animator and add them again with new settings? (change the friction behavior when items hit each other...) It's quite easy to do with animator.removeBehavior(frictionBehavior) – Mikael Apr 17 '15 at 05:03
  • Little confused on what you are asking here. Are you looking to have the bottom ball hit the 2 touching balls, have the bottom ball stop, have the center ball not move and the top ball to move up? – sangony May 14 '15 at 22:07
  • Not sure if it works in your case but, have you seen this related answer where gravity is set to zero and dynamic to YES (http://goo.gl/PAqZde) ? – Tommie C. May 15 '15 at 12:22

3 Answers3

5

I'm trying to set up some elastic collisions using Sprite Kit

I think you're misunderstanding the physics in play here. An elastic collision is an oversimplification of a collision. In reality, no collision results in a perfect transfer of energy. I've explained the physics here in an answer to the related question you linked to.

I suspect reading through that you will see that an "elastic" collision can be done in SpriteKit, but most people don't realize you need space between the objects. You shouldn't need to do your own physics calculations.

Community
  • 1
  • 1
Ben Kane
  • 9,331
  • 6
  • 36
  • 58
1

I'm not sure if this is exactly what you are trying to do, but if I wanted to compute results of collisions without the help of SpriteKit while leaving all other physics to SpriteKit, I would do the following:

  1. Leave the dynamic property always set to YES.
  2. Set the categoryBitMask property for each kind of object.
  3. Set the collisionBitMask property for an object to the disjunction of all categoryBitMasks for which you want to retain automatic collision calculations (e.g., walls, but not other objects), or just to zero, if you want to handle all collisions manually.
  4. Set the contactTestBitMask property for an object to anything for which you want to calculate collisions manually.
  5. Compute and set all resulting velocities in didBeginContact.

(In short, exclude your objects from their own collisionBitMask.)

This link might be helpful: Working with Collisions and Contacts

0
  1. Use applyImpulse instead of setVelocity. You want to be using impulses or forces to ensure the physics engine can warm up properly. Using setVelocity will yield unpredictable results for very close or overlapping objects.

  2. Ensure there is some distance between the objects and that they don't overlap.

  3. Setup the collision bit masks correctly.

This should all work perfectly.

Patrick Collins
  • 4,046
  • 3
  • 26
  • 29