Search code examples
xna-4.0

XNA: Identifying identical sprites created with for loop


G'day all,

In short, I'm using a for loop to create a bunch of identical sprites that I want to bounce around the screen. The problem is how do I write a collision detection process for the sprites. I have used the process of placing rectangles around sprites and using the .intersects method for rectangles but in that case I created each sprite separately and could identify each one uniquely. Now I have a bunch of sprites but no apparent way to pick one from another.

In detail, if I create an object called Bouncer.cs and give it the movement instructions in it's update() method then create a bunch of sprites using this in Game.cs:

for (int i = 1; i < 5; ++i)
{
    Vector2 position = new Vector2(i * 50, i * 50);
    Vector2 direction = new Vector2(i * 10, i * 10);
    Vector2 velocity = new Vector2(10);

    Components.Add(new Bouncer(this, position, direction, velocity, i));

 }

 base.Initialize();

I can draw a rectangle around each one using:

foreach (Bouncer component1 in Components)
{
    Bouncer thing = (Bouncer)component1;
    Rectangle thingRectangle;
    thingRectangle = new Rectangle((int)thing.position.X, (int)thing.position.Y, thing.sprite.Width, thing.sprite.Height);

But now, how do I check for a collision? I can hardly use:

if (thingRectangle.Intersects(thingRectangle))

I should point out I'm a teacher by trade and play with coding to keep my brain from turning to mush. Recently I have been working with Python and with Python I could just put all the sprites into a list:

sprites[];

Then I could simply refer to each as sprite[1] or sprite[2] or whatever its index in the list is. Does XNA have something like this?

Please let me know if any more code needs to be posted.

Thanks, Andrew.


Solution

  • One solution, which I use in my game engine, is to have a Logic code run inside the objects for every game Update, ie. every frame. It seems you already do this, according to the variable names, which indicate you run some physics code in the objects to update their positions.

    You might also want to create the collision rectangle inside the Bouncer's constructor so it's more accessible and you make good use of object oriented programming, maybe even make it an accessor, so you can make it update every time you call it instead of manually updating the bounding/collision box. For example:

    public Rectangle @BoundingBox {
        get { return new Rectangle(_Position.X, _Position.Y, width, height); }
    }
    

    Whichever way works, but the collision checks can be run inside the Bouncer object. You can either make the reference list of the Bouncer objects static or pass it to the objects itself. The code for collisions is very simply:

    foreach(Bouncer bouncer in Components) //Components can be a static List or you can pass it on in the constructor of the Bouncer object
    {
        if (bouncer.BoundingBox.Intersects(this.BoundingBox))
        {
            //they collided
        }
    }