Handle ball collisions against platform
This commit is contained in:
parent
9b458a1eec
commit
adaa571b28
5 changed files with 17 additions and 5 deletions
|
@ -12,7 +12,7 @@
|
||||||
|
|
||||||
double getRandomDouble(double maxValue);
|
double getRandomDouble(double maxValue);
|
||||||
|
|
||||||
Ball::Ball(QWidget* parent) : m_parent{parent}, m_isSpawning{true} {
|
Ball::Ball(QWidget* parent, std::shared_ptr<Platform> platform) : m_parent{parent}, m_platform{platform}, m_isSpawning{true} {
|
||||||
QRect rct = parent->rect();
|
QRect rct = parent->rect();
|
||||||
|
|
||||||
m_x = rand() % rct.width(); // random x pos
|
m_x = rand() % rct.width(); // random x pos
|
||||||
|
@ -49,6 +49,11 @@ void Ball::timerEvent(QTimerEvent*) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Platform collision (only if no longer spawning)
|
||||||
|
if (!m_isSpawning && m_platform->rect().intersects(QRect(m_x, m_y, RADIUS * 2.0, RADIUS * 2.0))) {
|
||||||
|
m_ky *= -1;
|
||||||
|
}
|
||||||
|
|
||||||
m_x += m_kx;
|
m_x += m_kx;
|
||||||
m_y += m_ky;
|
m_y += m_ky;
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
#ifndef BALL_H
|
#ifndef BALL_H
|
||||||
#define BALL_H
|
#define BALL_H
|
||||||
|
|
||||||
|
#include "platform.h"
|
||||||
#include <QPainter>
|
#include <QPainter>
|
||||||
#include <QTimer>
|
#include <QTimer>
|
||||||
#include <qobjectdefs.h>
|
#include <qobjectdefs.h>
|
||||||
|
@ -9,7 +10,7 @@ class Ball : public QTimer {
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Ball(QWidget* parent);
|
Ball(QWidget* parent, std::shared_ptr<Platform> platform);
|
||||||
|
|
||||||
void draw(QPainter& painter);
|
void draw(QPainter& painter);
|
||||||
|
|
||||||
|
@ -21,6 +22,7 @@ class Ball : public QTimer {
|
||||||
|
|
||||||
private:
|
private:
|
||||||
QWidget* m_parent;
|
QWidget* m_parent;
|
||||||
|
std::shared_ptr<Platform> m_platform;
|
||||||
double m_x;
|
double m_x;
|
||||||
double m_y;
|
double m_y;
|
||||||
double m_kx;
|
double m_kx;
|
||||||
|
|
|
@ -9,7 +9,7 @@ MainWindow::MainWindow(QWidget* parent) : QMainWindow(parent), ui(new Ui::MainWi
|
||||||
|
|
||||||
platform = std::make_shared<Platform>(this);
|
platform = std::make_shared<Platform>(this);
|
||||||
|
|
||||||
auto ball = std::make_shared<Ball>(this);
|
auto ball = std::make_shared<Ball>(this, platform);
|
||||||
connect(ball.get(), &Ball::deleted, this, &MainWindow::onDeleteBall);
|
connect(ball.get(), &Ball::deleted, this, &MainWindow::onDeleteBall);
|
||||||
balls.append(ball);
|
balls.append(ball);
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,8 +19,7 @@ Platform::Platform(MainWindow* parent) : m_parent{parent} {
|
||||||
}
|
}
|
||||||
|
|
||||||
void Platform::draw(QPainter& painter) {
|
void Platform::draw(QPainter& painter) {
|
||||||
QRect rct(m_x, m_y, PLATFORM_WIDTH, PLATFORM_HEIGHT);
|
painter.drawRect(rect());
|
||||||
painter.drawRect(rct);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Platform::timerEvent(QTimerEvent*) {
|
void Platform::timerEvent(QTimerEvent*) {
|
||||||
|
@ -34,3 +33,7 @@ void Platform::timerEvent(QTimerEvent*) {
|
||||||
|
|
||||||
m_x += direction * SPEED;
|
m_x += direction * SPEED;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
QRect Platform::rect() const {
|
||||||
|
return QRect(m_x, m_y, PLATFORM_WIDTH, PLATFORM_HEIGHT);
|
||||||
|
}
|
||||||
|
|
|
@ -18,6 +18,8 @@ class Platform : public QTimer {
|
||||||
|
|
||||||
void draw(QPainter& painter);
|
void draw(QPainter& painter);
|
||||||
|
|
||||||
|
QRect rect() const;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
virtual void timerEvent(QTimerEvent*) override;
|
virtual void timerEvent(QTimerEvent*) override;
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue