| 1 |
// Copyright (C) 2016 The Qt Company Ltd. |
| 2 |
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause |
| 3 |
|
| 4 |
#include "car.h" |
| 5 |
#include <QtWidgets/QtWidgets> |
| 6 |
#include <cmath> |
| 7 |
|
| 8 |
QRectF Car::boundingRect() const |
| 9 |
{ |
| 10 |
return QRectF(-35, -81, 70, 115); |
| 11 |
} |
| 12 |
|
| 13 |
Car::Car() |
| 14 |
{ |
| 15 |
startTimer(1000 / 33); |
| 16 |
setFlags(ItemIsMovable | ItemIsFocusable); |
| 17 |
} |
| 18 |
|
| 19 |
void Car::accelerate() |
| 20 |
{ |
| 21 |
if (speed < 10) |
| 22 |
++speed; |
| 23 |
} |
| 24 |
|
| 25 |
void Car::decelerate() |
| 26 |
{ |
| 27 |
if (speed > -10) |
| 28 |
--speed; |
| 29 |
} |
| 30 |
|
| 31 |
void Car::turnLeft() |
| 32 |
{ |
| 33 |
if (wheelsAngle > -30) |
| 34 |
wheelsAngle -= 5; |
| 35 |
} |
| 36 |
|
| 37 |
void Car::turnRight() |
| 38 |
{ |
| 39 |
if (wheelsAngle < 30) |
| 40 |
wheelsAngle += 5; |
| 41 |
} |
| 42 |
|
| 43 |
void Car::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) |
| 44 |
{ |
| 45 |
Q_UNUSED(option); |
| 46 |
Q_UNUSED(widget); |
| 47 |
|
| 48 |
painter->setBrush(Qt::gray); |
| 49 |
painter->drawRect(-20, -58, 40, 2); // front axel |
| 50 |
painter->drawRect(-20, 7, 40, 2); // rear axel |
| 51 |
|
| 52 |
painter->setBrush(color); |
| 53 |
painter->drawRect(-25, -79, 50, 10); // front wing |
| 54 |
|
| 55 |
painter->drawEllipse(-25, -48, 50, 20); // side pods |
| 56 |
painter->drawRect(-25, -38, 50, 35); // side pods |
| 57 |
painter->drawRect(-5, 9, 10, 10); // back pod |
| 58 |
|
| 59 |
painter->drawEllipse(-10, -81, 20, 100); // main body |
| 60 |
|
| 61 |
painter->drawRect(-17, 19, 34, 15); // rear wing |
| 62 |
|
| 63 |
painter->setBrush(Qt::black); |
| 64 |
painter->drawPie(-5, -51, 10, 15, 0, 180 * 16); |
| 65 |
painter->drawRect(-5, -44, 10, 10); // cocpit |
| 66 |
|
| 67 |
painter->save(); |
| 68 |
painter->translate(-20, -58); |
| 69 |
painter->rotate(wheelsAngle); |
| 70 |
painter->drawRect(-10, -7, 10, 15); // front left |
| 71 |
painter->restore(); |
| 72 |
|
| 73 |
painter->save(); |
| 74 |
painter->translate(20, -58); |
| 75 |
painter->rotate(wheelsAngle); |
| 76 |
painter->drawRect(0, -7, 10, 15); // front left |
| 77 |
painter->restore(); |
| 78 |
|
| 79 |
painter->drawRect(-30, 0, 12, 17); // rear left |
| 80 |
painter->drawRect(19, 0, 12, 17); // rear right |
| 81 |
} |
| 82 |
|
| 83 |
void Car::timerEvent(QTimerEvent *event) |
| 84 |
{ |
| 85 |
Q_UNUSED(event); |
| 86 |
|
| 87 |
const qreal axelDistance = 54; |
| 88 |
qreal wheelsAngleRads = qDegreesToRadians(wheelsAngle); |
| 89 |
qreal turnDistance = std::cos(wheelsAngleRads) * axelDistance * 2; |
| 90 |
qreal turnRateRads = wheelsAngleRads / turnDistance; // rough estimate |
| 91 |
qreal turnRate = qRadiansToDegrees(turnRateRads); |
| 92 |
qreal rotation = speed * turnRate; |
| 93 |
|
| 94 |
setTransform(QTransform().rotate(rotation), true); |
| 95 |
setTransform(QTransform::fromTranslate(0, -speed), true); |
| 96 |
|
| 97 |
if (!scene()->views().isEmpty()) { |
| 98 |
QRect viewRect = scene()->views().at(0)->sceneRect().toRect(); |
| 99 |
QTransform fx = transform(); |
| 100 |
qreal dx = fx.dx(); |
| 101 |
qreal dy = fx.dy(); |
| 102 |
while (dx < viewRect.left() - 10) |
| 103 |
dx += viewRect.width(); |
| 104 |
while (dy < viewRect.top() - 10) |
| 105 |
dy += viewRect.height(); |
| 106 |
while (dx > viewRect.right() + 10) |
| 107 |
dx -= viewRect.width(); |
| 108 |
while (dy > viewRect.bottom() + 10) |
| 109 |
dy -= viewRect.width(); |
| 110 |
setTransform(QTransform(fx.m11(), fx.m12(), fx.m13(), |
| 111 |
fx.m21(), fx.m22(), fx.m23(), |
| 112 |
dx, dy, fx.m33())); |
| 113 |
} |
| 114 |
|
| 115 |
update(); |
| 116 |
} |