Skip to content

Instantly share code, notes, and snippets.

@ThoseGrapefruits
Last active September 17, 2023 14:09
Show Gist options
  • Save ThoseGrapefruits/193c46f39cbcadff66b06a18cbc8cfe1 to your computer and use it in GitHub Desktop.
Save ThoseGrapefruits/193c46f39cbcadff66b06a18cbc8cfe1 to your computer and use it in GitHub Desktop.
SceneKit get ancestor-relative (and scene-relative) position and rotation
MIT License
Copyright (c) 2023 Logan Moore
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

README

Note that this uses another extension on CGFloat (also included here) to wrap values greater than CGFloat.pi.

Usage

Get the current node's position and rotation within the scene

  let (scenePosition, sceneRotation) = self.getPositionAndRotation(within: self.scene!)

Compare the relative rotation of two children within the coordinate space of a common ancestor.

Consider the following node tree somewhere within the scene:

commonAncestor
|> grandParent1
   |> parent1
      |> child1
|> parent2
   |> child2
let child1Rotation = child1.getRotation(within: commonAncestor)
let child2Rotation = child2.getRotation(within: commonAncestor)
let rotationDifference = child2Rotation - child1Rotation
import Foundation
import SpriteKit
extension SKNode {
func getPosition(within ancestor: SKNode) -> CGPoint {
let (position, _) = getPositionAndRotation(within: ancestor)
return position
}
private func getPositionAndRotation(within ancestor: SKNode) -> (CGPoint, CGFloat) {
var node = self
var childPosition = CGPoint.zero
var childRotation = CGFloat.zero
while let nodeParent = node.parent, node != ancestor {
let cosZ = cos(node.zRotation)
let sinZ = sin(node.zRotation)
childPosition = CGPoint(
x: node.position.x
+ cosZ * childPosition.x
- sinZ * childPosition.y,
y: node.position.y
+ sinZ * childPosition.x
+ cosZ * childPosition.y)
childRotation = (childRotation + node.zRotation).wrap(around: CGFloat.pi)
node = nodeParent
}
return (childPosition, childRotation)
}
func getRotation(within ancestor: SKNode) -> CGFloat {
let (_, rotation) = getPositionAndRotation(within: ancestor)
return rotation;
}
}
extension CGFloat {
/// Reduces a value into its smallest representation in the range `[-boundary,boundary]`, modulo `boundary * 2`. This is mainly useful for pulling things back into their closest-to-zero representation within the unit circle with `boundary: CGFloat.pi`.
func wrap(around boundary: CGFloat) -> CGFloat {
let doubleBoundary = boundary * 2
let remainder = truncatingRemainder(dividingBy: doubleBoundary)
let remainderAbs = abs(remainder)
// Aside: it feels like there should be a way to write this without a
// check here but that requires too much thinking.
if (remainderAbs > boundary) {
// We got back to a value in the range [-boundary*2, boundary*2], which
// needs to be pulled back into the [-boundary, boundary ] range.
return remainder < 0
? doubleBoundary + remainder
: -doubleBoundary + remainder
}
return remainder
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment