public static bool ballDetectCollision(Ball a, Ball b)
 {
     double radius = 8;
     double distance = Vector2.Distance(new Vector2(Convert.ToSingle(a.posX), Convert.ToSingle(a.posY)), new Vector2(Convert.ToSingle(b.posX), Convert.ToSingle(b.posY)));
     if (distance < radius * 2) return true;
     return false;
 }
Beispiel #2
0
        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here
	    
	        //Starting values for ball
            Position = new Vector2[15];
            Balls = new Ball[2];
            Balls[0] = new Ball(0, 100, 100, 0, 0, 0, 0, 0, 0, 0, 0, 0, gameTick);
            Balls[1] = new Ball(1, 100, 200, 0, 0, 0, 0, 0, 0, 0, 0, 0, gameTick);

            //Array for each toggleable key
            keyDown = new bool[15];
            for (int i = 0; i <= keyDown.Length - 1; i++)
            {
                keyDown[i] = false;
            }

	        //Starting general values
            currentBall = 0;
            accelDefault = 500;
	        frictDefault = 2;
            debug = false;
            ballMoveTest = true;
	        bumperElasticity =  0.2; //Between 0 and 1 (0-100%)
	        bumperBounce = 2;

            base.Initialize();
        }
Beispiel #3
0
        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here
            Slider sliderInstance = new Slider("null", 0, 0, 0);

            resolutionX = 1280;
            resolutionY = 720;
            graphics.PreferredBackBufferWidth = (int)resolutionX;
            graphics.PreferredBackBufferHeight = (int)resolutionY;
            graphics.ApplyChanges();

	        //Starting values for ball
            Sliders = new Slider[4];
            moveSlider = new bool[4];
            Sliders[0] = new Slider("Friction", Window.ClientBounds.Width - 180, 80, 0);
            Sliders[1] = new Slider("Elasticity", Window.ClientBounds.Width - 180, 120, 34);
            Sliders[2] = new Slider("Current Ball Elast.", Window.ClientBounds.Width - 180, 160, 0);
            Sliders[3] = new Slider("Current Ball Mass", Window.ClientBounds.Width - 180, 200, 0);
            for (int i = 0; i <= Sliders.Length - 1; i++)
                Slider.SliderLabelInt(Sliders, i);
            
            Position = new Vector2[16];
            Balls = new Ball[2];
            Balls[0] = new Ball(0, 100, 240, 0.16, 1, gameTick);
            Balls[1] = new Ball(1, 400, 240, 0.16, 1, gameTick);

            //Array for each toggleable key
            keyDown = new bool[15];
            for (int i = 0; i <= keyDown.Length - 1; i++)
                keyDown[i] = false;
            for (int i = 0; i <= moveSlider.Length - 1; i++)
                moveSlider[i] = false;

            mouseDown = false;

	        //Starting general values
            currentBall = 0;
            accelDefault = 500;
	        frictDefault = 0;
            debug = false;
            music = false;
            sliderActive = false;
            ballMoveTest = true;
            pause = false;
            console = true;
            currentCollision = false;
            firstPlay = false;
	        bumperElasticity = 0; //Between 0 and 1 (0-100%)
	        bounceBuffer = 2;

            Songs = new Song[5];

            base.Initialize();
        }
Beispiel #4
0
        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here
            graphics.PreferredBackBufferWidth = 1280;
            graphics.PreferredBackBufferHeight = 720;
            graphics.ApplyChanges();

	        //Starting values for ball
            Position = new Vector2[15];
            Balls = new Ball[15];
            Balls[0] = new Ball(0, 100, 100, 0.16, 1, gameTick);
            Balls[1] = new Ball(1, 100, 200, 0.16, 1, gameTick);
            Balls[2] = new Ball(2, 100, 300, 0.16, 1, gameTick);
            Balls[3] = new Ball(3, 200, 100, 0.16, 1, gameTick);
            Balls[4] = new Ball(4, 200, 200, 0.16, 1, gameTick);
            Balls[5] = new Ball(5, 200, 300, 0.16, 1, gameTick);
            Balls[6] = new Ball(6, 300, 100, 0.16, 1, gameTick);
            Balls[7] = new Ball(7, 300, 200, 0.16, 1, gameTick);
            Balls[8] = new Ball(8, 300, 300, 0.16, 1, gameTick);
            Balls[9] = new Ball(9, 400, 100, 0.16, 1, gameTick);
            Balls[10] = new Ball(10, 400, 200, 0.16, 1, gameTick);
            Balls[11] = new Ball(11, 400, 300, 0.16, 1, gameTick);
            Balls[12] = new Ball(12, 500, 100, 0.16, 1, gameTick);
            Balls[13] = new Ball(13, 500, 200, 0.16, 1, gameTick);
            Balls[14] = new Ball(14, 500, 300, 0.16, 1, gameTick);

            //Array for each toggleable key
            keyDown = new bool[15];
            for (int i = 0; i <= keyDown.Length - 1; i++)
            {
                keyDown[i] = false;
            }

	        //Starting general values
            currentBall = 0;
            accelDefault = 500;
	        frictDefault = -2;
            debug = true;
            music = true;
            ballMoveTest = true;
            pause = false;
            console = true;
            currentCollision = false;
	        bumperElasticity = 0.2; //Between 0 and 1 (0-100%)
	        bounceBuffer = 2;

            base.Initialize();
        }
Beispiel #5
0
        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here
	    
	        //Starting values for ball
            Position = new Vector2[16];
            Balls = new Ball[16];
            Balls[0] = new Ball(0, 100, 240, 0.16, 1, gameTick);
            Balls[1] = new Ball(1, 400, 240, 0.16, 1, gameTick);
            Balls[2] = new Ball(2, 415, 231, 0.16, 1, gameTick);
            Balls[3] = new Ball(3, 415, 249, 0.16, 1, gameTick);
            Balls[4] = new Ball(4, 430, 222, 0.16, 1, gameTick);
            Balls[5] = new Ball(5, 430, 240, 0.16, 1, gameTick);
            Balls[6] = new Ball(6, 430, 258, 0.16, 1, gameTick);
            Balls[7] = new Ball(7, 445, 213, 0.16, 1, gameTick);
            Balls[8] = new Ball(8, 445, 231, 0.16, 1, gameTick);
            Balls[9] = new Ball(9, 445, 249, 0.16, 1, gameTick);
            Balls[10] = new Ball(10, 445, 267, 0.16, 1, gameTick);
            Balls[11] = new Ball(11, 460, 204, 0.16, 1, gameTick);
            Balls[12] = new Ball(12, 460, 222, 0.16, 1, gameTick);
            Balls[13] = new Ball(13, 460, 240, 0.16, 1, gameTick);
            Balls[14] = new Ball(14, 460, 258, 0.16, 1, gameTick);
            Balls[15] = new Ball(15, 460, 276, 0.16, 1, gameTick);

            //Array for each toggleable key
            keyDown = new bool[15];
            for (int i = 0; i <= keyDown.Length - 1; i++)
            {
                keyDown[i] = false;
            }

	        //Starting general values
            currentBall = 0;
            accelDefault = 500;
	        frictDefault = -2;
            debug = true;
            music = true;
            ballMoveTest = true;
            pause = false;
            console = true;
            currentCollision = false;
	        bumperElasticity = 0.2; //Between 0 and 1 (0-100%)
	        bounceBuffer = 2;

            base.Initialize();
        }
        public void UpdateSlider(Slider[] Sliders, bool[] moveSlider, int i, Ball[] Balls, int currentBall, Vector2 mousePos, MouseState mouseState, double frictDefault, double bumperElasticity)
        {
            //Updates movement of Sliders
            if (Sliders[i].sliderName == "Current Ball Elast.")
                Sliders[i].curX = Balls[currentBall].elast * 145;
            if (Sliders[i].sliderName == "Current Ball Mass")
                Sliders[i].curX = Balls[currentBall].mass * 145;
            if (mousePos.X >= Sliders[i].posX + Sliders[i].curX && mousePos.X <= Sliders[i].posX + Sliders[i].curX + 15 && mousePos.Y >= Sliders[i].posY - 3 && mousePos.Y <= Sliders[i].posY + 20 && mouseState.LeftButton == ButtonState.Pressed && !moveSlider[i])
                moveSlider[i] = true;
            else if (mouseState.LeftButton == ButtonState.Released)
                moveSlider[i] = false;
            if (moveSlider[i])
                Sliders[i].curX = mousePos.X - Sliders[i].posX;
            if (Sliders[i].curX <= 0)
                Sliders[i].curX = 0;
            else if (Sliders[i].curX >= 145)
                Sliders[i].curX = 145;

            //Updates effected values of sliders
        }
        public static void CollisionCalculation(Ball ball1, Ball ball2, SoundEffect ballBallCollision)
        {
            if (ball1.velT != 0 || ball2.velT != 0)
                ballBallCollision.Play();

            double L = Math.Sqrt(Math.Pow((ball1.posX - ball2.posX), 2) + Math.Pow((ball1.posY - ball2.posY), 2)); // Length between ball1 and ball2.
            double ex = (ball1.posX - ball2.posX) / L; // X distance divided by total distance.
            double ey = (ball1.posY - ball2.posY) / L; // Y distance divided by total distance.

            double ox = -1 * ey; // The opposite Y distance divided by the total distance.
            double oy = ex; // The X distance divided by the total distance.

            double e1x = (ball1.velX * ex + ball1.velY * ey) * ex;
            double e1y = (ball1.velX * ex + ball1.velY * ey) * ey;
            double e2x = (ball2.velX * ex + ball2.velY * ey) * ex;
            double e2y = (ball2.velX * ex + ball2.velY * ey) * ey;

            double o1x = (ball1.velX * ox + ball1.velY * oy) * ox;
            double o1y = (ball1.velX * ox + ball1.velY * oy) * oy;
            double o2x = (ball2.velX * ox + ball2.velY * oy) * ox;
            double o2y = (ball2.velX * ox + ball2.velY * oy) * oy;
            double vxs = (ball1.mass * e1x + ball2.mass * e2x) / (ball1.mass + ball2.mass);
            double vys = (ball1.mass * e1y + ball2.mass * e2y) / (ball1.mass + ball2.mass);

            // Final velocity of ball 1 after collision.
            double vx1 = -e1x + 2 * vxs + o1x;
            double vy1 = -e1y + 2 * vys + o1y;

            // Final velocity of ball 2 after collision.
            double vx2 = -e2x + 2 * vxs + o2x;
            double vy2 = -e2y + 2 * vys + o2y;

            ball1.velX = (float)vx1 * ((ball1.elast + ball2.elast) / 2);
            ball1.velY = (float)vy1 * ((ball1.elast + ball2.elast) / 2);

            ball2.velX = (float)vx2 * ((ball1.elast + ball2.elast) / 2);
            ball2.velY = (float)vy2 * ((ball1.elast + ball2.elast) / 2);
        }
 public Collision (Ball a, Ball b)
 {
     Ball1 = a;
     Ball2 = b;
 }
