ParticleVortex.cs :  » Game » SDL » SdlDotNet » Particles » Manipulators » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Game » SDL 
SDL » SdlDotNet » Particles » Manipulators » ParticleVortex.cs
#region LICENSE
/*
 * Copyright (C) 2005 Rob Loach (http://www.robloach.net)
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
#endregion LICENSE

using System;
using System.Drawing;
using System.Diagnostics.CodeAnalysis;

using SdlDotNet.Core;
using SdlDotNet.Particles;

namespace SdlDotNet.Particles.Manipulators{
    /// <summary>
    /// A particle manipulator that pulls particles towards a point.
    /// </summary>
    /// <remarks>If the radius is set to -1, the pull (strength) on all particles is constant.</remarks>
    /// <example>
    /// The following example creates a particle vortex that pulls particles towards the middle of the screen.
    /// <code>
    /// ParticleVortex vortex = new ParticleVortex(0.3f);
    /// vortex.X = (float)(Video.Screen.Width / 2);
    /// vortex.Y = (float)(Video.Screen.Height / 2);
    /// particleSystem.Add(vortex);
    /// </code>
    /// </example>
    public class ParticleVortex : IParticleManipulator
    {
        /// <summary>
        /// Creates a particle vortex manipulator with the default values.
        /// </summary>
        public ParticleVortex()
        {
        }
        /// <summary>
        /// Creates a particle vortex manipulator from just the location.
        /// </summary>
        /// <param name="position">The position of the vortex.</param>
        public ParticleVortex(PointF position)
        {
            m_X = position.X;
            m_Y = position.Y;
        }
        /// <summary>
        /// Creates a particle vortex manipulator.
        /// </summary>
        /// <param name="position">The position of the vortex.</param>
        /// <param name="strength">The amount of pull applied to the particles.</param>
        /// <param name="radius">The size of the vortex. -1 is infinate size.</param>
        public ParticleVortex(PointF position, float strength, float radius)
        {
            m_X = position.X;
            m_Y = position.Y;
            m_Strength = strength;
            m_Radius = radius;
        }
        /// <summary>
        /// Creates a particle vortex manipulator with an infinate size.
        /// </summary>
        /// <param name="posX">The X coordinate of the vortex.</param>
        /// <param name="posY">The Y coordinate of the vortex.</param>
        /// <param name="strength">The amount of pull applied to the particles.</param>
        public ParticleVortex(float posX, float posY, float strength)
        {
            m_X = posX;
            m_Y = posY;
            m_Strength = strength;
        }
        /// <summary>
        /// Creates a particle vortex manipulator with an infinate size.
        /// </summary>
        /// <param name="posX">The X coordinate of the vortex.</param>
        /// <param name="posY">The Y coordinate of the vortex.</param>
        /// <param name="strength">The amount of pull applied to the particles.</param>
        /// <param name="radius">The size of the vortex. -1 is infinate size.</param>
        public ParticleVortex(float posX, float posY, float strength, float radius)
        {
            m_X = posX;
            m_Y = posY;
            m_Strength = strength;
            m_Radius = radius;
        }
        /// <summary>
        /// Creates a particle vortex manipulator from just the strength.
        /// </summary>
        /// <param name="strength">The amount of pull applied to the particles.</param>
        public ParticleVortex(float strength)
        {
            m_Strength = strength;
        }
        /// <summary>
        /// Creates a particle vortex manipulator from just the strength and radius.
        /// </summary>
        /// <param name="strength">The amount of pull applied to the particles.</param>
        /// <param name="radius">The size of the vortex. -1 is infinate size.</param>
        public ParticleVortex(float strength, float radius)
        {
            m_Strength = strength;
            m_Radius = radius;
        }

        private float m_X;
        private float m_Y;
        /// <summary>
        /// Gets and sets the Y coordinate of the vortex.
        /// </summary>
        [SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", Justification = "Correct Spelling")]
        public float Y
        {
            get
            {
                return m_Y;
            }
            set
            {
                m_Y = value;
            }
        }
        /// <summary>
        /// Gets and sets the X coordinate of the vortex.
        /// </summary>
        [SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", Justification = "Correct Spelling")]
        public float X
        {
            get
            {
                return m_X;
            }
            set
            {
                m_X = value;
            }
        }
        private float m_Radius = -1f;
        /// <summary>
        /// Gets and sets the size, the radius, of the vortex.  If set to -1 the pull will be constant on each particle.
        /// </summary>
        public float Radius
        {
            get
            {
                return m_Radius;
            }
            set
            {
                m_Radius = value;
            }
        }
        private float m_Strength = 0.2f;
        /// <summary>
        /// Gets and sets the amount of pull put onto each particle in the vortex's radius.
        /// </summary>
        public float Strength
        {
            get
            {
                return m_Strength;
            }
            set
            {
                m_Strength = value;
            }
        }
        #region IParticleManipulator Members

        /// <summary>
        /// Applies the vortex force on each particle in the vortex's radius.
        /// </summary>
        /// <param name="particles">The collection of particles to manipulate.</param>
        public void Manipulate(ParticleCollection particles)
        {
            if (particles == null)
            {
                throw new ArgumentNullException("particles");
            }
            foreach (BaseParticle p in particles)
            {
                Vector v = new Vector(p.X, p.Y, 0, m_X, m_Y, 0);
                if (m_Radius == -1f)
                {
                    v.Length = m_Strength;
                }
                else
                {
                    if (v.Length > m_Radius)
                        continue;
                    v.Length = (1f - v.Length / m_Radius) * m_Strength;
                }
                p.Velocity += v;
            }
        }

        #endregion
    }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.