Hi, people. I already understood the per pixel collision logic, but in my tests, i always got unexpected results. Sorry for my English
here is the code:
public class Game1 : Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
Color[] textureDataA;
Color[] textureDataB;
Texture2D objA;
Texture2D objB;
Vector2 objA_Position;
Vector2 objB_Position;
Color backgroundColor;
SpriteFont Text;
string message;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
/// <summary>
/// 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.
/// </summary>
protected override void Initialize()
{
// TODO: Add your initialization logic here
base.Initialize();
objA_Position = new Vector2(graphics.PreferredBackBufferWidth / 2, graphics.PreferredBackBufferHeight / 2);
}
/// <summary>
/// LoadContent will be called once per game and is the place to load
/// all of your content.
/// </summary>
protected override void LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);
//TODO: use this.Content to load your game content here
objA = Content.Load<Texture2D>("ObjA");
objB = Content.Load<Texture2D>("ObjB");
Text = Content.Load<SpriteFont>("Text");
textureDataA = new Color[objA.Width * objA.Height];
textureDataB = new Color[objB.Width * objB.Height];
objA.GetData<Color>(textureDataA);
objB.GetData<Color>(textureDataB);
}
/// <summary>
/// Allows the game to run logic such as updating the world,
/// checking for collisions, gathering input, and playing audio.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Update(GameTime gameTime)
{
// TODO: Add your update logic here
objB_Position = new Vector2(Mouse.GetState().Position.X, Mouse.GetState().Position.Y);
if (CollisionDetection(new Rectangle((int)objB_Position.X, (int)objA_Position.Y, objA.Width, objA.Height),
textureDataA,
new Rectangle((int)objB_Position.X, (int)objB_Position.Y, objB.Width, objB.Height),
textureDataB) == true)
{
backgroundColor = Color.Turquoise;
message = "true";
}
else
{
backgroundColor = Color.White;
message = "false";
}
base.Update(gameTime);
}
/// <summary>
/// This is called when the game should draw itself.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(backgroundColor);
//TODO: Add your drawing code here
spriteBatch.Begin();
spriteBatch.Draw(objA, objA_Position, Color.White);
spriteBatch.Draw(objB, objB_Position, Color.White);
spriteBatch.DrawString(Text, message, new Vector2(5, 5), Color.Black);
spriteBatch.End();
base.Draw(gameTime);
}
private bool CollisionDetection(Rectangle rectA, Color[] dataA, Rectangle rectB, Color[] dataB)
{
int top = Math.Max(rectA.Top, rectB.Top);
int bottom = Math.Min(rectA.Bottom, rectB.Bottom);
int left = Math.Max(rectA.Left, rectB.Left);
int right = Math.Max(rectA.Right, rectB.Right);
for (int i = top; i < bottom; i++)
{
for (int j = left; j < right; j++)
{
Color a = dataA[(j - rectA.Left) + (i - rectA.Top) * objA.Width];
Color b = dataB[(j - rectB.Left) + (i - rectB.Top) * objB.Width];
if (a.A != 0 && b.A != 0)
{
return (true);
}
}
}
return (false);
}
}
}