/// <summary> /// Checks collision between two rotated rectangles /// </summary> /// <param name="drawRectangle1">First rectangle</param> /// <param name="drawRectangle2">Second Rectangle</param> /// <returns>True or False</returns> public static bool CheckRotatedCollision(Asteroid asteroid, Projectile projectile) { float distance = 0.0f; Vector2 position1 = asteroid.Position; Vector2 position2 = projectile.Position; float Cathetus1 = Math.Abs(position1.X - position2.X); float Cathetus2 = Math.Abs(position1.Y - position2.Y); Cathetus1 *= Cathetus1; Cathetus2 *= Cathetus2; distance = (float)Math.Sqrt(Cathetus1 + Cathetus2); if ((int)distance < asteroid.CollisionRectangle.Width) { return true; } return false; }
/// <summary> /// Creates an asteroid /// </summary> private void CreateAsteroid() { RandomGenerator.Init(); //Generate random location int x = GetRandomLocation(GameConstants.SPAWN_BORDER_SIZE, GameConstants.VIEWPORT_WIDTH + GameConstants.SPAWN_BORDER_SIZE); int y = GetRandomLocation(GameConstants.SPAWN_BORDER_SIZE, GameConstants.VIEWPORT_HEIGHT - 2 * GameConstants.SPAWN_BORDER_SIZE); Vector2 asteroidPosition = new Vector2(x, y); //Generate random velocity float xVelocity = (float)(RandomGenerator.NextDouble() * 2 + .5); float yVelocity = (float)(RandomGenerator.NextDouble() * 2 + .5); if (RandomGenerator.Next(2) == 1) xVelocity *= -1.0f; if (RandomGenerator.Next(2) == 1) yVelocity *= -1.0f; Vector2 velocity = new Vector2(xVelocity, yVelocity); //Create new asteroid Asteroid newAsteroid = new Asteroid(ScreenManager.Textures["Objects/asteroid_pixelated"], asteroidPosition, velocity); //// make sure we don't spawn into a collision List<Rectangle> collisionrectangles = GetCollisionRectangles(); bool collFree = CollisionUtils.IsCollisionFree(newAsteroid.CollisionRectangle, collisionrectangles); while (!collFree) { newAsteroid.Position = new Vector2(GetRandomLocation(GameConstants.SPAWN_BORDER_SIZE, GameConstants.VIEWPORT_WIDTH - 2 * GameConstants.SPAWN_BORDER_SIZE), GetRandomLocation(GameConstants.SPAWN_BORDER_SIZE, GameConstants.VIEWPORT_HEIGHT - 2 * GameConstants.SPAWN_BORDER_SIZE)); collFree = true; } asteroidsList.Add(newAsteroid); }