Click here to Skip to main content
15,894,740 members
Articles / Programming Languages / XML

Fun With Gravity

Rate me:
Please Sign up or sign in to vote.
4.93/5 (77 votes)
28 Jul 2008CPOL4 min read 153K   3.5K   173  
A gravity simulation particle system
using System;
using System.Drawing;
using System.Xml.Serialization;
using System.Collections.Generic;

namespace ParticleSwarm {
	public class Ghost {
		public Vector Location;
		public Color Color;
		public PointF Velocity1;
		public PointF Velocity2;
		public PointF Acceleration1;
		public PointF Acceleration2;
		public Ghost(Vector location, Color color) {
			this.Location = location;
			this.Color = color;
		}
		public Ghost(Vector location, Color color, PointF vel1, PointF vel2, PointF acc1, PointF acc2) : this(location, color) {
			this.Velocity1 = vel1;
			this.Velocity2 = vel2;
			this.Acceleration1 = acc1;
			this.Acceleration2 = acc2;
		}
	}

	/// <summary></summary>
	public class Particle {
		public static int GhostCount = 40;
		public static int TopAlpha = 70;
		private List<Ghost> ghosts = new List<Ghost>();
		private Vector location;
		private Vector velocity;
		private Vector acceleration;
		private float mass;
		private float size;
		private Color color;

		public Particle(Vector loc, Vector vel) {
			this.location = loc;
			this.velocity = vel;
			this.color = Color.Black;
		}

		public Particle(Vector loc, Vector vel, Color color) : this(loc, vel) {
			this.color = color;
		}

		public Particle(Vector loc, Vector vel, float mass, float size, Color color) : this(loc, vel, color) {
			this.mass = mass;
			this.size = size;
		}

		public void Update() {
			int colorFactor = (TopAlpha - 10) / GhostCount;
			ghosts.Add(new Ghost(location, Color.FromArgb(TopAlpha - colorFactor, color.R, color.G, color.B)));
			if (ghosts.Count > GhostCount && ghosts.Count > 0) {
				ghosts.RemoveAt(0);
			}
			for (int i = 1; i < ghosts.Count; i++) {
				int factor = (ghosts.Count - i + 1) * colorFactor;
				ghosts[i].Color = Color.FromArgb(TopAlpha - (factor), color.R, color.G, color.B);
			}

			this.location += this.velocity;
		}

		public void Update(PointF vel1, PointF vel2, PointF acc1, PointF acc2) {
			int colorFactor = (TopAlpha - 10) / GhostCount;
			ghosts.Add(new Ghost(location, Color.FromArgb(TopAlpha - colorFactor, color.R, color.G, color.B), vel1, vel2, acc1, acc2));
			if (ghosts.Count > GhostCount && ghosts.Count > 0) {
				ghosts.RemoveAt(0);
			}
			for (int i = 1; i < ghosts.Count; i++) {
				int factor = (ghosts.Count - i + 1) * colorFactor;
				ghosts[i].Color = Color.FromArgb(TopAlpha - (factor), color.R, color.G, color.B);
			}

			this.location += this.velocity;
		}

		/// <summary></summary>
		[XmlElement("Location")]
		public Vector Location {
			get { return this.location; }
			set { this.location = value; }
		}

		/// <summary></summary>
		[XmlIgnore]
		public Vector MidLocation {
			get {
				return new Vector(this.location.X + this.size / 2, this.location.Y + this.size / 2, this.location.Z + this.size / 2);
			}
		}

		/// <summary></summary>
		[XmlElement("Velocity")]
		public Vector Velocity {
			get { return this.velocity; }
			set { this.velocity = value; }
		}

		/// <summary></summary>
		[XmlIgnore]
		public Vector Acceleration {
			get { return this.acceleration; }
			set { this.acceleration = value; }
		}

		/// <summary></summary>
		[XmlAttribute("mass")]
		public float Mass {
			get { return this.mass; }
			set { this.mass = value; }
		}

		/// <summary></summary>
		[XmlAttribute("size")]
		public float Size {
			get { return this.size; }
			set { this.size = value; }
		}

		/// <summary></summary>
		[XmlElement("Color")]
		public Color Color {
			get { return this.color; }
			set { this.color = value; }
		}

		[XmlIgnore]
		public List<Ghost> Ghosts {
			get { return ghosts; }
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) BoneSoft Software
United States United States
I've been in software development for more than a decade now. Originally with ASP 2.0 and VB6. I worked in Japan for a year doing Java. And have been with C# ever since.

In 2005 I founded BoneSoft Software where I sell a small number of developer tools.
This is a Organisation (No members)


Comments and Discussions