3

i am following this tutorial (i think it is written in JavaScript): 3D Terrain

Im trying it in Swift with SpriteKit but have a problem with the rotation at the x-axis.

So this is what it looks like in the tutorial: image from Tutorial

And this is where i am now: enter image description here

I have created this grid with following Code:

var shapePoints = [CGPoint]()
for i in 0...zeilen+1{
    for j in 0...spalten{
        shapePoints.append(CGPoint(x: zellenGroesse*j, y: zellenGroesse*i))
        shapePoints.append(CGPoint(x: zellenGroesse*j, y: zellenGroesse*(i+1)))
    }
}
let fertigeShape = SKShapeNode(points: &shapePoints, count: shapePoints.count)
self.addChild(fertigeShape)

Now i would like to rotate it by some degrees, but i can only rotate it at the z-axis, not the x-axis.

Is there a way in SpriteKit to rotate the node at the x-axis, too?

Thanks and best regards

aignetti
  • 481
  • 1
  • 3
  • 14

3 Answers3

7

You can since iOS11, with what's called an SKTransformNode. When you want to 3D perspective tilt a node, add it as a child to a SKTransformNode, and then set it's xRotation or yRotation, which is a value in radians.

let t = SKTransformNode()
t.addChild(someNode)
scene.addChild(t)
t.xRotation = CGFloat.pi // tilts someNode by 180 degrees

You can also animate the change, like this.

let degrees: CGFloat = 20
let radians = CGFloat.pi / 180 * degrees
let duration = 2.0
let tilt = SKAction.customAction(withDuration: duration) { (node, elapsed) in
    let percent = elapsed / CGFloat(duration)
    t.xRotation = percent * radians
}
tilt.timingMode = .easeOut
t.run(tilt)
Peter Parker
  • 2,121
  • 1
  • 17
  • 23
1

No, it isn't possible, SpriteKit is 2d. You should look at SceneKit for 3d. https://developer.apple.com/reference/scenekit

simonWasHere
  • 1,307
  • 11
  • 13
  • Thanks for your response. I've looked at SceneKit but for me it is just to complicated. :'( I've tried now 3 hours to get this mesh in SceneKit - and i could not create it .. (oh, and working with cameraNode makes me crazy - arghhhh) – aignetti Feb 12 '17 at 23:30
0

Hopefully you found a solution, but now Apple has released SKTransformNode - you can put your node(s) inside an SKTransformNode and rotate them around X and Y in addition to the standard Z

Ron Holmes
  • 81
  • 4