Qt Quick 3D Physics - Planet Example
Demonstrates using custom gravity configuration for character controllers and rigid bodies.

The Planet Example demonstrates how to implement dynamic, point-centered spherical gravity for a CharacterController and DynamicRigidBody objects in Qt Quick 3D Physics. Instead of relying on a uniform downward gravity vector, a GravityField in Point mode applies a directional gravity force pointing toward the center of the planet, allowing the character to walk seamlessly around a 3D planetoid surface while physics objects fall toward its center.
Overview
In standard physics scenes, gravity is uniform and points along the negative Y-axis. For spherical worlds or micro-planets, the gravity vector must be calculated dynamically based on an object's position relative to the planet's center.
This example highlights four main mechanics:
- Applying a dynamic gravity vector pointing toward the planet center with a GravityField.
- Orienting the CharacterController using its
gravityOrientationproperty combined with player input. - Pulling the cubes towards the center with the same GravityField as the character.
- Customizing the CharacterController slope limit, maximum step height, and sliding down steep slopes for curved geometry.
The example also provides a control panel to adjust the gravity strength, the planet size, the number of cubes, the movement speed, the slope limit, the maximum step height, whether the character slides down steep slopes, and the mid-air control while the scene is running.
Dynamic Gravity Calculation
The global PhysicsWorld has no gravity. Instead, a GravityField in Point mode pulls the character controller and the cubes towards the center of the planet:
GravityField {
id: gravityField
mode: GravityField.Point
gravity: gravitySlider.value
unitDistance: view3D.planetSize * 0.5
}Surface Alignment and Rotation
To keep the character upright relative to the spherical surface, its rotation incorporates the automatic alignment provided by gravityOrientation.
When navigating spherical or complex curved surfaces, traditional approaches compute surface orientation from scratch each frame using cross-products against a fixed reference vector:
QVector3D forward = {0, 0, -1};
QVector3D right = QVector3D::crossProduct(forward, up).normalized();This classic method inevitably hits a geometric limitation as the character approaches the sphere poles. When the gravity vector aligns directly with the global reference axis, the vectors lose orthogonal stability, causing sudden snapping or unwanted spinning.
To solve this singularity problem, the gravityOrientation property uses an incremental delta-rotation approach (parallel transport):
- It tracks the previous surface alignment vector derived from the local gravity field.
- As the character moves, it calculates the shortest arc rotation (
QQuaternion::rotationTo) between the old gravity normal and the new gravity normal. - This gravitational delta is incrementally accumulated, bypassing global world axes and preventing polar instability.
In QML, this orientation is seamlessly combined with the player's runtime look direction:
rotation: playerController.gravityOrientation.times(Quaternion.fromEulerAngles(0, wasd.rotationY, 0))Dynamic Objects Spawning
In addition to the character controller, dynamic cubic rigid bodies are spawned near the character's starting position. The GravityField pulls each of them towards the center of the planet, so the spawned cubes fall onto the surface. Changing the number of cubes or the size of the planet removes the cubes and spawns them again.
Files:
See also Qt Quick 3D Physics, CharacterController, PhysicsWorld, and DynamicRigidBody.
© 2026 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.