Jump to content
  • Advertisement
Sign in to follow this  
mistervirtue

Drawing A 2D Texture Border

This topic is 1943 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

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.

Edited by mistervirtue

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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

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!