We are about to switch to a new forum software. Until then we have removed the registration on this forum.
These are two small simulation, which i made for my physics exam, to demonstrate the doppler effect, and the interference of waves.
This is a simulation the interference of waves. There are two wave generators generating circular waves with the same frequency, the waves all has the same velocity, and thus wavelength. The generators are next to one another, and their waves are interfering with one another, they are creating a pattern of stripes. Left and right arrow keys moves the generators closer two, or farther from one another, changing the number of stripes generated.
float speedOfWaves=1;
int periodOfgenerator=20;
class circle_wave {//a circle with an increasing radius
float centX;
float centY;//the center of the wave
float v;//the speed of the waves
float r;//the distance the wave has traveled
color c;//the color reprasents weather it's a top or buttom
circle_wave(float tempV, float tempCentX, float tempCentY, color tempC) {
centX = tempCentX;
centY = tempCentY;
v = tempV;
r = 0;
c = tempC;
}
void beWave() {
r+=v;
stroke(c);
noFill();
ellipse(centX, centY, r*2, r*2);
}
}
class toneGenerator {
float x;
float y;//the position of the generator
int T;//the period of the generator
int cycles;
ArrayList<circle_wave> waves;//an arraylist of the waves emitted
toneGenerator(float tempX, float tempY, int tempT) {
T=tempT;
println(T);
x=tempX;
y=tempY;
waves = new ArrayList<circle_wave>();
// waves.add(new circle_wave(2, x, y, color(255, 255, 255, 100)));
}
void generate() {
fill(0);//display and move the generator
ellipse(x, y, 10, 10);
if (x > width) {
x=0;
} else if (x < 0) {
x=width;
}
//display and move the existing waves
for (int i = waves.size ()-1; i>=0; i--) {
circle_wave myWave = waves.get(i);
myWave.beWave();
if (myWave.r > width/3) {
waves.remove(i);
}
}
if (mousePressed || keyPressed) {
waves = new ArrayList<circle_wave>();
// waves.add(new circle_wave(0.5, x, y, color(255, 255, 255, 100)));
}
if (cycles%(T*2)==0) {
waves.add(new circle_wave(speedOfWaves, x, y, color(255, 255, 255)));
} else if (cycles%(T*2)==T) {
waves.add(new circle_wave(speedOfWaves, x, y, color(0, 0, 0)));
}
cycles++;
}
}
toneGenerator[] tone;
void setup() {
size(1000, 750);
tone = new toneGenerator[2];
tone[0] = new toneGenerator(width/2+20, height/2, periodOfgenerator);
tone[1] = new toneGenerator(width/2-20, height/2, periodOfgenerator);
}
void draw() {
background(122);
tone[0].generate();
tone[1].generate();
}
void keyPressed() {
if (key == CODED) {
if (keyCode == LEFT) {
tone[0].x+=2.5;
tone[1].x-=2.5;
} else if (keyCode == RIGHT) {
tone[0].x-=2.5;
tone[1].x+=2.5;
}
}
}
This simulation is a modified version of the previous one, there is only one wave generator on the screen. It is generating circular waves, with a constant frequency and velocity. Pressing the left and right arrow keys will speed it up, or slow it down, clicking spacebar will freeze it, and move the waves instead, as if the screen was moving with the same speed as the generator.
The waves are moving relative to the background (just like soundwaves is moving relative to the air around it) not the speed of the generator, so the waves in front of the generator will be compressed, and those behind will get stretched out (in the case of soundwaves, whe hear a different tone, depending on whether we are standing before or behind it, in case of light, we see a different color, depending on in what direction, and how fast, the source of light is moving relative to us).
When the generator is moving at the same speed as the waves, the waves are not getting away from it, and a shockwave builds up (in case of sound, the sound barrier). When the generator breaks the speed of the waves, the shockwave becomes cone shaped.
float speedOfWaves=2;
int periodOfgenerator=10;
boolean move=true;
class circle_wave {//a circle with an increasing radius
float centX;
float centY;//the center of the wave
float v;//the speed of the waves
float r;//the distance the wave has traveled
color c;//the color reprasents weather it's a top or buttom
circle_wave(float tempV, float tempCentX, float tempCentY, color tempC) {
centX = tempCentX;
centY = tempCentY;
v = tempV;
r = 0;
c = tempC;
}
void beWave(float wind) {
r+=v;
stroke(c,map(r,0,width/2,255,0));
noFill();
centX-=wind;
ellipse(centX, centY, r*2, r*2);
}
}
class toneGenerator {
float x;
float y;//the position of the generator
float xV;//the x velocity
int T;//the period of the generator
int cycles;
ArrayList<circle_wave> waves;//an arraylist of the waves emitted
toneGenerator(float tempXV, float tempX, float tempY, int tempT) {
T=tempT;
x=tempX;
y=tempY;
xV=tempXV;
waves = new ArrayList<circle_wave>();
// waves.add(new circle_wave(2, x, y, color(255, 255, 255, 100)));
}
void generate() {
fill(0);//display and move the generator
ellipse(x, y, 10, 10);
if (move) {
x+=xV;
}/* else {
for (int i = 0; i<waves.size (); i++) {
circle_wave myWave = waves.get(i);
myWave.centX-=xV;
}
}*/
if (x > width) {
x=0;
} else if (x < 0) {
x=width;
}
//display and move the existing waves
for (int i = waves.size ()-1; i>=0; i--) {
circle_wave myWave = waves.get(i);
if (!move) {
myWave.beWave(xV);
}
else {
myWave.beWave(0);
}
if (myWave.r > width/2) {
waves.remove(i);
}
}
if (mousePressed) {
waves = new ArrayList<circle_wave>();
// waves.add(new circle_wave(2, x, y, color(255, 255, 255, 100)));
}
if (cycles%(T*2)==0) {
waves.add(new circle_wave(speedOfWaves, x, y, color(255, 255, 255)));
} else if (cycles%(T*2)==T) {
waves.add(new circle_wave(speedOfWaves, x, y, color(0, 0, 0)));
}
cycles++;
}
}
toneGenerator tone;
void setup() {
size(1000, 750);
tone = new toneGenerator(0, width/2+20, height/2, periodOfgenerator);
}
void draw() {
background(122);
tone.generate();
}
void keyPressed() {
if (key == CODED) {
if (keyCode == LEFT) {
tone.xV-=0.25;
} else if (keyCode == RIGHT) {
tone.xV+=0.25;
}
} else if (key == ' ') {
move=!move;
}
}
Comments
it's The Doppler effect (or Doppler shift) with pp
http://en.wikipedia.org/wiki/Doppler_effect
;-)
very nice! :)
Corected the mispelling of the word »doppler« as »dobbler«.
thanks ;-)
I thought if you had to hand it in... ;-)
cool!