9

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
  • 49,178
  • 17
  • 136
  • 220
Robbe Verhoest
  • 401
  • 4
  • 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

19

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

Use simd_quatf initializer that has angle (it's expressed in radians) and axis parameters:

let scene = try! Experience.loadBox() 

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


Second approach

Use Transform's pitch, yaw and roll that are rotations about X, Y and Z axis expressed in radians.

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


Third approach

Use float4x4 initializer, representing each column with 4 slots horizontally:

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 */

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

Real-world visual representation of 4x4 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.

Read this post, to find out how to overcome the 180 degree rotation barrier.

Andy Jazz
  • 49,178
  • 17
  • 136
  • 220
  • 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
  • 1
    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
2

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
  • 401
  • 4
  • 9