Commit 8e7a235c authored by bbguimaraes's avatar bbguimaraes
Browse files

sms: isolated code for updating forces

parent ef75f42a
......@@ -37,6 +37,9 @@ class Display : public QGLWidget {
void set_draw_grid(bool d) {this->m_draw_grid = d;}
void set_draw_axes(bool d) {this->m_draw_axes = d;}
const Mass * selected() const {return this->m_selected;}
Vector selected_force() const {return this->m_selected_force;}
public slots:
void update_systems();
......@@ -59,6 +62,7 @@ class Display : public QGLWidget {
std::vector<Simulation *> m_simulations;
Mass * m_selected;
Vector m_selected_force;
Camera m_camera;
GLuint m_texture;
GLUquadric * m_quadric;
......
......@@ -88,7 +88,7 @@ void Display::select(Vector click, bool rate_limit) {
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
Rendering::set_camera(this->m_camera);
Mass * selected = nullptr;
this->m_selected = nullptr;
for(auto simulation : this->m_simulations)
for(auto system : *simulation->systems()) {
glRenderMode(GL_SELECT);
......@@ -97,13 +97,12 @@ void Display::select(Vector click, bool rate_limit) {
Rendering::draw_masses(
system, GL_SELECT, this->m_quadric, this->m_selected);
if(glRenderMode(GL_RENDER) != 0) {
selected = &(*system->masses())[select_buffer[3]];
this->m_selected = &(*system->masses())[select_buffer[3]];
break;
}
}
glMatrixMode(GL_PROJECTION);
glPopMatrix();
this->m_selected = selected;
}
/*slot*/
......@@ -185,7 +184,7 @@ void Display::mousePressEvent(QMouseEvent * event) {
void Display::mouseReleaseEvent(QMouseEvent * event) {
if(event->button() == Qt::LeftButton && this->m_selected)
this->m_selected->set_force(Vector());
this->m_selected_force = Vector();
}
void Display::mouseMoveEvent(QMouseEvent * event) {
......@@ -206,7 +205,7 @@ void Display::mouseMoveEvent(QMouseEvent * event) {
else if(this->m_selected) {
auto move = (this->m_click_position - position)
/ this->m_camera.distance();
this->m_selected->set_force(Vector(-move.x(), move.y()));
this->m_selected_force = Vector(-move.x(), move.y());
}
}
this->m_last_position = position;
......
......@@ -18,7 +18,8 @@ void init_sm_system3(SpringMassSystem * system, unsigned int side);
GLuint load_texture(const std::string & filename);
void move_system(SpringMassSystem * system, Vector v);
void update_simulations(std::vector<Simulation> * simulations);
void update_simulations(
std::vector<Simulation> * simulations, Display * display);
int main(int argc, char ** argv) {
const double UPDATE_RATE = 1.0f / 3000.0f;
......@@ -38,7 +39,9 @@ int main(int argc, char ** argv) {
QTimer timer;
QObject::connect(
&timer, &QTimer::timeout,
[&simulations](){update_simulations(&simulations);});
[&simulations, &display]() {
update_simulations(&simulations, &display);
});
QObject::connect(
&timer, &QTimer::timeout,
&display, &Display::updateGL);
......@@ -46,9 +49,19 @@ int main(int argc, char ** argv) {
return app.exec();
}
void update_simulations(std::vector<Simulation> * simulations) {
for(auto & simulation : *simulations)
void update_simulations(
std::vector<Simulation> * simulations, Display * display) {
const auto F = Vector();
for(auto & simulation : *simulations) {
for(auto system : *simulation.systems())
for(auto & mass : *system->masses()) {
auto f = F;
if(&mass == display->selected())
f = f + display->selected_force();
mass.set_force(f);
}
simulation.update();
}
}
void init_simulations(
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment