Click here to Skip to main content
15,895,142 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 153.1K   3.5K   173  
A gravity simulation particle system
using System;
using System.Xml.Serialization;

namespace ParticleSwarm {
	public class Vector {
		#region Private members
		private float m_Xcoord;

		private float m_Ycoord;

		private float m_Zcoord;
		#endregion

		#region Constructors
		public Vector() {
		}

		public Vector(float inX, float inY, float inZ) {
			m_Xcoord = inX;
			m_Ycoord = inY;
			m_Zcoord = inZ;
		}

		public Vector(float[] coordination) {
			m_Xcoord = coordination[0];
			m_Ycoord = coordination[1];
			m_Zcoord = coordination[2];
		}

		public Vector(Vector vector) {
			m_Xcoord = vector.X;
			m_Ycoord = vector.Y;
			m_Zcoord = vector.Z;
		}
		#endregion

		#region Public properties
		[XmlAttribute("x")]
		public float X {
			get { return m_Xcoord; }
			set { m_Xcoord = value; }
		}

		[XmlAttribute("y")]
		public float Y {
			get { return m_Ycoord; }
			set { m_Ycoord = value; }
		}

		[XmlAttribute("z")]
		public float Z {
			get { return m_Zcoord; }
			set { m_Zcoord = value; }
		}

		public static Vector Zero {
			get { return new Vector(0.0f, 0.0f, 0.0f); }
		}

		public static Vector XAxis {
			get { return new Vector(1.0f, 0.0f, 0.0f); }
		}

		public static Vector YAxis {
			get { return new Vector(0.0f, 1.0f, 0.0f); }
		}

		public static Vector ZAxis {
			get { return new Vector(0.0f, 0.0f, 1.0f); }
		}
		#endregion

		#region Methods
		public static Vector Add(Vector vector1, Vector vector2) {
			if ((object)vector1 == null || (object)vector2 == null)
				return null;
			return new Vector(vector1.X + vector2.X, vector1.Y + vector2.Y, vector1.Z + vector2.Z);
		}

		public static Vector Subtract(Vector vector1, Vector vector2) {
			if ((object)vector1 == null || (object)vector2 == null)
				return null;
			return new Vector(vector1.X - vector2.X, vector1.Y - vector2.Y, vector1.Z - vector2.Z);
		}

		public static Vector Negate(Vector vector) {
			if ((object)vector == null)
				return null;
			return new Vector(-vector.X, -vector.Y, -vector.Z);
		}

		public static Vector Multiply(Vector vector, float val) {
			if ((object)vector == null)
				return null;
			return new Vector(vector.X * val, vector.Y * val, vector.Z * val);
		}

		public static Vector Divide(Vector vector, float val) {
			if ((object)vector == null)
				return null;
			return new Vector(vector.X / val, vector.Y / val, vector.Z / val);
		}
		#endregion

		#region Operators
		public static bool operator ==(Vector vector1, Vector vector2) {
			if ((object)vector1 == null || (object)vector2 == null)
				return false;
			return ((vector1.X.Equals(vector2.X))
				&& (vector1.Y.Equals(vector2.Y))
				&& (vector1.Z.Equals(vector2.Z)));
		}

		public static bool operator !=(Vector vector1, Vector vector2) {
			if ((object)vector1 == null || (object)vector2 == null)
				return false;
			return ((!vector1.X.Equals(vector2.X)) 
				&& (!vector1.Y.Equals(vector2.Y))
				&& (!vector1.Z.Equals(vector2.Z)));
		}

		public static Vector operator+(Vector vector1, Vector vector2) {
			if ((object)vector1 == null || (object)vector2 == null)
				return null;
			return Vector.Add(vector1, vector2);
		}

		public static Vector operator-(Vector vector1, Vector vector2) {
			if ((object)vector1 == null || (object)vector2 == null)
				return null;
			return Vector.Subtract(vector1, vector2);
		}

		public static Vector operator-(Vector vector) {
			if ((object)vector == null)
				return null;
			return Vector.Negate(vector);
		}

		public static Vector operator*(Vector vector, float val) {
			if ((object)vector == null)
				return null;
			return Vector.Multiply(vector, val);
		}

		public static Vector operator/(Vector vector, float val) {
			if ((object)vector == null)
				return null;
			return Vector.Divide(vector, val);
		}
		#endregion

		#region Overides
		public override bool Equals(object obj) {
			Vector vector = obj as Vector;
			if ((Object)vector != null)
				return (m_Xcoord.Equals(vector.X)) 
					&& (m_Ycoord.Equals(vector.Y))
					&& (m_Zcoord.Equals(vector.Z));
			return false;
		}

		public override string ToString() {
			return string.Format("({0}, {1}, {2})", m_Xcoord, m_Ycoord, m_Zcoord);
		}

		public override int GetHashCode() {
			return m_Xcoord.GetHashCode() ^ m_Ycoord.GetHashCode() ^ m_Zcoord.GetHashCode();
		}
		#endregion
	}
}

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