-2

My goal is that "hit" will be output, but without changing the positions of card and anotherCard. They should touch each other, but not move. However didBegin is not called.

Struct:

struct physicBodyCharacters {

    static let cardNumber = 00000001 //1
    static let anotherCardNumber = 00000010 //2
    static let nobodyNumber = 00000100 //4
}

in viewDidLoad():

gameScene2.physicsWorld.gravity = CGVector(dx: 0, dy: -9.81)
    gameScene2.physicsWorld.contactDelegate = self

First Node:

card = SKSpriteNode(texture: cardTexture)
    card.position = CGPoint(x: gameScene2.size.width / 2 + 150, y: 95)
    card.zPosition = 3
    card.setScale(1)
    card.physicsBody = SKPhysicsBody(texture: cardTexture, size: card.size)
    card.physicsBody?.affectedByGravity = false
    card.physicsBody?.categoryBitMask = UInt32(physicBodyCharacters.cardNumber)
    card.physicsBody?.collisionBitMask = UInt32(physicBodyCharacters.nobodyNumber)
    card.physicsBody?.contactTestBitMask = UInt32(physicBodyCharacters.anotherCardNumber)

Second Node:

anotherCard = SKSpriteNode(texture: anotherCardTexture)
    anotherCard.position = CGPoint(x: 31 , y: 532)
    anotherCard.zPosition = 2
    anotherCard.setScale(1)
    anotherCard.physicsBody = SKPhysicsBody(texture: anotherCardTexture, size: battlefieldCard0.size)
    anotherCard.physicsBody?.affectedByGravity = false
    anotherCard.physicsBody?.categoryBitMask = UInt32(physicBodyCharacters.anotherCardNumber)
    anotherCard.physicsBody?.collisionBitMask = UInt32(physicBodyCharacters.nobodyNumber)
    anotherCard.physicsBody?.contactTestBitMask = UInt32(physicBodyCharacters.cardNumber)

didBegin() Function:

func didBegin(_ contact: SKPhysicsContact) {
    print("contact")
    let contanctMask = contact.bodyA.categoryBitMask | contact.bodyB.categoryBitMask

    switch contanctMask
    {
    case UInt32(physicBodyCharacters.cardNumber) | UInt32(physicBodyCharacters.anotherCardNumber):
        print("hit")
    default:
        break
    } 
}

For each answer I am very grateful.

TimKrs
  • 67
  • 1
  • 9
  • You are not showing all relevant lines of code. – El Tomato Dec 03 '18 at 01:14
  • Assuming you declared this method in your SKScene, "Make sure you write "self.physicsWorld.contactDelegate = self" somewhere before you need to detect contacts or else your contact methods won't be called. – KissTheCoder Dec 07 '18 at 18:24
  • `self.physicsWorld.contactDelegate = self` is already in my code. The problem must be somewhere else. – TimKrs Dec 08 '18 at 20:45

1 Answers1

1

To be notified when 2 nodes touch, but not to have them move, then you need to have contact detection turned on between the nodes (do that didBegin is called) but collision detection turned off (because it’s the collisions that cause the nodes to move when they touch).

This is done by correctly setting the collisionBitMask and contactTestBitMask.

You haven’t posted enough of your code for us to check this, but you might want to read through the following answers on other similar questions:

Step-by-step guide for collisions and contacts: https://stackoverflow.com/a/51041474/1430420

And a guide to collision and contactTest bit masks: https://stackoverflow.com/a/40596890/1430420

Manipulating bit masks to turn individual collision ans contacts off and on. https://stackoverflow.com/a/46495864/1430420

Edit:

I think your category definitions are wrong - they should be binary bit masks but you’ve defined them as decimals.

Try changing their definition to:

struct physicBodyCharacters {

static let cardNumber = 00000001 << 0 // 1
static let anotherCardNumber = 00000010 << 1 // 2
static let nobodyNumber = 00000100 << 2 // 4
Steve Ives
  • 7,894
  • 3
  • 24
  • 55
  • I have read the articles and tried to rewrite my code in some places. But still, the same problem exists. Can you help me out here since I've been sitting on this problem for a while? – TimKrs Dec 05 '18 at 20:25
  • You haven't listed a lot of your code. For example the code where you define the physics bodies, the category bit masks etc. If you'e changed the code, then add your new code to the question please. – Steve Ives Dec 05 '18 at 23:10
  • I've added more code now. Can you look at it again, please? – TimKrs Dec 08 '18 at 20:55
  • I changed it to binary bit masks, but it still doesn't work. – TimKrs Dec 15 '18 at 16:10