XNA: check collision on list of bricks

I am having a big struggle implementing a collision detect on a list of bricks. I have a game which randomly drops bricks that are supposed to stack up at the bottom of the screen. I managed to make them stop at the bottom of the screen but they don't stack.

I have this in my update function:

if (r.Next(0, 50) == 8)
{
    _bricks.Add(new NormalBrick(this, new Vector2(r.Next(0, 700), 100)));
}   

foreach(Brick b in _bricks)
{
    b.move(GraphicsDevice.Viewport);
}

My move() function has the following code:

public void move(Viewport viewport)
{      
    if (_position.Y == (viewport.Height - _texture.Height ))
    {
        _position = new Vector2(_position.X, _position.Y);
    }
    else
    {
        _position = new Vector2(_position.X, _position.Y + _speed);
    }
}

How can I make sure that the bricks don't all stop at the bottom of the screen, instead the brick has to check if there is a brick beneath it?

I have checked other questions on here but I couldn't find my answer and I have tried several things to get it fixed. Any help would be appreciated.

Answers


I would create Rectangles for each of your Bricks (unless you have already done so). Then in the Update() method, use something like brick.Rectangle.Intersects(anotherBrick) after movement. If true, then position the current brick above the intersecting bottom brick.

Make sure to move the brick's rectangle each time the brick moves.

I hope this helps. Let me know if you require any further assistance.


Need Your Help

multilanguage website - Zend:How should be form element names, table column manes, free text etc.. translated?

php zend-framework multilingual

How should be form element names, table column manes, free text etc.. translated in multilanguage website on Zend Farmework.