Continuing from last week, in this article I’ll be walking through some basic collision. Again, I’ll be starting with the code base from last week, so you can go and grab that over at my SkyDrive share.

To get started, we’ll need something to collide with. I’ve created some enemies for use with the game, which you can download from SkyDrive. I’ll start with Enemy-6.png, the one that looks like this:

Enemy-6

To do this, add it into the TriangleShooterContent project by right-clicking the project in the Solution Explorer and choosing Add –> Existing Item, and selecting Enemy-6.png. We’ll then need to create a variable to hold the texture, and a variable to track it’s position. In a later article, we’ll add some basic AI to control it, but for now, it will be static.

Variable Declaration
  1. Texture2D triangle;
  2. Vector2 position;
  3. float rotation;
  4.  
  5. Texture2D enemy;
  6. Vector2 enemyPosition;

Initialize Position
  1. protected override void Initialize()
  2. {
  3.     position = Vector2.Zero;
  4.     enemyPosition = new Vector2(600f, 200f);
  5.  
  6.     base.Initialize();
  7. }

 

Loading Content
  1. protected override void LoadContent()
  2. {
  3. ��   // Create a new SpriteBatch, which can be used to draw textures.
  4.     spriteBatch = new SpriteBatch(GraphicsDevice);
  5.  
  6.     triangle = Content.Load<Texture2D>("Triangle");
  7.     enemy = Content.Load<Texture2D>("Enemy-6");
  8. }

We’ll also need to add a line to the Draw method to display the enemy.

Drawing the Enemy
  1. protected override void Draw(GameTime gameTime)
  2. {
  3.     GraphicsDevice.Clear(Color.Black);
  4.  
  5.     spriteBatch.Begin();
  6.     spriteBatch.Draw(triangle, position, null, Color.White, rotation, new Vector2(0, triangle.Height / 2), 1f, SpriteEffects.None, 0f);
  7.     spriteBatch.Draw(enemy, enemyPosition, Color.White);
  8.     spriteBatch.End();
  9.  
  10.     base.Draw(gameTime);
  11. }

If we run the application, we now can see that there is an enemy with which we can collide.

image

But how can we tell if the collision has happened? How about we change the color of the triangle to red? To do this, we’ll have to add a variable to track the color, and set it to red when the collision happens.

Adding a Color Variable
  1. Texture2D triangle;
  2. Vector2 position;
  3. float rotation;
  4. Color triangleColor;

Initializing the Color
  1. protected override void Initialize()
  2. {
  3.     position = Vector2.Zero;
  4.     enemyPosition = new Vector2(600f, 200f);
  5.     triangleColor = Color.White;
  6.  
  7.     base.Initialize();
  8. }

Drawing Including the Color
  1. protected override void Draw(GameTime gameTime)
  2. {
  3.     GraphicsDevice.Clear(Color.Black);
  4.  
  5.     spriteBatch.Begin();
  6.     spriteBatch.Draw(triangle, position, null, triangleColor, rotation, new Vector2(0, triangle.Height / 2), 1f, SpriteEffects.None, 0f);
  7.     spriteBatch.Draw(enemy, enemyPosition, Color.White);
  8.     spriteBatch.End();
  9.  
  10.     base.Draw(gameTime);
  11. }

Now we’re ready for the collision. We’ll take care of it in the Update method. To check for collision, we have a few options. The easiest would be either rectangle intersection or testing a rectangle to see if it contains a point. In this case, I’ll be using the Rectangle.Contains. The Contains method takes a Point, while our position is a Vector2, so we’ll have to do a bit of casting. We’ll also have to construct a rectangle from the position and texture we have in order to find out if it contains the point. The lines that need to be added into the Update method are as follows:

Checking Collision by Contains
  1. if (new Rectangle((int)enemyPosition.X, (int)enemyPosition.Y, enemy.Width, enemy.Height).Contains((int)position.X, (int)position.Y))
  2. {
  3.     triangleColor = Color.Red;
  4. }

And now, when we run the application and run into the enemy, we get the following:

image

Next week, we’ll break everything out into classes, and maybe even add some AI to the enemies.

Download the latest version of the source code,