Jump to content
  • Advertisement
Sign in to follow this  

Need some guidance on drawing rectangles for collision in XNA

This topic is 1931 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Need some guidance on how to draw a rectangle around a brick in my breakout clone. I am using a 2d array in order to draw the map. However, I am stuck on how I can draw the rectangles around each individual brick to use for collision detection. Any advice would be greatly appreciated. Below is what I have so far. Am I on the right track, or is there an easier way to accomplish this task?



using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace BreakThroughTime
    class Levels1950
        // Variables for bricks        

        // Brick textures
        private Texture2D greenBrick, blueBrick, yellowBrick, redBrick;
        private List<Texture2D> brickTextureList;

        // Texture for background
        private Texture2D backgroundTexture;

        // List for rectangles used to detect collision when bricks are drawn
        private List<Rectangle> brickPosition;
        private Rectangle brickRectangle;

        // Bool to determine if a brick is active or not
        private bool isActive;

        // Bool to determine if level is complete
        private bool isLevelComplete;

        // Offsets for map
        private int xOffset = 140;
        private int yOffset = 50;

        // Level 1 map
        private int[,] map = new int[,]

        // Get upper bounds for map array
        int bound0;
        int bound1;

        // Constructor for LevelManager
        public Levels1950(Texture2D greenBrick, Texture2D blueBrick, Texture2D yellowBrick, Texture2D redBrick, Texture2D backgroundTexture)
            this.greenBrick = greenBrick;
            this.blueBrick = blueBrick;
            this.yellowBrick = yellowBrick;
            this.redBrick = redBrick;
            this.backgroundTexture = backgroundTexture;

            brickPosition = new List<Rectangle>();

            brickTextureList = new List<Texture2D>();

            isActive = true;
            isLevelComplete = false;

            bound0 = map.GetUpperBound(0);
            bound1 = map.GetUpperBound(1);

        public void CollisionRectanglesForBricks()
            for (int x = 0; x <= bound1; x++)
                for (int y = 0; y <= bound0; y++)
                    if (map[y,x] > 0)
                        brickRectangle = new Rectangle((x * greenBrick.Width) + xOffset, (y * greenBrick.Height) + yOffset, greenBrick.Width,


        public void Update(GameTime gameTime)

        public void Draw(SpriteBatch spriteBatch)

            spriteBatch.Draw(backgroundTexture, new Vector2(0, 0), Color.White);

            for (int x = 0; x <= bound1; x++)
                for (int y = 0; y <= bound0; y++)
                    if (map[y,x] == 1)
                        spriteBatch.Draw(brickTextureList.ElementAt(0), new Vector2((x * greenBrick.Width) + xOffset, (y * greenBrick.Height) + yOffset), Color.White);


Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!