//------------------------------------------------------------ void particle::update(){ vel = vel + frc; pos = pos + vel; age++; if( age > lifespan ){ dead = true; } resetForce(); }
void Entity::integrate(float deltaTime) { //Integration phase // dv = (Force /mass) * dt velocity_ += (md_.inv_mass * forces_) * deltaTime; prevPosition_ = position_; // dx = V * dt position_ += velocity_ * deltaTime; if(collider_) collider_->setPosition(position_); //Zero out the vector containing all the forces applied, //so it can be used again for the next frame resetForce(); }
void Physics::step( real_t dt ) { // TODO step the world forward by dt. Need to detect collisions, apply // forces, and integrate positions and orientations. // // Note: put RK4 here, not in any of the physics bodies // // Must use the functions that you implemented // // Note, when you change the position/orientation of a physics object, // change the position/orientation of the graphical object that represents // it Derivative initial; Derivative k1, k2, k3, k4; Vector3 sumAngles; for(unsigned int i = 0; i < spheres.size(); i++){ initial.velocity = spheres[i]->velocity; initial.angular_velocity = spheres[i]->angular_velocity; initial.position = spheres[i]->position; initial.orientation = spheres[i]->orientation; initial.angle = Vector3(0,0,0); resetForce(*spheres[i]); /****************************RK4**************************************/ /* k1 */ setPosition(initial, k1, initial, 0); updateState(*spheres[i], k1); spheres[i]->apply_force(gravity, Vector3(0,0,0)); for(unsigned int j = 0; j < springs.size(); j++){ if(springs[j]->body1->id == spheres[i]->id || springs[j]->body2->id == spheres[i]->id) springs[j]->step(dt); } Evaluate(k1, *spheres[i], dt); resetForce(*spheres[i]); /* k2 */ setPosition(k1, k2, initial, dt/2); updateState(*spheres[i], k2); spheres[i]->apply_force(gravity, Vector3(0,0,0)); for(unsigned int j = 0; j < springs.size(); j++){ if(springs[j]->body1->id == spheres[i]->id || springs[j]->body2->id == spheres[i]->id) springs[j]->step(dt/2); } Evaluate(k2, *spheres[i], dt); resetForce(*spheres[i]); /* k3 */ setPosition(k2, k3, initial, dt/2); updateState(*spheres[i], k3); spheres[i]->apply_force(gravity, Vector3(0,0,0)); for(unsigned int j = 0; j < springs.size(); j++){ if(springs[j]->body1->id == spheres[i]->id || springs[j]->body2->id == spheres[i]->id) springs[j]->step(dt/2); } Evaluate(k3, *spheres[i], dt); resetForce(*spheres[i]); /* k4 */ setPosition(k3, k4, initial, dt); updateState(*spheres[i], k4); spheres[i]->apply_force(gravity, Vector3(0,0,0)); for(unsigned int j = 0; j < springs.size(); j++){ if(springs[j]->body1->id == spheres[i]->id || springs[j]->body2->id == spheres[i]->id) springs[j]->step(dt); } Evaluate(k4, *spheres[i], dt); resetForce(*spheres[i]); /***************udpate position and orientation after RK4***************/ spheres[i]->position = initial.position + 1.0 / 6.0 * (k1.position + 2.0 * (k2.position + k3.position) + k4.position); spheres[i]->velocity = initial.velocity + 1.0 / 6.0 * (k1.velocity + 2.0 * (k2.velocity + k3.velocity) + k4.velocity); spheres[i]->angular_velocity = initial.angular_velocity + 1.0 / 6.0 * (k1.angular_velocity + 2.0 * (k2.angular_velocity + k3.angular_velocity) + k4.angular_velocity); sumAngles = 1.0 / 6.0 * (k1.angle + 2.0 * (k2.angle + k3.angle) + k4.angle); if(sumAngles != Vector3(0,0,0)){ spheres[i]->orientation = initial.orientation * Quaternion(normalize(sumAngles), length(sumAngles)); } else{ spheres[i]->orientation = initial.orientation; } spheres[i]->sphere->position = spheres[i]->position; spheres[i]->sphere->orientation = spheres[i]->orientation; /*******collision detection************/ for(unsigned int j = 0; j < spheres.size(); j++){ if(spheres[i]->id != spheres[j]->id){ collides(*spheres[i], *spheres[j], collision_damping); } } for(unsigned int j = 0; j < triangles.size(); j++){ collides(*spheres[i], *triangles[j], collision_damping); } for(unsigned int j = 0; j < planes.size(); j++){ collides(*spheres[i], *planes[j], collision_damping); } } }
void Particle::update(){ resetForce(); updateForce(); updatePos(); }
// ----------------------------------------------------------- void Particle::update(){ vel += acc; vel *= (1.0-damping); pos += vel; resetForce(); }
static void resetForces(int number_of_stars, struct star* star_array) { for(int i = 0; i < number_of_stars; ++i){ resetForce(&star_array[i]); } }
void TriParticle::update(){ resetForce(); updateForce(); updatePos(); rot += rotVel; }