Qt Quick 3D Physics - Simple Example▲
This example gives an introductory overview of the basic Quick 3D Physics features by going through the code of a simple example. Note that this introduction assumes a familiarity with the Qt Quick 3D module.
Setup▲
The whole example is contained and set up in the main.qml file. Let's first look at the object that creates the physical world. This node is called https://:
PhysicsWorld {
scene: viewport.scene
}We set the https:// property to the scene of our https://. The scene property defines where the simulation will look for physics nodes to simulate. Any physics nodes placed outside of the scene node will not be part of the simulation.
Scene▲
Let's have a look at the scene.
View3D {
id: viewport
anchors.fill: parent
environment: SceneEnvironment {
clearColor: "#d6dbdf"
backgroundMode: SceneEnvironment.Color
}
PerspectiveCamera {
position: Qt.vector3d(-200, 100, 500)
eulerRotation: Qt.vector3d(-20, -20, 0)
clipFar: 5000
clipNear: 1
}
DirectionalLight {
eulerRotation.x: -45
eulerRotation.y: 45
castsShadow: true
brightness: 1
shadowFactor: 100
}
StaticRigidBody {
position: Qt.vector3d(0, -100, 0)
eulerRotation: Qt.vector3d(-90, 0, 0)
collisionShapes: PlaneShape {}
Model {
source: "#Rectangle"
scale: Qt.vector3d(10, 10, 1)
materials: DefaultMaterial {
diffuseColor: "green"
}
castsShadows: false
receivesShadows: true
}
}
DynamicRigidBody {
position: Qt.vector3d(-100, 100, 0)
collisionShapes: BoxShape {
id: boxShape
}
Model {
source: "#Cube"
materials: PrincipledMaterial {
baseColor: "yellow"
}
}
}
DynamicRigidBody {
position: Qt.vector3d(0, 100, 0)
collisionShapes: SphereShape {
id: sphereShape
}
Model {
source: "#Sphere"
materials: PrincipledMaterial {
baseColor: "blue"
}
}
}
DynamicRigidBody {
position: Qt.vector3d(75, 200, 0)
collisionShapes: CapsuleShape {
id: capsuleShape
}
Model {
geometry: CapsuleGeometry {}
materials: PrincipledMaterial {
baseColor: "red"
}
}
}
}Our scene is just a https:// since we want to draw our physical objects. It contains a https:// and a https:// for rendering, but more importantly it contains a https:// and three https:// nodes. In physics, a rigid body is a non-deformable, solid body with uniform density. We have two types of rigid bodies available; https:// and https://. A https:// is a QML node which contains a static (immovable) rigid body. It is technically possible to move the body but it will incur a performance penalty. A https:// on the other hand is used for objects that can move. A https:// has a https:// property, when it is set to true the rigid body will not be influenced by external forces and can be controlled from scripts and animations. For instance a football would not be kinematic but an elevator or a moving platform typically would. This also means that if you update the position property of the football it would not move to that position whereas the elevator would.
To be able to interact with other physical objects the https:// property needs to be set. The https:// property is a list that can contain one or more shapes and will act as one rigid unit combined. You can position and rotate these shapes in relation to each other. Note that plane, triangle mesh and heightmap shapes only work with dynamic bodies if the body is kinematic.
Shapes▲
Our scene contains four physical objects (plane, box, ball, capsule). We will go through them one at a time.
Plane▲
StaticRigidBody {
position: Qt.vector3d(0, -100, 0)
eulerRotation: Qt.vector3d(-90, 0, 0)
collisionShapes: PlaneShape {}
Model {
source: "#Rectangle"
scale: Qt.vector3d(10, 10, 1)
materials: DefaultMaterial {
diffuseColor: "green"
}
castsShadows: false
receivesShadows: true
}
}We create a plane by making a https:// node. In this node we set the https:// property to contain a https://. Planes divide space into "above" and "below" them. Everything "below" the plane will collide with it and be pushed above it. The Plane lies on the YZ plane with "above" pointing towards positive X. We put a Model inside our https:// node to render the plane. This is a common pattern which makes sure that the model also moves and rotates in the same way when the physical object interacts in the scene. Since we want the plane to lie on the XY plane we rotate it using the eulerRotation property. We rotate the model in a similar way.
Box▲
DynamicRigidBody {
position: Qt.vector3d(-100, 100, 0)
collisionShapes: BoxShape {
id: boxShape
}
Model {
source: "#Cube"
materials: PrincipledMaterial {
baseColor: "yellow"
}
}
}We create a sphere by making a https:// node and a single https:// in https://. Since this is a dynamic node the box will interact with the scene by colliding and moving like a free object. Since the cube model is a hundred times bigger than a unit cube we need to scale it accordingly. Since this is a dynamic body and thus have a physical weight we set the https:// property.
Sphere▲
DynamicRigidBody {
position: Qt.vector3d(0, 100, 0)
collisionShapes: SphereShape {
id: sphereShape
}
Model {
source: "#Sphere"
materials: PrincipledMaterial {
baseColor: "blue"
}
}
}We create a sphere by making a https:// node and a single https:// in https://.
Capsule▲
DynamicRigidBody {
position: Qt.vector3d(75, 200, 0)
collisionShapes: CapsuleShape {
id: capsuleShape
}
Model {
geometry: CapsuleGeometry {}
materials: PrincipledMaterial {
baseColor: "red"
}
}
}We create a capsule by making a https:// node and a single https:// in https://. We build a capsule model by using the built-in CapsuleGeometry provided by Qt Quick 3D Physics.
List of Files▲
Files:
List of Files▲
-
simple/CMakeLists.txt
-
simple/main.cpp
-
simple/main.qml
-
simple/qml.qrc
-
simple/simple.pro