Beispiel #9
0
        public void ballMovementTest(KeyboardState keyState, Ball[] Balls, int currentBall, double accelDefault, double frictDefault, double bumperElasticity, double bounceBuffer, double resX, double resY)
        {
            //Sets acceleration in the X direction
            if (keyState.IsKeyDown(Keys.Left))
                Balls[currentBall].accelX = -accelDefault - Balls[currentBall].frictX;
            else if (keyState.IsKeyDown(Keys.Right))
                Balls[currentBall].accelX = accelDefault + Balls[currentBall].frictX;
            else
                Balls[currentBall].accelX = 0;

            Balls[currentBall].velX += Balls[currentBall].accelX * gameTick;

            //Sets acceleration in the Y direction
            if (keyState.IsKeyDown(Keys.Down))
                Balls[currentBall].accelY = accelDefault + Balls[currentBall].frictY;
            else if (keyState.IsKeyDown(Keys.Up))
                Balls[currentBall].accelY = -accelDefault - Balls[currentBall].frictY;
            else
                Balls[currentBall].accelY = 0;

            Balls[currentBall].velY += Balls[currentBall].accelY * gameTick;

            Ball.ballCalculations(Balls, frictDefault, gameTick, bumperElasticity, bounceBuffer, resX, resY);
        }
Beispiel #10
0
        public static Vector2 ballCalculations(Ball[] Balls, double frictDefault, double gameTick, double bumperElasticity, double bounceBuffer, double resX, double resY)
        {
            Vector2 BallVector = new Vector2(0, 0);

            //Loops to keep all balls active
            for (int i = 0; i <= Balls.Length - 1; i++)
            {
                //Math!
                Balls[i].velT = Math.Sqrt(Balls[i].velX * Balls[i].velX + Balls[i].velY * Balls[i].velY);
                Balls[i].angleRad = Math.Atan(Balls[i].velY / Balls[i].velX);
                Balls[i].angleDeg = Balls[i].angleRad * 180 / Math.PI;

                //Properly calculate friction
                if (Balls[i].angleRad == 0 || Balls[i].angleRad < 0 || Balls[i].angleRad > 0)
                {
                    Balls[i].frictX = frictDefault * Math.Abs(Math.Cos(Balls[i].angleRad)) * Balls[i].mass;
                    Balls[i].frictY = frictDefault * Math.Abs(Math.Sin(Balls[i].angleRad)) * Balls[i].mass;
                }
                else
                {
                    Balls[i].frictX = frictDefault * Balls[i].mass;
                    Balls[i].frictY = frictDefault * Balls[i].mass;
                }
                if (Balls[i].velX < 0)
                    Balls[i].frictX = -Balls[i].frictX;
                if (Balls[i].velY < 0)
                    Balls[i].frictY = -Balls[i].frictY;

                //Gets rid of e^17 Bug
                if (Math.Abs(Balls[i].frictX) < 0.001)
                    Balls[i].frictX = 0;
                if (Math.Abs(Balls[i].frictY) < 0.001)
                    Balls[i].frictY = 0;

                //Friciton to slow down the ball to 0 in the X direction
                if (Balls[i].accelX == 0 && Balls[i].velX > 0 || Balls[i].accelX == 0 && Balls[i].velX < 0)
                {
                    Balls[i].velX += Balls[i].frictX;
                    if (Math.Abs(Balls[i].velX) < -frictDefault)
                        Balls[i].velX = 0;
                }

                //Friciton to slow down the ball to 0 in the Y direction
                if (Balls[i].accelY == 0 && Balls[i].velY > 0 || Balls[i].accelY == 0 && Balls[i].velY < 0)
                {
                    Balls[i].velY += Balls[i].frictY;
                    if (Math.Abs(Balls[i].velY) < -frictDefault)
                        Balls[i].velY = 0;
                }

                //Move the ball
                Balls[i].posX += Balls[i].velX * gameTick;
                Balls[i].posY += Balls[i].velY * gameTick;

                //Completely stops ball
                if (Balls[i].velX < 1 && Balls[i].velX > -1 && Balls[i].accelX == 0)
                    Balls[i].velX = 0;
                if (Balls[i].velY < 1 && Balls[i].velY > -1 && Balls[i].accelY == 0)
                    Balls[i].velY = 0;

                //Bounces the ball off the wall
                if (Balls[i].posX < 0)
                {
                    Balls[i].velX *= -(1 - bumperElasticity);
                    Balls[i].posX += bounceBuffer;
                }
                else if (Balls[i].posX > resX - 16)
                {
                    Balls[i].velX *= -(1 - bumperElasticity);
                    Balls[i].posX -= bounceBuffer;
                }
                if (Balls[i].posY < 0)
                {
                    Balls[i].velY *= -(1 - bumperElasticity);
                    Balls[i].posY += bounceBuffer;
                }
                else if (Balls[i].posY > resY - 16)
                {
                    Balls[i].velY *= -(1 - bumperElasticity);
                    Balls[i].posY -= bounceBuffer;
                }

                Balls[i].momX = Balls[i].velX * Balls[i].mass;
                Balls[i].momY = Balls[i].velY * Balls[i].mass;
                if (Balls[i].accelX != 0)
                    Balls[i].fnetX = Balls[i].accelX * Balls[i].mass;
                else if (Balls[i].accelX == 0 && Balls[i].velT != 0)
                    Balls[i].fnetX = Balls[i].frictX * Balls[i].mass;
                else
                    Balls[i].fnetX = 0;
                if (Balls[i].accelY != 0)
                    Balls[i].fnetY = Balls[i].accelY * Balls[i].mass;
                else if (Balls[i].accelY == 0 && Balls[i].velT != 0)
                    Balls[i].fnetY = Balls[i].frictY * Balls[i].mass;
                else
                    Balls[i].fnetY = 0;

                BallVector = new Vector2((float)Balls[i].posX, (float)Balls[i].posY);
            }

            return BallVector;
        }
