Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

Collision detection is not working correctly when my 64x64 pixel block touches a bigger block. The big(rect2big) block measures 128x128 pixel. The falling block should change it's color to black when it touches another block, but that's not working if it touches the big block. The falling block changes it's color far too late when it touches the big block. You can see that in this video: http://www.vidup.de/v/QFeTK/

What is wrong? Why is collision detection not working correctly when the falling block touches the big block?

public class Game1 : Microsoft.Xna.Framework.Game
    GraphicsDeviceManager graphics;
    SpriteBatch spriteBatch;
    Texture2D rect, rectbig;
    Rectangle rect1, rect2, rect2big;
    bool rectcollision = false;

    public Game1()
        graphics = new GraphicsDeviceManager(this);
        Content.RootDirectory = "Content";
        TargetElapsedTime = TimeSpan.FromTicks(333333);
        InactiveSleepTime = TimeSpan.FromSeconds(1);
        this.graphics.PreferredBackBufferWidth = 480;
        this.graphics.PreferredBackBufferHeight = 800;
        graphics.IsFullScreen = true;

    protected override void Initialize()

    protected override void LoadContent()
        spriteBatch = new SpriteBatch(GraphicsDevice);
        rect = Content.Load<Texture2D>("block64");
        rectbig = Content.Load<Texture2D>("block128");
        rect1 = new Rectangle(200, 0, rect.Width, rect.Height);
        rect2 = new Rectangle(200, 200, rect.Width, rect.Height);
        rect2big = new Rectangle(200, 400, rectbig.Width, rectbig.Height);

    protected override void Update(GameTime gameTime)
        rect1 = new Rectangle(rect1.X, (int)(rect1.Y+1.0f), rect.Width, rect.Height);

        if (rect1.Intersects(rect2) || rect1.Intersects(rect2big))
            rectcollision = true;
            rectcollision = false;


    protected override void Draw(GameTime gameTime)
        spriteBatch.Draw(rect, rect2, null, Color.White, 0, new Vector2(rect.Width / 2, rect.Height / 2), SpriteEffects.None, 1);
        spriteBatch.Draw(rectbig, rect2big, null, Color.White, 0, new Vector2(rectbig.Width / 2, rectbig.Height / 2), SpriteEffects.None, 1);
        if (rectcollision == true)
          spriteBatch.Draw(rect, rect1, null, Color.Black, 0, new Vector2(rect.Width / 2, rect.Height / 2), SpriteEffects.None, 1);
            spriteBatch.Draw(rect, rect1, null, Color.White, 0, new Vector2(rect.Width / 2, rect.Height / 2), SpriteEffects.None, 1);
share|improve this question

1 Answer 1

up vote 0 down vote accepted

The rectangles are actually colliding properly but you are drawing them at a location other than where you they actually are. If you change the origin parameter in your draw call to be Vector2.Zero your collision code should work as expected. By specifying a non-zero origin you are drawing your rectangles at an offset location from the position where you are performing your collision detection.

share|improve this answer
Thanx. It works now. –  Bryan Aug 29 '13 at 20:03

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.