Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I rotate an object around only one axis in RealityKit?

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?

like image 648
Robbe Verhoest Avatar asked Dec 11 '19 21:12

Robbe Verhoest


People also ask

How do I rotate an object around a fixed axis?

You can use the transform.Rotate method to rotate an object around a fixed axis. The method has various constructors but a simple way to achieve what you want would be using the following depending on the axis you actually want to rotate the object around.

Is it possible to rotate the z axis on one axis only?

Hi, I would like to rotate toward another object on one axis only. In my case, the Z Axis. This works fine for anything that is less than 90 degrees from the horizontal from it. But other than that, it faces backwards. What could be the problem? The issues stems from you use of Euler angles, which can cause these sorts of problems.

Is there a way to rotate a GameObject on one axis?

Well, it was rather hard to rotate GameObject ONLY in one axis, nothing worked correctly, adding values to another axis, even RotateAround or Rotate, but... dx, dy, dz - how much you want to change value in degrees.

Is it possible to rotate around the z-axis?

Rotation around the z-axis would just be roll, which wouldn't be changed by transform.forward. Hmm. I'm not sure exactly what you're doing, but you can set transform.forward, transform.right, and transform.up as needed. Are you really trying to only rotate around the z-axis?


2 Answers

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.

like image 195
Andy Jazz Avatar answered Sep 23 '22 14:09

Andy Jazz


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))
like image 26
Robbe Verhoest Avatar answered Sep 20 '22 14:09

Robbe Verhoest