0
votes

I currently have two SKSpriteNodes that I have added SKPhysicsBodies to. When they have no SKJoint attached, they collide as expected. As soon as I add the SKPhysicsJoint, they just pass right through each other. Any joint I add functions properly, but the SKPhysicsJointLimit only limits the extent to which the nodes can travel apart from each other, not how close they can get. How can I fix this?

Here is code I am using for the joint:

let joint = SKPhysicsJointLimit.joint(withBodyA: object1.physicsBody!, bodyB: object2.physicsBody!, anchorA: CGPoint(x: object1.position.x + iconController.position.x, y: object1.position.y + iconController.position.y), anchorB: CGPoint(x: object2.position.x + iconController.position.x, y: object2.position.y + iconController.position.y))
    joint.maxLength = screen.height * 0.4

physicsWorld.add(joint)

PhysicsBody of both nodes:

self.physicsBody = SKPhysicsBody(circleOfRadius: self.size.width / 2)
self.physicsBody?.allowsRotation = false
self.physicsBody?.friction = 0
self.physicsBody?.mass = 0.1

I have tested it with different values for the above modifications of the SKPhysicsBody and it performs the same.

1

1 Answers

0
votes

An SKPhysicsJoint object connects two physics bodies so that they are simulated together by the physics world. You can use also SKPhysicJointPin:

A pin joint allows the two bodies to independently rotate around the anchor point as if pinned together.

If your objects work well before the SKPhysicsJoint addition with the physic engine so they fired the didBeginContact as you wish and as you have setted, I think your problem is simply a wrong anchor. Try to add:

let skView = self.view as! SKView skView.showsPhysics = true

to your scene initialization code: you will see an outline of the physic bodies and maybe you'll see the issue immediatly.

To help you I'll try to make an example of elements configured to collide each other:

enum CollisionTypes: UInt32 {
    case Boundaries = 1
    case Element = 2
}

class GameScene: SKScene,SKPhysicsContactDelegate {
    private var elements = [SKNode]()
    override func didMoveToView(view: SKView) {
        physicsWorld.gravity = CGVector(dx: 0, dy: 0)
        self.physicsWorld.contactDelegate = self
        let boundariesFrame = CGRectMake(20, 20, 200, 400)
        let boundaries = SKShapeNode.init(rect: boundariesFrame)
        boundaries.position = CGPointMake(350,150)
        let boundariesBody = SKPhysicsBody.init(edgeLoopFromRect: boundariesFrame)
        boundariesBody.dynamic = false
        boundariesBody.categoryBitMask = CollisionTypes.Boundaries.rawValue
        boundariesBody.contactTestBitMask = CollisionTypes.Element.rawValue
        boundaries.physicsBody = boundariesBody
        addChild(boundaries)
        for index in 0..<5 {
            let element = SKShapeNode(circleOfRadius: 10)
            let body = SKPhysicsBody(circleOfRadius: 10)
            body.linearDamping = 0
            // body.mass = 0
            body.dynamic = true
            body.categoryBitMask = CollisionTypes.Element.rawValue
            body.contactTestBitMask = CollisionTypes.Boundaries.rawValue | CollisionTypes.Element.rawValue
            body.collisionBitMask = CollisionTypes.Boundaries.rawValue | CollisionTypes.Element.rawValue
            element.physicsBody = body
            element.position = CGPoint(x: size.width / 2, y: size.height / 2 - 30 * CGFloat(index))
            elements.append(element)
            addChild(element)
        }
    }
}

Hope it can help you to find your issue.