SpriteKit detect collision without setting dynamic to true?
Asked Answered
C

2

8

I want my sprites collisions and contacts to be detected, but I don't want them to move dynamically (I just need to know that they've touched).

didBeginContact(contact: SKPhysicsContact!) is only called if I set my player's physicsBody.dynamic to true. How can I get these delegate method calls without effecting the position or movement of my player?

Cantle answered 15/6, 2014 at 0:58 Comment(0)
A
5

You can pin objects on the screen, so you don't need to set the gravity to 0 (If you want to keep the gravity for other objects). Set the object up like this:

    object.physicsBody.dynamic = true
    object.physicsBody.affectedByGravity = false
    object.physicsBody.pinned = true

With this setup your object can collide with other objects without moving.

Ashling answered 25/8, 2014 at 14:33 Comment(0)
C
1

The physicsBodys follow the physics world set up by my scene. When they collide, they interact with the physicsWorld, which has a default gravity that pulls them downward.

To fix this issue, in the init method of my Scene I set

self.physicsWorld.gravity = CGVectorMake(0, 0)

Dynamic still has to be set to true because I want the physics bodies to interact with the physics world, but I don't want the physics world to effect them, so this is the resolution.

Cantle answered 15/6, 2014 at 1:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.