Jump to content

  • Log In with Google      Sign In   
  • Create Account


Drawing A 2D Texture Border


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
5 replies to this topic

#1 mistervirtue   Members   -  Reputation: 590

Like
0Likes
Like

Posted 30 May 2013 - 03:29 PM

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, 30 May 2013 - 04:06 PM.


Sponsor:

#2 unbird   Crossbones+   -  Reputation: 4862

Like
1Likes
Like

Posted 30 May 2013 - 04:04 PM

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.



#3 mistervirtue   Members   -  Reputation: 590

Like
0Likes
Like

Posted 30 May 2013 - 04:08 PM

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.



#4 unbird   Crossbones+   -  Reputation: 4862

Like
1Likes
Like

Posted 30 May 2013 - 04:14 PM

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



#5 mistervirtue   Members   -  Reputation: 590

Like
2Likes
Like

Posted 30 May 2013 - 04:18 PM

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



#6 unbird   Crossbones+   -  Reputation: 4862

Like
1Likes
Like

Posted 30 May 2013 - 04:21 PM

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






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS