Qt Sensors - Maze QML Example▲
Maze in QML▲
To write a QML application that will use the TiltSensor QML sensors type you need to do the following steps:
To import the Qt Sensors QML types into your application, use the following import statement in your .qml file:
import
QtSensors
Then, add the Sensor QML types into your qml file.
In this example we use the TiltSensor:
TiltSensor
{
id
:
tiltSensor
active
:
true
}
The mouse should move by a factor of the tilt value:
var xstep =
0
;
xstep =
tiltSensor.reading.yRotation *
0.1 //acceleration
var ystep =
0
;
ystep =
tiltSensor.reading.xRotation *
0.1 //acceleration
The walk direction of the mouse takes into account some collision detection:
if
(xstep &
lt; 1
&
amp;&
amp; xstep &
gt; 0
)
xstep =
0
else
if
(xstep &
gt; -
1
&
amp;&
amp; xstep &
lt; 0
)
xstep =
0
if
(ystep &
lt; 1
&
amp;&
amp; ystep &
gt; 0
)
ystep =
0
;
else
if
(ystep &
gt; -
1
&
amp;&
amp; ystep &
lt; 0
)
ystep =
0
;
if
((xstep &
lt; 0
&
amp;&
amp; mouseCtrl.x &
gt; 0
&
amp;&
amp; Lib.canMove(mouseCtrl.x +
xstep,mouseCtrl.y))) {
xval =
mouseCtrl.
x +
xstep;
}
else
if
(xstep &
gt; 0
&
amp;&
amp; mouseCtrl.x &
lt; (Lib.cellDimension *
(Lib.dimension -
1
))
&
amp;&
amp; Lib.canMove(mouseCtrl.x +
xstep,mouseCtrl.y)) {
xval =
mouseCtrl.
x +
xstep;
}
else
xval =
mouseCtrl.x;
if
(ystep &
lt; 0
&
amp;&
amp; mouseCtrl.y &
gt; 0
&
amp;&
amp; Lib.canMove(mouseCtrl.x, mouseCtrl.y +
ystep)) {
yval =
mouseCtrl.
y +
ystep;
}
else
if
(ystep &
gt; 0
&
amp;&
amp; (mouseCtrl.y &
lt; (Lib.cellDimension *
(Lib.dimension -
1
)))
&
amp;&
amp; Lib.canMove(mouseCtrl.x, mouseCtrl.y +
ystep)) {
yval =
mouseCtrl.
y +
ystep;
}
else
yval =
mouseCtrl.y
mouseCtrl.move(xval, yval);
The rotation of the mouse image is determined according to the angle that the mouse is moving.
var a =
newy -
mouse.y
var b =
newx -
mouse.x
angle
=
Math.atan2(-
b, a) *
mouse.radians_to_degrees
if
(angle
&
lt; 0
)
angle
=
360
+
angle
img.rotation =
angle
mouse.x =
newx;
mouse.y =
newy;