我正在尝试创建两条锚定在某个点(精灵)并旋转以在它们之间形成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˚?
答案 0 :(得分:2)
为了使两条线分开30°,您可以使用<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<link rel="Stylesheet" type="text/css" href="style.css">
<script src="main.js"></script>
</head>
<body>
<div id="error_container"></div>
<div class="cVal_container">
<button onclick="cVal(0)" type="button">Error Code 0 (Default)</button>
<button onclick="cVal(1)" type="button">Error Code 1</button>
<button onclick="cVal(2)" type="button">Error Code 2</button>
<button onclick="cVal(3)" type="button">Error Code 3</button>
<button onclick="cVal(4)" type="button">Error Code 4</button>
<button onclick="cVal(5)" type="button">Error Code 5</button>
<button onclick="cVal(6)" type="button">Error Code 6</button>
<button onclick="cVal(7)" type="button">Error Code 7</button>
<button onclick="cVal(8)" type="button">Error Code 8</button>
<button onclick="cVal(9)" type="button">Error Code 9</button>
<button onclick="cVal(10)" type="button">Error Code 10</button>
<button onclick="cVal(11)" type="button">Error Code 11</button>
<button onclick="cVal(12)" type="button">Error Code 12</button>
<button onclick="cVal(13)" type="button">Error Code 13</button>
<button onclick="cVal(14)" type="button">Error Code 14</button>
<button onclick="cVal(15)" type="button">Error Code 15</button>
<button onclick="cVal(16)" type="button">Error Code 16</button>
</div>
</body>
</html>
,这听起来就是这样:它将两个SKPhysicsJointFixed
固定在一个固定位置。由于您已经按照自己想要的方式对其进行了定位,因此只需将此代码添加到另一个physicsBodies
的位置即可:
SKPhysicsJoints
结果:
答案 1 :(得分:0)
如果你让线节点成为锚点精灵的子节点(而不是场景),旋转锚点精灵节点会旋转所有的线条,而不会对物理做任何特殊的事情。你只需要记住锚点,使它们正确对齐(即线的锚在其末端而不是中心)