Rotating the skeleton position vector is easy using quaternions.
In the initialiser, set skeleton.position.y
to 4.0.
Remove:
skeleton.position = [3 * sin(timer * 10), 3, 3 * cos(timer * 10)]
// look at
let lookat = float4x4(eye: skeleton.position, center: .zero, up: [0, 1, 0])
skeleton.quaternion = simd_quatf(lookat)
Add:
let quaternion = simd_quatf(
angle: Float(0.005),
axis: [0, 0, 1])
skeleton.position = quaternion.act(skeleton.position)
This will make the skeleton rotate around the centre on the z axis.
It would make viewing easier if you substitute the ground plane for a sphere. You can get an earth usdz file from https://solarsystem.nasa.gov/resources/2393/earth-3d-model/ and scale it down to 0.005.
Although the skeleton is rotating around the origin, the model itself doesn’t rotate to be parallel to the rotation sphere.
Add this code:
skeleton.rotation.z += 0.005
You make the skeleton’s model rotation match the radians you are rotating the position vector.
In 3d this is mathematically a bit more complex, but that’s the general principle.
General hint: if you add a small amount to one value and the skeleton goes in the wrong direction, you probably need to do the opposite
Edit:
A couple of Apple goodies:
Quaternions starting at 13:56:
Using Accelerate and simd - WWDC18 - Videos - Apple Developer
The link lorenalexm posted visually shows the .act(_:)
method that I used:
https://developer.apple.com/documentation/accelerate/simd/working_with_quaternions
Rotating a cube by transforming its vertices:
https://developer.apple.com/documentation/accelerate/rotating_a_cube_by_transforming_its_vertices