0

I have a scene with two nodes one is a shape node and the other one is a sprite node. I added to the scene and define its respective physics body properties. Everything looks good until I added a new Fixed joint between the two nodes, as you can see the physic debug show a diagonal line through the screen, I don't know what this means, the anchor point seems to be misaligned.

Note: The anchor node is an image with 1x1 size

This is the code

class GameScene: SKScene, SKPhysicsContactDelegate {

    private var anchor = SKSpriteNode(imageNamed: "anchor")
    private var curve: SKShapeNode!

    override func didMove(to view: SKView) {
        self.physicsWorld.contactDelegate = self

        self.addAnchor()
        self.addCurve()

        // This is the code is causing the diagonal line
        let jointOneFixed = SKPhysicsJointFixed.joint(withBodyA: anchor.physicsBody!, bodyB: curve.physicsBody!, anchor: anchor.position)
        self.physicsWorld.add(jointOneFixed)
    }

    func addAnchor(){
        anchor.position = CGPoint(x: self.size.width / 2, y: self.size.height + 1)
        anchor.anchorPoint = CGPoint(x: 0.5, y: 0.5)
        anchor.setScale(1)
        anchor.zPosition = 2
        anchor.name = "anchor"
        anchor.physicsBody = SKPhysicsBody(rectangleOf: CGSize(width: anchor.size.width, height: anchor.size.height))
        anchor.physicsBody?.isDynamic = false
        anchor.physicsBody?.affectedByGravity = false
        anchor.physicsBody?.allowsRotation = true
        self.addChild(anchor)
    }

    func addCurve() {
        let startPoint = CGPoint(x: anchor.position.x, y: anchor.position.y)
        let endPoint = CGPoint(x: self.size.width / 2, y: self.size.height - 400)

        // Create line with SKShapeNode
        curve = SKShapeNode()
        curve.zPosition = 9
        let path = UIBezierPath()
        path.move(to: startPoint)

        path.addCurve(to: endPoint, controlPoint1: CGPoint(x: endPoint.x - 50, y: self.size.height - 140), controlPoint2: CGPoint(x: endPoint.x + 70, y: self.size.height - 260))

        curve.path = path.cgPath
        curve.strokeColor = UIColor.red
        curve.lineWidth = 9
        curve.name = "curve"
        curve.physicsBody = SKPhysicsBody(edgeLoopFrom: curve.path!)
        self.addChild(curve)
    }
}

This is the screenshot (I'm not able to include images in my posts yet, so this is the link) Screenshot

user3267053
  • 166
  • 7

0 Answers0