-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathball.cpp
More file actions
executable file
·98 lines (86 loc) · 3.42 KB
/
ball.cpp
File metadata and controls
executable file
·98 lines (86 loc) · 3.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include "ball.h"
#include <iostream>
void Ball::render(QPainter &painter) {
// use our colour
painter.setBrush(m_brush);
// circle centered
painter.drawEllipse(m_pos.toPointF(), m_radius, m_radius);
}
StageTwoBall::~StageTwoBall() {
// this will invoke recursion - delete ball calls ~StageTwoBall()
// so it's like a DFS deletion.
for (StageTwoBall *ball : *m_children) delete ball;
delete m_children;
}
void StageTwoBall::addChild(StageTwoBall* child) {
m_children->push_back(child);
}
bool StageTwoBall::maybeBreakBall(QVector2D deltaV, std::vector<Ball*>* balls) {
// make sure if it breaks, you set its parent to nullptr
float energyOfCollision = m_mass * deltaV.lengthSquared();
if (m_strength < energyOfCollision) {
// ball should break and then disappear itself
// the children need to graduate into m_balls array
// I can do that based on the fact I receive true from this method,
// and put that method inside Game!!
// otherwise we get circular references and we are fucked
// do not want to deal wit hthat shit again
QVector2D preCollisionVelocity = getVelocity();
float energyPerBall = energyOfCollision/numChildren();
QVector2D pointOfCollision((-deltaV.normalized())*m_radius);
// for each component ball
for (auto it = m_children->begin(); it != m_children->end(); ++it) {
float componentBallMass = (*it)->m_mass;
QVector2D componentBallVelocity =
preCollisionVelocity
+ sqrt(energyPerBall/componentBallMass)
* ((*it)->getPosition() - pointOfCollision).normalized();
(*it)->setVelocity(componentBallVelocity);
// fix its position, shouldn't be relative to the parent anymore
(*it)->m_pos = (*it)->getPosition();
// maybe if I graduate it here, nothing fucked will happen
// it does mean other balls will check if they collided with them,
// and why not? Live and let live bro, chuck in that m_balls
// array and mutate the fuck out of it
// (lol)
// deleting reference to parent
(*it)->m_parent = nullptr;
// pushing it onto our balls array (fucking sneaK)
balls->push_back(*it);
}
return true;
}
return false;
}
double StageTwoBall::getMass() const {
double result = m_mass;
for (StageTwoBall* child : *m_children)
result += child->getMass();
// absolutely gorge, so happy with this
return result;
}
QVector2D StageTwoBall::getVelocity() const {
// if the ball is just a child,
// then when asked about the velocity of this ball,
// just report its parent's velocity (a real snitch move)
if (m_parent != nullptr) {
return m_parent->getVelocity();
}
// otherwise it's not a child, this is the default behaviour.
return Ball::getVelocity();
}
QVector2D StageTwoBall::getPosition() const {
if (m_parent != nullptr) {
return m_parent->getPosition() + m_pos;
}
return Ball::getPosition();
}
void StageTwoBall::render(QPainter &painter) {
painter.setBrush(m_brush);
// circle centered
painter.drawEllipse(getPosition().toPointF(), m_radius, m_radius);
// draw children inside if they exist
for (StageTwoBall* child : *m_children) {
child->render(painter);
}
}