On this page

GravityField QML Type

A gravity of its own, for the whole scene or a region of it. More...

Import Statement: import QtQuick3D.Physics
Since: Qt 6.13
Inherits:

PhysicsNode

Properties

Detailed Description

A GravityField applies its own gravity to every DynamicRigidBody and CharacterController it acts on, on top of the gravity they already have: the world's gravity for a dynamic rigid body, and its own gravity property for a character controller.

A field with no collisionShapes acts on every body in the scene, wherever it is. A field with collision shapes acts only on the bodies that overlap them, and stops acting on a body when it leaves them. That is how you build a low gravity room, an updraft, a corridor where down is sideways, or the space around a planet.

In the default Directional mode the gravity points the same way and has the same strength everywhere, as world gravity does; it is uniform rather than Newtonian. In Point mode it points at the field's own position, which is how you build a spherical or planetary world: bodies fall toward it from any direction, and with unitDistance set the gravity weakens with distance, as Newtonian gravity does.

A negative gravity reverses either mode. A Point field with a negative gravity pushes bodies away from its position instead of pulling them in, which is how you build a repulsor.

A body does not need sendTriggerReports set to be affected by a field with collision shapes, and the field does not report the bodies inside it either: it has no equivalent of TriggerBody's bodyEntered and bodyExited signals or its collisionCount property, and a body's enteredTriggerBody and exitedTriggerBody signals are not emitted for it. To be notified of what enters the shapes, or to count what is inside them, place a TriggerBody with the same shapes alongside the field. The contact and trigger report properties of PhysicsNode have no effect on a GravityField.

A field adds its gravity to what a body already has rather than replacing it, so fields compose with world gravity, with each other, and with a character controller's own gravity. A field never overwrites a value, or a binding, that you set yourself, and a field stopping to act on a body simply stops its contribution. A DynamicRigidBody with gravityEnabled set to false is not affected by a field either.

A CharacterController has no such switch. Setting its gravity to zero only takes away its own gravity, not that of the fields acting on it, so a character that is to be free of gravity altogether, for instance while it swims or climbs a ladder, has to be kept out of the fields with the filter groups described below.

A field respects filterGroup and filterIgnoreGroups: a body in a group the field ignores, or one that ignores the field's group, is left alone. For a field without collision shapes this is how part of the scene is kept out of it, which world gravity cannot do.

For example, the following weakens gravity inside a box by pushing back against it, so that bodies inside fall at a fifth of the usual rate:

GravityField {
    gravity: -785
    collisionShapes: BoxShape { extents: Qt.vector3d(400, 400, 400) }
}

and this defines a planet that bodies fall toward from any direction and any distance:

PhysicsWorld {
    // Required: world gravity would otherwise keep pulling everything
    // down on top of the planet's gravity.
    gravity: Qt.vector3d(0, 0, 0)
}

StaticRigidBody {
    id: planet
    collisionShapes: SphereShape { diameter: 280 }
}

GravityField {
    position: planet.position
    mode: GravityField.Point
    gravity: 981
    unitDistance: 140
}

Note that PhysicsWorld.gravity has to be set to a zero vector here. World gravity defaults to pulling everything down, and a field adds to it rather than replacing it, so without that the bodies would fall down past the planet instead of onto it.

Fields add up, so two Point fields with unitDistance set give the combined gravity of a planet and its moon, weakening with distance from each.

The two ways of finding bodies differ in what they cost. A field with collision shapes finds what is inside them the way a TriggerBody does, which costs the simulation one collision pair per body it overlaps, updated on every step for as long as that body is awake. That is cheap for shapes that cover part of a scene. A field without collision shapes goes through every body in the scene, one at a time, before every step, and is applied to the awake ones it acts on, which costs more in a scene with many bodies.

This also decides what moving a field costs. Moving a field with shapes only involves the bodies inside them, however many bodies the scene has. A field that moves around should therefore be given shapes, sized to the area it should affect. A Point field without shapes wakes every body it acts on on each step it moves, so moving one keeps all of those bodies awake. Moving a Directional field without shapes changes nothing, since its gravity does not depend on where it is.

A Directional field without shapes or filter groups does what world gravity does; prefer world gravity then, which is applied to all bodies at once as part of each simulation step, and so performs better, since you do not pay for filtering that you do not use. Keep in mind that world gravity does not act on a CharacterController, so a character then needs its own gravity set.

Note: A CharacterController is pulled by a Point field like any other body, but it still stands and walks with respect to the world's up axis, so it cannot walk on the side of a planet built this way. It falls toward the field's position, and lands on, walks over, and steps up whatever is below it in world space.

Note: A body resting on a surface is still allowed to go to sleep, exactly as it would under world gravity. Bodies are woken when the field starts or stops acting on them, for instance as they enter or leave its shapes or as filter groups change, when its gravity changes, when a Point field moves, and when the field is destroyed, not continuously while the field acts on them.

See also TriggerBody, DynamicRigidBody, and CharacterController.

Property Documentation

direction : vector3d [default: Qt.vector3d(0, -1, 0), since 6.13]

This property defines the direction the field's gravity points in, when mode is GravityField.Directional. It need not be normalized; only its direction is used, with gravity giving the magnitude. It is ignored in Point mode.

It is in scene space, like the world's own gravity, so rotating the field turns its collision shapes without turning the gravity.

This property was introduced in Qt 6.13.

See also mode and gravity.

gravity : real [default: 981, since 6.13]

This property defines the gravitational acceleration applied to bodies, along direction in Directional mode or toward the field's position in Point mode. In Point mode with unitDistance set, it is the acceleration at that distance from the field's position, and weaker elsewhere. A negative value reverses it: against direction, or away from the field's position rather than toward it, which makes a Point field a repulsor.

This property was introduced in Qt 6.13.

See also unitDistance and PhysicsWorld::gravity.

mode : enumeration [default: GravityField.Directional, since 6.13]

This property defines which way the field's gravity points.

ConstantDescription
GravityField.DirectionalAlong direction, the same everywhere. This is the default.
GravityField.PointToward the field's own position, so bodies fall in from any direction, or away from it with a negative gravity. unitDistance applies in this mode only.

This property was introduced in Qt 6.13.

See also direction and gravity.

unitDistance : real [default: 0, since 6.13]

This property defines, in Point mode, the distance from the field's position at which the acceleration equals gravity. Beyond it the acceleration falls off with the inverse square of the distance, as Newtonian gravity does, so at twice the distance it is a quarter as strong. Inside it the acceleration falls off linearly toward the centre, as it would inside a solid sphere of uniform density, so at half the distance it is half as strong, and at the centre there is none. For example, for a planet with a radius of 100 and a surface gravity of 4, set gravity to 4 and unitDistance to 100.

A value of 0 disables falloff, giving the same acceleration everywhere. A body exactly at the field's position then has no direction to fall in and is given no acceleration.

It is ignored in Directional mode.

Range: [0, inf)

This property was introduced in Qt 6.13.

See also mode and gravity.

© 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.