public static bool PassSector(Car objA, Background background, Color markerColor) { // Calculate a matrix which transforms from A's local space into // world space and then into B's local space Matrix transformAToB = objA.transformMatrix * Matrix.Invert(background.transformMatrix); //For each row of pixel in A for (int yA = 0; yA < objA.Height; yA++) { //For each pixel in that row for (int xA = 0; xA < objA.Width; xA++) { //Calculate this pixel's location in B Vector2 positionInB = Vector2.Transform(new Vector2(xA, yA), transformAToB); int xB = (int)Math.Round(positionInB.X); int yB = (int)Math.Round(positionInB.Y); if (xB >= 0 && xB < background.Width && yB >= 0 && yB < background.Height) { //Get colors of the overlapping pixels Color colorA = objA.colorData[xA + yA * objA.Width]; Color colorB = background.colorData[xB + yB * background.Width]; //If both pixels are not completely transparent if (colorA.A != 0 && colorB == markerColor) { //Intersection found return true; } } } } //No intersection return false; }
public override void Update(GameTime gameTime) { KeyboardState newState = Keyboard.GetState(); if (lastCountdown == new TimeSpan()) lastCountdown = gameTime.TotalGameTime; if (countdown > 0 && lastCountdown + countdownTime < gameTime.TotalGameTime) { countdown -= 1; lastCountdown = gameTime.TotalGameTime; } if (countdown <= 1) { if (!gameDone) { //Update Cars if (Car.OnGrass(carBasic, background)) { carBasic.speedMax = 4.5f; carBasic.accel = 0.05f; carBasic.friction = 0.2f; } else { carBasic.speedMax = 6; carBasic.accel = 0.1f; carBasic.friction = 0.1f; } carBasic.Update(); Car.AICarSteer(aiCar, background); aiCar.Update(); } else { if (oldState.IsKeyUp(Keys.Enter) && newState.IsKeyDown(Keys.Enter)) { ScreenEvent.Invoke(this, new EventArgs()); } } } //Check and change car sectors if (carBasic.sector == 2 && Car.PassSector(carBasic, background, sector1Color)) { carBasic.sector = 1; carBasic.lap += 1; } else if (carBasic.sector == 1 && Car.PassSector(carBasic, background, sector2Color)) { carBasic.sector = 2; } if (aiCar.sector == 2 && Car.PassSector(aiCar, background, sector1Color)) { aiCar.sector = 1; aiCar.lap += 1; } else if (aiCar.sector == 1 && Car.PassSector(aiCar, background, sector2Color)) { aiCar.sector = 2; } if (carBasic.lap >= 10) { gameDone = true; winner = carBasic; } else if (aiCar.lap >= 10) { gameDone = true; winner = aiCar; } oldState = newState; base.Update(gameTime); }