We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › Photons, mirrors, etc.
Page Index Toggle Pages: 1
Photons, mirrors, etc. (Read 602 times)
Photons, mirrors, etc.
Mar 2nd, 2006, 2:09am
 
I've been playing with this for the past several hours and I keep reaching dead ends.

What I'd like is some sort of environment in which I can set up a series of "mirrors" off of which particles/photons reflect. The idea is it would allow you to try out designs for things like periscopes and prisms.

The hardest part seems to be finding a good algorithm for handling collisions between the photons and mirrors... They often work for a few scenarios, but perform erratically in others. Here's one such example:

Code:
  boolean collide(Mirror m) {
if(m == lastCollision)
return false;

// Endpoints of mirror
float x1 = m.xPos + 0.5 * m.len * cos(m.angle);
float y1 = m.yPos + 0.5 * m.len * sin(m.angle);
float x2 = m.xPos - 0.5 * m.len * cos(m.angle);
float y2 = m.yPos - 0.5 * m.len * sin(m.angle);

// Endpoints of photon
float x3 = xPos;
float y3 = yPos;
float x4 = xPos + speed * cos(angle);
float y4 = yPos + speed * sin(angle);

// Calculate the intersection
float ua = ((x4 - x3) * (y1 - y3) - (y4 - y3) * (x1 - x3)) / ((y4 - y3) * (x2 - x1) - (x4 - x3) * (y2 - y1));
float ub = ((x2 - x1) * (y1 - y3) - (y2 - y1) * (x1 - x3)) / ((y4 - y3) * (x2 - x1) - (x4 - x3) * (y2 - y1));

if(ua >= 0 && ua <= 1 && ub >= 0 && ub <= 1) {
// Hack based on the Law of Cosines and some guesswork
float a = m.len / 2;
float b = dist(m.xPos, m.yPos, xPos, yPos);
float c = dist(x1, y1, xPos, yPos);
float ang = acos((sq(a) + sq(b) - sq(c)) / (2 * a * b));
angle -= PI - 2 * ang;

print("collision ");
lastCollision = m;
return true;
}

return false;
}


It successfully identifies all collisions, but the reflection is incorrect.

Any thoughts?
Re: Photons, mirrors, etc.
Reply #1 - Mar 2nd, 2006, 2:50am
 
Aha, I got it working marginally better... it's not perfect, but it works enough to do this...

http://rgov.org/img/reflection/
Re: Photons, mirrors, etc.
Reply #2 - Mar 2nd, 2006, 5:25pm
 
Just a thought but you might want to look up interference. Two light wavefronts (and these wavefronts are spherical mind) of the same wavelength will cancel one another out. This is how holograms are made. The laser passes through the holographic plate, hits the subject and bounces back to interfere with the laser light still coming forward. The spherical interference patterns create parabolic mirrors of silver halide. This is how holograms appear three-dimensional.

When taking the hologram the setup needs to be perfectly still. A vibration of greater than the wavelength of the light used will disturb the interference pattern.

Another tidbit of information is that first surface reflection mirrors are used in the setup, unlike normal mirrors where the reflective surface is behind a pane of glass.

This is probably irrelevant but I'm just sharing.
Re: Photons, mirrors, etc.
Reply #3 - Mar 3rd, 2006, 4:38am
 
Neat... I don't know if I'll be doing that much detail just yet, though - right now it's more of a pinball simulator Smiley

I have re-written some of my code at http://rgov.org/img/reflection/test/ ... you can now drag the mirror around, grow it, and spin it by grabbing onto the center of the line or its endpoints. I thought I had the reflection code working sure-thing, but when I changed it to the "stream" as it is now, I see that it falls apart. Argh!
Page Index Toggle Pages: 1