Qt Quick 3D Physics - Cannon Example▲
This example demonstrates how to create and delete physical objects on demand. The scene consists of a number of stacks of boxes. You can move around by using WASD and the mouse and shoot a ball by pressing space.
The scene is setup with the usual Qt Quick 3D objects like view, camera and light:
Sélectionnez
PerspectiveCamera {
id
:
camera
position
:
Qt.vector3d(-
4000
, 5000
, 10000
)
eulerRotation :
Qt.vector3d(-
20
, -
20
, 0
)
clipFar
:
500000
clipNear
:
100
}
DirectionalLight {
eulerRotation
:
Qt.vector3d(-
45
, 45
, 0
)
castsShadow
:
true
brightness
:
1
shadowMapQuality
:
Light.ShadowMapQualityVeryHigh
shadowFactor
:
100
}
We also add a static floor:
Sélectionnez
StaticRigidBody {
eulerRotation
:
Qt.vector3d(-
90
, 0
, 0
)
collisionShapes
:
PlaneShape {}
Model {
source
:
"#Rectangle"
scale
:
Qt.vector3d(2000
, 2000
, 0
)
materials
:
PrincipledMaterial {
baseColor
:
"green"
}
castsShadows
:
false
receivesShadows
:
true
}
}
We create a Node we use as the spawner of objects and put inside our view:
Sélectionnez
Node {
id
:
shapeSpawner
property
var instancesBoxes
:
[]
property
var instancesSpheres
:
[]
property
int
stackCount
:
0
function
createStack(stackZ) {
var size =
10
;
var extents =
400
;
for (
var i =
0
;
i &
lt;
size;
i++
) {
for (
var j =
0
;
j &
lt;
size-
i;
j++
) {
var component =
Qt.createComponent
(
"box.qml"
);
let x =
j*
2
-
size +
i;
let y =
i*
2
+
1
;
let z =
-
5
*
stackZ;
let center =
Qt.vector3d
(
x,
y,
z).times
(
0
.
5
*
extents);
let box =
component.createObject
(
shapeSpawner,
{
position
:
center,
xyzExtents
:
extents}
);
instancesBoxes.push
(
box);
if (
box ===
null) {
console.log
(
"Error creating object"
);
}
}
}
}
function
createBall(position
, forward
) {
var diameter =
600
;
var speed =
20000
;
var component =
Qt.createComponent
(
"sphere.qml"
);
let sphere =
component.createObject
(
shapeSpawner,
{
position
:
position,
linearVelocity
:
forward
.times
(
speed),
sphereDiameter
:
diameter}
);
instancesSpheres.push
(
sphere);
if (
sphere ===
null) {
console.log
(
"Error creating object"
);
}
}
function
reset() {
instancesSpheres.forEach
(
sphere =&
gt;
{
sphere.destroy
(
);
}
);
instancesBoxes.forEach
(
boxes =&
gt;
{
boxes.destroy
(
);
}
);
instancesSpheres =
[];
instancesBoxes =
[];
for (
var stackI =
0
;
stackI &
lt;
stackSlider.
value;
stackI