Jump to content
  • Advertisement
Sign in to follow this  
mistervirtue

XNA Code Help - Drawing Collision Rectangle

This topic is 2037 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 have question about drawing collision rectangles. For testing purposes i want to draw a thin rectangle around my sprite (like an overlay), but i am not sure how. Would i use a primative, or would i have to put a texutre around the collisionRect itself? 

 

Any help would be appericated.

Edited by mistervirtue

Share this post


Link to post
Share on other sites
Advertisement

Rectangle shipBounds = new Rectangle((int)ship.shipPos.X, (int)ship.shipPos.Y, shipSprite.Width, shipSprite.Height);

 

code from my game. Pretty much you just create a rectangle object with the same dimensions as your sprite

 

for (int x = bulletBounds.Count - 1; x > -1; x-- )
            {
                for (int y = alienBounds.Count - 1; y > -1; y--)
                {
                    if (bulletBounds[x].Intersects(alienBounds[y]))
                    {
                        alienPosition.RemoveAt(y);
                        alienBounds.RemoveAt(y);
                        //bulletBounds.RemoveAt(x);
                        hit++;
                        break;
                    }
                }

 

This is my collision method. bulletBounds and alienBounds are both rectangle arrays. You can see how Intersects works also

Edited by burnt_casadilla

Share this post


Link to post
Share on other sites

To draw the rectangle, you can just create a texture which is Transparent, but has a Border of 1px around the edge, then just draw it as it has a normal rectangle using the spritebatch.

Share this post


Link to post
Share on other sites

To draw the rectangle, you can just create a texture which is Transparent, but has a Border of 1px around the edge, then just draw it as it has a normal rectangle using the spritebatch.

 

Other way is to create new texture of 1 pixel, set its color to... whatever and use 4 rectangles (Height = x, Width = 1 and vice versa) to form whatever rectangle you want.

I do not have exact code because I didn't need it myself.

Share this post


Link to post
Share on other sites

As you mention Sprite i assume we are talking 2D here, So read what @burnt_casadilla said and generate a rectangle, this rectangle should represent the collision area of your sprite.

 

you already draw with something similar to this?

spriteBatch.Draw(SpriteTexture, SpritePosition, SpriteColor);
or
spriteBatch.Draw(spriteTexture, SpriteREct, SpriteColor);

 

So generate a rectangle like:

Rectangle collisionRect = new rectangle(SpritePosition.X, SpritePosition.Y, SpriteTexture.Width, SpriteTexture.Height);

Then using what I said, create a rectangle in Paint (or image editing program like Photoshop, Paint.NET etc.) which is ... i dunno 32x32. Create a 2px(or 1px) white border creating a transparent square in the center. Save this and Load it into your game using Content.Load<Texture2D>(...)

 

then simply, in your draw call:

// CollisionRectangleTexture - the texture we jsut created;
spriteBatch.Draw(CollisionRectangleTexture, collisionRect, Color.Red); //NOTE: the texture was created white, so when you specify a shade here, it appears as that colour.

 

There are other ways to achieve this: however i beleive this is the simplest, creating the texture as @Aurioch said, using a render-target is overkill imo.

 

3D primitive drawing @Dave Hunts idea would be more painful, if your just doing 2D unless you know what your doing, stay away from 3D drawing.

Edited by Andy474

Share this post


Link to post
Share on other sites

@burnt_casadilla

for (int x = bulletBounds.Count - 1; x > -1; x-- )
            {
                for (int y = alienBounds.Count - 1; y > -1; y--)
                {
                    if (bulletBounds[x].Intersects(alienBounds[y]))
                    {
                        alienPosition.RemoveAt(y);
                        alienBounds.RemoveAt(y);
                        //bulletBounds.RemoveAt(x);
                        hit++;
                        break;
                    }
                }

 

Be Very careful about removing objects from lists while iterating over them: while this will work because your reversing over the array from n-1 --> 0, If you when forward over the array (0 --> n-1) you will find bugs!

 

E.g.

 

List<int> myArray = new List<int>(new int[] { 1,2,3,4,5});
    int sum = 0;
    for(int i=0; i < myArray.Count; i++)    
    {
        sum+= myArray;
        myArray.RemoveAt(i);
    }
 
Yield: 9
//which is incorrect as the sum of 1,2,3,4,5 = 15
//This yields 1 + 3 + 5 = 9
Edited by Andy474

Share this post


Link to post
Share on other sites

Any code pointers would be nice.

I gave you one in my response above.

Sorry about that dave, I am colorblind. Seeing that different shade of blue is hard for me when i am not on my home computer. My bad.

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!