InvadingOctopus/octopuskit

How to get contact events triggered?

pradeeproark opened this issue · 2 comments

Category: 'Behavior'

Describe the bug
didBegin and didEnd of PhysicsContactComponent not getting called.

To Reproduce
Steps to reproduce the behavior:

  1. Start with a new 'SwiftUI/tvOS' project
  2. Follow instructions in https://invadingoctopus.io/octopuskit/documentation/guide.html#physics-collisions to setup two components to collide/contact with each other.
  3. Add the following code:
final class BunnyContactComponent: PhysicsContactComponent {

     func didBegin(_ contact: SKPhysicsContact) {
        print("hungry bunny")
    }
    
    override func didEnd(_ contact: SKPhysicsContact, entityBody: SKPhysicsBody, opposingBody: SKPhysicsBody, scene: OKScene?) {
        print("hungry bunny")
    }
}
  1. Build for 'tvOS'
  2. Run and perform these actions: Try to move the bunny around with a PointerControlledPositioningComponent and let it collide with a collectible component ( a carrot sprite with a physics body)
  3. Bunny pushes the carrot around without triggering the contact event

Expected behavior
didBegin/didEnd Contact event in BunnyContactComponent should be called

Screenshots
ezgif-2-5f05a32234ce

Build Environment (what you're developing on)

  • OctopusKit Version: 'develop'
  • macOS Version: '10.15.6'
  • Xcode Version: 'Xcode-12 beta 6 (12A8189n)'
  • Swift Version: '5.3'

Target Device (what you're compiling for)

  • Device: 'AppleTV Simulator'
  • Target OS Version: 'tvOS'

Additional context
The scene has the following setup in createContents

self.entity?.addComponents([sharedMouseOrTouchEventComponent, sharedPointerEventComponent, PhysicsWorldComponent(), PhysicsComponent(physicsBody: SKPhysicsBody(edgeLoopFrom: self.frame))])
        
        
self.componentSystems.createSystem(forClass: BunnyContactComponent.self)

I see that I missed adding the sharedPhysicsEventComponent to self.entity in the scene. with that added it all works now.

When you use the same contact handler for multiple entities, you'll want to make sure to process each event for only one entity, otherwise you'll be printing "hungry bunny" twice!

That is, didBegin will get called for Entity A and Entity B.