Beispiel #11
0
 public void drawLine(Ball b1, Ball b2)
 {
     double length = Vector2.Distance(new Vector2 ((float)b1.posX, (float)b1.posY), new Vector2 ((float)b2.posX, (float)b2.posY));
     double angle = Math.Atan((b2.posY - b1.posY) / (b2.posX - b1.posX));
     if (b1.posX > b2.posX)
         spriteBatch.Draw(whitePixel, new Rectangle((int)b2.posX, (int)b2.posY , (int)length, 1), null, Color.White, (float)angle, Vector2.Zero, SpriteEffects.None, 0);
     else if (b1.posX < b2.posX)
         spriteBatch.Draw(whitePixel, new Rectangle((int)b1.posX, (int)b1.posY, (int)length, 1), null, Color.White, (float)angle, Vector2.Zero, SpriteEffects.None, 0);
 }
 public void resetBalls(KeyboardState keyState, Ball[] Balls)
 {
     if (keyState.IsKeyDown(Keys.R))
     {
         for (int i = 0; i <= Balls.Length - 1; i++)
         {
             Balls[i].posX = Balls[i].defX;
             Balls[i].posY = Balls[i].defY;
             Balls[i].velX = 0;
             Balls[i].velY = 0;
             Balls[i].accelX = 0;
             Balls[i].accelY = 0;
             Balls[i].frictX = 0;
             Balls[i].frictY = 0;
             Balls[i].velT = 0;
         }
     }
 }
 public static double BallMassSlider(Slider[] Sliders, int currentBall, Ball[] Balls)
 {
     Balls[currentBall].mass = Sliders[3].curX / 145;
     return Balls[currentBall].mass;
 }
 public static double BallElasticitySlider(Slider[] Sliders, int currentBall, Ball[] Balls)
 {
     Balls[currentBall].elast = Sliders[2].curX / 145;
     return Balls[currentBall].elast;
 }
Beispiel #15
0
        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here

            graphics.PreferredBackBufferWidth = 1280;
            graphics.PreferredBackBufferHeight = 720;
            graphics.ApplyChanges();

	        //Starting values for ball
            Sliders = new Slider[3];
            moveSlider = new bool[3];
            Sliders[0] = new Slider("Friction", Window.ClientBounds.Width - 180, 80, 0);
            Sliders[1] = new Slider("Elasticity", Window.ClientBounds.Width - 180, 120, 34);
            Sliders[2] = new Slider("Current Ball Elast.", Window.ClientBounds.Width - 180, 160, 0);
            Position = new Vector2[16];
            Balls = new Ball[10];
            Balls[0] = new Ball(0, 100, 240, 0.16, 1, gameTick);
            Balls[1] = new Ball(1, 400, 240, 0.16, 1, gameTick);
            //Balls[2] = new Ball(2, 415, 231, 0.16, 1, gameTick);
            //Balls[3] = new Ball(3, 415, 249, 0.16, 1, gameTick);
            //Balls[4] = new Ball(4, 430, 222, 0.16, 1, gameTick);
            //Balls[5] = new Ball(5, 430, 240, 0.16, 1, gameTick);
            //Balls[6] = new Ball(6, 430, 258, 0.16, 1, gameTick);
            //Balls[7] = new Ball(7, 445, 213, 0.16, 1, gameTick);
            //Balls[8] = new Ball(8, 445, 231, 0.16, 1, gameTick);
            //Balls[9] = new Ball(9, 445, 249, 0.16, 1, gameTick);
            //Balls[10] = new Ball(10, 445, 267, 0.16, 1, gameTick);
            //Balls[11] = new Ball(11, 460, 204, 0.16, 1, gameTick);
            //Balls[12] = new Ball(12, 460, 222, 0.16, 1, gameTick);
            //Balls[13] = new Ball(13, 460, 240, 0.16, 1, gameTick);
            //Balls[14] = new Ball(14, 460, 258, 0.16, 1, gameTick);
            //Balls[15] = new Ball(15, 460, 276, 0.16, 1, gameTick);

            Balls[2] = new Ball(2, 420, 236, 0.16, 1, gameTick);
            Balls[3] = new Ball(3, 420, 254, 0.16, 1, gameTick);
            Balls[4] = new Ball(4, 435, 227, 0.16, 1, gameTick);
            Balls[5] = new Ball(5, 435, 245, 0.16, 1, gameTick);
            Balls[6] = new Ball(6, 435, 263, 0.16, 1, gameTick);
            Balls[7] = new Ball(7, 450, 218, 0.16, 1, gameTick);
            Balls[8] = new Ball(8, 450, 236, 0.16, 1, gameTick);
            Balls[9] = new Ball(9, 450, 254, 0.16, 1, gameTick);

            //Array for each toggleable key
            keyDown = new bool[15];
            for (int i = 0; i <= keyDown.Length - 1; i++)
                keyDown[i] = false;
            for (int i = 0; i <= moveSlider.Length - 1; i++)
                moveSlider[i] = false;

            mouseDown = false;

	        //Starting general values
            currentBall = 0;
            accelDefault = 500;
	        frictDefault = 0;
            debug = false;
            music = false;
            sliderActive = false;
            ballMoveTest = true;
            pause = false;
            console = true;
            currentCollision = false;
            firstPlay = false;
	        bumperElasticity = 0; //Between 0 and 1 (0-100%)
	        bounceBuffer = 2;

            base.Initialize();
        }
