notes_COSqueues

Notes_COSqueues

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: dynamics. ・Maxwell-Boltzmann: distribution of speeds as a function of temperature. ・Einstein: explain Brownian motion of pollen grains. 39 40 Warmup: bouncing balls Warmup: bouncing balls Time-driven simulation. N bouncing balls in the unit square. public class BouncingBalls { public static void main(String args) { int N = Integer.parseInt(args[0]); Ball balls = new Ball[N]; for (int i = 0; i < N; i++) balls[i] = new Ball(); while(true) { StdDraw.clear(); for (int i = 0; i < N; i++) { balls[i].move(0.5); balls[i].draw(); } StdDraw.show(50); } main simulation loop } } public class Ball { private double rx, ry; private double vx, vy; private final double radius; public Ball(...) { /* initialize position and % java BouncingBalls 100 // position // velocity // radius velocity */ check for collision with walls } public void move(double dt) { if ((rx + vx*dt < radius) || (rx + vx*dt > 1.0 - radius)) { vx = -vx; } if ((ry + vy*dt < radius) || (ry + vy*dt > 1.0 - radius)) { vy = -vy; } rx = rx + vx*dt; ry = ry + vy*dt; } public void draw() { StdDraw.filledCircle(rx, ry, radius); } } Missing. Check for balls colliding with each other. ・Physics problems: when? what effect? ・CS problems: which object does the check? too many checks? 41 42 Time-driven simulation Time-driven simulation ・Discretize time in quanta of size dt. ・Update the position of each particle after every dt units of time, Main drawbacks. ・~ N / 2 overlap checks per time quantum. ・Simulation is too slow if dt is very small. ・May miss collisions if dt is too large. dt too small: excessive computation 2 and check for overlaps. ・If overlap, roll back the clock to the time of the collision, update the velocities of the colliding particles, and continue the simulation. (if colliding particles fail to overlap when we are looking) dt too small: excessive computation dt too large: may miss collisions dt too large: may miss collisions t t + dt t + 2 dt (collision detected) t + Δt (roll back clock) Fundamental challenge for time-driven simulation 43 44 Event-driven simulation Part...
View Full Document

This document was uploaded on 02/20/2014 for the course COS 226 at Princeton.

Ask a homework question - tutors are online