Beispiel #1
0
        internal static void HandleAsteroidCollision(GameObject firstCollider, GameObject secondCollider)
        {
            int speedDivisor = 3; // Decrease the speed change from collision. It is too much without it.
            int collisionCooldown = 3000; // Max milliseconds before collission can occur again.

            if (firstCollider.collisionTimer.ElapsedMilliseconds > collisionCooldown &&
                secondCollider.collisionTimer.ElapsedMilliseconds > collisionCooldown &&
                !(firstCollider.X < 0 && firstCollider.X > 900) &&
                !(firstCollider.Y < 0 && firstCollider.Y > 800))
            {
                if (firstCollider.X < secondCollider.X) // firstCollider is to the left of secondCollider.
                {
                    firstCollider.Xspeed -= secondCollider.Xspeed / speedDivisor; // firstCollider increases speed to the left.
                    secondCollider.Xspeed += firstCollider.Xspeed / speedDivisor; // secondCollider decreases speed to the left.
                }
                else // firstCollider is to the right of secondCollider.
                {
                    firstCollider.Xspeed += secondCollider.Xspeed / speedDivisor; // firstCollider increases speed to the right.
                    secondCollider.Xspeed -= firstCollider.Xspeed / speedDivisor; // secondCollider decreases speed to the right.
                }

                if (firstCollider.Y < secondCollider.Y)  // firstCollider is above secondCollider.
                {
                    firstCollider.Yspeed -= secondCollider.Yspeed / speedDivisor; // firstCollider decreases.
                    firstCollider.Yspeed = Math.Max(firstCollider.Yspeed, 1); // Making sure it doesn't go back up.

                    secondCollider.Yspeed += firstCollider.Yspeed / speedDivisor; // secondCollider increases speed.
                }
                else  // firstCollider is under secondCollider.
                {
                    firstCollider.Yspeed += secondCollider.Yspeed / speedDivisor; // secondCollider increases speed.

                    secondCollider.Yspeed -= firstCollider.Yspeed / speedDivisor; // firstCollider decreases speed.
                    secondCollider.Yspeed = Math.Max(secondCollider.Yspeed, 1); // Making sure it doesn't go back up.
                }

                firstCollider.collisionTimer.Restart();
                secondCollider.collisionTimer.Restart();
            }
        }
Beispiel #2
0
 internal static bool TwoObjectsCollide(GameObject firstObject, GameObject secondObject)
 {
     if (IsPointInCircleRadius(firstObject.X + (firstObject.img.Width / 2),   // Try to get the center of each image
                               firstObject.Y + (firstObject.img.Height / 2),  // not the upper left corner
                               secondObject.X + (secondObject.img.Width / 2),
                               secondObject.Y + (secondObject.img.Height / 2),
                               secondObject.collisionRadius))
     {
         return true;
     }
     else
     {
         return false;
     }
 }
Beispiel #3
0
        internal static bool IsObjectCenterInsideRectanlge(GameObject firstObject, GameObject secondObject)
        {
            int rectangleSizeModifier = 50; // Increase the size of the rectangle.

            if (isPointInRectangle(firstObject.X + (firstObject.img.Width / 2),
                                   firstObject.Y + (firstObject.img.Height / 2),
                                   secondObject.X - rectangleSizeModifier,
                                   secondObject.Y - rectangleSizeModifier,
                                   secondObject.img.Width + rectangleSizeModifier,
                                   secondObject.img.Height + rectangleSizeModifier))
            {
                return true;
            }
            else
            {
                return false;
            }
        }