aboutsummaryrefslogtreecommitdiffstats
path: root/examples/quick3dphysics/charactercontroller/main.qml
blob: 1d37d6b7d60bb29a12c6d3c68d78215992c5039d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// Copyright (C) 2023 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause

import QtQuick
import QtQuick3D
import QtQuick3D.Helpers
import QtQuick3D.Physics
import QtQuick3D.Physics.Helpers as Helpers

Window {
    width: 1280
    height: 720
    visible: true
    title: qsTr("Qt Quick 3D Physics - Character Controller")
    PhysicsWorld {
        id: physicsWorld
        running: true
        scene: viewport.scene
    }

    View3D {
        id: viewport
        anchors.fill: parent

        environment: SceneEnvironment {
            clearColor: "#8090d0"
            backgroundMode: SceneEnvironment.SkyBox
            antialiasingMode: SceneEnvironment.MSAA
            antialiasingQuality: SceneEnvironment.High
            lightProbe: Texture {
                textureData: ProceduralSkyTextureData {}
            }
            specularAAEnabled: true
        }

        DirectionalLight {
            eulerRotation.x: -30
            eulerRotation.y: -70
            ambientColor: "#666"
            brightness: 0.1
        }

        //! [building]
        Building {
            id: building
            onTeleporterTriggered: character.teleportHome()
        }
        //! [building]

        //! [charactercontroller]
        CharacterController {
            id: character
            //! [position]
            property vector3d startPos: Qt.vector3d(800, 175, -850)
            position: startPos
            function teleportHome() {
                character.teleport(character.startPos)
                wasd.cameraRotation.x = 180
            }
            //! [position]

            //! [capsuleshape]
            collisionShapes: CapsuleShape {
                id: capsuleShape
                diameter: 50
                height: wasd.crouchActive ? 0 : 100
                Behavior on height {
                    NumberAnimation { duration: 300 }
                }
            }
            property real characterHeight: capsuleShape.height + capsuleShape.diameter
            //! [capsuleshape]

            //! [triggerreports]
            sendTriggerReports: true
            //! [triggerreports]

            //! [movement]
            movement: Qt.vector3d(wasd.sideSpeed, 0, wasd.forwardSpeed)
            Behavior on movement {
                PropertyAnimation { duration: 200 }
            }
            //! [movement]

            //! [gravity]
            gravity: building.inGravityField ? Qt.vector3d(0, 100, 0) : physicsWorld.gravity
            //! [gravity]

            //! [camera]
            eulerRotation.y: wasd.cameraRotation.x
            PerspectiveCamera {
                id: camera
                position: Qt.vector3d(0, character.characterHeight / 2 - 10, 0)
                eulerRotation.x: wasd.cameraRotation.y
                clipFar: 10000
                clipNear: 10
            }
            //! [camera]
        }
        //! [charactercontroller]
    }

    //! [wasd]
    Wasd {
        id: wasd
        property real walkingSpeed: 500
        property real speedFactor: sprintActive ? 3 : crouchActive ? 0.3 : 1
        property real sideSpeed: (moveLeft ? -1 : moveRight ? 1 : 0) * walkingSpeed * speedFactor
        property real forwardSpeed: (moveForwards ? -1 : moveBackwards ? 1 : 0) * walkingSpeed * speedFactor
        cameraRotation.x: 180
    }
    //! [wasd]
}