title: 2D collision detection not working XNA C#
body: <p>I am brand new to XNA and C# (I started 2 days ago, so please forgive the sloppy programming), and I have run into a problem with a simple game I am making. I can't seem to find the solution in any of the online tutorials. I am trying to utilize rectangle collision detection but I cannot get it to work properly. The character falls completely through the floor and it seems there is no collision ever registering in any place. I cannot find my logic error here. Below I posted a bit of my code that pertains to the collision detection. I can post more if necessary. Thank you for the help in advance! </p>
  
  <p>Level.cs</p>
  
  <pre><code>//The method below is called in the initial LoadContent method in Game1.cs. 'LoadBlocks()' is supposed to read in a text file and setup the level's "blocks" (the floor or platforms in the game). 
  
  public void LoadBlocks(int level){
      if (level == 0)
          {
              fileName = "filepath";
          }
  
          System.IO.StreamReader file = new System.IO.StreamReader(fileName);
          while ((line = file.ReadLine()) != null)
          {
              for (int i = 0; i &lt; 35; i++)
              {
                  rectBlock = new Rectangle((int)positionBlock.X, (int)positionBlock.Y, width / 30, height / 30);
  
                  if (line.Substring(i, 1).Equals(","))
                  {
                      positionBlock.X += (width / 100) * 24;
                  }
                  if (line.Substring(i, 1).Equals("#"))
                  {  //block -&gt; (bool isPassable, Texture2D texture, Rectangle rectangle, Vector2 position)
                      block = new Block(false, textureBlock, rectBlock, positionBlock);
                      blocks.Add(block);
                      positionBlock.X += (width / 100) * 24;
                  }
              }
  
  
              positionBlock.Y += (height / 100) * 8;
              positionBlock.X = 0;
          }
      }
  
  //This method below updates the character 'bob' position and velocity.
   public void UpdatePlayer()
      {
          bob.position += bob.velocity;
          bob.rectangle = new Rectangle((int)bob.position.X, (int)bob.position.Y, width / 30, height / 30);
  
          float i = 1;
          bob.velocity.Y += 0.15f * i;
          foreach (Block block in blocks)
          {
              if (bob.isOnTopOf(bob.rectangle, block.rectangle))
              {
                  bob.velocity.Y = 0f;
                  bob.hasJumped = false;
              }
          }
      }
  </code></pre>
  
  <p>Character.cs</p>
  
  <pre><code>//Here is my whole Character class for 'bob'
  
  public class Character
  {
      int height = GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Height;
      int width = GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Width;
  
      int health;
      String name;
      bool gender;
      Texture2D texture;
      public Vector2 position;
      public Vector2 velocity;
      public bool hasJumped;
      public Rectangle rectangle;
  
      public Character(int newHealth, String newName, bool newGender, Texture2D newTexture, Vector2 newPosition)
      {
          health = newHealth;
          gender = newGender;
          name = newName;
          texture = newTexture;
          position = newPosition;
          hasJumped = true;
          rectangle = new Rectangle(0,0, width/30,height/30);
          velocity = Vector2.Zero;
      }
  
      public bool isOnTopOf(Rectangle r1, Rectangle r2)
      {
          const int penetrationMargin = 5;
  
          return (r1.Bottom &gt;= r2.Top - penetrationMargin &amp;&amp;
             r1.Bottom &lt;= r2.Top &amp;&amp;
             r1.Right &gt;= r2.Left + 5 &amp;&amp;
             r1.Left &lt;= r2.Right - 5);
  
      }
  
  
      public void Draw(SpriteBatch spriteBatch)
      {
          spriteBatch.Draw(texture, rectangle,null, Color.White,0,position,SpriteEffects.None,0);
      }
  
  }
  </code></pre>
  
  <p>Sorry if this is too much posted code or is confusing, but any help is greatly appreciated! Thanks!</p>