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()
{
base.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;
}
else
rectcollision = false;
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
spriteBatch.Begin();
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);
else
spriteBatch.Draw(rect, rect1, null, Color.White, 0, new Vector2(rect.Width / 2, rect.Height / 2), SpriteEffects.None, 1);
spriteBatch.End();
base.Draw(gameTime);
}
}