Drawing A 2D Texture Border

Started by
4 comments, last by unbird 10 years, 10 months ago

Hello Fellow Gamedev'ers,

I need assistance drawing a border around my a texture for collision testing purposes. I have a Base Sprite class, a subclass UserSprite, and a game Component called SpriteManager. I am trying to draw a border around all of my textures so i can test collision, but I don't really know how. I have tried some stuff on my own but I can't really get anything to work.

My current plan of attack is as follows. I have the base Sprite Class get the collisionRectangle


//This is under the base Sprite Class. I get the collision Rectangle here.
        
public Rectangle collisionRect
        {
            get
            {
                return new Rectangle(
                    (int)position.X + collisionOffset,
                    (int)position.Y + collisionOffset,
                    frameSize.X - (collisionOffset * 2),
                    frameSize.Y - (collisionOffset * 2));
            }
        }

I then have the draw method draw the sprite and the border


        public virtual void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(textureImage, position,
                new Rectangle(currentFrame.X * frameSize.X, currentFrame.Y * frameSize.Y,
                    frameSize.X, frameSize.Y),Color.White,0,Vector2.Zero,1f,myEffect,0);

//I am trying have it draw both the sprite itself and the border around it. 

            spriteBatch.Draw(testingRecTexture, collisionRect, Color.Green);
        }

and I then Load the files for sprite and the border in my spriteManager


        protected override void LoadContent()
        {
            spriteBatch = new SpriteBatch(Game.GraphicsDevice);

            player = new UserControlledSprite(Game.Content.Load<Texture2D>(@"images/Warlock"),
                Vector2.Zero,new Point(378,413), 10, new Point(0,0),new Point(1,1), new Vector2 (3,3));

            testingRec = Game.Content.Load<Texture2D>(@"images/testRec");
         
            base.LoadContent();
            
        }

The code runs, but the border does not a apper. I am just trying have each texture have a green border around it. If i could get a nudge or a shove in the right direction, that would be awesome.

Advertisement

Describe "isn't working". Screenshot might also help.

First I'd check if the rectangle draws at all, meaning: don't draw the sprite (your first SpriteBatch.Draw call). Also try a different collisionOffset, a negative one, basically drawing a bigger rectangle.

Maybe it's a good idea to draw all the rectangles in a separate pass, after you've drawn your sprites, basically layering them over everything else. Haven't touched XNA quite some time, but it might be that - depending on what flags you use in SpriteBatch.Begin - the batch gets sorted so your rectangles are drawn first, no matter in which order you call the Draw methods. If your rectangles are fully covered by the sprite, then they're just hidden.

The code it self runs. All of my sprites appear and do the stuff they are supposed to do, but I can't borders I want to draw over them aren't being drawn.

Ok, what flags do you use for SpriteBatch.Begin() and what happens if you disable the first draw call.

I actually just solved my own problem! I feel so good. My issue was I wasn't draw the testingRec in the SpriteManager.

laugh.png That was a quick remote debugging session. Congrats. Glad it works.

This topic is closed to new replies.

Advertisement