Search code examples
swiftsprite-kitgame-physics

Directional Collision Detection in Swift


I am aware of how to use the SpriteKit physics engine to detect when a collision occurs between two nodes, however, I'd like to be able to detect this and also get the direction relative to one of the objects that the collision occurred. Incase anyone is curious, I'm asking this so I can make a platform game and I need to detect when something is resting on the top of the floor. Any help is appreciated, thanks!


Solution

  • Implement the SKPhysicsContactDelegate on one of your objects (perhaps your scene object). Set your scene's physicsWorld.contactDelegate to the object.

    In the contact delegate's didBeginContact(_:) method, you are given an SKPhysicsContact object. The contact's contactNormal property should be the direction you're interested in.