c simulation of flat motion animation method

  • 2020-05-17 06:11:17
  • OfStack

The FillElliple of Graphics object is mainly used to draw 1 round ball, and then the display position can be controlled by time sharing.
The main steps are as follows:
1. Create a new form and add the following functions to the code mode to control the speed of x and y axis respectively.

        private int runTime=25;// Set the motion time of the flat toss (animation duration) 
        private double Xs(double t)
        {
            double v0 = 15;
            return v0 * t;
        }
        private double Ys(double t)
        {
            double g = 9.8;
            return 0.5 * g * t * t;
        }

2. Next, I bind the control motion method to the click event of the main form, which can be changed. The code is as follows:

        private void Form1_Click(object sender, EventArgs e)
        {
            Graphics g = this.CreateGraphics();
            double t;
            for (t = 0; t < runTime; t += .5)
            {
                System.Threading.Thread.Sleep(10);
                g.Clear(this.BackColor);
                double x = Xs(1.5 * t) + 50;
                double y = Ys(0.3 * t) + 5;
                g.FillEllipse(Brushes.Red, Convert.ToSingle(x), Convert.ToSingle(y), 30, 30);
            }
        }


Related articles: