Click here to Skip to main content
Click here to Skip to main content
Articles » Multimedia » GDI+ » General » Downloads
 
Add your own
alternative version
Go to top

Fun With Gravity

, 28 Jul 2008
A gravity simulation particle system.
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace ParticleSwarm {
	public class Form1 : System.Windows.Forms.Form {
		private System.Windows.Forms.PictureBox pictureBox1;
		private System.Windows.Forms.Timer timer1;
		private System.Windows.Forms.ListView listView1;
		private System.Windows.Forms.ColumnHeader columnHeader2;
		private System.Windows.Forms.ColumnHeader columnHeader3;
		private System.Windows.Forms.ColumnHeader columnHeader4;
		private System.Windows.Forms.ColumnHeader columnHeader5;
		private System.Windows.Forms.ColumnHeader columnHeader6;
		private System.Windows.Forms.ColumnHeader columnHeader7;
		private System.Windows.Forms.CheckBox checkBox1;
		private System.Windows.Forms.CheckBox checkBox2;
		private System.Windows.Forms.CheckBox checkBox3;
		private System.Windows.Forms.TextBox textBox1;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.TextBox textBox2;
		private System.Windows.Forms.Label label3;
		private System.Windows.Forms.TextBox textBox3;
		private System.Windows.Forms.Label label4;
		private System.Windows.Forms.TextBox textBox4;
		private System.Windows.Forms.ColumnHeader columnHeader8;
		private System.Windows.Forms.Label label5;
		private System.Windows.Forms.TextBox textBox5;
		private System.Windows.Forms.Button button5;
		private System.Windows.Forms.Button button6;
		private System.Windows.Forms.Button button7;
		private System.Windows.Forms.CheckBox checkBox4;
		private System.Windows.Forms.CheckBox checkBox5;
		private System.Windows.Forms.MainMenu mainMenu1;
		private System.Windows.Forms.MenuItem menuItem1;
		private System.Windows.Forms.MenuItem menuItem2;
		private System.Windows.Forms.MenuItem menuItem3;
		private System.Windows.Forms.MenuItem menuItem4;
		private System.Windows.Forms.CheckBox checkBox6;
		private System.Windows.Forms.Button button1;
		private System.ComponentModel.IContainer components;
		private Brush fadeBrush;

		public Form1() {
			InitializeComponent();
			fadeBrush = new SolidBrush(Color.FromArgb(10, pictureBox1.BackColor.R, pictureBox1.BackColor.G, pictureBox1.BackColor.B));
			SetStyle(ControlStyles.AllPaintingInWmPaint |
					ControlStyles.UserPaint |
					ControlStyles.DoubleBuffer,
					true);
		}

		protected override void Dispose(bool disposing) {
			if (disposing) {
				if (components != null) {
					components.Dispose();
				}
			}
			base.Dispose(disposing);
		}

		#region Windows Form Designer generated code
		private void InitializeComponent() {
			this.components = new System.ComponentModel.Container();
			this.pictureBox1 = new System.Windows.Forms.PictureBox();
			this.timer1 = new System.Windows.Forms.Timer(this.components);
			this.listView1 = new System.Windows.Forms.ListView();
			this.columnHeader8 = new System.Windows.Forms.ColumnHeader();
			this.columnHeader5 = new System.Windows.Forms.ColumnHeader();
			this.columnHeader6 = new System.Windows.Forms.ColumnHeader();
			this.columnHeader7 = new System.Windows.Forms.ColumnHeader();
			this.columnHeader2 = new System.Windows.Forms.ColumnHeader();
			this.columnHeader3 = new System.Windows.Forms.ColumnHeader();
			this.columnHeader4 = new System.Windows.Forms.ColumnHeader();
			this.checkBox1 = new System.Windows.Forms.CheckBox();
			this.checkBox2 = new System.Windows.Forms.CheckBox();
			this.checkBox3 = new System.Windows.Forms.CheckBox();
			this.textBox1 = new System.Windows.Forms.TextBox();
			this.label1 = new System.Windows.Forms.Label();
			this.label2 = new System.Windows.Forms.Label();
			this.textBox2 = new System.Windows.Forms.TextBox();
			this.label3 = new System.Windows.Forms.Label();
			this.textBox3 = new System.Windows.Forms.TextBox();
			this.label4 = new System.Windows.Forms.Label();
			this.textBox4 = new System.Windows.Forms.TextBox();
			this.label5 = new System.Windows.Forms.Label();
			this.textBox5 = new System.Windows.Forms.TextBox();
			this.button5 = new System.Windows.Forms.Button();
			this.button6 = new System.Windows.Forms.Button();
			this.button7 = new System.Windows.Forms.Button();
			this.checkBox4 = new System.Windows.Forms.CheckBox();
			this.checkBox5 = new System.Windows.Forms.CheckBox();
			this.mainMenu1 = new System.Windows.Forms.MainMenu(this.components);
			this.menuItem1 = new System.Windows.Forms.MenuItem();
			this.menuItem2 = new System.Windows.Forms.MenuItem();
			this.menuItem3 = new System.Windows.Forms.MenuItem();
			this.menuItem4 = new System.Windows.Forms.MenuItem();
			this.checkBox6 = new System.Windows.Forms.CheckBox();
			this.button1 = new System.Windows.Forms.Button();
			((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
			this.SuspendLayout();
			// 
			// pictureBox1
			// 
			this.pictureBox1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
						| System.Windows.Forms.AnchorStyles.Left)
						| System.Windows.Forms.AnchorStyles.Right)));
			this.pictureBox1.BackColor = System.Drawing.Color.White;
			this.pictureBox1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
			this.pictureBox1.Location = new System.Drawing.Point(84, 0);
			this.pictureBox1.Name = "pictureBox1";
			this.pictureBox1.Size = new System.Drawing.Size(540, 380);
			this.pictureBox1.TabIndex = 0;
			this.pictureBox1.TabStop = false;
			this.pictureBox1.BackColorChanged += new System.EventHandler(this.pictureBox1_BackColorChanged);
			this.pictureBox1.Resize += new System.EventHandler(this.pictureBox1_Resize);
			this.pictureBox1.MouseUp += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseUp);
			// 
			// timer1
			// 
			this.timer1.Interval = 10;
			this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
			// 
			// listView1
			// 
			this.listView1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)
						| System.Windows.Forms.AnchorStyles.Right)));
			this.listView1.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
            this.columnHeader8,
            this.columnHeader5,
            this.columnHeader6,
            this.columnHeader7,
            this.columnHeader2,
            this.columnHeader3,
            this.columnHeader4});
			this.listView1.FullRowSelect = true;
			this.listView1.HeaderStyle = System.Windows.Forms.ColumnHeaderStyle.Nonclickable;
			this.listView1.HideSelection = false;
			this.listView1.Location = new System.Drawing.Point(0, 384);
			this.listView1.Name = "listView1";
			this.listView1.Size = new System.Drawing.Size(620, 80);
			this.listView1.Sorting = System.Windows.Forms.SortOrder.Descending;
			this.listView1.TabIndex = 4;
			this.listView1.UseCompatibleStateImageBehavior = false;
			this.listView1.View = System.Windows.Forms.View.Details;
			this.listView1.KeyUp += new System.Windows.Forms.KeyEventHandler(this.listView1_KeyUp);
			// 
			// columnHeader8
			// 
			this.columnHeader8.Text = "Mass";
			// 
			// columnHeader5
			// 
			this.columnHeader5.Text = "X";
			// 
			// columnHeader6
			// 
			this.columnHeader6.Text = "Y";
			// 
			// columnHeader7
			// 
			this.columnHeader7.Text = "Z";
			// 
			// columnHeader2
			// 
			this.columnHeader2.Text = "V.X";
			this.columnHeader2.Width = 89;
			// 
			// columnHeader3
			// 
			this.columnHeader3.Text = "V.Y";
			this.columnHeader3.Width = 97;
			// 
			// columnHeader4
			// 
			this.columnHeader4.Text = "V.Z";
			this.columnHeader4.Width = 102;
			// 
			// checkBox1
			// 
			this.checkBox1.Location = new System.Drawing.Point(4, 4);
			this.checkBox1.Name = "checkBox1";
			this.checkBox1.Size = new System.Drawing.Size(72, 16);
			this.checkBox1.TabIndex = 6;
			this.checkBox1.Text = "Trace";
			this.checkBox1.CheckedChanged += new System.EventHandler(this.checkBox1_CheckedChanged);
			// 
			// checkBox2
			// 
			this.checkBox2.Location = new System.Drawing.Point(4, 44);
			this.checkBox2.Name = "checkBox2";
			this.checkBox2.Size = new System.Drawing.Size(72, 16);
			this.checkBox2.TabIndex = 7;
			this.checkBox2.Text = "Show Vel";
			this.checkBox2.CheckedChanged += new System.EventHandler(this.checkBox2_CheckedChanged);
			// 
			// checkBox3
			// 
			this.checkBox3.Location = new System.Drawing.Point(4, 84);
			this.checkBox3.Name = "checkBox3";
			this.checkBox3.Size = new System.Drawing.Size(72, 16);
			this.checkBox3.TabIndex = 8;
			this.checkBox3.Text = "Show Acc";
			this.checkBox3.CheckedChanged += new System.EventHandler(this.checkBox3_CheckedChanged);
			// 
			// textBox1
			// 
			this.textBox1.Location = new System.Drawing.Point(0, 152);
			this.textBox1.Name = "textBox1";
			this.textBox1.Size = new System.Drawing.Size(76, 20);
			this.textBox1.TabIndex = 9;
			this.textBox1.Text = "0";
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(0, 136);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(76, 16);
			this.label1.TabIndex = 10;
			this.label1.Text = "Vel X";
			// 
			// label2
			// 
			this.label2.Location = new System.Drawing.Point(0, 176);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(76, 16);
			this.label2.TabIndex = 12;
			this.label2.Text = "Vel Y";
			// 
			// textBox2
			// 
			this.textBox2.Location = new System.Drawing.Point(0, 192);
			this.textBox2.Name = "textBox2";
			this.textBox2.Size = new System.Drawing.Size(76, 20);
			this.textBox2.TabIndex = 11;
			this.textBox2.Text = "0";
			// 
			// label3
			// 
			this.label3.Location = new System.Drawing.Point(0, 256);
			this.label3.Name = "label3";
			this.label3.Size = new System.Drawing.Size(76, 16);
			this.label3.TabIndex = 14;
			this.label3.Text = "Mass";
			// 
			// textBox3
			// 
			this.textBox3.Location = new System.Drawing.Point(0, 272);
			this.textBox3.Name = "textBox3";
			this.textBox3.Size = new System.Drawing.Size(76, 20);
			this.textBox3.TabIndex = 13;
			this.textBox3.Text = "100";
			// 
			// label4
			// 
			this.label4.Location = new System.Drawing.Point(0, 296);
			this.label4.Name = "label4";
			this.label4.Size = new System.Drawing.Size(76, 16);
			this.label4.TabIndex = 16;
			this.label4.Text = "Size";
			// 
			// textBox4
			// 
			this.textBox4.Location = new System.Drawing.Point(0, 312);
			this.textBox4.Name = "textBox4";
			this.textBox4.Size = new System.Drawing.Size(76, 20);
			this.textBox4.TabIndex = 15;
			this.textBox4.Text = "2";
			// 
			// label5
			// 
			this.label5.Location = new System.Drawing.Point(0, 216);
			this.label5.Name = "label5";
			this.label5.Size = new System.Drawing.Size(76, 16);
			this.label5.TabIndex = 18;
			this.label5.Text = "Vel Z";
			// 
			// textBox5
			// 
			this.textBox5.Location = new System.Drawing.Point(0, 232);
			this.textBox5.Name = "textBox5";
			this.textBox5.Size = new System.Drawing.Size(76, 20);
			this.textBox5.TabIndex = 17;
			this.textBox5.Text = "0";
			// 
			// button5
			// 
			this.button5.Location = new System.Drawing.Point(0, 336);
			this.button5.Name = "button5";
			this.button5.Size = new System.Drawing.Size(20, 20);
			this.button5.TabIndex = 19;
			this.button5.Text = "B";
			this.button5.Click += new System.EventHandler(this.button5_Click);
			// 
			// button6
			// 
			this.button6.Location = new System.Drawing.Point(28, 336);
			this.button6.Name = "button6";
			this.button6.Size = new System.Drawing.Size(20, 20);
			this.button6.TabIndex = 20;
			this.button6.Text = "M";
			this.button6.Click += new System.EventHandler(this.button6_Click);
			// 
			// button7
			// 
			this.button7.Location = new System.Drawing.Point(56, 336);
			this.button7.Name = "button7";
			this.button7.Size = new System.Drawing.Size(20, 20);
			this.button7.TabIndex = 21;
			this.button7.Text = "S";
			this.button7.Click += new System.EventHandler(this.button7_Click);
			// 
			// checkBox4
			// 
			this.checkBox4.Checked = true;
			this.checkBox4.CheckState = System.Windows.Forms.CheckState.Checked;
			this.checkBox4.Location = new System.Drawing.Point(4, 104);
			this.checkBox4.Name = "checkBox4";
			this.checkBox4.Size = new System.Drawing.Size(72, 16);
			this.checkBox4.TabIndex = 23;
			this.checkBox4.Text = "Acc Box";
			this.checkBox4.CheckedChanged += new System.EventHandler(this.checkBox4_CheckedChanged);
			// 
			// checkBox5
			// 
			this.checkBox5.Checked = true;
			this.checkBox5.CheckState = System.Windows.Forms.CheckState.Checked;
			this.checkBox5.Location = new System.Drawing.Point(4, 64);
			this.checkBox5.Name = "checkBox5";
			this.checkBox5.Size = new System.Drawing.Size(72, 16);
			this.checkBox5.TabIndex = 22;
			this.checkBox5.Text = "Vel Box";
			this.checkBox5.CheckedChanged += new System.EventHandler(this.checkBox5_CheckedChanged);
			// 
			// mainMenu1
			// 
			this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
            this.menuItem1});
			// 
			// menuItem1
			// 
			this.menuItem1.Index = 0;
			this.menuItem1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
            this.menuItem2,
            this.menuItem3,
            this.menuItem4});
			this.menuItem1.Text = "Simulation";
			// 
			// menuItem2
			// 
			this.menuItem2.Index = 0;
			this.menuItem2.Shortcut = System.Windows.Forms.Shortcut.CtrlE;
			this.menuItem2.Text = "Start";
			this.menuItem2.Click += new System.EventHandler(this.button3_Click);
			// 
			// menuItem3
			// 
			this.menuItem3.Index = 1;
			this.menuItem3.Shortcut = System.Windows.Forms.Shortcut.F2;
			this.menuItem3.Text = "Stop";
			this.menuItem3.Click += new System.EventHandler(this.button2_Click);
			// 
			// menuItem4
			// 
			this.menuItem4.Index = 2;
			this.menuItem4.Shortcut = System.Windows.Forms.Shortcut.F5;
			this.menuItem4.Text = "Pause";
			this.menuItem4.Click += new System.EventHandler(this.button4_Click);
			// 
			// checkBox6
			// 
			this.checkBox6.Checked = true;
			this.checkBox6.CheckState = System.Windows.Forms.CheckState.Checked;
			this.checkBox6.Location = new System.Drawing.Point(4, 24);
			this.checkBox6.Name = "checkBox6";
			this.checkBox6.Size = new System.Drawing.Size(72, 16);
			this.checkBox6.TabIndex = 24;
			this.checkBox6.Text = "Collisions";
			this.checkBox6.CheckedChanged += new System.EventHandler(this.checkBox6_CheckedChanged);
			// 
			// button1
			// 
			this.button1.Location = new System.Drawing.Point(0, 360);
			this.button1.Name = "button1";
			this.button1.Size = new System.Drawing.Size(76, 20);
			this.button1.TabIndex = 25;
			this.button1.Text = "Background";
			this.button1.Click += new System.EventHandler(this.button1_Click_1);
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(620, 465);
			this.Controls.Add(this.button1);
			this.Controls.Add(this.checkBox6);
			this.Controls.Add(this.checkBox4);
			this.Controls.Add(this.checkBox5);
			this.Controls.Add(this.button7);
			this.Controls.Add(this.button6);
			this.Controls.Add(this.button5);
			this.Controls.Add(this.label5);
			this.Controls.Add(this.textBox5);
			this.Controls.Add(this.label4);
			this.Controls.Add(this.textBox4);
			this.Controls.Add(this.label3);
			this.Controls.Add(this.textBox3);
			this.Controls.Add(this.label2);
			this.Controls.Add(this.textBox2);
			this.Controls.Add(this.label1);
			this.Controls.Add(this.textBox1);
			this.Controls.Add(this.checkBox3);
			this.Controls.Add(this.checkBox2);
			this.Controls.Add(this.checkBox1);
			this.Controls.Add(this.listView1);
			this.Controls.Add(this.pictureBox1);
			this.Menu = this.mainMenu1;
			this.Name = "Form1";
			this.Text = "Gravity Simulator";
			((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
			this.ResumeLayout(false);
			this.PerformLayout();

		}
		#endregion

		[STAThread]
		static void Main() {
			Application.Run(new Form1());
		}

		private ParticleSystem ps;

		private void button2_Click(object sender, System.EventArgs e) {
			ps.Stop();
			timer1.Enabled = false;
			menuItem3.Enabled = false;
			menuItem4.Enabled = false;
			menuItem4.Text = "Pause";
		}

		private void pictureBox1_Resize(object sender, EventArgs e) {
			blank = new Bitmap(pictureBox1.Width, pictureBox1.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
			using (Graphics g = Graphics.FromImage(blank)) {
				g.Clear(Color.FromArgb(20, 255, 255, 255));
			}
		}

		private int ticks;
		private Bitmap blank;
		private void timer1_Tick(object sender, System.EventArgs e) {
			menuItem3.Enabled = true;
			menuItem4.Enabled = true;
			if (!ps.IsRunning) {
				timer1.Enabled = false;
				menuItem3.Enabled = false;
				MessageBox.Show(this, "Done");
			}

			using (Bitmap bmp = new Bitmap(pictureBox1.Width, pictureBox1.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb)) {
				//if (blank == null) {
				//    blank = new Bitmap(pictureBox1.Width, pictureBox1.Height);
				//    using (Graphics g = Graphics.FromImage(blank)) {
				//        g.Clear(Color.FromArgb(20, 255, 255, 255));
				//    }
				//}

				using (Graphics g = Graphics.FromImage(bmp)) {
					ps.Draw(g, pictureBox1.ClientRectangle);
				}
				using (Graphics g = pictureBox1.CreateGraphics()) {
					//if (checkBox1.Checked) {
					//    //System.Drawing.Imaging.ImageAttributes middleImageAttributes = new System.Drawing.Imaging.ImageAttributes();
					//    //System.Drawing.Imaging.ColorMatrix middleColorMatrix = new System.Drawing.Imaging.ColorMatrix();
					//    //middleColorMatrix.Matrix33 = 0.1f;
					//    //middleImageAttributes.SetColorMatrix(middleColorMatrix, System.Drawing.Imaging.ColorMatrixFlag.Default, System.Drawing.Imaging.ColorAdjustType.Bitmap);
					//    //g.DrawImage(blank, pictureBox1.ClientRectangle, 0, 0, pictureBox1.Width, pictureBox1.Height, GraphicsUnit.Pixel, middleImageAttributes);
					//    ////g.FillRectangle(fadeBrush, pictureBox1.ClientRectangle);
					//    g.DrawImage(blank, 0, 0);
					//}
					g.DrawImage(bmp, 0, 0);
				}
			}

			//Graphics g = pictureBox1.CreateGraphics();
			//ps.Draw(g, pictureBox1.Bounds);

			//if (ps.Particles.Count < 50) {
			//    ticks++;
			//    if (ticks > 50) {
			//        listView1.Items.Clear();
			//        foreach (Particle p in ps.Particles) {
			//            ListViewItem lvi = new ListViewItem(p.Mass.ToString());
			//            lvi.ForeColor = p.Color;
			//            lvi.SubItems.Add(p.Location.X.ToString());
			//            lvi.SubItems.Add(p.Location.Y.ToString());
			//            lvi.SubItems.Add(p.Location.Z.ToString());
			//            lvi.SubItems.Add(p.Velocity.X.ToString());
			//            lvi.SubItems.Add(p.Velocity.Y.ToString());
			//            lvi.SubItems.Add(p.Velocity.Z.ToString());
			//            lvi.Tag = p;
			//            listView1.Items.Add(lvi);
			//        }
			//        ticks = 0;
			//    }
			//} else {
			//    if (listView1.Items.Count > 0) { listView1.Items.Clear(); }
			//}
		}

		private void button1_Click(object sender, System.EventArgs e) {
			ps = new SingleParticle(new Particle(new Vector(2, 40, 0), new Vector(0.4f, 0, 0), 2, 2, Color.Black));
			Graphics g = pictureBox1.CreateGraphics();
			ps.Start(g, pictureBox1.Bounds);
			timer1.Enabled = true;
		}

		private void button3_Click(object sender, System.EventArgs e) {
			ps = new GravityParticles();
			ps.BackgroundColor = pictureBox1.BackColor;
			Graphics g = pictureBox1.CreateGraphics();
			ps.Start(g, pictureBox1.Bounds);
			timer1.Enabled = true;
			ps.Trace = checkBox1.Checked;
			ps.ShowVelocityVectors = checkBox2.Checked;
			ps.ShowAccelerationVectors = checkBox3.Checked;
			ps.ShowVelocityBox = checkBox5.Checked;
			ps.ShowAccelerationBox = checkBox4.Checked;
			ps.DetectCollisions = checkBox6.Checked;
		}

		private void button4_Click(object sender, System.EventArgs e) {
			ps.IsRunning = !ps.IsRunning;
			timer1.Enabled = ps.IsRunning;
			menuItem4.Text = ps.IsRunning ? "Pause" : "Resume";
			if (!ps.IsRunning) {
				listView1.Items.Clear();
				foreach (Particle p in ps.Particles) {
					ListViewItem lvi = new ListViewItem(p.Mass.ToString());
					lvi.ForeColor = p.Color;
					lvi.SubItems.Add(p.Location.X.ToString());
					lvi.SubItems.Add(p.Location.Y.ToString());
					lvi.SubItems.Add(p.Location.Z.ToString());
					lvi.SubItems.Add(p.Velocity.X.ToString());
					lvi.SubItems.Add(p.Velocity.Y.ToString());
					lvi.SubItems.Add(p.Velocity.Z.ToString());
					lvi.Tag = p;
					listView1.Items.Add(lvi);
				}
			} else {
				if (listView1.Items.Count > 0) { listView1.Items.Clear(); }
			}
		}

		private void pictureBox1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e) {
			if (ps == null) { return; }
			Point pt = new Point(e.X, e.Y);
			float vx = 0; try { vx = float.Parse(textBox1.Text); } catch {}
			float vy = 0; try { vy = float.Parse(textBox2.Text); } catch {}
			float vz = 0; try { vz = float.Parse(textBox5.Text); } catch {}
			float m = 0; try { m = float.Parse(textBox3.Text); } catch {}
			float s = 0; try { s = float.Parse(textBox4.Text); } catch {}
			ps.Particles.Add(new Particle(new Vector(pt.X, pt.Y, 0), new Vector(vx, vy, vz), m, s, NewColor()));
		}
		private Color NewColor() {
			Random r = new Random(Environment.TickCount);
			return Color.FromArgb(r.Next(0, 255), r.Next(0, 255), r.Next(0, 255));
		}

		private void checkBox1_CheckedChanged(object sender, System.EventArgs e) {
			if (ps == null) { return; }
			ps.Trace = checkBox1.Checked;
		}

		private void checkBox2_CheckedChanged(object sender, System.EventArgs e) {
			if (ps == null) { return; }
			ps.ShowVelocityVectors = checkBox2.Checked;
		}

		private void checkBox5_CheckedChanged(object sender, System.EventArgs e) {
			if (ps == null) { return; }
			ps.ShowVelocityBox = checkBox5.Checked;
		}

		private void checkBox3_CheckedChanged(object sender, System.EventArgs e) {
			if (ps == null) { return; }
			ps.ShowAccelerationVectors = checkBox3.Checked;
		}

		private void checkBox4_CheckedChanged(object sender, System.EventArgs e) {
			if (ps == null) { return; }
			ps.ShowAccelerationBox = checkBox4.Checked;
		}

		private void checkBox6_CheckedChanged(object sender, System.EventArgs e) {
			if (ps == null) { return; }
			ps.DetectCollisions = checkBox6.Checked;
		}

		private void button5_Click(object sender, System.EventArgs e) {
			textBox3.Text = "100000000"; //mass 10000000
			textBox4.Text = "8"; //size
			textBox1.Text = "0"; //vx
			textBox2.Text = "0"; //vy
			textBox5.Text = "0"; //vz
		}

		private void button6_Click(object sender, System.EventArgs e) {
			textBox3.Text = "1000"; //mass
			textBox4.Text = "6"; //size
			textBox1.Text = "2"; //vx 7
			textBox2.Text = "0"; //vy
			textBox5.Text = "0"; //vz
		}

		private void button7_Click(object sender, System.EventArgs e) {
			textBox3.Text = "100"; //mass
			textBox4.Text = "4"; //size
			textBox1.Text = "2"; //vx 9
			textBox2.Text = "0"; //vy
			textBox5.Text = "0"; //vz
		}

		private void listView1_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e) {
			if (ps == null) { return; }
			ArrayList pp = new ArrayList();
			ArrayList ls = new ArrayList();
			if (e.KeyCode == Keys.Delete) {
				foreach (ListViewItem lvi in listView1.SelectedItems) {
					Particle p = lvi.Tag as Particle;
					if (p != null) { pp.Add(p); ls.Add(lvi); }
				}
			}
			foreach (Particle p in pp) {
				ps.Particles.Remove(p);
			}
			foreach (ListViewItem lvi in ls) {
				listView1.Items.Remove(lvi);
			}
		}

		private void pictureBox1_BackColorChanged(object sender, System.EventArgs e) {
			if (ps != null) {
				ps.BackgroundColor = pictureBox1.BackColor;
			}
		}

		private void button1_Click_1(object sender, System.EventArgs e) {
			ColorDialog cd = new ColorDialog();
			if (cd.ShowDialog(this) == DialogResult.OK) {
				if (ps != null) {
					ps.BackgroundColor = cd.Color;
				}
				pictureBox1.BackColor = cd.Color;
				fadeBrush = new SolidBrush(Color.FromArgb(10, pictureBox1.BackColor.R, pictureBox1.BackColor.G, pictureBox1.BackColor.B));
			}
		}
	}
}

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)

Share

About the Author

BoneSoft
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.
Group type: Organisation (No members)



| Advertise | Privacy | Mobile
Web03 | 2.8.140916.1 | Last Updated 28 Jul 2008
Article Copyright 2007 by BoneSoft
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid