parent
5c45de0785
commit
2c1d9b12c7
5 changed files with 102 additions and 4 deletions
@ -1,12 +1,37 @@ |
|||||||
#include "Simulation.h" |
#include "Simulation.h" |
||||||
#include "Pendulum.h" |
#include "Pendulum.h" |
||||||
#include <QPainter> |
#include <QPainter> |
||||||
|
#include <QTimer> |
||||||
|
|
||||||
Simulation::Simulation() = default; |
Simulation::Simulation() { |
||||||
|
timer = new QTimer(this); |
||||||
|
QTimer::connect(timer, &QTimer::timeout, this, &Simulation::update); |
||||||
|
timer->setInterval(updateInterval); |
||||||
|
timer->start(); |
||||||
|
}; |
||||||
|
|
||||||
void Simulation::draw(QPainter *p, int screenSize) const { |
void Simulation::draw(QPainter *p, int screenSize) const { |
||||||
double scale = screenSize / size; |
double scale = screenSize * 0.95 / size; |
||||||
|
|
||||||
for (const auto pendulum : pendula) |
for (const auto pendulum : pendula) |
||||||
pendulum->draw(p, scale); |
pendulum->draw(p, scale); |
||||||
} |
} |
||||||
|
|
||||||
|
void Simulation::update() { |
||||||
|
if (!isPlaying) |
||||||
|
return; |
||||||
|
|
||||||
|
double h = (timescale * updateInterval) / 1000; |
||||||
|
|
||||||
|
h /= substeps; |
||||||
|
|
||||||
|
for (int i = 0; i < substeps; i++) |
||||||
|
for (const auto pendulum : pendula) |
||||||
|
pendulum->update(h, gravity); |
||||||
|
} |
||||||
|
|
||||||
|
void Simulation::clearPendula() { |
||||||
|
for (auto p : pendula) |
||||||
|
delete p; |
||||||
|
pendula.clear(); |
||||||
|
} |
||||||
|
Loading…
Reference in new issue