Example #1
0
        /// <summary>
        /// Constructs a new particle emitter object.
        /// </summary>
        public ParticleEmitter(ParticleSystem particleSystem, float particlesPerSecond, Vector3 initialPosition)
        {
            ParticleSystem = particleSystem;

            _timeBetweenParticles = 1.0f / particlesPerSecond;
            ParticlesPerSecond = particlesPerSecond;
            _previousPosition = initialPosition;

            AllEmitters.Add(this);
        }
Example #2
0
        /// <summary>
        /// Updates the emitter, creating the appropriate number of particles
        /// in the appropriate positions.
        /// </summary>
        public void Update(Vector3 newPosition, ParticleSystem particleSystem)
        {
            float elapsedSeconds = Engine.Instance.FrameTime;

            // Work out how much time has passed since the previous update.

            if (elapsedSeconds > 0 && Enabled)
            {
                // Work out how fast we are moving.
                Vector3 velocity = (newPosition - _previousPosition) / elapsedSeconds;

                // If we had any time left over that we didn't use during the
                // previous update, add that to the current elapsed time.
                float timeToSpend = _timeLeftOver + elapsedSeconds;

                // Counter for looping over the time interval.
                float currentTime = -_timeLeftOver;

                // Create particles as long as we have a big enough time interval.
                while (timeToSpend > _timeBetweenParticles)
                {
                    currentTime += _timeBetweenParticles;
                    timeToSpend -= _timeBetweenParticles;

                    // Work out the optimal position for this particle. This will produce
                    // evenly spaced particles regardless of the object speed, particle
                    // creation frequency, or game update rate.
                    float mu = currentTime / elapsedSeconds;

                    Vector3 position = Vector3.Lerp(_previousPosition, newPosition, mu);

                    // Create the particle.
                    particleSystem.AddParticle(position, velocity);
                }

                // Store any time we didn't use, so it can be part of the next update.
                _timeLeftOver = timeToSpend;
            }

            _previousPosition = newPosition;
        }