【问题标题】:How to keep a 30˚ distance between two lines anchored at a point如何在锚定点的两条线之间保持 30° 的距离
【发布时间】:2017-05-27 20:14:43
【问题描述】:

我正在尝试创建两条线,它们锚定在某个点(精灵)并旋转以在它们之间形成 30 度角。下面是我想要实现的图像。

这是我到目前为止所做的:

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)

}

下面是运行上述代码的图像(它们靠得很近)。

如何确保线条始终相距 30°,即使在旋转时也保持 30°?

【问题讨论】:

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

标签: swift swift3 sprite-kit


【解决方案1】:

要使两条线之间的间距正好为 30°,您可以使用 SKPhysicsJointFixed,这就是它听起来的样子:它将两条 physicsBodies 固定在一个固定位置。由于您已经将它们按您想要的方式放置,只需将此代码添加到您拥有其他 SKPhysicsJoints 的位置即可:

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

结果:

【讨论】:

  • 这行得通,但我唯一的问题是物理实体使接触/碰撞变得不切实际
  • @iGetIt 抱歉,使用物理实体是一个糟糕的方法。有一种更简单的方法是使用关节来保持它们固定。请参阅我的更新答案。
  • @Robert 请查看我的问题。如果您能提供帮助,我将不胜感激stackoverflow.com/questions/44294768/…
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 2020-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-26
    • 2017-07-06
    • 2019-06-26
    • 2020-05-07
    • 1970-01-01
    相关资源
    最近更新 更多