private void OpenGLControl_OpenGLDraw(object sender, SharpGL.SceneGraph.OpenGLEventArgs args)
        {
            OpenGL gl = args.OpenGL;

            /// Clear The Screen And The Depth Buffer
            gl.Clear(OpenGL.GL_COLOR_BUFFER_BIT | OpenGL.GL_DEPTH_BUFFER_BIT);

            /// Move Left And Into The Screen
            gl.LoadIdentity();
            gl.Translate(0.0f, 0.0f, -100.0f);

            frames++;
            if (count < 90)
            {
                float    x    = (float)Gaussian.Generate(-10, 10);
                float    y    = (float)yRand.GenerateDouble();
                CubeMesh star = new CubeMesh(x, y, 0);
                star.Mass  = (float)Gaussian.Generate(.2, .5);
                star.Scale = new Vector3(star.Mass, star.Mass, star.Mass);
                stars.Add(star);
                count++;
            }

            foreach (var star in stars)
            {
                Attractor pull = new Attractor();
                pull.Mass = star.Mass;
                foreach (var other in stars)
                {
                    if (star != other)
                    {
                        Attractor otherPull = new Attractor();
                        otherPull.Mass = other.Mass;
                        other.ApplyForce(pull.CalculateAttraction(other));
                        star.ApplyForce(otherPull.CalculateAttraction(star));
                    }
                }
                star.Draw(gl);
                gl.Color(colorRand.GenerateDouble(), colorRand.GenerateDouble(), colorRand.GenerateDouble());
            }

            if (frames == 300)
            {
                frames = 0;
                stars.Clear();
                count = 0;
            }
        }
