RickyTheCoder - 2 months ago 13x

iOS Question

I'm trying to rotate an

`SCNBox`

`swipe gestures`

`Y-axis`

`SCNAction.rotateByX`

`X-axis`

`Z-axis`

`Y-axis`

What I have notice is that any rotation perform on either of the X,Y,Z axes changes the direction in which the other axes point.

Example: Default position

Then after a rotation in the

`Z-axis`

Of course this pose a problem because now when I

`swipe left or right`

`X-axis`

`Y-axis`

I apologize for my lack of understanding on this subject as this is my first go at 3d graphics.

Answer

I had the same issue, here's what I use to give the desired behavior:

```
func panGesture(sender: UIPanGestureRecognizer) {
let translation = sender.translationInView(sender.view!)
let pan_x = Float(translation.x)
let pan_y = Float(-translation.y)
let anglePan = sqrt(pow(pan_x,2)+pow(pan_y,2))*(Float)(M_PI)/180.0
var rotVector = SCNVector4()
rotVector.x = -pan_y
rotVector.y = pan_x
rotVector.z = 0
rotVector.w = anglePan
// apply to your model container node
boxNode.rotation = rotVector
if(sender.state == UIGestureRecognizerState.Ended) {
let currentPivot = boxNode.pivot
let changePivot = SCNMatrix4Invert(boxNode.transform)
boxNode.pivot = SCNMatrix4Mult(changePivot, currentPivot)
boxNode.transform = SCNMatrix4Identity
}
}
```

Source (Stackoverflow)

Comments