bool CheckBuildingCollision(Building building) { if (Vector3.Distance(building.Location, location) < 1400) { //Calculate shells location relative to the building Vector3 loc = new Vector3(location.X - building.Location.X, location.Y, location.Z - building.Location.Z); //Create an enlarged hitbox based on the shell's radius Vector3 hitBox = new Vector3(building.Size.X + radius, building.Size.Y + radius, building.Size.Z + radius); //Check to see if the shell passes through the buildings hitbox if (CheckHitBoxCollision(hitBox, velocity, loc)) { tankGame.Components.Remove(building); return true; } } return false; }
/// <summary> /// Checks if this tank is colliding with a building /// </summary> bool CheckBuildingCollision(Building building) { if (Vector3.Distance(building.Location, tankLocation) < 1300) { //relative location and rotation float rot = totalRotation; //Calculate tanks location relative to building Vector2 loc = new Vector2((tankLocation.X - building.Location.X), (tankLocation.Z - building.Location.Z)); //Calculate other tanks corners relative to this building Vector2 topLeft = new Vector2(loc.X + tankSize.X * (float)Math.Cos(rot) + tankSize.Z * (float)Math.Sin(rot), loc.Y + tankSize.Z * (float)Math.Cos(rot) - tankSize.X * (float)Math.Sin(rot)); Vector2 topRight = new Vector2(loc.X - tankSize.X * (float)Math.Cos(rot) + tankSize.Z * (float)Math.Sin(rot), loc.Y + tankSize.Z * (float)Math.Cos(rot) + tankSize.X * (float)Math.Sin(rot)); Vector2 botLeft = new Vector2(loc.X + tankSize.X * (float)Math.Cos(rot) - tankSize.Z * (float)Math.Sin(rot), loc.Y - tankSize.Z * (float)Math.Cos(rot) - tankSize.X * (float)Math.Sin(rot)); Vector2 botRight = new Vector2(loc.X - tankSize.X * (float)Math.Cos(rot) - tankSize.Z * (float)Math.Sin(rot), loc.Y - tankSize.Z * (float)Math.Cos(rot) + tankSize.X * (float)Math.Sin(rot)); Vector2 hitBox = new Vector2(building.Size.X, building.Size.Z); if (InsideRectangle(topLeft, hitBox) || InsideRectangle(topRight, hitBox) || InsideRectangle(botLeft, hitBox) || InsideRectangle(botRight, hitBox)) return true; //Checks to see if this buildings corners are in the tank else if (InsideTriangle(topLeft, topRight, botLeft, hitBox) || InsideTriangle(topLeft, topRight, botLeft, new Vector2(-hitBox.X, hitBox.Y)) || InsideTriangle(topLeft, topRight, botLeft, new Vector2(hitBox.X, -hitBox.Y)) || InsideTriangle(topLeft, topRight, botLeft, new Vector2(-hitBox.X, -hitBox.Y)) || InsideTriangle(botRight, topRight, botLeft, hitBox) || InsideTriangle(botRight, topRight, botLeft, new Vector2(-hitBox.X, hitBox.Y)) || InsideTriangle(botRight, topRight, botLeft, new Vector2(hitBox.X, -hitBox.Y)) || InsideTriangle(botRight, topRight, botLeft, new Vector2(-hitBox.X, -hitBox.Y))) return true; } return false; }
/// <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() { //Initialize Input input = new GameServices.InputState(); input.Update(); Services.AddService(typeof(GameServices.InputState), input); //create camera camera = new GameServices.CameraState( MathHelper.PiOver4, (float)graphics.GraphicsDevice.Viewport.Width / (float)graphics.GraphicsDevice.Viewport.Height, 1.0f, 100000.0f); Services.AddService(typeof(GameServices.CameraState), camera); //create audip audio = new GameServices.AudioLibrary(); audio.InitializeAudio(); Services.AddService(typeof(GameServices.AudioLibrary), audio); //create tanks playerTank = new Objects.Tank(this, new Vector3(0, 0, 0)); playerTank.Health = 10; playerTank.AI = false; Components.Add(playerTank); //buildings buildings = new List<List<Objects.Building>>(); List<Objects.Building> listception = new List<Objects.Building>(); Objects.Building build; for (int i = 1; i <= worldsize.Y / 3000; i++) { for (int j = 1; j <= worldsize.X / 3000; j++) { build = new Objects.Building(this, new Vector3(i * 3000 + 435 - worldsize.Y / 2, 0, j * 3000 + 435 - worldsize.X / 2)); build.Enabled = false; Components.Add(build); } } Random x = new Random(); int n = x.Next(5, 50); Objects.Tank tank; for (int i = 0; i < n; i++) { do tank = new Objects.Tank(this, new Vector3(x.Next(-15, 16) * 3000, 0, x.Next(-15, 16) * 3000)); while (tank.CollisionDetection()); Components.Add(tank); } camera.Update(playerTank.Location, playerTank.TurretRotation); GenerateGround(worldsize.X, worldsize.Y); base.Initialize(); }