2

I'm trying to make my SCNNode automatically rotate horizontally by itself. This is the code I have so far:

box.scale = SCNVector3(x: 0.26, y: 0.26, z: 0.26)
box.position = SCNVector3(0.15, 3.85, -3)

How do I make this box spin horizontally automatically? And for a plus, is there a way to make it happen with user interaction enabled too? Thanks!

Update: If I didn't sound as clear, this is what I mean: You know when you spin a basketball on your finger and how it's spinning horizontally? That's the effect I'm trying to achieve! Thanks!

Amit Kalra
  • 4,085
  • 6
  • 28
  • 44

2 Answers2

6

You can either

Edit:

let action = SCNAction.repeatForever(SCNAction.rotate(by: .pi, around: SCNVector3(0, 1, 0), duration: 5))
box.runAction(action)

You can change the transform like this

let oldTransform = box.transform
let rotation = SCNMatrix4MakeRotation(axis.x, axis.y, axis.z angle)
SCNTransaction.begin()
SCNTransaction.animationDuration = 0.5
box.transform = SCNMatrix4Mult(rotation, oldTransform)
SCNTransaction.commit()

To use user input you can for example change the angle of the rotation or the duration with a gesture recognizter

jlsiewert
  • 3,494
  • 18
  • 41
  • 1
    What's axis.x, y, z and angle? When I do SCNMatrix4MakeRotation it sets this as default: angle, x, y, z. I don't know what to put in these values? Do I put the same xyz as my position? And fro angle I do 360 for it to rotate full way around? Thanks! – Amit Kalra Jul 21 '17 at 21:07
  • I updated the question to make it sound more clear. I want the spin effect of a basketball! – Amit Kalra Jul 21 '17 at 21:16
  • `axis` is the axis you want to rotate around, eg `[0, 1, 0]` or `worldUp`, depending on wether you want to rotate around the local- or global y-axis, and `angle` is the rotation angle in radians, so `.pi * 2` means a full rotation. – jlsiewert Jul 21 '17 at 22:00
  • Oh okay, so I could set the axis' to 0 and just change the angle? Or do I set xyz to be the same as my position? – Amit Kalra Jul 21 '17 at 22:24
  • The axis is the rotation axis, so use (1, 0, 0) if you want to rotate around the x axis, (0,1,0) for the nodes y axis and (0,0,1) for z – jlsiewert Jul 21 '17 at 22:26
  • So what does box.runAction(SCNAction.repeatForever(SCNAction.rotateBy(x: 1, y: 0, z: 0, duration: 1)), forKey: "Changing") do? Nothing happens at all, and when I do this, nothing still happens :( let oldTransform = box.transform let rotation = SCNMatrix4MakeRotation(1, 0, 0, 30) SCNTransaction.begin() SCNTransaction.animationDuration = 10 box.transform = SCNMatrix4Mult(rotation, oldTransform) SCNTransaction.commit() – Amit Kalra Jul 22 '17 at 03:21
  • Okay, this code kinda works to what I want it to do. It rotates, but it moves the box around itself, it doesn't leave the box in one spot and just spin it. – Amit Kalra Jul 22 '17 at 08:01
0

SwiftUI using SceneView updated the accepted answer with some slightly newer syntax.

Works well on iOS14/iOS15

    let oldTransform = cubeNode.transform
    let rotation = SCNMatrix4MakeRotation(GLKMathDegreesToRadians(90), 0, 1, 0)
    SCNTransaction.begin()
    SCNTransaction.animationDuration = 0.5
    cubeNode.transform = SCNMatrix4Mult(rotation, oldTransform)
    SCNTransaction.commit()
user3069232
  • 8,587
  • 7
  • 46
  • 87