Performing your own physics calculations for collisions in a Sprite set

I am trying to set up some resilient collisions using the Sprite Kit. There is a problem with the case when several objects are next to each other, as I asked in Sprite Kit physics collision problem

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

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

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

+46
ios collision-detection sprite-kit skphysicsbody
Jan 28 '14 at 17:20
source share
3 answers

I am trying to set up some resilient collisions using the Sprite Kit

I think you misunderstand physics here. Elastic collision is simplification of collision. In fact, no collision results in perfect energy transfer. I explained physics here in response to a question related to you.

I suspect I read that you will see that an โ€œelasticโ€ collision can be made in SpriteKit, but most people donโ€™t understand that you need space between objects. You do not need to do your own physics calculations.

+5
May 18 '15 at 21:30
source share

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

  • Leave the dynamic property always set to YES .
  • Set the categoryBitMask property for each type of object.
  • Set the collisionBitMask property for an object to disjunction all BitMasks for which you want to save automatic collision calculations (for example, walls, but not other objects) or just to zero if you want to handle all collisions manually.
  • Set the contactTestBitMask property to an object for something for which you want to manually calculate the collision.
  • Compute and set all resulting speeds in didBeginContact .

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

This link may be useful: Work with conflicts and contacts

+1
May 15 '15 at 13:38
source share
  • Use applyImpulse instead of setVelocity. You want to use impulses or forces so that the physics engine can warm up properly. Using setVelocity will give unpredictable results for very close or overlapping objects.

  • Make sure that there is some distance between the objects and that they do not overlap.

  • Configure conflict bit masks correctly.

Everything should work fine.

0
May 19 '15 at 6:00 a.m.
source share



All Articles