private void ParticleWindow_MouseDown(object sender, MouseEventArgs e) { mouseDown = true; PrevMouse_e = e; ForceEmitter _emitter = new ForceEmitter(e.X, e.Y, 200, 200); //Forces RectangleF rect = new RectangleF(); rect.X = _emitter.OuterX; rect.Y = _emitter.OuterY; rect.Width = _emitter.W; rect.Height = _emitter.H; //Top-Left Qudrant RectangleF Q1 = new RectangleF(); Q1.X = rect.X; Q1.Y = rect.Y; Q1.Width = rect.Width / 2; Q1.Height = rect.Height / 2; //Bottom-Left Quadrant RectangleF Q2 = new RectangleF(); Q2.X = rect.X; Q2.Y = rect.Y + (rect.Width / 2); Q2.Width = rect.Width / 2; Q2.Height = rect.Height / 2; //Top-Right RectangleF Q3 = new RectangleF(); Q3.X = rect.X + (rect.Height / 2); Q3.Y = rect.Y; Q3.Width = rect.Width / 2; Q3.Height = rect.Height / 2; //Bottom-Right RectangleF Q4 = new RectangleF(); Q4.X = rect.X + (rect.Height / 2); Q4.Y = rect.Y + (rect.Width / 2); Q4.Width = rect.Width / 2; Q4.Height = rect.Height / 2; for (int i = 0; i < Particle.activeParticles.Count; i++) { PointF point = new PointF(); point.X = Particle.activeParticles[i].x; point.Y = Particle.activeParticles[i].y; Particle _temp = Particle.activeParticles[i]; //If the particle is in the rectangle and it is not being pulled yet if (rect.Contains(point) && _temp.pulled == false) { float rise; float run; float slope; rise = _emitter.Y - _temp.y; run = _emitter.X - _temp.x; slope = rise / run; //If the slope is negative trajectories get calculated differently //Top-Left if (slope > 0 && rise > 0 && run > 0) { _temp.yVel = (rise * -1) / 60; _temp.xVel = (run * 1) / 60; } //Top-Right else if (slope < 0 && rise > 0 && run < 0) { _temp.yVel = (rise * -1) / 60; _temp.xVel = (run * 1) / 60; } //Bottom-Right else if (slope > 0 && rise < 0 && run < 0) { _temp.yVel = (rise * -1) / 60; _temp.xVel = (run * 1) / 60; } //Bottom-Left else if (slope < 0 && rise < 0 && run > 0) { _temp.yVel = (rise * -1) / 60; _temp.xVel = (run * 1) / 60; } _temp.Gravity = 0.0f; } //Re-Enable gravity if the particle exits the vortex if (!rect.Contains(point)) { _temp.pulled = false; _temp.Gravity = -0.1f; } else { _temp.pulled = true; } //Hold the particle in place if it hits the emitter if ((point.X <= (_emitter.X + _temp.w * 3) && point.X >= (_emitter.X - _temp.w * 3)) && point.Y <= (_emitter.Y + _temp.h * 3) && point.Y >= (_emitter.Y - _temp.h * 3)) { _temp.x = _emitter.X; _temp.y = _emitter.Y; } } }
//On click we will generate a force that will push particles around private void ParticleWindow_Click(object sender, MouseEventArgs e) { //TO do that we first create a force emitter, this will be a //rectangular area around the mouse. ForceEmitter _emitter = new ForceEmitter(e.X, e.Y, 400, 400); //Center the rectangle around the emitter RectangleF rect = new RectangleF(); rect.X = _emitter.OuterX; rect.Y = _emitter.OuterY; rect.Width = _emitter.W; rect.Height = _emitter.H; /* * To generate forces properly for each particle, we are going to * need to run different equations depending on where in the recangle * the particle landed, because essentially we don't have to do a lot * right now in terms of generating higher velocities and such, for now * we care about changing the direction of the particle, which is simply * swapping the sign of the particle's x and y velocities depending on * what quadrant of the rectangle they fell into. Here we are creating * 4 quadrants that we will test against. */ //Top-Left Qudrant RectangleF Q1 = new RectangleF(); Q1.X = rect.X; Q1.Y = rect.Y; Q1.Width = rect.Width / 2; Q1.Height = rect.Height / 2; //Bottom-Left Quadrant RectangleF Q2 = new RectangleF(); Q2.X = rect.X; Q2.Y = rect.Y + (rect.Width / 2); Q2.Width = rect.Width / 2; Q2.Height = rect.Height / 2; //Top-Right RectangleF Q3 = new RectangleF(); Q3.X = rect.X + (rect.Height / 2); Q3.Y = rect.Y; Q3.Width = rect.Width / 2; Q3.Height = rect.Height / 2; //Bottom-Right RectangleF Q4 = new RectangleF(); Q4.X = rect.X + (rect.Height / 2); Q4.Y = rect.Y + (rect.Width / 2); Q4.Width = rect.Width / 2; Q4.Height = rect.Height / 2; /* * We will need to test each of the particles x and y * positions to see which quadrant they fall into, * while it would be nice to do this based on how much of * the particle is inside of a quadrant then applying * the force based on that, for the moment it feels too * expensive to do so we will look at it later. */ for (int i = 0; i < Particle.activeParticles.Count; i++) { PointF point = new PointF(); point.X = Particle.activeParticles[i].x; point.Y = Particle.activeParticles[i].y; Particle _temp = Particle.activeParticles[i]; _temp.Gravity = -0.1f; float rX = (float)rnd.Next(-5, 5); float rY = (float)rnd.Next(-5, 5); /* * Below is the math we need to perform to increase * velocity on click to apply force and to make * sure that depending on what side/quadrant * of the cursor the particle was in that we * re-position the particles trajectory accordingly */ if (_temp.pulled == true) { //Reassign random x and y velocities: rX = (float)rnd.Next(-5, 5); rY = (float)rnd.Next(-5, 5); rX = (float)rnd.Next(-5, 5); rY = (float)rnd.Next(-5, 5); rX = (float)rnd.Next(-5, 5); rY = (float)rnd.Next(-5, 5); _temp.pulled = false; } //Check Q1, xVel negative if greater than 0, //yVel positive if less than 0 if (Q1.Contains(point)) { _temp.xVel *= 5; _temp.yVel *= 5; if (_temp.xVel > 0) { _temp.xVel = _temp.xVel * -1; } if (_temp.yVel < 0) { _temp.yVel = _temp.yVel * 1; } } //Check Q2, xVel negative if greater than 0, //yVel negative if greater than 0 if (Q2.Contains(point)) { _temp.xVel *= 5; _temp.yVel *= 5; if (_temp.xVel > 0) { _temp.xVel = _temp.xVel * -1; } if (_temp.yVel > 0) { _temp.yVel = _temp.yVel * -1; } } //Check Q3, xVel positive is less than 0, //yVel positive if less than 0 if (Q3.Contains(point)) { _temp.xVel *= 5; _temp.yVel *= 5; if (_temp.xVel < 0) { _temp.xVel = _temp.xVel * 1; } if (_temp.yVel < 0) { _temp.yVel = _temp.yVel * 1; } } //Check Q4, xVel negative if less than zero and yVel negative //if greater than 0 if (Q4.Contains(point)) { _temp.xVel *= 5; _temp.yVel *= 5; if (_temp.xVel < 0) { _temp.xVel = _temp.xVel * -1; } if (_temp.yVel > 0) { _temp.yVel = _temp.yVel * -1; } } } mouseDown = false; }