void Mass_spring_viewer::time_integration(float dt) { switch (integration_) { case Euler: { /** \todo (Part 1) Implement Euler integration scheme \li Hint: compute_forces() computes all forces for the current positions and velocities. */ compute_forces(); for (unsigned int i=0; i<body_.particles.size(); ++i){ Particle *p = &body_.particles.at(i); //update position p->position = p->position + dt*p->velocity; //calculate the new acceleration using newton's second law p->acceleration = p->force/p->mass; p->velocity = p->velocity + dt*p->acceleration; } break; } case Midpoint: { /** \todo (Part 2) Implement the Midpoint time integration scheme \li The Particle class has variables position_t and velocity_t to store current values \li Hint: compute_forces() computes all forces for the current positions and velocities. */ break; } case Verlet: { /** \todo (Part 2) Implement the Verlet time integration scheme \li The Particle class has a variable acceleration to remember the previous values \li Hint: compute_forces() computes all forces for the current positions and velocities. */ break; } } // impulse-based collision handling if (collisions_ == Impulse_based) { impulse_based_collisions(); } glutPostRedisplay(); }
void Mass_spring_viewer::time_integration(float dt) { switch (integration_) { case Euler: { /** \todo (Part 1) Implement Euler integration scheme \li Hint: compute_forces() computes all forces for the current positions and velocities. */ compute_forces(); for(unsigned int i = 0; i < body_.particles.size(); i++) { body_.particles[i].velocity += (dt/body_.particles[i].mass)*body_.particles[i].force; body_.particles[i].position += dt*body_.particles[i].velocity; } break; break; } case Midpoint: { /** \todo (Part 2) Implement the Midpoint time integration scheme \li The Particle class has variables position_t and velocity_t to store current values \li Hint: compute_forces() computes all forces for the current positions and velocities. */ break; } case Verlet: { /** \todo (Part 2) Implement the Verlet time integration scheme \li The Particle class has a variable acceleration to remember the previous values \li Hint: compute_forces() computes all forces for the current positions and velocities. */ break; } } // impulse-based collision handling if (collisions_ == Impulse_based) { impulse_based_collisions(); } glutPostRedisplay(); }
void Rigid_body_viewer::time_integration(float dt) { // compute all forces compute_forces(); /** \todo Implement explicit Euler time integration \li update position and orientation \li update linear and angular velocities \li call update_points() at the end to compute the new particle positions */ body_.linear_velocity += body_.force * dt / body_.mass; body_.position += body_.linear_velocity * dt; body_.angular_velocity += body_.torque * dt / body_.inertia; body_.orientation += body_.angular_velocity * dt; body_.update_points(); // handle collisions impulse_based_collisions(); }
void Mass_spring_viewer::time_integration(float dt) { switch (integration_) { case Euler: { /** \todo (Part 1) Implement Euler integration scheme \li The Particle class has variables position_t and velocity_t to store current values \li Hint: compute_forces() computes all forces for the current positions and velocities. */ compute_forces (); for (std::vector<Particle>::iterator p_it = body_.particles.begin (); p_it != body_.particles.end (); ++p_it) if (!p_it->locked) { /// 1) p_it->acceleration = p_it->force / p_it->mass; /// 2) p_it->position = p_it->position + dt * p_it->velocity; /// 3) p_it->velocity = p_it->velocity + dt * p_it->acceleration; } break; } case Midpoint: { /** \todo (Part 2) Implement the Midpoint time integration scheme \li The Particle class has variables position_t and velocity_t to store current values \li Hint: compute_forces() computes all forces for the current positions and velocities. */ compute_forces(); for (std::vector<Particle>::iterator p_it = body_.particles.begin (); p_it != body_.particles.end (); ++p_it) if (!p_it->locked) { p_it->position_t = p_it->position; p_it->velocity_t = p_it->velocity; p_it->acceleration = p_it->force/p_it->mass; p_it->position += 0.5*dt * p_it->velocity; p_it->velocity += 0.5*dt * p_it->acceleration; } compute_forces(); for (std::vector<Particle>::iterator p_it = body_.particles.begin (); p_it != body_.particles.end (); ++p_it) if (!p_it->locked) { p_it->acceleration = p_it->force / p_it->mass; p_it->position = p_it->position_t + dt * p_it->velocity; p_it->velocity = p_it->velocity_t + dt * p_it->acceleration; } break; } case Verlet: { /** \todo (Part 2) Implement the Verlet time integration scheme \li The Particle class has a variable acceleration to remember the previous values \li Hint: compute_forces() computes all forces for the current positions and velocities. */ compute_forces(); // x(t) v(t-h) for (std::vector<Particle>::iterator p_it = body_.particles.begin (); p_it != body_.particles.end (); ++p_it) if (!p_it->locked) { vec2 a = p_it->force / p_it->mass; p_it->velocity += 0.5*dt*(p_it->acceleration + a); // x(t) v(t) p_it->position += dt*p_it->velocity + 0.5*dt*dt*a; p_it->acceleration = a; } break; } case Implicit: { /// The usual force computation method is called, and then the jacobian matrix dF/dx is calculated compute_forces (); compute_jacobians (); /// Finally the linear system is composed and solved solver_.solve (dt, particle_mass_, body_.particles); break; } } // impulse-based collision handling if (collisions_ == Impulse_based) { impulse_based_collisions(); } // E_c = 1/2 m*v^2 float total_E = 0.0f; for (std::vector<Particle>::iterator p_it = body_.particles.begin (); p_it != body_.particles.end (); ++p_it) total_E += 0.5 * p_it->mass * dot(p_it->velocity, p_it->velocity); log_file << total_E << "\n"; glutPostRedisplay(); }