Moving an airplane on a globe

Hi,

I’m trying to figure out how to make an airplane move forward on a globe. The airplane can trust forward and turn to the left and right, and trust forward in the new forward facing direction.
I don’t think it will work if I set the 3D anchor of the airplane to the center of the globe and rotate the anchor – I thing this feels like some kind of Spherical math. The airplane should also stay on the globe no matter where the airplane is.

Example:
aircraft-orbit-86196476-333d704fc4d94c20b35e9048fe4f88ee

Could anybody give me some directions on how to create this?

thx

You could investigate quaternions.

Quaternions would actually make more sense for the camera rotation in your other question, I think. But for the book, we’re using Euler.

1 Like

I would think that you could also fake the aircraft “moving”. To simulate the aircraft flying around the globe, rotate the globe along the desired axis; the aircraft could then be animated to face the same direction as the rotation.

Your thought process of rotating its anchor should simulate flying just as well. This would only handle the apparent position aircraft over the globe, again you would have to rotate the object to face the direction of rotation. You would also have to take camera movement into account to keep the object visible, not so much with the prior suggestion.

Apple has some documentation pointed directly at quaternions that could assist:
https://developer.apple.com/documentation/accelerate/simd/working_with_quaternions

If you want a library over Metal that may assist in easing the work, there is Satin:

2 Likes

Thanks I will look into it :slight_smile:

Hi,
I tried using the look at function in the code but as you can see the result is quite hilarious :wink:
I’ve attached the project so you can see what I did – cannot really understand how to get this working. Any help is much appreciated :slight_smile:

https://www.dropbox.com/s/udz0op7qir9b1sg/FlyingSkeleton.mov?dl=0

Screenshot  2022-10-06 at 01.20.28

Animation.zip (325.4 KB)

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 :wink:

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

1 Like

Thanks I will look into all that.

But I’m also wondering why eye function in the MathLibrary is not doing what I think it is supposed to do?

If I understand it correctly it should return a quat which I can apply on my object to make it look at the center?

// look at
let lookat = float4x4(eye: skeleton.position, center: .zero, up: [0, 1, 0])
skeleton.quaternion = simd_quatf(lookat)

What does “lookAt” mean in this situation?

skeleton.position is the point between the skeleton’s feet (the skeleton’s origin) located in world space. You’re creating a direction vector between the world origin and the skeleton’s feet and then creating a quaternion from that. You aren’t changing the orientation of the skeleton around the skeleton’s origin.

Aha sorry, to be clear on that, I mean the skeleton would face the world center at 0,0,0 no matter how the skeleton moves around. Its entire body would be facing to that point. So if I just change the position, I would like to get the new orientation to apply to the skeleton after it moved.

Hi all and @caroline
Hope you are doing well.

I experimented more with the look at function and this example shows an issue knows as the “Quaternion flip”. So my question is how can it be solved, as you see the object “flips” when it is on the other side.
The blue arrow is pointing towards the center, but then the entire object is pointing towards something else like it is reversed or inverted somehow.

https://www.dropbox.com/s/7nn9u55qigiaeew/QuaternionFlip2.mov?dl=0

Lighting.zip (194.2 KB)

thx for all the help