Beispiel #16
0
        /// <summary>
        /// Calculates for Ball Collisions.
        /// </summary>
        //public void ballCollisionMovement()
        //{
        //    double ball1VelTemp, ball2VelTemp, posSlope;
        //    double slopeBall1, slopeBall2;
        //    double inBall1X, inBall1Y, inBall2X, inBall2Y;
        //    double testBall1X, testBall1Y, testBall2X, testBall2Y;
        //    double outBall1X = 1, outBall1Y = 1, outBall2X = 1, outBall2Y = 1;

        //    if (activeCollision)
        //    {
        //        if (ballCollisions.Ball1.velX == 0)
        //            inBall1X = 1;
        //        else
        //            inBall1X = ballCollisions.Ball1.velX;
        //        if (ballCollisions.Ball1.velY == 0)
        //            inBall1Y = 1;
        //        else
        //            inBall1Y = ballCollisions.Ball1.velY;
        //        if (ballCollisions.Ball2.velX == 0)
        //            inBall2X = 1;
        //        else
        //            inBall2X = ballCollisions.Ball2.velX;
        //        if (ballCollisions.Ball2.velY == 0)
        //            inBall2Y = 1;
        //        else
        //            inBall2Y = ballCollisions.Ball2.velY;

        //        //Orignal Math also possibly new directions for collisions
        //        testBall2X = (ballCollisions.Ball1.velX / Math.Abs(inBall1X)) * ballCollisions.Ball1.velT * Math.Sin((Math.PI / 2 - Math.Abs(Math.Atan(vectorSlopeFinder(Position[ballCollisions.Ball1.ballNumber], Position[ballCollisions.Ball2.ballNumber])) * Math.Cos(Math.Abs(Math.Atan(vectorSlopeFinder(Position[ballCollisions.Ball1.ballNumber], Position[ballCollisions.Ball2.ballNumber])))))));
        //        testBall2Y = (ballCollisions.Ball1.velY / Math.Abs(inBall1X)) * ballCollisions.Ball1.velT * Math.Cos((Math.PI / 2 - Math.Abs(Math.Atan(vectorSlopeFinder(Position[ballCollisions.Ball1.ballNumber], Position[ballCollisions.Ball2.ballNumber])) * Math.Cos(Math.Abs(Math.Atan(vectorSlopeFinder(Position[ballCollisions.Ball1.ballNumber], Position[ballCollisions.Ball2.ballNumber])))))));

        //        testBall1X = -(ballCollisions.Ball1.velX / Math.Abs(inBall1X)) * ballCollisions.Ball1.velT * Math.Sin((Math.Abs(Math.Atan(vectorSlopeFinder(Position[ballCollisions.Ball1.ballNumber], Position[ballCollisions.Ball2.ballNumber])) * Math.Cos(Math.PI - Math.Abs(Math.Atan(vectorSlopeFinder(Position[ballCollisions.Ball1.ballNumber], Position[ballCollisions.Ball2.ballNumber])))))));
        //        testBall1Y = (ballCollisions.Ball1.velY / Math.Abs(inBall1X)) * ballCollisions.Ball1.velT * Math.Cos((Math.Abs(Math.Atan(vectorSlopeFinder(Position[ballCollisions.Ball1.ballNumber], Position[ballCollisions.Ball2.ballNumber])) * Math.Cos(Math.PI - Math.Abs(Math.Atan(vectorSlopeFinder(Position[ballCollisions.Ball1.ballNumber], Position[ballCollisions.Ball2.ballNumber])))))));

        //        //Directions?
        //        if (testBall1X != 0)
        //            outBall1X = testBall1X / Math.Abs(testBall1X);
        //        if (testBall1Y != 0)
        //            outBall1Y = testBall1Y / Math.Abs(testBall1Y);
        //        if (testBall2X != 0)
        //            outBall2X = testBall2X / Math.Abs(testBall2X);
        //        if (testBall2Y != 0)
        //            outBall2Y = testBall2Y / Math.Abs(testBall2Y);

        //        //Find Slopes
        //        posSlope = vectorSlopeFinder(Position[ballCollisions.Ball1.ballNumber], Position[ballCollisions.Ball2.ballNumber]);
        //        slopeBall1 = ballCollisions.Ball1.velY / ballCollisions.Ball1.velX;
        //        slopeBall2 = ballCollisions.Ball2.velY / ballCollisions.Ball2.velX;

        //        //Check if Head-On Collision
        //        if (Math.Abs(ballCollisions.Ball1.angleRad) == Math.Abs(Math.Atan(posSlope)) || Math.Abs(ballCollisions.Ball2.angleRad) == Math.Abs(Math.Atan(posSlope)))
        //        {
        //            if (ballCollisions.Ball1.velT != 0 && ballCollisions.Ball2.velT == 0)
        //            {
        //                ballCollisions.Ball2.velX = ballCollisions.Ball1.velX * (ballCollisions.Ball1.elast + ballCollisions.Ball2.elast) / 2;
        //                ballCollisions.Ball2.velY = ballCollisions.Ball1.velY * (ballCollisions.Ball1.elast + ballCollisions.Ball2.elast) / 2;
        //                ballCollisions.Ball2.posX += bounceBuffer * ballCollisions.Ball2.velX * gameTick;
        //                ballCollisions.Ball2.posY += bounceBuffer * ballCollisions.Ball2.velY * gameTick;
        //                ballCollisions.Ball1.velX = 0;
        //                ballCollisions.Ball1.velY = 0;
        //            }
        //            else if (ballCollisions.Ball2.velT != 0 && ballCollisions.Ball1.velT == 0)
        //            {
        //                ballCollisions.Ball1.velX = ballCollisions.Ball2.velX * (ballCollisions.Ball1.elast + ballCollisions.Ball2.elast) / 2;
        //                ballCollisions.Ball1.velY = ballCollisions.Ball2.velY * (ballCollisions.Ball1.elast + ballCollisions.Ball2.elast) / 2;
        //                ballCollisions.Ball1.posX += bounceBuffer * ballCollisions.Ball1.velX * gameTick;
        //                ballCollisions.Ball1.posY += bounceBuffer * ballCollisions.Ball1.velY * gameTick;
        //                ballCollisions.Ball2.velX = 0;
        //                ballCollisions.Ball2.velY = 0;
        //            }
        //            else if (ballCollisions.Ball1.velT != 0 && ballCollisions.Ball2.velT != 0)
        //            {
        //                if (Math.Abs(ballCollisions.Ball1.velT) > Math.Abs(ballCollisions.Ball2.velT))
        //                {
        //                    ballCollisions.Ball2.velX = (ballCollisions.Ball1.velX + ballCollisions.Ball2.velX) * (ballCollisions.Ball1.elast + ballCollisions.Ball2.elast) / 2;
        //                    ballCollisions.Ball2.velY = (ballCollisions.Ball1.velY + ballCollisions.Ball2.velY) * (ballCollisions.Ball1.elast + ballCollisions.Ball2.elast) / 2;
        //                    ballCollisions.Ball2.posX += bounceBuffer * ballCollisions.Ball2.velX * gameTick;
        //                    ballCollisions.Ball2.posY += bounceBuffer * ballCollisions.Ball2.velY * gameTick;
        //                    ballCollisions.Ball1.velX = 0;
        //                    ballCollisions.Ball1.velY = 0;
        //                }
        //                else if (Math.Abs(ballCollisions.Ball1.velT) < Math.Abs(ballCollisions.Ball2.velT))
        //                {
        //                    ballCollisions.Ball1.velX = (ballCollisions.Ball2.velX + ballCollisions.Ball1.velX) * (ballCollisions.Ball1.elast + ballCollisions.Ball2.elast) / 2;
        //                    ballCollisions.Ball1.velY = (ballCollisions.Ball2.velY + ballCollisions.Ball1.velY) * (ballCollisions.Ball1.elast + ballCollisions.Ball2.elast) / 2;
        //                    ballCollisions.Ball1.posX += bounceBuffer * ballCollisions.Ball1.velX * gameTick;
        //                    ballCollisions.Ball1.posY += bounceBuffer * ballCollisions.Ball1.velY * gameTick;
        //                    ballCollisions.Ball2.velX = 0;
        //                    ballCollisions.Ball2.velY = 0;
        //                }
        //            }
        //        }
        //        else if (ballCollisions.Ball1.velT == 0 && ballCollisions.Ball2.velT == 0)
        //        {
        //            if (ballCollisions.Ball1.posX > ballCollisions.Ball2.posX)
        //            {
        //                outBall1X = 1;
        //                outBall2X = -1;
        //            }
        //            if (ballCollisions.Ball1.posX < ballCollisions.Ball2.posX)
        //            {
        //                outBall1X = -1;
        //                outBall2X = 1;
        //            }
        //            if (ballCollisions.Ball1.posX == ballCollisions.Ball2.posX)
        //            {
        //                outBall1X = 0;
        //                outBall2X = 0;
        //            }
        //            if (ballCollisions.Ball1.posY > ballCollisions.Ball2.posY)
        //            {
        //                outBall1Y = 1;
        //                outBall2Y = -1;
        //            }
        //            if (ballCollisions.Ball1.posY < ballCollisions.Ball2.posY)
        //            {
        //                outBall1Y = -1;
        //                outBall2Y = 1;
        //            }
        //            if (ballCollisions.Ball1.posY == ballCollisions.Ball2.posY)
        //            {
        //                outBall1Y = 0;
        //                outBall2Y = 0;
        //            }
        //        }
        //        else
        //        {
        //            //Final VT of Ball 1 = (sqrt((vxa + vxb)^2 + (vya + vyb)^2) * cos(Atan(slope)))
        //            ball1VelTemp = Math.Sqrt(Math.Pow(ballCollisions.Ball1.velX + ballCollisions.Ball2.velX, 2) + Math.Pow(ballCollisions.Ball1.velY + ballCollisions.Ball2.velY, 2)) * Math.Abs(Math.Cos((Math.Atan(posSlope))));
        //            //Final VT of Ball 2 = (sqrt((vxa + vxb)^2 + (vya + vyb)^2) * sin(Atan(slope)))
        //            ball2VelTemp = Math.Sqrt(Math.Pow(ballCollisions.Ball1.velX + ballCollisions.Ball2.velX, 2) + Math.Pow(ballCollisions.Ball1.velY + ballCollisions.Ball2.velY, 2)) * Math.Abs(Math.Cos(Math.PI / 2 - Math.Atan(posSlope)));

        //            //Final VX of Ball 1 = dir * VT * sin(Atan(slope))
        //            ballCollisions.Ball1.velX = outBall1X * ball1VelTemp * Math.Abs(Math.Sin(Math.Atan(posSlope)));
        //            ballCollisions.Ball1.posX += bounceBuffer * ballCollisions.Ball1.velX * gameTick;
        //            //Final VY of Ball 1 = dir * VT * cos(Atan(slope))
        //            ballCollisions.Ball1.velY = outBall1Y * ball1VelTemp * Math.Abs(Math.Cos(Math.Atan(posSlope)));
        //            ballCollisions.Ball1.posY += bounceBuffer * ballCollisions.Ball1.velY * gameTick;
        //            //Final VX of Ball 2 = dir * VT * cos(Atan(slope))
        //            ballCollisions.Ball2.velX = outBall2X * ball2VelTemp * Math.Abs(Math.Cos(Math.Atan(posSlope)));
        //            ballCollisions.Ball2.posX += bounceBuffer * ballCollisions.Ball2.velX * gameTick;
        //            //Final VY of Ball 2 = dir * VT * sin(Atan(slope))
        //            ballCollisions.Ball2.velY = outBall2Y * ball2VelTemp * Math.Abs(Math.Sin(Math.Atan(posSlope)));
        //            ballCollisions.Ball2.posY += bounceBuffer * ballCollisions.Ball2.velY * gameTick;
        //        }
        //    }
        //}

        public void CollisionCalculation(Ball ball1, Ball ball2)
        {
            #region Documentation
            //position ball 1 = (x1,y1)
            //position ball 2 = (x2,y2)
            //Velocity Ball 1 	= (vx1,vy1)
            //Velocity Ball2 	= (vx2,vy2) 

            //L = sqrt((x1-x2)2+(y1-y2)2)
            //ex =  (x1-x2)/L
            //ey =  (y1-y2)/L
            //ox = -ey
            //oy = ex
            //e1x = (v1x*ex+v1y*ey)*ex
            //e1y = (v1x*ex+v1y*ey)*ey
            //e2x = (v2x*ex+v2y*ey)*ex
            //e2y = (v2x*ex+v2y*ey)*ey

            //o1x = (v1x*ox+v1y*oy)*ox
            //o1y = (v1x*ox+v1y*oy)*oy
            //o2x = (v2x*ox+v2y*oy)*ox
            //o2y = (v2x*ox+v2y*oy)*oy
            //vxs = (m1 *e1x +m2 *e2x ) / (m1 +m2 )
            //vys = (m1 *e1y +m2 *e2y ) / (m1 +m2 )

            //Velocity Ball 1 after Collision
            //vx1 = -e1x + 2* vxs + o1x 
            //vy1 = -e1y + 2* vys + o1y
            //Velocity Ball 2 after Collision
            //vx2 = -e2x + 2* vxs + o2x 
            //vy2 = -e2y + 2* vys + o2y
            #endregion
                
                double L = Math.Sqrt(Math.Pow((ball1.posX - ball2.posX), 2) + Math.Pow((ball1.posY - ball2.posY), 2)); // Length between ball1 and ball2.
                double ex = (ball1.posX - ball2.posX) / L; // X distance divided by total distance.
                double ey = (ball1.posY - ball2.posY) / L; // Y distance divided by total distance.

                double ox = -1 * ey;
                double oy = ex;

                double e1x = (ball1.velX * ex + ball1.velY * ey) * ex;
                double e1y = (ball1.velX * ex + ball1.velY * ey) * ey;
                double e2x = (ball2.velX * ex + ball2.velY * ey) * ex;
                double e2y = (ball2.velX * ex + ball2.velY * ey) * ey;

                double o1x = (ball1.velX * ox + ball1.velY * oy) * ox;
                double o1y = (ball1.velX * ox + ball1.velY * oy) * oy;
                double o2x = (ball2.velX * ox + ball2.velY * oy) * ox;
                double o2y = (ball2.velX * ox + ball2.velY * oy) * oy;
                double vxs = (ball1.mass * e1x + ball2.mass * e2x) / (ball1.mass + ball2.mass);
                double vys = (ball1.mass * e1y + ball2.mass * e2y) / (ball1.mass + ball2.mass);

                // Final velocity of ball 1 after collision.
                double vx1 = -e1x + 2 * vxs + o1x;
                double vy1 = -e1y + 2 * vys + o1y;

                // Final velocity of ball 2 after collision.
                double vx2 = -e2x + 2 * vxs + o2x;
                double vy2 = -e2y + 2 * vys + o2y;

                ball1.velX = (float)vx1;
                ball1.velY = (float)vy1;

                ball2.velX = (float)vx2;
                ball2.velY = (float)vy2;
        }
Beispiel #17
0
        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (Keyboard.GetState().IsKeyDown(Keys.Escape))
                this.Exit();

            // TODO: Add your update logic here
            prevKeyState = keyState;
            keyState = Keyboard.GetState();
            KeyToggles keyInstance = new KeyToggles();
            Slider sliderInstance = new Slider();
            Ball ballInstance = new Ball();

            //Mouse
            mouseState = Mouse.GetState();

            mousePos.X = mouseState.X;
            mousePos.Y = mouseState.Y;

            //Count the gameTick
            gameTick = gameTime.ElapsedGameTime.TotalSeconds;
            
            //Play first music
            if (music && !firstPlay)
            {
                MediaPlayer.Play(Songs[4]);
                firstPlay = true;
            }

            sliderActive = sliderInstance.tabListToggle(mousePos, resolutionX, resolutionY);

            debug = keyInstance.debugToggle(keyState, debug, keyDown);

            music = keyInstance.musicToggle(keyState, music, keyDown);

            pause = keyInstance.pauseToggle(keyState, pause, keyDown);

            keyInstance.songSelect(keyState, Songs);

            keyInstance.resetBalls(keyState, Balls);

            if (ballMoveTest)
                ballChange(); //Change Ball

            //Update Sliders
            for (int i = 0; i <= Sliders.Length - 1; i++)
            {
                sliderInstance.UpdateSlider(Sliders, moveSlider, i, Balls, currentBall, mousePos, mouseState, frictDefault, bumperElasticity);
                frictDefault = Slider.FricitonSlider(Sliders, frictDefault);
                bumperElasticity = Slider.BallElasticitySlider(Sliders, currentBall, Balls);
                Balls[currentBall].elast = Slider.BallElasticitySlider(Sliders, currentBall, Balls);
            }

            Balls[currentBall].mass = Slider.BallMassSlider(Sliders, currentBall, Balls);

            if (!pause)
            {
                if (console)
                    consoleWindow();

                if (ballMoveTest)
                    //Runs Ball Movement Test
                    ballInstance.ballMovementTest(keyState, Balls, currentBall, accelDefault, frictDefault, bumperElasticity, bounceBuffer, resolutionX, resolutionY);

                //Runs Ball Position Calculationspublic void ballMovementTest(KeyboardState keyState, Ball[] Balls, int currentBall, double accelDefault, double frictDefault, double bumperElasticity, double bounceBuffer, double resX, double resY)
                Ball.ballCalculations(Balls, frictDefault, gameTick, bumperElasticity, bounceBuffer, resolutionX, resolutionY);

                //Detects Ball Collosion (TEST)
                for (int a = 0; a <= Balls.Length - 2; a++)
                {
                    for (int b = a + 1; b <= Balls.Length - 1; b++)
                    {
                        if (Collision.ballDetectCollision(Balls[a], Balls[b]))
                        {
                            ballCollisions = new Collision(Balls[a], Balls[b]);
                            activeCollision = true;
                            a = Balls.Length;
                            b = Balls.Length;
                        }
                        else if (!Collision.ballDetectCollision(Balls[a], Balls[b]))
                            activeCollision = false;
                    }
                }

                // Determines if a collision is occuring between any 2 balls.
                int element = 0;
                while (Balls.Length > element)
                {
                    int other = element + 1;
                    while (Balls.Length > other)
                    {
                        if (Collision.ballDetectCollision(Balls[element], Balls[other]))
                            Collision.CollisionCalculation(Balls[element], Balls[other], ballBallCollision);
                        other++;
                    }
                    element++;
                }
            }
        }
