From 902b2b76002080051bcb34bac96a564b413bf825 Mon Sep 17 00:00:00 2001 From: Mari Baburina Date: Tue, 26 Mar 2024 17:21:26 +0100 Subject: [PATCH] A new structure, Motion, is introduced to handle speed and position calculations in GameObject and Collisions. A delta can optionally be set for each Motion, simulating a drag effect upon collision. --- Debug_demo/Collisions.cpp | 6 +++++- Debug_demo/GameObject.cpp | 17 ++++++++++++----- Debug_demo/GameObject.h | 36 +++++++++++++++++++++++++++++++++--- 3 files changed, 50 insertions(+), 9 deletions(-) diff --git a/Debug_demo/Collisions.cpp b/Debug_demo/Collisions.cpp index d746b4e..409096c 100644 --- a/Debug_demo/Collisions.cpp +++ b/Debug_demo/Collisions.cpp @@ -50,7 +50,11 @@ bool applyCollision(Ball& b, } if (mulX == -1 || mulY == -1) { - b.setSpeed(QPointF(b.getSpeed().x() * mulX, b.getSpeed().y() * mulY)); + QPointF newSpeed(b.getSpeed().x() * mulX, b.getSpeed().y() * mulY); + b.setSpeed(newSpeed); + + b.getSpeedMotion().setDelta(QPointF(-0.1 * newSpeed.x(), -0.1 * newSpeed.y())); + return true; } else { diff --git a/Debug_demo/GameObject.cpp b/Debug_demo/GameObject.cpp index 2b5f0a6..d0238a0 100644 --- a/Debug_demo/GameObject.cpp +++ b/Debug_demo/GameObject.cpp @@ -1,3 +1,4 @@ +#include #include "Ball.h" #include "GameObject.h" @@ -5,22 +6,28 @@ GameObject::GameObject(const QPointF& pos, const QPointF& speed) : pos_(pos), speed_(speed) {} const QPointF& GameObject::getPos() const { - return pos_; + return pos_.getValue(); } const QPointF& GameObject::getSpeed() const { - return speed_; + return speed_.getValue(); } //FIXME: check const void GameObject::calc(int msec) { - pos_ += speed_ * (msec / 1000.); + speed_.calculate(msec); + pos_.calculate(msec); + pos_.setValue(pos_.getValue() + speed_.getValue() * (msec / 1000.)); + + if (QVector2D(speed_.getValue()).length() < 0.03) { + speed_.setDelta(std::nullopt); + } } void GameObject::setPos(const QPointF& pos) { - pos_ = pos; + pos_.setValue(pos); } void GameObject::setSpeed(const QPointF& speed) { - speed_ = speed; + speed_.setValue(speed); } diff --git a/Debug_demo/GameObject.h b/Debug_demo/GameObject.h index e66f783..def5cf6 100644 --- a/Debug_demo/GameObject.h +++ b/Debug_demo/GameObject.h @@ -1,10 +1,36 @@ #pragma once - +#include #include #include class QPainter; +struct Motion { + explicit Motion(const QPointF& value) : value_(value) {} + + const QPointF& getValue() const { + return value_; + } + + void setValue(const QPointF& value) { + value_ = value; + } + + void setDelta(const std::optional& delta){ + delta_ = delta; + } + + void calculate(int msec) { + if (delta_) { + value_ += *delta_ * (msec / 1000.); + } + } + +private: + QPointF value_; + std::optional delta_; +}; + struct GameObject { GameObject(const QPointF& pos, const QPointF& speed); @@ -13,6 +39,10 @@ struct GameObject { const QPointF& getSpeed() const; + Motion& getSpeedMotion() { + return speed_; + } + void setPos(const QPointF& pos); void setSpeed(const QPointF& speed); @@ -24,7 +54,7 @@ struct GameObject { virtual QRectF aabb() const = 0; private: - QPointF pos_; - QPointF speed_; + Motion pos_; + Motion speed_; };