0
votes

Title pretty much sums up my question. I am having a difficult time with 2D collision, so I thought it would be easier to actually see the bounding boxes I am using. That way, I can see if it intersects with my other bounding box. I'm checking for a player sprite and a set of spikes sprite. I want to see the bounding boxes. Is there any relatively simple way to do this (Emphasis on the simple, as I am quite new to XNA)? Any help is appreciated, thanks!

Here is some sample code for my collision method...

protected bool Collide()
{
    if (!isRolling)
    {
        playerRect = new Rectangle((int)pos.X, (int)pos.Y, size.X, size.Y);
        spikesRect = new Rectangle((int)pos2.X, (int)pos2.Y, 65, 80);
        return playerRect.Intersects(spikesRect);
    }
    else
    {
        playerRect = new Rectangle((int)pos.X, (int)pos.Y + offset, 5, 5);
        spikesRect = new Rectangle((int)pos2.X, (int)pos2.Y, 65, 80);
        return playerRect.Intersects(spikesRect);
    }
}

So I need to draw playerRect and spikesRect to the screen.

2
FYI, posting relevant code and what you have already tried would help us help you.Austin Henley
Sorry, there you go. :)TheUnrealMegashark

2 Answers

2
votes

You can check out this example (if all you care about is drawing lines, read this) or you can try the following code that uses FillMode.WireFrame. Something like:

spriteBatch.Begin(SpriteSortMode.Immediate,BlendState.Opaque);
RasterizerState state = new RasterizerState();
state.FillMode = FillMode.WireFrame;
spriteBatch.GraphicsDevice.RasterizerState = state;

//loop this for all sprites!
spriteBatch.Draw(sprite, position, Color.White);

spriteBatch.End();

Based on the code you have now posted, all you need to do in your draw code is:

spriteBatch.Draw(playerTexture, playerRect, color);
//...
spriteBatch.Draw(spikesTexture, spikesRect, color);

Just make sure the draw order is high enough.

0
votes

Create a 1x1 texture of a single colour and then use spriteBatch.Draw to stretch it so if fills your bounding rectangle:

Texture2D pixel;
public override void LoadContent()
{
pixel = new Texture2D(this.GraphicsDevice,1,1);
Color[] colourData = new Color[1];
colourData[0] = Color.White; //The Colour of the rectangle
pixel.SetData<Color>(colourData);

}

public void DrawRectangle(SpriteBatch spriteBatch, Rectangle bounds)
{
spriteBatch.Draw(pixel,bounds,Color.White);
}

That's some very thrown together code, but basically the texture has to be created once, preferably in your game's load content method. Those 4 lines create a texture of 1 pixel and colour that pixel white. 'this' refers to the game itself, as the game always keeps a copy of its own GraphicsDevice, the variable is created as a field in the game class so it can be created once and used any time after. It is important that the texture isn't local to a single method and being created each time it is called as textures aren't designed to be thrown away like that.

The second method is a public method that can be called to draw the rectangle to screen. You can simply substitute calls to the method with the single line it contains (swapping out the arguments ofc) but having a public method means it can be called outside the main game class (as opposed to being limited by the private scope of the sprite batch and the texture).