Spritekit 围绕中心旋转多个节点
Spritekit rotating multiple nodes around center
我想根据所有这些节点的中心点,使用 UIRotationGesture 对多个节点(我的游戏中选定的节点)应用旋转。我已经可以旋转单个节点,只需更改它的 zRotation。
多节点的问题是它会根据中心节点更改位置和 zRotation,我似乎无法理解如何管理它。
我想要这样的东西:
我必须旋转单个节点的是:
旋转手势期间
theRotation = CGFloat(sender.rotation) + self.offset
theRotation = theRotation * -1
node.rotation = theRotation
旋转手势后
self.offset = theRotation * -1
你知道如何在旋转过程中为我的节点设置正确的位置和角度吗?
我尝试了什么:
- 我尝试在中心添加一个节点(我的图片中的白点代表中心)并将我的节点的 parent 更改为这个节点,然后应用 zRotation这个节点,然后替换右边的parents。这没有用,因为我似乎无法更改 parent(我的节点消失了),这是我的另一个堆栈问题。
- 我尝试更改节点的锚点以适合中心点,然后使用 theRotation 旋转它们。它没有用,因为我似乎无法将锚点设置在中心位置(我有)。我尝试更改中心位置的坐标系以适合节点的坐标系,但这仍然无效。
node.convertPoint(center, fromNode: Self)
当它大约是 -1;-.5(或类似的东西)时,给我的协调就像 -58;-74。我不明白这个。
所以现在我想自己计算位置和旋转,因为那些没有用,但我需要一个关于如何计算这些的想法,因为我对 trigonometry/linear 代数不是很好,遗憾的是够了。
谢谢你的帮助!
我如何计算我的中心:
var maxX = nodesSelected[0].position.x
var minX = nodesSelected[0].position.x
var maxY = nodesSelected[0].position.y
var minY = nodesSelected[0].position.y
for node in nodesSelected{
if node.position.x > maxX{
maxX = node.position.x
}
if node.position.x < minX{
minX = node.position.x
}
if node.position.y > maxY{
maxY = node.position.y
}
if node.position.y > maxY{
minY = node.position.y
}
}
return CGPoint(x: (maxX-minX)/2+minX, y: (maxY-minY)+minY/2)
我如何计算旋转半径(节点与中心之间的距离):
extension CGPoint {
func distance(point: CGPoint) -> CGFloat {
return abs(CGFloat(hypotf(Float(point.x - x), Float(point.y - y))))
}
我如何获得轮换:
sender.rotation
给定一个rotationAngle
,你可以用下面的代码计算每个节点的新位置,你需要懂一点三角学才能看懂代码。
这里我有一个 SKShapeNode
的数组,我称之为 dots
(它相当于图像中的绿色节点)。 centralDot
将是您的中心 SKSpriteNode
。
for dot in dots {
let dx = dot.position.x - centralDot!.position.x // Get distance X from center
let dy = dot.position.y - centralDot!.position.y // Get distance Y from center
let current_angle = atan(dy / dx) // Current angle is the arctan of dy / dx
let next_angle = current_angle - rotationAngle // Sum how much you want to rotate in radians
// the new x is: center + radius*cos(x)
// the new y is: center + radius*sin(y)
// if dx < 0 you need to get the oposite value of the position
let new_x = dx >= 0 ? centralDot!.position.x + rotationRadius * cos(next_angle) : centralDot!.position.x - rotationRadius * cos(next_angle)
let new_y = dx >= 0 ? centralDot!.position.y + rotationRadius * sin(next_angle) : centralDot!.position.y - rotationRadius * sin(next_angle)
let new_point = CGPoint(x: new_x, y: new_y)
let action = SKAction.moveTo(new_point, duration: 0.2)
dot.runAction(action)
}
希望对您有所帮助
更新:
第一个代码没有帮助,所以我尝试了另一个。这个在我的测试中效果更好。
for i in 0..<dots.count {
let dot = dots[i]
let angle = rotationAngle + CGFloat(M_PI_2 * Double(i))
let new_x = rotationRadius * cos(angle) + centralDot!.position.x
let new_y = rotationRadius * sin(angle) + centralDot!.position.y
let new_point = CGPoint(x: new_x, y: new_y)
let action = SKAction.moveTo(new_point, duration: 1/60)
dot.runAction(action)
}
rotationRadius
是一个常量,你想要的中心到绿色节点的距离。
我想根据所有这些节点的中心点,使用 UIRotationGesture 对多个节点(我的游戏中选定的节点)应用旋转。我已经可以旋转单个节点,只需更改它的 zRotation。
多节点的问题是它会根据中心节点更改位置和 zRotation,我似乎无法理解如何管理它。
我想要这样的东西:
我必须旋转单个节点的是: 旋转手势期间
theRotation = CGFloat(sender.rotation) + self.offset
theRotation = theRotation * -1
node.rotation = theRotation
旋转手势后
self.offset = theRotation * -1
你知道如何在旋转过程中为我的节点设置正确的位置和角度吗?
我尝试了什么:
- 我尝试在中心添加一个节点(我的图片中的白点代表中心)并将我的节点的 parent 更改为这个节点,然后应用 zRotation这个节点,然后替换右边的parents。这没有用,因为我似乎无法更改 parent(我的节点消失了),这是我的另一个堆栈问题。
- 我尝试更改节点的锚点以适合中心点,然后使用 theRotation 旋转它们。它没有用,因为我似乎无法将锚点设置在中心位置(我有)。我尝试更改中心位置的坐标系以适合节点的坐标系,但这仍然无效。
node.convertPoint(center, fromNode: Self)
当它大约是 -1;-.5(或类似的东西)时,给我的协调就像 -58;-74。我不明白这个。
所以现在我想自己计算位置和旋转,因为那些没有用,但我需要一个关于如何计算这些的想法,因为我对 trigonometry/linear 代数不是很好,遗憾的是够了。
谢谢你的帮助!
我如何计算我的中心:
var maxX = nodesSelected[0].position.x
var minX = nodesSelected[0].position.x
var maxY = nodesSelected[0].position.y
var minY = nodesSelected[0].position.y
for node in nodesSelected{
if node.position.x > maxX{
maxX = node.position.x
}
if node.position.x < minX{
minX = node.position.x
}
if node.position.y > maxY{
maxY = node.position.y
}
if node.position.y > maxY{
minY = node.position.y
}
}
return CGPoint(x: (maxX-minX)/2+minX, y: (maxY-minY)+minY/2)
我如何计算旋转半径(节点与中心之间的距离):
extension CGPoint {
func distance(point: CGPoint) -> CGFloat {
return abs(CGFloat(hypotf(Float(point.x - x), Float(point.y - y))))
}
我如何获得轮换:
sender.rotation
给定一个rotationAngle
,你可以用下面的代码计算每个节点的新位置,你需要懂一点三角学才能看懂代码。
这里我有一个 SKShapeNode
的数组,我称之为 dots
(它相当于图像中的绿色节点)。 centralDot
将是您的中心 SKSpriteNode
。
for dot in dots {
let dx = dot.position.x - centralDot!.position.x // Get distance X from center
let dy = dot.position.y - centralDot!.position.y // Get distance Y from center
let current_angle = atan(dy / dx) // Current angle is the arctan of dy / dx
let next_angle = current_angle - rotationAngle // Sum how much you want to rotate in radians
// the new x is: center + radius*cos(x)
// the new y is: center + radius*sin(y)
// if dx < 0 you need to get the oposite value of the position
let new_x = dx >= 0 ? centralDot!.position.x + rotationRadius * cos(next_angle) : centralDot!.position.x - rotationRadius * cos(next_angle)
let new_y = dx >= 0 ? centralDot!.position.y + rotationRadius * sin(next_angle) : centralDot!.position.y - rotationRadius * sin(next_angle)
let new_point = CGPoint(x: new_x, y: new_y)
let action = SKAction.moveTo(new_point, duration: 0.2)
dot.runAction(action)
}
希望对您有所帮助
更新:
第一个代码没有帮助,所以我尝试了另一个。这个在我的测试中效果更好。
for i in 0..<dots.count {
let dot = dots[i]
let angle = rotationAngle + CGFloat(M_PI_2 * Double(i))
let new_x = rotationRadius * cos(angle) + centralDot!.position.x
let new_y = rotationRadius * sin(angle) + centralDot!.position.y
let new_point = CGPoint(x: new_x, y: new_y)
let action = SKAction.moveTo(new_point, duration: 1/60)
dot.runAction(action)
}
rotationRadius
是一个常量,你想要的中心到绿色节点的距离。