public void AddRandomBurst(Point location, int count) { for (int i = 0; i < count; i++) { Particle p = new Particle(); double radius = Radius + (_random.NextDouble() * 2.0 - 1.0) * RadiusVariation; double lifetime = _random.NextDouble() * 3.0 + 1.0; p.Location = location; p.Velocity = new Vector(_random.NextDouble() * 200.0 - 100.0, _random.NextDouble() * -200 + 100.0); p.LifeTime = _timeTracker.ElapsedTime; p.DeathTime = _timeTracker.ElapsedTime + TimeSpan.FromSeconds(lifetime); p.Diameter = 2.0 * radius; Color startColor = StartColor; Color startColorVariation = StartColorVariation; Color endColor = EndColor; Color endColorVariation = EndColorVariation; Color startRandColor = Color.FromScRgb(startColorVariation.ScA * (float)(_random.NextDouble() * 2.0 - 1.0),startColorVariation.ScR * (float)(_random.NextDouble() * 2.0 - 1.0),startColorVariation.ScG * (float)(_random.NextDouble() * 2.0 - 1.0),startColorVariation.ScB * (float)(_random.NextDouble() * 2.0 - 1.0)); Color endRandColor = Color.FromScRgb(endColorVariation.ScA * (float)(_random.NextDouble() * 2.0 - 1.0), endColorVariation.ScR * (float)(_random.NextDouble() * 2.0 - 1.0), endColorVariation.ScG * (float)(_random.NextDouble() * 2.0 - 1.0), endColorVariation.ScB * (float)(_random.NextDouble() * 2.0 - 1.0)); p.StartColor = startColor + startRandColor; p.EndColor = endColor + endRandColor; _particles.Add(p); } }
private void UpdateParticles(double deltatime) { //drop particles from mouse position if (_mouseDropsParticles && IsMouseOver) { _secondsUntilDrop -= deltatime; if (_secondsUntilDrop < 0.0) { AddRandomBurst(_lastMousePosition - new Vector(Radius / 2.0, Radius / 2.0), 1); _secondsUntilDrop = MouseDropDelay + (_random.NextDouble() * 2.0 - 1.0) * MouseDropDelayVariation; } } if (_particles.Count > 0) { InvalidateVisual(); } //update all particles for (int i = 0; i < _particles.Count;) { //_particles[i] Particle p = _particles[i]; if (_gravitateToMouse) { p.Velocity += (_lastMousePosition - p.Location) * _gravitateScale; } else { p.Velocity += _gravity; } p.Location += p.Velocity * deltatime; if (_bounceOffContainer) { double radius = p.Diameter / 2.0; if (p.Location.X - radius < 0.0) { p.Location.X = radius; p.Velocity.X *= -0.9; } else if (p.Location.X > ActualWidth - radius) { p.Location.X = ActualWidth - radius; p.Velocity.X *= -0.9; } if (p.Location.Y - radius < 0.0) { p.Location.Y = radius; p.Velocity.Y *= -0.9; } else if (p.Location.Y > ActualHeight - radius) { p.Location.Y = ActualHeight - radius; p.Velocity.Y *= -0.9; } } //only increment counter for live particles if (_timeTracker.ElapsedTime > p.DeathTime) { _particles.RemoveAt(i); } else { i++; } } }