加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

swift – 如何在两条锚点之间保持30?的距离

发布时间:2020-12-14 04:57:58 所属栏目:百科 来源:网络整理
导读:我正在尝试创建两条锚定在某个点(精灵)并旋转以在它们之间形成30度角的线.下面是我想要实现的图像. 这是我到目前为止所做的: extension Int { var degreesToRadians: Double { return Double(self) * .pi / 180 }}extension FloatingPoint { var degreesToR
我正在尝试创建两条锚定在某个点(精灵)并旋转以在它们之间形成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!,y: self.armLeft.frame.maxY))
    self.physicsWorld.add(pinAndLeftArmJoint)

}

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

enter image description here

如何确保线条总是相隔30?并且即使在旋转时也能保持30?的距离?

解决方法

为了让你的两条线分开正好30°,你可以使用一个SKPhysicsJointFixed,这听起来就是这样:它将两个物理实体固定在一个固定的位置.既然您已经按照自己想要的方式定位它们,只需将此代码添加到您拥有其他SKPhysicsJoints的位置即可:

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

结果:

Result image

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读