title: How to make an obstacle hit a wall not go through the wall/obstacle
body: <p>I need some help figuring out how i can make the ambulance go around the obstacle rather than going through it. This is what i have so far...
  My plan is to make a racing game, the red box represents my car and I'm trying to figure out how to make it so it doesn't collide with the rectangles so they can't just cheat and cross over to the finish line, they have to go through the track. This is what i want to do</p>
  
  <p><a href="https://i.stack.imgur.com/apgBf.png" rel="nofollow noreferrer">https://i.stack.imgur.com/apgBf.png</a></p>
  
  <p>So far i just have the car and one of the rectangles but can't get it to not cross over the rectangle. </p>
  
  <p>I'm trying to make it so the ambulance can't just go past that obstacle but has to go around it, i want to make a track, a racing game but stuck </p>
  
  <pre><code>using Microsoft.Xna.Framework;
  using Microsoft.Xna.Framework.Graphics;
  using Microsoft.Xna.Framework.Input;
  
  namespace FinalProject
  {
      /// &lt;summary&gt;
      /// This is the main type for your game.
      /// &lt;/summary&gt;
      public class Game1 : Game
      {
          GraphicsDeviceManager graphics;
          SpriteBatch spriteBatch;
  
          int count;
          int speed = 5;
          Texture2D background, carUp, carDown, carRight, carLeft, obstacle;
          Rectangle backgroundRec, carRec, carRecDown, carRecUp, carRecLeft, carRecRight, carRec1, obstacleRec;
  
  
          public Game1()
          {
              graphics = new GraphicsDeviceManager(this);
              Content.RootDirectory = "Content";
          }
  
          /// &lt;summary&gt;
          /// 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.
          /// &lt;/summary&gt;
          protected override void Initialize()
          {
              base.Initialize();
              carRec = new Rectangle(500, 300, 100, 100);
              carRecDown = new Rectangle(500, (300 + speed), 100, 100);
              carRecLeft = new Rectangle((500 - speed), 300, 100, 100);
              carRecUp = new Rectangle(500, (300 - speed), 100, 100);
              carRecRight = new Rectangle((500 + speed), 300, 100, 100);
              backgroundRec = new Rectangle(0, 0, GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height);
              obstacleRec = new Rectangle(100, 240, 100, 300);
          }
  
          /// &lt;summary&gt;
          /// LoadContent will be called once per game and is the place to load
          /// all of your content.
          /// &lt;/summary&gt;
          protected override void LoadContent()
          {
              spriteBatch = new SpriteBatch(GraphicsDevice);
  
              carUp = Content.Load&lt;Texture2D&gt;("up");
              carDown = Content.Load&lt;Texture2D&gt;("down");
              carRight = Content.Load&lt;Texture2D&gt;("right");
              carLeft = Content.Load&lt;Texture2D&gt;("left");
              background = Content.Load&lt;Texture2D&gt;("background");
              obstacle = Content.Load&lt;Texture2D&gt;("obstacle");
          }
  
          /// &lt;summary&gt;
          /// UnloadContent will be called once per game and is the place to unload
          /// game-specific content.
          /// &lt;/summary&gt;
          protected override void UnloadContent()
          {
              // TODO: Unload any non ContentManager content here
          }
  
          /// &lt;summary&gt;
          /// Allows the game to run logic such as updating the world,
          /// checking for collisions, gathering input, and playing audio.
          /// &lt;/summary&gt;
          /// &lt;param name="gameTime"&gt;Provides a snapshot of timing values.&lt;/param&gt;
          protected override void Update(GameTime gameTime)
          {
              if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
                  Exit();
              KeyboardState keys = Keyboard.GetState();
              GamePadState pad1 = GamePad.GetState(PlayerIndex.One);
              carRecDown.Y = carRec.Y - speed;
              carRecLeft.X = carRec.X - speed;
              carRecRight.X = carRec.X + speed;
              carRecUp.Y = carRec.X + speed;
              // Keyboard 
  
              if (keys.IsKeyDown(Keys.Up) &amp;&amp; !(obstacleRec.Intersects(carRecUp)))
              {
                  count = 1;
                  carRec.Y -= speed;
              }
              if (keys.IsKeyDown(Keys.Down) &amp;&amp; !(obstacleRec.Intersects(carRecDown)))
              {
                  count = 2;
                  carRec.Y += speed;
              }
              if (keys.IsKeyDown(Keys.Right) &amp;&amp; !(obstacleRec.Intersects(carRecRight)))
              {
                  count = 3;
                  carRec.X += speed;
              }
              if (keys.IsKeyDown(Keys.Left) &amp;&amp; !(obstacleRec.Intersects(carRecLeft)))
              {
                  count = 4;
                  carRec.X -= speed;
              }
  
              /* if (obstacleRec.Intersects(carRec))
               {
                   carRec.X +=1 ;
               }*/
  
  
  
              base.Update(gameTime);
          }
  
          /// &lt;summary&gt;
          /// This is called when the game should draw itself.
          /// &lt;/summary&gt;
          /// &lt;param name="gameTime"&gt;Provides a snapshot of timing values.&lt;/param&gt;
          protected override void Draw(GameTime gameTime)
          {
              GraphicsDevice.Clear(Color.CornflowerBlue);
  
              spriteBatch.Begin();
              spriteBatch.Draw(background, backgroundRec, Color.White);
              spriteBatch.Draw(obstacle, obstacleRec, Color.White);
              if (count == 0)
                  spriteBatch.Draw(carUp, carRec, Color.White);
              if (count == 1)
                  spriteBatch.Draw(carUp, carRec, Color.White);
              if (count == 2)
                  spriteBatch.Draw(carDown, carRec, Color.White);
              if (count == 3)
                  spriteBatch.Draw(carRight, carRec, Color.White);
              if (count == 4)
                  spriteBatch.Draw(carLeft, carRec, Color.White);
              spriteBatch.End();
  
              base.Draw(gameTime);
          }
      }
  }
  </code></pre>