6

I'm trying to rotate a cube around its z-axis but I can't find how.

Is there a way in RealityKit to do this?

Andy Jazz
  • 36,357
  • 13
  • 107
  • 175
Robbe Verhoest
  • 341
  • 2
  • 9
  • 2
    I'm not familiar with RealityKit, but in the documentation I see mention of a [Transform](https://developer.apple.com/documentation/realitykit/transform) component, which has a rotation property in quaternion form that presumably you can manipulate. Again though, I haven't used RealityKit, so I'm just venturing a guess based on the documentation. – scg Dec 11 '19 at 22:09
  • 1
    Thanks! Because of this I found the answer – Robbe Verhoest Dec 11 '19 at 23:49

2 Answers2

12

In RealityKit there are, at least, three ways to rotate an object around single axis.

In each example we rotate an object counterclockwise (CCW).


First approach

let boxScene = try! Experience.loadBox()

boxScene.steelBox?.orientation = simd_quatf(angle: .pi/4,    /* 45 Degrees */
                                             axis: [0,0,1])  /* About Z axis */


Second approach

boxScene.steelBox?.transform = Transform(pitch: 0, 
                                           yaw: 0, 
                                          roll: .pi/4)      /* Around Z axis */

pitch, yaw and roll are rotations about X, Y and Z axis expressed in radians.


Third approach

let a: Float = cos(.pi/4)
let b: Float = sin(.pi/4)

let matrix = float4x4([ a, b, 0, 0 ],        /* column 0 */
                      [-b, a, 0, 0 ],        /* column 1 */
                      [ 0, 0, 1, 0 ],        /* column 2 */
                      [ 0, 0, 0, 1 ])        /* column 3 */

boxAnchor.steelBox?.setTransformMatrix(matrix, relativeTo: nil)

Visual representation of rotation matrix looks like this:

let a: Float = cos(.pi/4)
let b: Float = sin(.pi/4)

//  0  1  2  3
 ┌              ┐
 |  a -b  0  0  |
 |  b  a  0  0  |
 |  0  0  1  0  |
 |  0  0  0  1  |
 └              ┘

If you wanna know more about Rotation Matrices, read this post.

Andy Jazz
  • 36,357
  • 13
  • 107
  • 175
  • 1
    Thanks for the answer! As an extra: Do you know how you make this rotation animated? – Robbe Verhoest Dec 13 '19 at 18:48
  • You can use `playAnimation(_:transitionDuration:startsPaused:)` instance method for this. Also, read this useful post: https://medium.com/twinkl-educational-publishers/how-to-animate-ar-objects-with-swiftui-and-realitykit-b14730c4fad9 – Andy Jazz Dec 13 '19 at 19:05
  • 1
    Thank you so much for your time. Helped me a lot! – Robbe Verhoest Dec 13 '19 at 19:09
  • Hi Andy, I read the article and followed the tutorial but this post works with Reality Composer and its behaviours. I am actually trying to figure out how to make a rotation for example animated in code. Would you know how to do this? – Robbe Verhoest Dec 13 '19 at 23:57
  • Create a new question about animation, please. – Andy Jazz Dec 14 '19 at 05:42
  • I made a [new question](https://stackoverflow.com/questions/59335075/how-to-animate-a-transform-in-realitykit) about animation. I found most of it mean while but I can't figure out which parameters a translation and scale need. That's the only thing I'm still struggling with. – Robbe Verhoest Dec 14 '19 at 12:29
  • Found it and changed [my answer](https://stackoverflow.com/questions/59335075/how-to-animate-a-transform-in-realitykit/59335076#59335076) :) – Robbe Verhoest Dec 14 '19 at 14:04
  • Hi, another question: Now you always rotate around the center of the object. Is it possible to change this center point so I can rotate the whole object for example around one side of itself? – Robbe Verhoest Dec 14 '19 at 15:16
1

For people who are also searching for this you need to use transform and rotation. This needs a simd_quatf where you give the angle and the axis.

In my case i had to use this:

"object".transform.rotation = simd_quatf(angle: GLKMathDegreesToRadians(90), axis: SIMD3(x: 0, y: 0, z: 1))
Robbe Verhoest
  • 341
  • 2
  • 9