// FLIGHT404 v7 // // Magnetic particle experiment. // // Positively charged particle behavior in a gravitational field. // // Thanks to Tom Carden (http://www.tom-carden.co.uk/p5/) for assistance with // the vector vs. cartesian debate. He was very helpful in getting this experiment // to work properly. // // Project began in April 2004 as an attempt to execute an explanation of the // physics behind magnetic particles written by 'miked' on the processing.org forum. // http://processing.org/discourse/yabb/YaBB.cgi?board=Contribution_Simlation;action=display;num=1082567431;start= // // applet size variables int xSize = 400; int ySize = 400; int xMid = xSize/2; int yMid = ySize/2; // gravity variables int xGrav = 0; int yGrav = 0; int zGrav = 0; float gxv; float gyv; float gzv; float gravity = 0.1; // camera variables float xMouse, yMouse; float rotationVar; float elevation, azimuth, twist, distance; int totalParticles = 0; int maxParticles = 720; Particle[] particle; int totalLights = 0; int maxLights = 4; BImage myField; boolean gravLines = false; boolean connections = false; boolean orbs = true; boolean planes = false; boolean centerLit = false; int counter = 0; void setup(){ size(xSize, ySize); lights(); background(255); colorMode(RGB,255); sphereDetail(20); elevation = radians(15.0f); azimuth = radians(0.0f); distance = 150.0f; myField = loadImage("field.gif"); brightToAlpha(myField); particle = new Particle[maxParticles]; for (int i=0; i 0){ doLights(); } if (mousePressed && counter == 0 && totalParticles < maxParticles - 1){ //particle[totalParticles] = new Particle(mouseX - xMid, mouseY - yMid, sin(totalParticles/4.0) * 20.0, totalParticles, 1.0); particle[totalParticles] = new Particle(random(-10.0,10.0),random(-10.0,10.0),random(-10.0,10.0), totalParticles, 1.0); totalParticles ++; if (totalLights < maxLights - 1){ totalLights ++; } counter ++; } if (mouseX != pmouseX && mouseY != pmouseY){ counter = 0; } for (int i=0; i 0.01) { xv += A * (x - particle[i].x) / R; yv += A * (y - particle[i].y) / R; zv += A * (z - particle[i].z) / R; } if (R < 35.0){ totalConnections ++; if (connections){ strokeAlpha -= (strokeAlpha - ((35.0 - R) * 7.0)) * .1; } else { strokeAlpha -= (strokeAlpha - 0) * .1; } //stroke(225,245,255, strokeAlpha); //line(particle[i].x, particle[i].y, particle[i].z, x, y, z); } } } xv *= damp; yv *= damp; zv *= damp; //Q -= (Q - ((totalConnections / 5.0) + 0.5)) * .1; } void applyGravity(){ gDist = findDistance(xGrav,yGrav,zGrav,x,y,z); if (gDist > 0.1) { gxv = gravity * (xGrav - x) / gDist; gyv = gravity * (yGrav - y) / gDist; gzv = gravity * (zGrav - z) / gDist; xv += gxv; yv += gyv; zv += gzv; } if (gravLines && gDist < 200){ stroke(255,0,0); line(x, y, z, x + gxv*14.0, y + gyv*14.0, z + gzv*14.0); } } void move(){ x += xv; y += yv; z += zv; } void render(){ if (!orbs){ mySize -= (mySize - 1.0) * .1; } else { mySize -= (mySize - ((totalConnections/4.0) + (255 - zDepth)/40.0)) * .5; } push(); translate(x, y, z); zDepth = ((screenZ(0,0,0) * 100.0) - 99.0) * 500.0; //rotateZ(azimuth); //rotateX(-elevation); //rotateY(twist); if (orbs){ if (centerLit){ tint(255 - (gDist*gDist)/5.0, 255 - (gDist*gDist)/5.0, 255 - (gDist*gDist)/5.0); } else { tint(255 - (zDepth * 0.5), 255 - (zDepth * 0.7), 255 - (zDepth * 1.0)); } } else { tint(255,255,255); } //println(tempColor); //image(myField, 0 - mySize/2.0, 0 - mySize/2.0, mySize, mySize); noStroke(); fill(255); sphere(mySize*0.75); pop(); } } // create alpha for a bitmap by analyzing brightness (code by Ryan Alexander of Motion Theory) void brightToAlpha(BImage b){ b.format = RGBA; for(int i=0; i < b.pixels.length; i++) { b.pixels[i] = color(255,255,255,255 - brightness(b.pixels[i])); } } // find distance between 2 points in 2D space float findDistance(float x1, float y1, float x2, float y2){ float xd = x1 - x2; float yd = y1 - y2; float td = sqrt(xd * xd + yd * yd); return td; } // find distance between 2 points in 3D space float findDistance(float x1, float y1, float z1, float x2, float y2, float z2){ float xd = x1 - x2; float yd = y1 - y2; float zd = z1 - z2; float xyd = sqrt(xd * xd + yd * yd); float td = sqrt(zd * zd + xyd * xyd); return td; } // find angle in radians between 2 points in 2D space float findAngle(float x1, float y1, float x2, float y2){ float xd = x1 - x2; float yd = y1 - y2; float t = atan2(yd,xd); return t; } //