How to keep the distance between two lines fixed at a point

I am trying to create two lines that are tied at a certain point (sprite) and rotate, forming a 30-degree angle between them. Below is the image I want to achieve.

enter image description here

This is what I have done so far:

extension Int { var degreesToRadians: Double { return Double(self) * .pi / 180 } } extension FloatingPoint { var degreesToRadians: Self { return self * .pi / 180 } var radiansToDegrees: Self { return self * 180 / .pi } } class GameScene: SKScene, SKPhysicsContactDelegate { var anchorSprite = SKSpriteNode(imageNamed: "anchorSprite") var armLeft = SKSpriteNode(imageNamed: "lineSprite") var armRight = SKSpriteNode(imageNamed: "lineSprite") override func didMove(to view: SKView) { self.physicsWorld.gravity = CGVector(dx: 0, dy: -1.8) self.physicsWorld.contactDelegate = self var tealBg = SKSpriteNode(imageNamed: "tealBg") tealBg.position = CGPoint(x: frame.midX, y: frame.midY) tealBg.zPosition = 10 addChild(tealBg) anchorSprite.position = CGPoint(x: frame.midX, y: frame.midY + frame.midY/2) anchorSprite.zPosition = 20 anchorSprite.physicsBody = SKPhysicsBody(rectangleOf: anchorSprite.frame.size) anchorSprite.physicsBody?.categoryBitMask = pinCategory anchorSprite.physicsBody?.isDynamic = false addChild(anchorSprite) armRight.anchorPoint = CGPoint(x: 0.5, y: 1) armRight.position = anchorSprite.position armRight.zPosition = 20 armRight.physicsBody = SKPhysicsBody(rectangleOf: armRight.frame.size) armRight.zRotation = CGFloat(Double(15).degreesToRadians)//CGFloat(Double.pi/6) armRight.physicsBody!.isDynamic = true addChild(armRight) armLeft.anchorPoint = CGPoint(x: 0.5, y: 1) armLeft.position = anchorSprite.position armLeft.zPosition = 20 armLeft.physicsBody = SKPhysicsBody(rectangleOf: armRight.frame.size) armLeft.zRotation = CGFloat(Double(-15).degreesToRadians)//CGFloat(-Double.pi/6) armLeft.physicsBody!.isDynamic = true addChild(armLeft) //Pin joints var pinAndRightArmJoint = SKPhysicsJointPin.joint(withBodyA: anchorSprite.physicsBody!, bodyB: armRight.physicsBody!, anchor: CGPoint(x: anchorSprite.position.x, y: self.armRight.frame.maxY)) self.physicsWorld.add(pinAndRightArmJoint) var pinAndLeftArmJoint = SKPhysicsJointPin.joint(withBodyA: anchorSprite.physicsBody!, bodyB: armLeft.physicsBody!, anchor: CGPoint(x: anchorSprite.position.x, y: self.armLeft.frame.maxY)) self.physicsWorld.add(pinAndLeftArmJoint) } 

Below is the image from the code above (they are close to each other).

enter image description here

How can I make sure that the lines are always at a distance from each other and support them even when turning?

+5
source share
2 answers

To keep your two lines exactly 30 ° SKPhysicsJointFixed , you can use SKPhysicsJointFixed , which is what it looks like: it captures two physicsBodies together in a fixed position. Since you already arrange them the way you want, just add this code where you have other SKPhysicsJoints to hold them this way:

 let fixArms = SKPhysicsJointFixed.joint(withBodyA: armLeft.physicsBody!, bodyB: armRight.physicsBody!, anchor: CGPoint.zero) self.physicsWorld.add(fixArms) 

Result:

Image result

+2
source

If you create child nodes of the anchor sprite strings (instead of a scene), rotating the anchor node will rotate all the strings together with it, without doing anything special with physics. You just need to keep in mind the anchor points so that they align correctly (i.e., the line Anchor on the limbs, not the center).

0
source

Source: https://habr.com/ru/post/1268283/


All Articles