Example #1
0
        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
            case Keys.R:
                start();
                break;

            case Keys.F:
                if (WindowState == FormWindowState.Maximized)
                {
                    WindowState = FormWindowState.Normal;
                }
                else
                {
                    WindowState = FormWindowState.Maximized;
                }
                break;

            case Keys.V:
                showVector = !showVector;
                break;

            case Keys.C:
                showCenter = !showCenter;
                break;

            case Keys.B:
                showFrame = !showFrame;
                break;

            case Keys.Space:
                if (newWallBuffer.Count < 3)
                {
                    break;
                }

                BallsModel.Wall wall = new BallsModel.Wall(
                    newWallBuffer.ToArray(),
                    1000,
                    FSharpOption <Guid> .None
                    );

                newWallBuffer.Clear();

                room.addWall(wall);
                break;
            }
        }
Example #2
0
 public void addWall(BallsModel.Wall wall)
 {
     BallsModel.State newState = statesBuffer.Last().addWall(wall);
     statesBuffer.Add(newState);
 }
Example #3
0
        private void start()
        {
            //BallsModel.Ball[] balls = new BallsModel.Ball[]
            //{
            //    new BallsModel.Ball(
            //        new BallsModel.Vector(20.0, 50.0),
            //        1.0,
            //        new BallsModel.Circle(5.0, 5.0, 1.0),
            //        FSharpOption<Guid>.None
            //    ),
            //    new BallsModel.Ball(
            //        new BallsModel.Vector(-20.0, 60.0),
            //        2.0,
            //        new BallsModel.Circle(5.0, 5.0, 2.0),
            //        FSharpOption<Guid>.None
            //    )
            //};

            Random rnd = new Random();

            BallsModel.Ball[] balls = new BallsModel.Ball[ballsCount].Select(_ =>
            {
                const double speed = 3;
                double dx          = rnd.NextDouble() * 5 * speed;
                double dy          = rnd.NextDouble() * 5 * speed;
                double r           = rnd.NextDouble() * 2 + 0.1;
                //double dx = 3;
                //double dy = 0.1;

                return(new BallsModel.Ball(
                           new BallsModel.Vector(dx, dy),
                           1.0,
                           new BallsModel.Circle(8.0, 5.0, r),
                           FSharpOption <Guid> .None
                           ));
            }).ToArray();

            double wallWeight = Math.Pow(10, 5);

            BallsModel.Wall[] walls = new BallsModel.Wall[] {
                //new BallsModel.Wall(
                //    new BallsModel.Point[]
                //    {
                //        new BallsModel.Point(20.0, 10.0),
                //        new BallsModel.Point(20.0, 45.0),
                //        new BallsModel.Point(25.0, 45.0),
                //        new BallsModel.Point(25.0, 10.0),
                //    },
                //    wallWeight,
                //    FSharpOption<Guid>.None
                //),
                //new BallsModel.Wall(
                //    new BallsModel.Point[]
                //    {
                //        new BallsModel.Point(20.0, 6.0),
                //        new BallsModel.Point(20.0, 7.0),
                //        new BallsModel.Point(25.0, 7.0),
                //        new BallsModel.Point(25.0, 6.0),
                //    },
                //    wallWeight,
                //    FSharpOption<Guid>.None
                //),

                // anchors
                new BallsModel.Wall(
                    new BallsModel.Point[]
                {
                    new BallsModel.Point(2.0, 0.0),
                    new BallsModel.Point(-1.0, 3.0),
                    new BallsModel.Point(-1.0, 0.0),
                },
                    wallWeight,
                    FSharpOption <Guid> .None
                    ),
                new BallsModel.Wall(
                    new BallsModel.Point[]
                {
                    new BallsModel.Point(boxWidth - 2, 0.0),
                    new BallsModel.Point(boxWidth + 1, 3.0),
                    new BallsModel.Point(boxWidth + 1, 0.0),
                },
                    wallWeight,
                    FSharpOption <Guid> .None
                    ),
                new BallsModel.Wall(
                    new BallsModel.Point[]
                {
                    new BallsModel.Point(boxWidth + 1, boxHeight - 2),
                    new BallsModel.Point(boxWidth - 2, boxHeight + 1),
                    new BallsModel.Point(boxWidth - 2, boxHeight + 1),
                },
                    wallWeight,
                    FSharpOption <Guid> .None
                    ),
                new BallsModel.Wall(
                    new BallsModel.Point[]
                {
                    new BallsModel.Point(-1.0, boxHeight - 2),
                    new BallsModel.Point(2.0, boxHeight + 1),
                    new BallsModel.Point(-1.0, boxHeight + 1),
                },
                    wallWeight,
                    FSharpOption <Guid> .None
                    ),

                // square walls
                new BallsModel.Wall(
                    new BallsModel.Point[]
                {
                    new BallsModel.Point(boxWidth - 2, 0.0),
                    //new BallsModel.Point(39.0, 3.0),
                    new BallsModel.Point(boxWidth + 1, 3.0),
                    new BallsModel.Point(boxWidth + 1, 0.0),
                },
                    wallWeight,
                    FSharpOption <Guid> .None
                    ),
                new BallsModel.Wall(
                    new BallsModel.Point[]
                {
                    new BallsModel.Point(-1.0, 0.0),
                    new BallsModel.Point(boxWidth + 1, 0.0),
                    new BallsModel.Point(boxWidth + 1, 1.0),
                    new BallsModel.Point(-1.0, 1.0),
                },
                    wallWeight,
                    FSharpOption <Guid> .None
                    ),
                new BallsModel.Wall(
                    new BallsModel.Point[]
                {
                    new BallsModel.Point(boxWidth, 0.0),
                    new BallsModel.Point(boxWidth, boxHeight + 1),
                    new BallsModel.Point(boxWidth + 1, boxHeight + 1),
                    new BallsModel.Point(boxWidth + 1, 0.0),
                },
                    wallWeight,
                    FSharpOption <Guid> .None
                    ),
                new BallsModel.Wall(
                    new BallsModel.Point[]
                {
                    new BallsModel.Point(boxWidth + 1, boxHeight),
                    new BallsModel.Point(-1.0, boxHeight),
                    new BallsModel.Point(-1.0, boxHeight + 1),
                    new BallsModel.Point(boxWidth + 1, boxHeight + 1),
                },
                    wallWeight,
                    FSharpOption <Guid> .None
                    ),
                new BallsModel.Wall(
                    new BallsModel.Point[]
                {
                    new BallsModel.Point(0.0, 0.0),
                    new BallsModel.Point(0.0, boxHeight + 1),
                    new BallsModel.Point(-1.0, boxHeight + 1),
                    new BallsModel.Point(-1.0, 0.0),
                },
                    wallWeight,
                    FSharpOption <Guid> .None
                    ),

                // other walls
                new BallsModel.Wall(
                    new BallsModel.Point[]
                {
                    new BallsModel.Point(20.0, 20.0),
                    new BallsModel.Point(30.0, 15.0),
                    new BallsModel.Point(35.0, 30.0),
                    new BallsModel.Point(25.0, 33.0),
                },
                    wallWeight,
                    FSharpOption <Guid> .None
                    ),
            };

            BallsModel.State initialState =
                new BallsModel.State(
                    balls,
                    walls,
                    (double)stopWatch.ElapsedMilliseconds / 1000,
                    FSharpOption <
                        Microsoft.FSharp.Collections.FSharpMap <
                            Guid,
                            BallsModel.Collision
                            >
                        > .None
                    );
            room = new Room(initialState);

            renderTimer.Interval = 4;
            renderTimer.Tick    += (s, a) =>
            {
                Invalidate();
            };

            textStateTimer.Interval = 100;
            textStateTimer.Tick    += (s, a) =>
            {
                printState();
            };

            textStateTimer.Start();
            renderTimer.Start();
            stopWatch.Start();

            lastRealTime = (double)stopWatch.ElapsedMilliseconds / 1000;
            lastSimTime  = lastRealTime;
        }