0

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.

mhillsman
  • 770
  • 1
  • 8
  • 26

1 Answers1

0

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.

Alessandro Ornano
  • 34,887
  • 11
  • 106
  • 133
  • I had previously investigated what might be happening by showing the physics as you suggested; however, showing the physics only showed what I thought it should be showing. Both my anchors were correctly positioned in the middle of each node with a line indicating the joint between them. Again, the joint works as I want it to in an outward direction, but when the nodes move inward towards each other they simply don't collide. – mhillsman Jul 17 '16 at 07:08
  • 1
    So your object collided well before SKPhysicsJoint addition? I ask it because in your question you don't report any code. – Alessandro Ornano Jul 17 '16 at 07:10
  • Yes, they collided fine. In fact, even when the joint is added they collide normally with all nodes except for each other. I'll add some of my code to my question. – mhillsman Jul 17 '16 at 07:14
  • Ok, I'll read it and after I'll try to update my answer. Meanwhile I've added an example to help you about the collision between objects. – Alessandro Ornano Jul 17 '16 at 07:22
  • @AlessandroOrnano could you please assist me with this question https://stackoverflow.com/questions/44241484/how-to-limit-the-movement-of-two-anchored-lines-so-they-swing-continually-like-a – iGetIt May 30 '17 at 12:00