예제 #1
0
        public void CollideWithRectangle(BouncingRectangle bouncingRect)
        {
            bool currentCollision = false;

            foreach (Vector2 point in bouncingRect.GetRotatedCorners())
            {
                Vector2 adjustedPointCoords = point - position;

                Vector2 unrotatedPointCoords = Vector2.Transform(adjustedPointCoords, Matrix.Invert(rotationMatrix));

                Rectangle hitbox = new Rectangle(-Width / 2, -Height / 2, Width, Height);

                if (hitbox.Contains((int)Math.Round(unrotatedPointCoords.X), (int)Math.Round(unrotatedPointCoords.Y)))
                {
                    Vector2 tempVelocity = velocity;
                    velocity = bouncingRect.velocity;
                    bouncingRect.velocity       = tempVelocity;
                    rotationSpeed              *= -1;
                    bouncingRect.rotationSpeed *= -1;
                    currentCollision            = true;
                    break;
                }
            }
            //if (!colliding && currentCollision)
            //{
            //    Vector2 tempVelocity = velocity;
            //    velocity = bouncingRect.velocity;
            //    bouncingRect.velocity = tempVelocity;
            //    rotationSpeed *= -1;
            //    bouncingRect.rotationSpeed *= -1;
            //}
            colliding = currentCollision;
        }
예제 #2
0
        public bool CheckCollisionWithRectangle(BouncingRectangle bouncingRect)
        {
            foreach (Vector2 point in bouncingRect.GetRotatedCorners())
            {
                Vector2 adjustedPointCoords = point - position;

                Vector2 unrotatedPointCoords = Vector2.Transform(adjustedPointCoords, Matrix.Invert(rotationMatrix));

                Rectangle hitbox = new Rectangle(-Width / 2, -Height / 2, Width, Height);

                if (hitbox.Contains((int)Math.Round(unrotatedPointCoords.X), (int)Math.Round(unrotatedPointCoords.Y)))
                {
                    return(true);
                }
            }
            return(false);
        }
예제 #3
0
 protected override void LoadContent()
 {
     random        = new Random();
     spriteBatch   = new SpriteBatch(GraphicsDevice);
     bouncingRects = new List <BouncingRectangle>();
     for (int i = 0; i < 15; i++)
     {
         int size = random.Next(20, 100);
         BouncingRectangle tempBouncingRect;
         bool contained = false;
         do
         {
             tempBouncingRect = new BouncingRectangle(Content.Load <Texture2D>("WhitePixel"), new Vector2(random.Next(75, GraphicsDevice.Viewport.Width - 75), random.Next(75, GraphicsDevice.Viewport.Height - 75)), new Color(random.Next(0, 255), random.Next(0, 255), random.Next(0, 255)), new Vector2(size, size), new Rectangle(0, 0, GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height), .025f, new Vector2(random.Next(3, 6), random.Next(3, 6)));
             foreach (BouncingRectangle bouncingRect in bouncingRects)
             {
                 if (bouncingRect.CheckCollisionWithRectangle(tempBouncingRect))
                 {
                     contained = true;
                 }
             }
         } while (contained);
         bouncingRects.Add(tempBouncingRect);
     }
 }