XNA Code Help - Drawing Collision Rectangle

Started by
11 comments, last by Andy474 10 years, 11 months ago

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.

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

If you see a post from me, you can safely assume its C# and XNA :)

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.

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.

Also, have a look at the shape rendering sample in the XNA Education Catalog. It shows how to draw bounding volumes and other shapes for debugging.

Any code pointers would be nice.

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.

@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

Any code pointers would be nice.

I gave you one in my response above.

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.

This topic is closed to new replies.

Advertisement