INFO702 - TPs
Loading...
Searching...
No Matches
collider.cpp
1/****************************************************************************
2** Author: J.-O. Lachaud, University Savoie Mont Blanc
3** (vaguely adapted from Qt colliding mices example)
4**
5** Copyright (C) 2015 The Qt Company Ltd.
6** Contact: http://www.qt.io/licensing/
7****************************************************************************/
8
9#include <cmath>
10#include <cstdlib>
11#include <QtWidgets>
12#include "objects.hpp"
13
14static const int AsteroidCount = 10;
15
16int main(int argc, char **argv)
17{
18 // Initializes Qt.
19 QApplication app(argc, argv);
20 // Initializes the random generator.
21 srand( QTime(0, 0, 0).secsTo(QTime::currentTime()));
22
23 // Creates a graphics scene where we will put graphical objects.
24 QGraphicsScene graphical_scene;
25 graphical_scene.setSceneRect(0, 0, IMAGE_SIZE, IMAGE_SIZE);
26 graphical_scene.setItemIndexMethod(QGraphicsScene::NoIndex);
27
28 // We choose to check intersection with 100 random points.
29 logical_scene = new LogicalScene( 100 );
30
31 // Creates a few asteroids...
32 for (int i = 0; i < AsteroidCount; ++i) {
33 QColor cok( 150, 130, 110 );
34 QColor cko( 255, 240, 0 );
35
36 // A master shape gathers all the elements of the shape.
37 MasterShape* asteroid = new Asteroid( cok, cko,
38 ( rand() % 20 + 20 ) / 10.0 /* speed */,
39 (double) (10 + rand() % 40) /* radius */ );
40 // Set direction and position
41 asteroid->setRotation(rand() % 360);
42 asteroid->setPos( IMAGE_SIZE/2 + ::sin((i * 6.28) / AsteroidCount) * 200,
43 IMAGE_SIZE/2 + ::cos((i * 6.28) / AsteroidCount) * 200 );
44 // Add it to the graphical scene
45 graphical_scene.addItem( asteroid );
46 // and to the logical scene
47 logical_scene->formes.push_back( asteroid );
48 }
49
50
51 // Standard stuff to initialize a graphics view with some background.
52 QGraphicsView view(&graphical_scene);
53 view.setRenderHint(QPainter::Antialiasing);
54 view.setBackgroundBrush(QPixmap(":/images/stars.jpg"));
55 view.setCacheMode(QGraphicsView::CacheBackground);
56 view.setViewportUpdateMode(QGraphicsView::BoundingRectViewportUpdate);
57 view.setDragMode(QGraphicsView::NoDrag); // QGraphicsView::ScrollHandDrag
58 view.setWindowTitle(QT_TRANSLATE_NOOP(QGraphicsView, "Space - the final frontier"));
59 view.setHorizontalScrollBarPolicy ( Qt::ScrollBarAlwaysOff );
60 view.setVerticalScrollBarPolicy ( Qt::ScrollBarAlwaysOff );
61 view.resize( IMAGE_SIZE, IMAGE_SIZE );
62 view.show();
63
64 // Creates a timer that will call `advance()` method regularly.
65 QTimer timer;
66 QObject::connect(&timer, SIGNAL(timeout()), &graphical_scene, SLOT(advance()));
67 timer.start( 30 ); // every 30ms
68
69 return app.exec();
70}
71
72
An asteroid is a simple shape that moves linearly in some direction.
Definition objects.hpp:62
A class to store master shapes and to test their possible collisions with a randomized algorithm.
Definition objects.hpp:89
Polymorphic class that represents the top class of any complex shape.
Definition objects.hpp:38