| 1 | /****************************************************************************
|
|---|
| 2 | **
|
|---|
| 3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
|---|
| 4 | ** All rights reserved.
|
|---|
| 5 | ** Contact: Nokia Corporation (qt-info@nokia.com)
|
|---|
| 6 | **
|
|---|
| 7 | ** This file is part of the examples of the Qt Toolkit.
|
|---|
| 8 | **
|
|---|
| 9 | ** $QT_BEGIN_LICENSE:BSD$
|
|---|
| 10 | ** You may use this file under the terms of the BSD license as follows:
|
|---|
| 11 | **
|
|---|
| 12 | ** "Redistribution and use in source and binary forms, with or without
|
|---|
| 13 | ** modification, are permitted provided that the following conditions are
|
|---|
| 14 | ** met:
|
|---|
| 15 | ** * Redistributions of source code must retain the above copyright
|
|---|
| 16 | ** notice, this list of conditions and the following disclaimer.
|
|---|
| 17 | ** * Redistributions in binary form must reproduce the above copyright
|
|---|
| 18 | ** notice, this list of conditions and the following disclaimer in
|
|---|
| 19 | ** the documentation and/or other materials provided with the
|
|---|
| 20 | ** distribution.
|
|---|
| 21 | ** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
|
|---|
| 22 | ** the names of its contributors may be used to endorse or promote
|
|---|
| 23 | ** products derived from this software without specific prior written
|
|---|
| 24 | ** permission.
|
|---|
| 25 | **
|
|---|
| 26 | ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|---|
| 27 | ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|---|
| 28 | ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|---|
| 29 | ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|---|
| 30 | ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|---|
| 31 | ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|---|
| 32 | ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|---|
| 33 | ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|---|
| 34 | ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|---|
| 35 | ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|---|
| 36 | ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
|
|---|
| 37 | ** $QT_END_LICENSE$
|
|---|
| 38 | **
|
|---|
| 39 | ****************************************************************************/
|
|---|
| 40 |
|
|---|
| 41 | #include "car.h"
|
|---|
| 42 | #include <QtGui/QtGui>
|
|---|
| 43 | #include <math.h>
|
|---|
| 44 |
|
|---|
| 45 | static const double Pi = 3.14159265358979323846264338327950288419717;
|
|---|
| 46 |
|
|---|
| 47 | QRectF Car::boundingRect() const
|
|---|
| 48 | {
|
|---|
| 49 | return QRectF(-35, -81, 70, 115);
|
|---|
| 50 | }
|
|---|
| 51 |
|
|---|
| 52 | Car::Car() : color(Qt::green), wheelsAngle(0), speed(0)
|
|---|
| 53 | {
|
|---|
| 54 | startTimer(1000 / 33);
|
|---|
| 55 | setFlag(QGraphicsItem::ItemIsMovable, true);
|
|---|
| 56 | setFlag(QGraphicsItem::ItemIsFocusable, true);
|
|---|
| 57 | }
|
|---|
| 58 |
|
|---|
| 59 | void Car::accelerate()
|
|---|
| 60 | {
|
|---|
| 61 | if (speed < 10)
|
|---|
| 62 | ++speed;
|
|---|
| 63 | }
|
|---|
| 64 |
|
|---|
| 65 | void Car::decelerate()
|
|---|
| 66 | {
|
|---|
| 67 | if (speed > -10)
|
|---|
| 68 | --speed;
|
|---|
| 69 | }
|
|---|
| 70 |
|
|---|
| 71 | void Car::turnLeft()
|
|---|
| 72 | {
|
|---|
| 73 | if (wheelsAngle > -30)
|
|---|
| 74 | wheelsAngle -= 5;
|
|---|
| 75 | }
|
|---|
| 76 |
|
|---|
| 77 | void Car::turnRight()
|
|---|
| 78 | {
|
|---|
| 79 | if (wheelsAngle < 30)
|
|---|
| 80 | wheelsAngle += 5;
|
|---|
| 81 | }
|
|---|
| 82 |
|
|---|
| 83 | void Car::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
|
|---|
| 84 | {
|
|---|
| 85 | Q_UNUSED(option);
|
|---|
| 86 | Q_UNUSED(widget);
|
|---|
| 87 |
|
|---|
| 88 | painter->setBrush(Qt::gray);
|
|---|
| 89 | painter->drawRect(-20, -58, 40, 2); // front axel
|
|---|
| 90 | painter->drawRect(-20, 7, 40, 2); // rear axel
|
|---|
| 91 |
|
|---|
| 92 | painter->setBrush(color);
|
|---|
| 93 | painter->drawRect(-25, -79, 50, 10); // front wing
|
|---|
| 94 |
|
|---|
| 95 | painter->drawEllipse(-25, -48, 50, 20); // side pods
|
|---|
| 96 | painter->drawRect(-25, -38, 50, 35); // side pods
|
|---|
| 97 | painter->drawRect(-5, 9, 10, 10); // back pod
|
|---|
| 98 |
|
|---|
| 99 | painter->drawEllipse(-10, -81, 20, 100); // main body
|
|---|
| 100 |
|
|---|
| 101 | painter->drawRect(-17, 19, 34, 15); // rear wing
|
|---|
| 102 |
|
|---|
| 103 | painter->setBrush(Qt::black);
|
|---|
| 104 | painter->drawPie(-5, -51, 10, 15, 0, 180 * 16);
|
|---|
| 105 | painter->drawRect(-5, -44, 10, 10); // cocpit
|
|---|
| 106 |
|
|---|
| 107 | painter->save();
|
|---|
| 108 | painter->translate(-20, -58);
|
|---|
| 109 | painter->rotate(wheelsAngle);
|
|---|
| 110 | painter->drawRect(-10, -7, 10, 15); // front left
|
|---|
| 111 | painter->restore();
|
|---|
| 112 |
|
|---|
| 113 | painter->save();
|
|---|
| 114 | painter->translate(20, -58);
|
|---|
| 115 | painter->rotate(wheelsAngle);
|
|---|
| 116 | painter->drawRect(0, -7, 10, 15); // front left
|
|---|
| 117 | painter->restore();
|
|---|
| 118 |
|
|---|
| 119 | painter->drawRect(-30, 0, 12, 17); // rear left
|
|---|
| 120 | painter->drawRect(19, 0, 12, 17); // rear right
|
|---|
| 121 | }
|
|---|
| 122 |
|
|---|
| 123 | void Car::timerEvent(QTimerEvent *event)
|
|---|
| 124 | {
|
|---|
| 125 | Q_UNUSED(event);
|
|---|
| 126 |
|
|---|
| 127 | const qreal axelDistance = 54;
|
|---|
| 128 | qreal wheelsAngleRads = (wheelsAngle * Pi) / 180;
|
|---|
| 129 | qreal turnDistance = ::cos(wheelsAngleRads) * axelDistance * 2;
|
|---|
| 130 | qreal turnRateRads = wheelsAngleRads / turnDistance; // rough estimate
|
|---|
| 131 | qreal turnRate = (turnRateRads * 180) / Pi;
|
|---|
| 132 | qreal rotation = speed * turnRate;
|
|---|
| 133 |
|
|---|
| 134 | rotate(rotation);
|
|---|
| 135 | translate(0, -speed);
|
|---|
| 136 | update();
|
|---|
| 137 | }
|
|---|