summaryrefslogtreecommitdiffstats
path: root/quick3d/chapter04/index.rst
blob: de5a7c46a2712678dc0bc4dfa41c46c45e2fecc6 (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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
..
    ---------------------------------------------------------------------------
    Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
    All rights reserved.
    This work, unless otherwise expressly stated, is licensed under a
    Creative Commons Attribution-ShareAlike 2.5.
    The full license document is available from
    http://creativecommons.org/licenses/by-sa/2.5/legalcode .
    ---------------------------------------------------------------------------

Player Movement
===============

In our game, we want the player to control the     hamburger* movement using the keyboard input. It should be possible to move it from left to right and up and down. Furthermore, the orientation should depend on the current acceleration.

In order to achieve realistic flight behavior, the movement is controlled using some basic movement equations. For the sake of good order, we will be implementing the logic code of our game into a new `Gamelogic.qml` file.


Update-timer
------------

Before we implement the movement equations, we do need an `update` timer that periodically updates the     hamburger*`s position and acceleration. An interval value of *50ms* should be sufficient for achieving fluent movement.

Later on, within the `onTriggereed` signal handler, the movement equations will be processed and the     hamburger* position should be updated.

.. code-block:: js

    //Gamelogic.qml

    import QtQuick 2.0

    Item {

        Timer {
           id: gameTimer
           running: true
           interval: 50
           repeat: true
           onTriggered: {
           ...
            // update position...
           }
        }
    }


Keyinput
--------

To handle the keybord input, we first need to set the value of the `focus` property of the root item to `true` to handle the key events. We also need four new variables (one for each key), which are either set to `true` or `false` depending on the press state of the keys. Within `onPressed` and `onReleased` we handle the eventual key events as shown in the code below:

 This whole construct is necessary because we want to allow the user to press and hold more then one key at a time. We also need four new variables (one for each key), which are either set to `true` or `false` depending on the press state of the keys. Movement processing is then performed in the update-timer's `onTriggered` signal:

.. code-block:: js


  Item {
    ...
    focus: true
    property bool upPressed: false
    property bool downPressed: false
    property bool leftPressed: false
    property bool rightPressed: false

    //Handling of basic key events
    Keys.onPressed: {
        if(event.key == Qt.Key_A)
            leftPressed = true
        if(event.key == Qt.Key_D)
            rightPressed = true
        if(event.key == Qt.Key_W)
            upPressed = true
        if(event.key == Qt.Key_S)
            downPressed = true
        if(event.key == Qt.Key_Space)
            fireLaser();
    }
    Keys.onReleased: {
        if(event.key == Qt.Key_A)
            leftPressed = false
        if(event.key == Qt.Key_D)
            rightPressed = false
        if(event.key == Qt.Key_W)
            upPressed = false
        if(event.key == Qt.Key_S)
            downPressed = false
    }
  }

Later we will perform the movement processing in the update-timer's `onTriggered` signal handler.

Then we have to instantiate the `Gamelogic` component in the main `game.qml` file.

.. code-block:: js

  //game.qml

  Viewport {
    ...
    Gamelogic {id: gameLogic}
    ...
  }


Basic motion equations
----------------------

In the our `SpaceBurger` game, the     hamburger* will be seen from the back (if there is any for a *hamburger*). So we set the camera's eye position to (`0, 0,-30`). The player can then move it on the `y` and `x` axes. To make sure that the *hamburger* will remain in the screen view, we define `x` and `y` boundaries that will restrict the movement. The `x` and `y` bounds could be calculated from the camera parameters, but a we can simply set `4.5` value for the x-bound and `5` value for the y-bound.

.. Note:: The `y` and `x` bound parameters will change with the aspect ratio of the viewport you are using and in general with the camera parameters!

.. code-block:: js

    //game.qml
    ...
    Viewport {
      ...
      property real x_bound: 4.5
      property real y_bound: 5
      ...
    }

To move the     hamburger* object, we will be using the `two basic motion equations for constant acceleration <http://en.wikipedia.org/wiki/Motion_equation#Constant_linear_acceleration>`_ . The motion equations are based on the acceleration, the current speed and the position values.

    //Velocity is acceleration multiplied with time plus the initial speed
    v = a    t + v0
    //Distance is velocity multiplied with time plus the initial distance
    s = v    t + s0

We create a new `Player.qml` file to define the `Hamburger` as a separate component, and calculate its speed and acceleration for the `x` and `y` axes an. Those values are then saved in the `vx`, `vy`, `ax` and `ay` properties as shown in the code below:

.. code-block:: js

    //Player.qml
    import QtQuick 2.0
    import Qt3D 1.0

    Item3D {

        property real vx: 0
        property real vy: 0

        property real ax: 0
        property real ay: 0

        mesh: Mesh { source: "hamburger/models/hamburger.dae" }

        scale: 0.1
    }

Since we can build a tree structure with an `Item3D`, we will define a root `Item3D` for the top level which contains all the visible 3D items of the scene. The `player` object will then be a child of an `Item3D` element. Furthermore, we set the `camera` to a position behind the burger:

.. code-block:: js

    //game.qml

    Viewport {
        ...
        Item3D {
            id: level

            Player {
                id: player
            }
        }

        camera: Camera {
           id: cam
           eye: Qt.vector3d(0, 0,-30)
        }
        ...
    }

We will also define a variable called `maneuverability` in the `Gamelogic.qml` in order to have better control over the flight parameters. A convenient value for the `maneuverability` will be 0.3:

.. code-block:: js

    // Gamelogic.qml
    ...
    property real maneuverability: 0.3
    //The game timer is our event loop. It processes the key events
    //and updates the position of the hamburger
    Timer {
        id: gameTimer
        running: true
        interval: 50
        repeat: true
        onTriggered: {
            //Velocity is updated
            player.vx+=player.ax    0.05
            player.vy+=player.ay    0.05
            //Acceleration is updated
            player.ax=(player.ax+maneuverability    leftPressed
          + maneuverability*rightPressed)/1.1
            player.ay=(player.ay+maneuverability    downPressed
          + maneuverability*upPressed)/1.1
            //Position is updated
            player.position.x += player.vx    0.05
            player.position.y += player.vy    0.05
            //If the player exceeds a boundary, the movement is stopped
            if (player.position.x>x_bound) {
                player.position.x = x_bound
                player.vx = 0;
                if (player.ax>0)
                    player.ax = 0
            }
            else if (player.position.x<-x_bound) {
                player.position.x = -x_bound
                player.vx = 0
                if (player.ax<0)
                    player.ax = 0
            }
            else if (player.position.y<-y_bound) {
                player.position.y = -y_bound
                player.vy = 0
                if (player.ay<0)
                    player.ay = 0
            }
            else if (player.position.y>y_bound) {
                player.position.y = y_bound
                player.vy = 0
                if (player.ay>0)
                    player.ay = 0
            }
        }
    }
    ...

Now we should be able to move the     hamburger* smoothly over the screen and the movement should stop on the viewport boundaries.

.. note:: For a realistic flight behavior, the     hamburger* should turn into the flight direction.

Transformations
---------------

There are currently four transformation types available in the `Qt3D` module: `Rotation3D`, `Scale3D`, `Translation3D` and `LookAtTransform`. The names should be fairly self-explanatory.

One or more transformations can be applied to an `Item3D`'s `transform` or `pretransform` properties. The `pretransform` property however is intended to transform the model before all other transformations, because it may be in an unconventional scale, rotation or translation after loading.

As explained above, we want the     hamburger* to rotate in the flight direction, so we need to achieve three things:

    * When moving *hamburger* along the `x` axis (left or right), the *hamburger* should roll a bit into flight direction. (the rotation axis is the `z` axis)
    * When moving *hamburger* along the `x` axis (left or right), it should move the nose in flight direction. (the rotation axis is the `y` axis)
    * When moving *hamburger* along the `y` axis (up or down), the *hamburger* should move its front up or down. (the rotation axis is the `x` axis)

Now we can add the different transformations to the `transform` property in the  `Player.qml` and specify their axis. We are connecting the angle of each rotation directly to the acceleration, which will have a fairly good-looking result. The scalar factors have been obtained by trial and error:

.. code-block:: js

    //Player.qml
    ...
    transform: [
        Rotation3D {
            angle: -10    ay
            axis: "1, 0, 0"
        },
        Rotation3D {
            angle: 5    ax
            axis: "0, 1, 0"
        },
        Rotation3D {
            angle: -20    ax
            axis: "0, 0, 1"
        }
    ]
    ...


When moving the     hamburger*, you might notice that the rolling behavior is a bit strange. That is because the balance point of the object is not at the origin. We can however correct this very easily by applying a `Translation3D` to the `pretransform` property. In addition to this, the scaling was moved into `pretransform` as well (i.e we have to remove the scale property in the `Player`). Furthermore a rotation of `45°` on the `y` axis was added for aesthetic reasons.

.. code-block:: js

    //Player.qml
    pretransform: [
        Scale3D {
            scale: 0.1
        },
    //Moving the objects origin into the balance point
        Translation3D {
            translate: "0,-1,0"
        },
        Rotation3D {
            angle: 45
            axis: "0, 1, 0"
        }
    ]
    ...

The     *hamburger** object could now be controlled by the player:

.. image:: img/movement.png
    :scale: 60%
    :align: center

.. rubric:: What's Next?

Next we add the `onion rings` to be hit by the player in our game. For this, we will introduce dynamic object creation, collision detection and how to use textures and predefined shapes.