Exemple #2
0
        private void OpenGLControl_OpenGLDraw(object sender, SharpGL.SceneGraph.OpenGLEventArgs args)
        {
            Title = "basketball-game";

            OpenGL gl = args.OpenGL;

            gl.Clear(OpenGL.GL_COLOR_BUFFER_BIT | OpenGL.GL_DEPTH_BUFFER_BIT);

            RenderColor(gl);
            Render3D(gl);

            gl.LoadIdentity();
            //Manipulate Camera
            gl.LookAt(0 - mouseVector.x - movementVector.x, 0 + mouseVector.y - movementVector.y, 20.0f + zoom, 0 - movementVector.x, 0 + mouseVector.y, zoom, 0, 1, 0);
            //Then Translate
            gl.Translate(0.0f, 10.0f, -50.0f);

            //Draw 3D Objects
            #region Draw 3D Objects
            Ball.DrawBasketBall(gl, 60, 0, 0, 0);
            Ground.DrawCube(gl, 0, 120, 0);
            Pole.DrawCube(gl, 100, 100, 100);
            Board.DrawCube(gl, 0, 200, 200);
            RingEdge.DrawCube(gl, 0, 200, 200);

            RimNet.Draw(gl, 50, 50, 50);
            CenNet.Draw(gl, 40, 40, 40);

            //simulatedBall.DrawCircle(gl, 1);
            //Aiming Line Draw
            if (showLine)
            {
                if (isBallThrown)
                {
                    //Line.DrawLine(gl, Ball, Ball.Velocity, 1, 200, 60, 60);
                    Line.DrawSimulatedPath(gl, Ball, Ball.Velocity, 1, 200, 60, 60);
                }
                else
                {
                    //Line.DrawLine(gl, Ball, modifierVector, 1, 200, 60, 60);
                    Line2.DrawSimulatedPath(gl, simulatedBall, simulatedVector, 1, 200, 0, 0);
                }
            }
            //End Drawing Most 3D Objects
            #endregion

            #region Draw Text


            var angle = (Math.Truncate((Math.Atan(modifierVector.y / modifierVector.x) * 180 / Math.PI) * 100.0) / 100.0);
            //Above
            gl.DrawText(5, 630, 1, 0, 0, "Arial", 15, "Round: " + curRound);
            gl.DrawText(5, 600, 1, 0, 0, "Arial", 30, "Score: " + curScore);
            gl.DrawText(5, 570, 1, 0, 0, "Arial", 20, "Camera Movement Enabled: " + Keyboard.IsKeyToggled(toggleMovementKey).ToString());
            gl.DrawText(5, 480, 1, 0, 0, "Arial", 30, gameStage);

            //Below
            gl.DrawText(230, 20, 1, 0, 0, "Arial", 15, "Angle: " + angle);
            gl.DrawText(400, 5, 1, 0, 0, "Arial", 15, "Increments: " + increments);
            gl.DrawText(400, 20, 1, 0, 0, "Arial", 15, "Power: " + power);
            gl.DrawText(900, 20, 1, 0, 0, "Arial", 15, "Reset in: " + curCounter);


            #endregion


            showBall.Rotation += 5;
            #region WINDY Score when > 10
            //WINDY!!!!!!!!!!!!!! --- SCORE > 10
            if (curScore >= 10)
            {
                isWindy   = true;
                gameStage = "It's a bit windy";
                ObjectMesh Leave = new ObjectMesh()
                {
                    Scale = new Vector3(0.5f, 0.5f, 0),
                    Mass  = 0.3f
                };
                leaveRandGaussian = (float)Randomizer.Gaussian(-0, 10);
                leaveRandNorm     = (float)Randomizer.Generate(0.1f, 0.5f);
                Leave.Position    = new Vector3(leaveRandGaussian + 150, leaveRandGaussian, 30);
                Leave.Mass        = leaveRandNorm;
                Leaves.Add(Leave);
                foreach (var leaf in Leaves)
                {
                    leaf.DrawCube(gl, 0, 60, 0);
                    leaf.ApplyGravity();
                    leaf.ApplyForce(rightWindVector);
                    leaf.Velocity.Clamp(-0.2f, -0.2f, 0);
                    if (leaf.HasCollidedWith(Ground))
                    {
                        leaf.Position.y = Ground.Position.y + Ground.Scale.y + leaf.Scale.y;
                        leaf.Velocity.y = (-(leaf.Velocity.y));
                        leaf.ApplyFriction();
                    }
                    else
                    {
                        leaf.ApplyGravity();
                        leaf.ApplyFriction();
                    }
                }
            }
            #endregion

            #region Meteor Fell Score When > 20
            if ((curScore >= 20) && (enabledMetoer))
            {
                gameStage = "Some kind of meteor...";
                Meteor.DrawCircle(gl);
                Meteor.ApplyForce(meteorFallingVector);
                if (Meteor.Position.y <= -30)
                {
                    Meteor.Velocity.y *= 0;
                    Meteor.Position.y  = -30;
                    isMeteorFall       = true;
                }
            }
            #endregion

            #region Ball Thrown Code
            //You pressed Space
            if (isBallThrown)
            {
                curCounter--;
                if (curCounter > maxCounter - 2)
                {
                    Ball.Velocity = modifierVector / 1.5f;
                }
                Ball.Rotation += 10;
            }
            #endregion

            #region Scoring
            if (CenNet.Contains(Ball))
            {
                Console.WriteLine("Ball is in CenNet...");
                if (!isScored)
                {
                    curScore++;
                    randNorm     = (float)Randomizer.Generate(-10, 10);
                    randGaussian = (float)Randomizer.Gaussian(1, 2);
                    Console.WriteLine("Player Scores!");
                    isScored = true;
                }

                Ball.ApplyForce(RimNet.CalculateDragForce(Ball) * 1);
                Ball.ApplyForce(CenNet.CalculateDragForce(Ball) * 2);
            }
            #endregion

            #region Reset Code
            if (curCounter < 0)
            {
                Pole.Position.x     += randNorm;
                Board.Position.x    += randNorm;
                RingEdge.Position.x += randNorm;
                RimNet.x            += randNorm;
                CenNet.x            += randNorm;

                if ((randNorm < 1) && (randNorm > -1))
                {
                    Board.Position.y    += randGaussian;
                    RimNet.y            += randGaussian;
                    CenNet.y            += randGaussian;
                    RingEdge.Position.y += randGaussian;
                }

                if ((Pole.Position.x > 70) || (Pole.Position.x < 30))
                {
                    Pole.Position.x     = 50;
                    Board.Position.x    = 48.5f;
                    RingEdge.Position.x = 42.5f;
                    RimNet.x            = 45.5f;
                    CenNet.x            = 45.5f;
                }
                curCounter = maxCounter;
                isScored   = false;
                curRound++;
            }

            if (curCounter == maxCounter)
            {
                //isSpaceHeld = false;
                isBallThrown     = false;
                Ball.Velocity.x *= 0.0f;
                Ball.Velocity.y *= 0.0f;
                Ball.Position    = BallDefaultPos;
                randNorm        *= 0;
                randGaussian    *= 0;
                isScored         = false;
            }
            #endregion

            #region Controls
            // These controls only available when Ball is not yet thrown
            if (!isBallThrown)
            {
                //Increment Adjuster
                if (Keyboard.IsKeyDown(increaseKey) && !Keyboard.IsKeyToggled(toggleMovementKey))
                {
                    increments -= 0.1f;
                }
                else if (Keyboard.IsKeyDown(decreaseKey) && !Keyboard.IsKeyToggled(toggleMovementKey))
                {
                    increments += 0.2f;
                }
                increments = GameUtils.Constrain(increments, minIncrements, maxIncrements);

                //Adjust Power and Angle modified by Increments
                lineLength = modifierVector.GetLength();
                power      = (Math.Truncate(lineLength * 100.0) / 100.0);
                power     *= 10;


                if (Keyboard.IsKeyDown(upKey) && (power < 100) && !Keyboard.IsKeyToggled(toggleMovementKey))
                {
                    aimAngle          = Math.Atan((modifierVector.y) / (modifierVector.x));
                    modifierVector.x += increments * (float)Math.Cos(aimAngle);
                    modifierVector.y += increments * (float)Math.Sin(aimAngle);
                }
                if (Keyboard.IsKeyDown(downKey) && (power > 10) && !Keyboard.IsKeyToggled(toggleMovementKey))
                {
                    aimAngle          = Math.Atan((modifierVector.y) / (modifierVector.x));
                    modifierVector.x -= increments * (float)Math.Cos(aimAngle);
                    modifierVector.y -= increments * (float)Math.Sin(aimAngle);
                }
                if (Keyboard.IsKeyDown(rightKey) && !Keyboard.IsKeyToggled(toggleMovementKey))
                {
                    aimAngle         = Math.Atan(modifierVector.y / modifierVector.x);
                    aimAngle        -= (Math.PI / 180) + ((Math.PI / 90) * increments);
                    modifierVector.x = lineLength * (float)Math.Cos(aimAngle);
                    modifierVector.y = lineLength * (float)Math.Sin(aimAngle);
                }
                if (Keyboard.IsKeyDown(leftKey) && !Keyboard.IsKeyToggled(toggleMovementKey))
                {
                    aimAngle         = Math.Atan(modifierVector.y / modifierVector.x);
                    aimAngle        += (Math.PI / 180) + ((Math.PI / 90) * increments);
                    modifierVector.x = lineLength * (float)Math.Cos(aimAngle);
                    modifierVector.y = lineLength * (float)Math.Sin(aimAngle);
                }
            }

            //Play Ball
            if (Keyboard.IsKeyDown(shootKey))
            {
                isBallThrown = true;
            }

            //Restart
            if (Keyboard.IsKeyDown(resetKey) && (curCounter < maxCounter))
            {
                curCounter = -2;
            }

            if (Keyboard.IsKeyToggled(toggleLineKey))
            {
                showLine            = false;
                isDrawSimulatedPath = false;
            }
            else
            {
                showLine            = true;
                isDrawSimulatedPath = true;
            }

            //Force Random
            if (Keyboard.IsKeyDown(randomizeKey))
            {
                randNorm     = (float)Randomizer.Generate(-10, 10);
                randGaussian = (float)Randomizer.Gaussian(1, 2);
                curCounter   = -2;
            }

            //Add Score Cheat
            if (Keyboard.IsKeyDown(cheatKey))
            {
                randNorm     = (float)Randomizer.Generate(-10, 10);
                randGaussian = (float)Randomizer.Gaussian(1, 2);
                curCounter   = -2;
                curScore++;
            }

            if (Keyboard.IsKeyToggled(toggleMovementKey))
            {
                mouseVector = new Vector3(mousePos.x / (50 * (1 / sensitivity)), mousePos.y / (30 * (1 / sensitivity)), 0);
                eyex        = 0 - mouseVector.x + movementVector.x;
                eyey        = 0 + mouseVector.y - movementVector.y;
                eyez       += zoom;
                cenx        = 0 - movementVector.x;
                ceny        = 0 + mouseVector.y;
                cenz        = zoom;

                if (Keyboard.IsKeyDown(increaseKey))
                {
                    zoom += 1;
                }
                if (Keyboard.IsKeyDown(decreaseKey))
                {
                    zoom -= 1;
                }
                if (Keyboard.IsKeyDown(upKey))
                {
                    //movementVector.y -= 1;
                    zoom -= 1;
                }
                if (Keyboard.IsKeyDown(downKey))
                {
                    //movementVector.y -= -1;
                    zoom += 1;
                }
                if (Keyboard.IsKeyDown(leftKey))
                {
                    movementVector.x -= -1;
                }
                if (Keyboard.IsKeyDown(rightKey))
                {
                    movementVector.x -= 1;
                }
            }

            if (Keyboard.IsKeyDown(quitKey))
            {
                Environment.Exit(0);
            }
            #endregion

            #region Physics and Collision
            //Simulated Ball


            //Main Ball
            if (Ball.HasCollidedWith(Ground))
            {
                Ball.Velocity.y = (-(Ball.Velocity.y) / 2);
                Ball.Position.y = Ground.Position.y + Ground.Scale.y + Ball.Radius;
                Ball.ApplyFriction();

                if (isWindy)
                {
                    Ball.ApplyForce(rightWindVector);
                }
                if (isMeteorFall)
                {
                    Ball.ApplyForce(Meteor.CalculateAttraction(Ball));
                }
            }
            else
            {
                Ball.ApplyGravity(0.2f);
                Ball.ApplyFriction();
                if (isWindy == true)
                {
                    Ball.ApplyForce(rightWindVector);
                }
                if (isMeteorFall == true)
                {
                    Ball.ApplyForce(Meteor.CalculateAttraction(Ball));
                }
            }

            if (Ball.HasCollidedWith(Board))
            {
                if (Ball.Position.y > Board.Position.y + Board.Scale.y)
                {
                    Ball.Velocity.y = (-(Ball.Velocity.y) / 2);
                }
                Ball.Velocity.x = (-(Ball.Velocity.x) / 1.5f);
            }

            //Ball Collided with Meteor
            //Get distance between Ball and Metoer then check against its scale
            if (GameUtils.GetDistanceBetween(Meteor, Ball) < (Ball.Radius + Meteor.Radius) + 2.0f)
            {
                Ball.Velocity.x = (-(Ball.Velocity.x) / 1.5f);
                Ball.Velocity.y = (-(Ball.Velocity.y) / 1.5f);
            }

            if (Ball.HasCollidedWith(RingEdge))
            {
                if (Ball.Position.y > RingEdge.Position.y + RingEdge.Scale.y)
                {
                    Ball.Velocity.y = (-(Ball.Velocity.y) / 2);
                }
                Ball.Velocity.x = (-(Ball.Velocity.x) / 1.5f);
            }

            if (Ball.HasCollidedWith(Pole))
            {
                Ball.Velocity.x = (-(Ball.Velocity.x) / 1.5f);
            }
            #endregion
        }
