forked from cleroy8/PositionBasedDynamics
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdrawarea.cpp
More file actions
187 lines (155 loc) · 5.98 KB
/
Copy pathdrawarea.cpp
File metadata and controls
187 lines (155 loc) · 5.98 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#include "drawarea.h"
#include "spherecollider.h"
#include <QOpenGLWidget>
#include <QLine>
DrawArea::DrawArea(QWidget *parent)
: QOpenGLWidget{parent}
{
const float ratio = m_width/m_height;
const int height = 500;
this->setFixedSize(QSize(height*ratio,height));
context = Context();
context.setNbUpdates(nbUpdates);
std::srand(std::time({}));
is_random = false;
healthPoints = nbUpdates*50;
}
void DrawArea::paintEvent(QPaintEvent *event) {
QPainter p(this);
this->show(&p, event, context);
}
void DrawArea::initializeGL(){
QOpenGLFunctions *f = QOpenGLContext::currentContext()->functions();
f->glClearColor(0.0,0.0,0.0,0.0);
}
void DrawArea::show(QPainter *painter, QPaintEvent *event, Context& context) {
QOpenGLFunctions *f = QOpenGLContext::currentContext()->functions();
f->glClear(GL_COLOR_BUFFER_BIT);
const int height = this->height();
painter->fillRect(event->rect(), QBrush(Qt::white));
auto particles = context.getParticles();
// draw colliders
QColor colliderColor;
for(int i=0; i<context.getColliders().size(); i++) {
const std::variant<PlaneCollider, SphereCollider, BoxCollider> coll_var = context.getColliders()[i];
if(context.getCollisionsToggle()) {
colliderColor = std::visit([this](auto& arg) -> QColor {return arg.getColor();}, coll_var);
}
else {
colliderColor = defaultColliderColor;
}
painter->setPen(colliderColor);
painter->setBrush(QBrush(colliderColor));
std::visit([painter, this](auto& arg) {drawCollider(painter, arg);}, coll_var);
}
// draw particles
Vec2 viewPos;
for(int i=0; i<context.getNbParticles(); i++) {
auto part = particles[i];
// translate in view coordinates
viewPos=worldToView(part->getPos());
const float rad = height*part->getRad()/m_height;
painter->setPen(part->getColor());
painter->setBrush(QBrush(part->getColor()));
const QRectF target(viewPos.getX()-rad, viewPos.getY()-rad, rad*2, rad*2);
painter->drawEllipse(target);
}
}
void DrawArea::animate() {
float dt = 0.01;
float n = float(nbUpdates); // need a float to make a float division with dt/n
for (int i=0; i<n; i++){
context.updatePhysicalSystem(dt/n);
}
this->update();
}
void DrawArea::reset() {
context.reset();
this->update();
}
void DrawArea::randomColor() {
is_random = !(is_random);
}
void DrawArea::toggleCollisions() {
context.changeCollisions();
}
void DrawArea::setHealth(const int newHealth) {
healthPoints = nbUpdates*newHealth;
}
const Vec2 DrawArea::worldToView(const Vec2 world_pos) const {
const float py = (this->height())*(1-world_pos.getY()/m_height);
return Vec2 {this->width()*world_pos.getX()/m_width, py};
}
const Vec2 DrawArea::viewToWorld(const Vec2 view_pos) const{
const float y = m_height*(1-view_pos.getY()/(this->height()));
return Vec2 {m_width*view_pos.getX()/this->width(), y};
}
void DrawArea::drawCollider(QPainter *painter, const PlaneCollider PlaneCollider) {
const Vec2 pc = PlaneCollider.getPoint();
const Vec2 nc = PlaneCollider.getNormal();
Vec2 p1, p2;
if (nc.getY()!=0){
const float coeff = -nc.getX()/nc.getY();
p1 = worldToView(Vec2(0,pc.getY()-coeff*pc.getX()));
p2 = worldToView(Vec2(m_width, pc.getY()+coeff*(m_width - pc.getX())));
} else { // vertical planes
p1 = worldToView(Vec2(pc.getX(), 0));
p2 = worldToView(Vec2(pc.getX(), m_height));
}
const QLine line(p1.getX(), p1.getY(),p2.getX(), p2.getY());
painter->drawLine(line);
}
void DrawArea::drawCollider(QPainter *painter, const SphereCollider sphereCollider) {
const Vec2 pc = worldToView(sphereCollider.getPoint());
const float rc = this->height() * sphereCollider.getRadius()/m_height;
const QRectF target(pc.getX()-rc, pc.getY()-rc, 2*rc, 2*rc);
painter->drawEllipse(target);
}
void DrawArea::drawCollider(QPainter *painter, const BoxCollider boxCollider) {
const Vec2 u = boxCollider.getU();
const Vec2 v = boxCollider.getV();
const float width = boxCollider.getWidth();
const float height = boxCollider.getHeight();
const Vec2 p1 = worldToView(boxCollider.getPoint() + (width/2 * u) + (height/2 * v));
const Vec2 p2 = worldToView(boxCollider.getPoint() + (width/2 * u) - (height/2 * v));
const Vec2 p3 = worldToView(boxCollider.getPoint() - (width/2 * u) + (height/2 * v));
const Vec2 p4 = worldToView(boxCollider.getPoint() - (width/2 * u) - (height/2 * v));
QList<QPoint> points;
points.append(QPoint(p1.getX(), p1.getY()));
points.append(QPoint(p2.getX(), p2.getY()));
points.append(QPoint(p4.getX(), p4.getY()));
points.append(QPoint(p3.getX(), p3.getY()));
points.append(QPoint(p1.getX(), p1.getY()));
const QPolygon polygon = QPolygon(points);
painter->drawPolygon(polygon);
}
// Take the mouse position when there is a double click
// Then call the paintEvent method (with update)
void DrawArea::mouseDoubleClickEvent(QMouseEvent *event) {
const QPointF position = event->position();
const Vec2 viewPos = Vec2(position.x(), position.y());
const Vec2 worldPos = viewToWorld(viewPos);
QColor color;
if(is_random) {
// Random color: int between 0 and 254 to avoid white (which would not be seen)
const int red = std::rand() % 255;
const int green = std::rand() % 255;
const int blue = std::rand() % 255;
color = QColor(red,green,blue);
}
else {
color = defaultPartColor;
}
Particle particle = Particle(worldPos, Vec2(), 0.5, 1, healthPoints, color);
context.addParticle(particle);
this->update();
}
void DrawArea::mousePressEvent(QMouseEvent *event){
QPointF position = event->position();
Vec2 viewPos = Vec2(position.x(), position.y());
Vec2 worldPos = viewToWorld(viewPos);
context.setSelectedParticle(worldPos);
}
void DrawArea::link(){
context.linkSelectedParticles();
}