public void Simulate(double dt) { if (dragging) { VectorN dragForce = new VectorN(2); dragForce.v[0] = mousex - dragged.pos.v[0]; dragForce.v[1] = mousey - dragged.pos.v[1]; dragForce.Scale(50 * dragged.mass); dragged.ApplyForce(dragForce); dragged.Unfreeze(); } for (int i = 0; i < particles.Count; i++) { if (particles[i] != dragged) { particles[i].ApplyGravity(); } } dynamics.SimulateFast(dt); CheckBounds(); }
private void CheckBounds() { double restitution = 0.6; double erp = 0.3; for (int i = 0; i < particles.Count; i++) { double[] pos = particles[i].pos.v; double r = particles[i].radius; if (pos[0] < leftBound + r || pos[0] > rightBound - r || pos[1] > topBound - r || pos[1] < bottomBound + r) { double penetration = 0; VectorN dir = new VectorN(2); particles[i].v.Scale(restitution); if (pos[0] < leftBound + r) { particles[i].v.v[0] *= -1; penetration = leftBound - pos[0] + r; dir.v[0] = 1; } if (pos[0] > rightBound - r) { particles[i].v.v[0] *= -1; penetration = pos[0] - rightBound + r; dir.v[0] = -1; } if (pos[1] > topBound - r) { particles[i].v.v[1] *= -1; penetration = pos[1] - topBound + r; dir.v[1] = -1; } if (pos[1] < bottomBound + r) { particles[i].v.v[1] *= -1; penetration = bottomBound - pos[1] + r; dir.v[1] = 1; } particles[i].v.Scale(restitution); VectorN additional = particles[i].v.NormalizeR(); particles[i].v.Add(additional.ScaleR(penetration * erp)); dir.Scale(penetration); particles[i].pos.Add(dir); } } }