Exemple #3
0
        private void OpenGLControl_OpenGLDraw(object sender, SharpGL.SceneGraph.OpenGLEventArgs args)
        {
            this.Title = "APLIMAT Final Exam";
            OpenGL gl = args.OpenGL;

            // Clear The Screen And The Depth Buffer
            gl.Clear(OpenGL.GL_COLOR_BUFFER_BIT | OpenGL.GL_DEPTH_BUFFER_BIT);

            // Move Left And Into The Screen
            gl.LoadIdentity();
            gl.Translate(0.0f, 0.0f, -100.0f);


            Ocean.Draw(gl);
            gl.Color(1.0f, 1.0f, 1.0f);

            //gl.Color((byte)192, (byte)192, (byte)192);
            //Rock.Mass = (float)Gaussian.Generate(randMass.Generate(), randMass.Generate());
            //Rock.Scale = new Vector3(Rock.Mass, Rock.Mass, 0);
            //Rock.ApplyForce(Vector3.arcRight * 0.01f);
            //Rock.ApplyGravity();

            gl.Color(0, 0, 0);
            blackHole.Position.x = 0;
            blackHole.Position.y = -40;
            blackHole.Mass       = 5;
            blackHole.Scale      = new Vector3(blackHole.Mass, blackHole.Mass, 0);
            blackHole.Draw(gl);


            if (myRocks.Count <= 20)
            {
                for (int x = 0; x < 20; x++)
                {
                    myRocks.Add(new CubeMesh());
                    myRocks[x].Position = new Vector3(randPos.Generate(), randPos.Generate(), 0);
                    myRocks[x].Mass     = (float)Gaussian.Generate(randMass.Generate(), randMass.Generate());
                    myRocks[x].Scale    = new Vector3(myRocks[x].Mass, myRocks[x].Mass, 0);
                }
            }

            foreach (var rock in myRocks)
            {
                gl.Color((byte)192, (byte)192, (byte)192);
                rock.ApplyGravity();
                rock.Draw(gl);
                if (Ocean.Contains(rock))
                {
                    var dragForce = Ocean.CalculateDragForce(rock);
                    rock.ApplyForce(dragForce);
                    rock.ApplyForce(blackHole.CalculateAttraction(rock));
                }
                if (rock.Position.y <= -40)
                {
                    rock.Position.y  = -40;
                    rock.Velocity.y *= -1;
                }
            }

            counter++;

            //for (int x = 0; x < 10; x++)
            //{
            //    myRocks.Add(new CubeMesh());
            //    myRocks[x].Position = new Vector3(-50 - (x * 10), 30, 0);
            //    gl.Color((byte)192, (byte)192, (byte)192);
            //    myRocks[x].Mass = (float)Gaussian.Generate(randMass.Generate(), randMass.Generate());
            //    myRocks[x].Scale = new Vector3(Rock.Mass, Rock.Mass, 0);
            //    myRocks[x].ApplyGravity();
            //    myRocks[x].Draw(gl);
            //    if (Ocean.Contains(myRocks[x]))
            //    {
            //        var dragForce = Ocean.CalculateDragForce(myRocks[x]);
            //        //myRocks[x].ApplyForce(dragForce);
            //        //myRocks[x].ApplyForce(blackHole.CalculateAttraction(myRocks[x]));
            //    }
            //    if (myRocks[x].Position.y <= -40)
            //    {
            //        myRocks[x].Position.y = -40;
            //        myRocks[x].Velocity.y *= -1;
            //    }
            //}



            //if (counter >= 20)
            //{
            //    Rock.ApplyForce(Vector3.Right * 0.01f);
            //    Rock.ApplyForce(Vector3.Up * 0.01f);
            //    Rock.ApplyGravity();
            //    Rock.Draw(gl);
            //    myRocks.Add(Rock);
            //    if (Ocean.Contains(Rock))
            //    {
            //        var dragForce = Ocean.CalculateDragForce(Rock);
            //        Rock.ApplyForce(dragForce);

            //    }
            //}

            //if(counter == 120)
            //{
            //    counter = 0;
            //    Rock.Position = new Vector3(-75.0f, -8.0f, 0);
            //}


            //gl.Color(1.0, 1.0, 1.0);
            //bullet.Draw(gl);

            //if (Keyboard.IsKeyDown(Key.Space))
            //{
            //    Rock.Draw(gl);
            //}

            //if (Keyboard.IsKeyDown(Key.D))
            //{
            //    myCube.ApplyForce(Vector3.Right * speed);
            //}

            //if (Keyboard.IsKeyDown(Key.A))
            //{
            //    myCube.ApplyForce(Vector3.Left * speed);
            //}
            //if (Keyboard.IsKeyDown(Key.S))
            //{
            //    myCube.ApplyForce(Vector3.Down * speed);
            //}

            //if (Mouse.LeftButton == MouseButtonState.Pressed)
            //{

            //}

            //myCube.ApplyGravity();

            //if (myCube.Position.y <= -50)
            //{
            //    myCube.Position.y = -50;
            //    myc
            //}
        }