2017-05-27 120 views
5

我想创建两条线锚定在某个点(精灵)和旋转,以形成它们之间的30度角。下面是我想要实现的图像。如何保持两个线之间的30˚距离锚定在一个点

enter image description here

这是我迄今所做的:

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) 

} 

下面是运行上面的代码(它们彼此靠近)的图像。

enter image description here

我怎样才能确保线路总是相隔30˚旋转和即使保持30˚分开?

+0

你试过armLeft.zRotation = CGFloat(Double(345).degreesToRadians)吗? –

回答

2

为了让你的两条线完全分开30°,你可以使用SKPhysicsJointFixed,这听起来很像:它将两个physicsBodies固定在一个固定位置。既然你已经有他们定位自己想要的方式,只需添加以下代码,您有其他SKPhysicsJoints持有他们的方式:

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

结果:

Result image

+0

这是行得通的,但我唯一的问题是物理机构让联系/碰撞不现实 – iGetIt

+0

@iGetIt对不起,使用物理机构是一个糟糕的方式去做这件事。有一个更简单的方法使用联合来保持它们的固定。看到我更新的答案。 – Robert

+0

@Robert请查看我的问题。我会很感激,如果你可以帮助https://stackoverflow.com/questions/44294768/how-to-rotate-sprites-around-a-joint – Containment

0

如果您行节点锚点精灵的子节点(而不是场景),旋转锚点精灵节点将会旋转所有的线,而不会对物理做任何特殊的处理。你只需要介意锚点,以便它们正确对齐(即线的锚点在其末端而不是中心)