Skip to content

Commit

Permalink
fixed an issue where mouse attract vec was updated before a user inte…
Browse files Browse the repository at this point in the history
…racted
  • Loading branch information
corybarr committed Dec 29, 2011
1 parent 84f36e4 commit b3833a0
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 6 deletions.
28 changes: 24 additions & 4 deletions alphaBlended3DParticles.pde
Expand Up @@ -2,14 +2,15 @@ Particle p[];
float fadeRate;
int numParticles = 50;
PVector mouseAttractVec;
boolean userHasInteracted = false;

void setup() {
size(500, 500);
background(0);
smooth();
noStroke();
frameRate(30);
mouseAttractVec = new PVector(1.0f, 1.0f, 1.0f);
mouseAttractVec = new PVector(0.0f, 0.0f, 0.0f);

fadeRate = 15.0f;
p = new Particle[numParticles];
Expand All @@ -21,18 +22,37 @@ void setup() {

void draw() {

if (frameCount % (int) (frameRate / 4) == 0) {
mouseAttractVec = getMouseAttractVec();
if (frameCount == 0) {
mouseAttractVec = new PVector(0.0f, 0.0f, 0.0f);
}

if (userHasInteracted && (frameCount % (int) (frameRate / 4) == 0)) {
mouseAttractVec = getMouseAttractVec();
}
background(0);
updateParticles(p);
}

void mouseMoved() {
userHasInteracted = true;
}

void mousePressed() {
userHasInteracted = true;
}

PVector getMouseAttractVec() {
//for some reason, processing sets the mouseX
//and mouseY to upper left corner. Detecting
//and correcting. Assuming user can never get a
//mouseX, mouseY of (0, 0);
if (mouseX == 0 && mouseY == 0) {
println("were both 0 on frame " + frameCount);
}

float newXVal = map(mouseX, 0, width, -1, 1);
float newYVal = map(mouseY, 0, height, -1, 1);

return new PVector(newXVal, newYVal, 0.0f);
}

Expand Down
32 changes: 30 additions & 2 deletions applet_js/alphaBlended3DParticles.pde
@@ -1,13 +1,15 @@
Particle p[];
float fadeRate;
int numParticles = 50;
PVector mouseAttractVec;

void setup() {
size(500, 500);
background(0);
smooth();
noStroke();
frameRate(30);
mouseAttractVec = new PVector(1.0f, 1.0f, 1.0f);

fadeRate = 15.0f;
p = new Particle[numParticles];
Expand All @@ -18,8 +20,20 @@ void setup() {
}

void draw() {

if (frameCount % (int) (frameRate / 4) == 0) {
mouseAttractVec = getMouseAttractVec();
}

background(0);
updateParticles(p);
updateParticles(p);
}

PVector getMouseAttractVec() {
float newXVal = map(mouseX, 0, width, -1, 1);
float newYVal = map(mouseY, 0, height, -1, 1);

return new PVector(newXVal, newYVal, 0.0f);
}

void updateParticles(Particle _p[]) {
Expand All @@ -44,6 +58,8 @@ class Particle {
int life = 0;
float zScalar = 0.9; //for determining how the z axis is graphically interpreted
float curPartSize;
float innerRingMultiplier;
boolean allowAttraction = true;

Particle() {

Expand All @@ -53,6 +69,7 @@ class Particle {
velocity.mult(random(minMagnitude, maxMagnitude));
partSize = random(30, 60);
curPartSize = partSize;
innerRingMultiplier = random(0.2, 5);
}

void render() {
Expand All @@ -63,7 +80,7 @@ class Particle {
for (int i = 0; i < numInnerRings; i++) {
float curWhiteVal = map(i, 0, numInnerRings - 1, 0, 255);
float curAlpha = map(i, 0, numInnerRings - 1, 20, 200);
float ringSize = curPartSize / (i * 2.0f + 1.0f);
float ringSize = curPartSize / (i * innerRingMultiplier + 1.0f);

fill(curWhiteVal, curWhiteVal, 255, curAlpha);
ellipse(position.x, position.y, ringSize, ringSize);
Expand Down Expand Up @@ -101,6 +118,17 @@ class Particle {
}

void move() {
if (allowAttraction) {
float attractionAmount;
PVector localMouseAttract = mouseAttractVec.get();
localMouseAttract.mult(0.08);

float curMagnitude = velocity.mag();
velocity.add(localMouseAttract);
velocity.normalize();
velocity.mult(curMagnitude);
}

position.add(velocity);
}

Expand Down

0 comments on commit b3833a0

Please sign in to comment.