aboutsummaryrefslogtreecommitdiffstats
path: root/tests/manual/friction/main.qml
blob: 6f8c307ecb7448cb0d7fe35d58af3ccd382de994 (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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
// Copyright (C) 2022 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
import QtQuick
import QtQuick3D
import QtQuick3D.Physics
import QtQuick3D.Helpers
import QtQuick.Controls
import QtQuick.Layouts

Window {
    id: appWindow
    width: 1280
    height: 720
    visible: true
    title: qsTr("PhysX Friction Test")

    PhysicsWorld {
        id: physicsWorld
        gravity: Qt.vector3d(0, -9.81, 0)
        running: false
        typicalLength: typicalLengthSlider.value
        typicalSpeed: typicalSpeedSlider.value
        scene: viewport.scene
    }

    View3D {
        id: viewport
        anchors.fill: parent

        environment: SceneEnvironment {
            antialiasingMode: SceneEnvironment.MSAA
        }

        focus: true
        Keys.onSpacePressed: {
            console.log("toggle physics")
            physicsWorld.running = !physicsWorld.running
        }

        Node {
            id: scene1
            PerspectiveCamera {
                id: camera1
                z: 5
                x: -2
                y: 1
                eulerRotation : Qt.vector3d(-20, -20, 0)
                clipFar: 500
                clipNear: 0.01
            }

            DirectionalLight {
                eulerRotation: Qt.vector3d(-45, 45, 0)
                castsShadow: true
                brightness: 1
                shadowMapQuality: Light.ShadowMapQualityVeryHigh
                shadowBias: 0.05
                pcfFactor: 0.005
            }


            PhysicsMaterial {
                id: testMaterial
                staticFriction: staticFrictionSlider.value
                dynamicFriction: dynamicFrictionSlider.value
                restitution: restitutionSlider.value
            }

            DynamicRigidBody {
                id: floor
                isKinematic: true
                eulerRotation: Qt.vector3d(-79, -90, 0)
                kinematicEulerRotation: Qt.vector3d(-79, -90, 0)
                scale: Qt.vector3d(0.3, 0.3, 0.3)
                physicsMaterial: testMaterial
                collisionShapes: PlaneShape {
                    enableDebugDraw: true
                }
                Model {
                    source: "#Rectangle"
                    materials: PrincipledMaterial {
                        baseColor: "green"
                    }
                }
            }

            DynamicRigidBody {
                id: box
                physicsMaterial: testMaterial
                mass: 50

                function init() {
                    console.log("Reinit pos")
                    box.reset(Qt.vector3d(0, 5.9, 0), Qt.vector3d(0, 0, 0));
                }

                y: 0.9
                Model {
                    source: "#Cube"
                    materials: PrincipledMaterial {
                        baseColor: "red"
                    }
                    scale: Qt.vector3d(0.01, 0.01, 0.01)
                }
                collisionShapes: BoxShape {
                    extents: Qt.vector3d(1, 1, 1)
                    enableDebugDraw: true
                }
            }
        } // scene

    } // View3D

    WasdController {
        keysEnabled: true
        property bool controllingUnit: false
        controlledObject: camera1
        speed: 0.02
        shiftSpeed: 0.2

        Keys.onPressed: (event)=> {
            if (keysEnabled) handleKeyPress(event);
            if (event.key === Qt.Key_Space) {
                if (box.isKinematic) {
                    console.log("set box to moving")
                    box.isKinematic = false
                    physicsWorld.running = true
                } else {
                    physicsWorld.running = !physicsWorld.running
                    console.log("Set physics to", physicsWorld.running)
                }
            } else if (event.key === Qt.Key_J) {
                floor.kinematicEulerRotation.x++
            } else if (event.key === Qt.Key_K) {
                floor.kinematicEulerRotation.x--
            } else if (event.key === Qt.Key_I) {
                box.init()
            }
        }
        Keys.onReleased: (event)=> { if (keysEnabled) handleKeyRelease(event) }
    }

    ColumnLayout {

        Label {
            text: "Static friction: " + staticFrictionSlider.value
        }
        Slider {
            id: staticFrictionSlider
            focusPolicy: Qt.NoFocus
            from: 0
            to: 1
            value: 0.1
        }

        Label {
            text: "Dynamic friction: " + dynamicFrictionSlider.value
        }
        Slider {
            id: dynamicFrictionSlider
            focusPolicy: Qt.NoFocus
            from: 0
            to: 1
            value: 0.1
        }

        Label {
            text: "Restitution: " + restitutionSlider.value
        }
        Slider {
            id: restitutionSlider
            focusPolicy: Qt.NoFocus
            from: 0
            to: 1
            value: 0.1
        }

        Label {
            text: "Typical length: " + typicalLengthSlider.value
        }
        Slider {
            id: typicalLengthSlider
            focusPolicy: Qt.NoFocus
            from: 0
            to: 10
            value: 1
        }

        Label {
            text: "Typical speed: " + typicalSpeedSlider.value
        }
        Slider {
            id: typicalSpeedSlider
            focusPolicy: Qt.NoFocus
            from: 0
            to: 10
            value: 1
        }
    }
}