+ Reply to Thread
Results 1 to 2 of 2

Thread: Gravity Simulation using C#

  1. #1
    Junior Member
    Join Date
    May 2012
    Posts
    1

    Gravity Simulation using C#

    Hey guys , i want to mix the physics and math with programming today.
    The Main aim is to simulate the real life using programming.

    Isn't a nice topic ?

    Say i want to simulate the gravity in programming , can anyone do that ?
    Last edited by marvin; 11-02-2012 at 12:26 PM.

  2. #2
    Junior Member
    Join Date
    May 2012
    Posts
    10
    I`ll create a small game called : Gravity Ball , the aim here is to raise a ball in the air and then leave it and it should go down
    because of gravity.
    Will we use any graphic library ?
    No , it will be a normal coding.

    Steps :
    -------
    1- creating a windows form application
    2- downloading any ball picture from the internet
    3- inserting a picturebox into form
    4- picturebox`s background image will be the ball you has just downloaded
    5- clicking on the ball and raise it up
    6- leave the mouse and it will go down to the button of the form [i.e : floor]

    Variables :
    -----------
    Code:
            // that to define if the mouse button is being clicked or not , at the moment it will be false
            bool mouse_down = false;
    Functions :
    -----------
    While holding the ball with mouse we should get the mouse coordinates while it moves inside the form to be able to move the ball in
    different locations depending on the mouse position.
    Code:
            private void Get_M_Pos()
            {
                int mouse_x = 0;
                int mouse_y = 0;
                
                // while mouse_down = true , that means the mouse button is being clicked and not yet released
                while (mouse_down)
                {
                    mouse_x = Cursor.Position.X;
                    mouse_y = Cursor.Position.Y;
                    
                    // moving the ball according to the mouse position
                    pictureBox1.Location = new Point(mouse_x, mouse_y);
                }
                // once the mouse button is released the gravity function will be started
                Thread t = new Thread(new ThreadStart(gravity));
                t.Start();
            }
    The Gravity function that is supposed to send the ball to earth.
    Code:
            private void gravity()
            {
                // 1- get the Y location of the ball
                // 2- Y should be less than [form height - the height of ball] that`s for the ball to be exactly on the floor
                // 3- then loop and raise the y value till you reach the value of [form height - the height of ball] to send the ball down to floor
                for (int y = pictureBox1.Location.Y; y < (this.Height - pictureBox1.Height); y += 2)
                {
                    pictureBox1.Location = new Point(pictureBox1.Location.X, y);
                }
            }
    Inside Form1_Load ball should be already on the floor and it is also good to disable the Checking of CrossThreadCalls because it will annoy you
    during debugging
    Code:
            private void Form1_Load(object sender, EventArgs e)
            {
                CheckForIllegalCrossThreadCalls = false;
    
                pictureBox1.Location = new Point(pictureBox1.Location.X, this.Height - pictureBox1.Height);
            }
    Thee last step is to add these two events :
    Code:
            private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
            {
                mouse_down = true;
    
                Thread t = new Thread(new ThreadStart(Get_M_Pos));
                t.Start();
            }
    
            private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
            {
                mouse_down = false;
            }
    The overall Code :
    ------------------
    Code:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.Threading;
    
    namespace Ball_Gravity_Game
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            // to avoid flickering while form loading
            protected override CreateParams CreateParams
            {
                get
                {
                    CreateParams cp = base.CreateParams;
                    cp.ExStyle |= 0x02000000;
                    return cp;
                }
            }// end the avoid flicker method
    
            // that to define if the mouse button is being clicked or not , at the moment it will be false
            bool mouse_down = false;
    
            private void Get_M_Pos()
            {
                int mouse_x = 0;
                int mouse_y = 0;
                
                // while mouse_down = true , that means the mouse button is being clicked and not yet released
                while (mouse_down)
                {
                    mouse_x = Cursor.Position.X;
                    mouse_y = Cursor.Position.Y;
                    
                    // moving the ball according to the mouse position
                    pictureBox1.Location = new Point(mouse_x, mouse_y);
                }
                // once the mouse button is released the gravity function will be started
                Thread t = new Thread(new ThreadStart(gravity));
                t.Start();
            }
    
            private void gravity()
            {
                // 1- get the Y location of the ball
                // 2- Y should be less than [form height - the height of ball] that`s for the ball to be exactly on the floor
                // 3- then loop and raise the y value till you reach the value of [form height - the height of ball] to send the ball down to floor
                for (int y = pictureBox1.Location.Y; y < (this.Height - pictureBox1.Height); y += 2)
                {
                    pictureBox1.Location = new Point(pictureBox1.Location.X, y);
                    //Thread.Sleep(1);
                }
            }
    
            private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
            {
                mouse_down = true;
    
                Thread t = new Thread(new ThreadStart(Get_M_Pos));
                t.Start();
            }
    
            private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
            {
                mouse_down = false;
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                CheckForIllegalCrossThreadCalls = false;
    
                pictureBox1.Location = new Point(pictureBox1.Location.X, this.Height - pictureBox1.Height);
            }
        }
    }
    Finally i should mention that if you love math and physics i`d really advice you to start considering Graphic programming as it really fun.

+ Reply to Thread

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts