public static bool CheckPuckWallCollision(Rectangle rectangle, Puck puck, Table table)
        {
            int intDistanceLeftX = Math.Abs(rectangle.Left - (int)puck.CenterX);
            int intDistanceRightX = Math.Abs(rectangle.Right - (int)puck.CenterX);
            int intDistanceTopY = Math.Abs(rectangle.Top - (int)puck.CenterY);
            int intDistanceBottomY = Math.Abs(rectangle.Bottom - (int)puck.CenterY);

            int intDistanceTopGoalLeftX = Math.Abs(table.TopGoal.Left - (int)puck.CenterX);
            int intDistanceTopGoalRightX = Math.Abs(table.TopGoal.Right - (int)puck.CenterX);
            int intDistanceTopGoalBottomY = Math.Abs(table.TopGoal.Bottom - (int)puck.CenterY);

            int intDistanceBottomGoalLeftX = Math.Abs(table.BottomGoal.Left - (int)puck.CenterX);
            int intDistanceBottomGoalRightX = Math.Abs(table.BottomGoal.Right - (int)puck.CenterX);
            int intDistanceBottomGoalTopY = Math.Abs(table.BottomGoal.Top - (int)puck.CenterY);
            if (intDistanceLeftX <= puck.Radius)
            {
                PuckHitsWallCollision(puck, "Side");
                return true;
            }
            if (intDistanceRightX <= puck.Radius)
            {
                PuckHitsWallCollision(puck, "Side");
                return true;
            }
            if (intDistanceTopY <= puck.Radius)
            {

                if (table.TopGoal.Contains((int)puck.CenterX, (int)puck.Y) && intDistanceTopGoalBottomY <= puck.Radius)
                {

                        puck.CenterX = 0;
                        puck.CenterY = 0;
                        table.GoalA = true;
                        return true;

                }

                PuckHitsWallCollision(puck, "");
                return true;
            }
            if (intDistanceBottomY <= puck.Radius)
            {
                if (table.BottomGoal.Contains((int)puck.CenterX, ((int)puck.CenterY + puck.Radius)) && intDistanceBottomGoalTopY <= puck.Radius)
                {

                        puck.CenterX = 0;
                        puck.CenterY = 0;
                        table.GoalB = true;
                        return true;
                }
                PuckHitsWallCollision(puck, "");
                return true;
            }
            return false;
        }
        public static bool CheckPaddlePuckCollision(Point paddle, Puck puck)
        {
            int intDistanceX = (int)(paddle.X + 32) - (int)puck.CenterX;
            int intDistanceY = (int)(paddle.Y + 32) - (int)puck.CenterY;
            int intRadii =  32 + puck.Radius;

            if ((intDistanceX * intDistanceX) + (intDistanceY * intDistanceY) < intRadii * intRadii)
            {

                return true;
            }

            return false;
        }
Exemple #3
0
        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            game = this;
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);

            // TODO: use this.Content to load your game content here
            mainMenuScreen = new MainMenuScreen(game);
            cursor = new Cursor(game);
            table = new Table(game);
            puck = new Puck(game);
            paddle = new Paddle(game);
        }
 public static void PuckHitsWallCollision(Puck puck, string strWall)
 {
     if (strWall == "Side")
     {
         puck.SpeedX = puck.SpeedX * -1;
         puck.SpeedY = puck.SpeedY * 1;
     }
     else
     {
         puck.SpeedX = puck.SpeedX * 1;
         puck.SpeedY = puck.SpeedY * -1;
     }
 }
 public static void MovingPuckCollision(Paddle paddle, Puck puck)
 {
     MovingPaddleCollision(paddle, puck, 10);
 }
        //public static Point PaddlePuckCollisionDest(Paddle paddle, Puck puck)
        //{
        //    Point point = new Point();
        //    //Standing puck, Moving paddle
        //    double dblCollisionDist = Math.Sqrt(Math.Pow(puck.CenterX - paddle.CenterX, 2) + Math.Pow(puck.CenterY - paddle.CenterY, 2));
        //    double n_x = (puck.CenterX - paddle.X) /  dblCollisionDist;
        //    double n_Y = (puck.CenterY - paddle.Y) / dblCollisionDist;
        //    double p = 2 * (paddle.StartCenterX * n_x + paddle.StartCenterY * n_Y) / (paddle.Mass + puck.Mass); //Division is for the mass of an object
        //    double w_x = paddle.StartCenterX - p * paddle.Mass * n_x - p * paddle.CenterX * n_x;
        //    double w_y = paddle.StartCenterY - p * paddle.Mass * n_Y - p * paddle.CenterY * n_Y;
        //    //puck.Speed = (float)(Math.Abs(p) / 20);
        //    point.X = (int)w_x;
        //    point.Y = (int)w_y;
        //    return point;
        //}
        public static void MovingPaddleCollision(Paddle paddle, Puck puck, float force)
        {
            //lower the force number the faster the puck goes
            float DirX = (paddle.CenterX - puck.CenterX) / force;
            float DirY = (paddle.CenterY - puck.CenterY)  / force;

                DirX = DirX * -1;
                DirY = DirY * -1;

            puck.SpeedX = DirX;
            puck.SpeedY = DirY;
        }