Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion Debug_demo/Collisions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
17 changes: 12 additions & 5 deletions Debug_demo/GameObject.cpp
Original file line number Diff line number Diff line change
@@ -1,26 +1,33 @@
#include <QVector2D>
#include "Ball.h"
#include "GameObject.h"

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) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will reset the delta if magnitude of speed vector is less than some threshold. However, the exact threshold value might need some adjustment.

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);
}
36 changes: 33 additions & 3 deletions Debug_demo/GameObject.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,36 @@
#pragma once


#include <optional>
#include <QPointF>
#include <QRectF>
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<QPointF>& delta){
delta_ = delta;
}

void calculate(int msec) {
if (delta_) {
value_ += *delta_ * (msec / 1000.);
}
}

private:
QPointF value_;
std::optional<QPointF> delta_;
};

struct GameObject {

GameObject(const QPointF& pos, const QPointF& speed);
Expand All @@ -13,6 +39,10 @@ struct GameObject {

const QPointF& getSpeed() const;

Motion& getSpeedMotion() {
return speed_;
}

void setPos(const QPointF& pos);

void setSpeed(const QPointF& speed);
Expand All @@ -24,7 +54,7 @@ struct GameObject {
virtual QRectF aabb() const = 0;

private:
QPointF pos_;
QPointF speed_;
Motion pos_;
Motion speed_;
};