Beispiel #18
0
        public void CollisionCalculation(Ball ball1, Ball ball2)
        {
            if (ball1.velT != 0 || ball2.velT != 0)
            {
                //ballBallCollision.Play();
            }

                double L = Math.Sqrt(Math.Pow((ball1.posX - ball2.posX), 2) + Math.Pow((ball1.posY - ball2.posY), 2)); // Length between ball1 and ball2.
                double ex = (ball1.posX - ball2.posX) / L; // X distance divided by total distance.
                double ey = (ball1.posY - ball2.posY) / L; // Y distance divided by total distance.

                double ox = -1 * ey; // The opposite Y distance divided by the total distance.
                double oy = ex; // The X distance divided by the total distance.

                double e1x = (ball1.velX * ex + ball1.velY * ey) * ex; // Dot product of ball 1 times the direction of ball 1 in the x direction.
                double e1y = (ball1.velX * ex + ball1.velY * ey) * ey; // Dot product of ball 1 times the direction of ball 1 in the y direction.
                double e2x = (ball2.velX * ex + ball2.velY * ey) * ex; // Dot product of ball 2 times the direction of ball 1 in the x direction.
                double e2y = (ball2.velX * ex + ball2.velY * ey) * ey; // Dot product of ball 2 times the direction of ball 1 in the y direction.

                double o1x = (ball1.velX * ox + ball1.velY * oy) * ox; // Dot product of ball 1 times the direction of ball 2 in the x direction.
                double o1y = (ball1.velX * ox + ball1.velY * oy) * oy; // Dot product of ball 1 times the direction of ball 2 in the y direction.
                double o2x = (ball2.velX * ox + ball2.velY * oy) * ox; // Dot product of ball 2 times the direction of ball 2 in the x direction.
                double o2y = (ball2.velX * ox + ball2.velY * oy) * oy; // Dot product of ball 2 times the direction of ball 2 in the y direction.
                double vxs = (ball1.mass * e1x + ball2.mass * e2x) / (ball1.mass + ball2.mass); // Total out going velcoity of the two balls in the x direction according to their masses 
                double vys = (ball1.mass * e1y + ball2.mass * e2y) / (ball1.mass + ball2.mass); // Total out going velcoity of the two balls in the y direction according to their masses 

                // Final velocity of ball 1 after collision.
                double vx1 = -e1x + 2 * vxs + o1x;
                double vy1 = -e1y + 2 * vys + o1y;

                // Final velocity of ball 2 after collision.
                double vx2 = -e2x + 2 * vxs + o2x;
                double vy2 = -e2y + 2 * vys + o2y;

                ball1.velX = (float)vx1 * ((ball1.elast + ball2.elast) / 2);
                ball1.velY = (float)vy1 * ((ball1.elast + ball2.elast) / 2);

                ball2.velX = (float)vx2 * ((ball1.elast + ball2.elast) / 2);
                ball2.velY = (float)vy2 * ((ball1.elast + ball2.elast) / 2);
        }
Beispiel #19
0
 public bool DetectCircularCollision(Ball a, Ball b)
 {
     double radius = 16;//Math.Sqrt(a.centX * a.centY + b.centX * b.centY);
     double distance = Vector2.Distance(new Vector2(Convert.ToSingle(a.posX), Convert.ToSingle(a.posY)), new Vector2(Convert.ToSingle(b.posX), Convert.ToSingle(b.posY)));
     if (distance < radius) return true;
     return false;
 }