15,507,372 members
3.00/5 (1 vote)
See more:
Hello Codeproject,

I am currently adding boxes to my game which will act as a collision system. This works fine except that I'm having problems with arrays. Here is my code:

C#
```/* Clear the bounding boxes. */
BoundingBoxes.Clear();

/* Loop through every block. */
for (int i = 0; i < ChunkWidth; i++)
{
for (int j = 0; j < ChunkHeight; j++)
{
/* Create a new rectangle. */
Tangle = new Rectangle(i * 32, j * 32, 32, 32);

/* Add the rectangle. */
if(Blocks[i,j].Material != BlockMaterial.Material_Air)
}
}```

So, ChunkWidth and Height are both 20. So you would expect them to run up to 19 and then stop. And 19 * 32 = 608, however in my BoundingBoxes there is no single rectangle that has the X set to 608, and there are only 18 rectangles.

Which I've been thinking about, and it's normal, assuming that it starts at 0 instead of 1, but in my code I have both 0 and 19 in my array list, but since it never reaches 19 inside the list I can't have the cooardinate 608 and my collision fails. Any help?

Edit:
Here is the list when debugging:
Quote:
- BoundingBoxes Count = 19 System.Collections.Generic.List<microsoft.xna.framework.rectangle>
+ [0] {X:0 Y:32 Width:32 Height:32} Microsoft.Xna.Framework.Rectangle
+ [1] {X:32 Y:64 Width:32 Height:32} Microsoft.Xna.Framework.Rectangle
+ [2] {X:64 Y:96 Width:32 Height:32} Microsoft.Xna.Framework.Rectangle
+ [3] {X:96 Y:128 Width:32 Height:32} Microsoft.Xna.Framework.Rectangle
+ [4] {X:128 Y:160 Width:32 Height:32} Microsoft.Xna.Framework.Rectangle
+ [5] {X:160 Y:192 Width:32 Height:32} Microsoft.Xna.Framework.Rectangle
+ [6] {X:192 Y:224 Width:32 Height:32} Microsoft.Xna.Framework.Rectangle
+ [7] {X:224 Y:256 Width:32 Height:32} Microsoft.Xna.Framework.Rectangle
+ [8] {X:256 Y:288 Width:32 Height:32} Microsoft.Xna.Framework.Rectangle
+ [9] {X:288 Y:320 Width:32 Height:32} Microsoft.Xna.Framework.Rectangle
+ [10] {X:320 Y:352 Width:32 Height:32} Microsoft.Xna.Framework.Rectangle
+ [11] {X:352 Y:384 Width:32 Height:32} Microsoft.Xna.Framework.Rectangle
+ [12] {X:384 Y:416 Width:32 Height:32} Microsoft.Xna.Framework.Rectangle
+ [13] {X:416 Y:448 Width:32 Height:32} Microsoft.Xna.Framework.Rectangle
+ [14] {X:448 Y:480 Width:32 Height:32} Microsoft.Xna.Framework.Rectangle
+ [15] {X:480 Y:512 Width:32 Height:32} Microsoft.Xna.Framework.Rectangle
+ [16] {X:512 Y:544 Width:32 Height:32} Microsoft.Xna.Framework.Rectangle
+ [17] {X:544 Y:576 Width:32 Height:32} Microsoft.Xna.Framework.Rectangle
+ [18] {X:576 Y:608 Width:32 Height:32} Microsoft.Xna.Framework.Rectangle

As you can see there is no [19], which makes sense. But I need a [19] but when I for instance make the for loop "<=" it goes out of bounds with checking what type of block it is, that's normal too.

Edit:Edit:

After thinking about it again, it makes no sense that there is no 19th object as that 0 and 19 are both in the for loop. Any suggestions?
Posted
Updated 16-Jun-13 2:16am
v3
Surendra Adhikari SA 16-Jun-13 7:03am
your loop is fine . what this line is for : if(Blocks[i,j].Material != BlockMaterial.Material_Air) ?
Deviant Sapphire 16-Jun-13 7:06am
Basically,

If the block is not air,
we can collide with it(for instance dirt, grass).
Deviant Sapphire 16-Jun-13 7:16am
But after thinking about it, it can't stop at 18. Since 0 is in the for loop and 19 too. I'm so confused.

## Solution 1

If you are not getting a rectangle added to the BoundingBoxes which has an X of 608, then you need to look at your Blocks array, because that is the only thing stopping a rectangle being added when i*32 == 608, or i == 608 / 32, or i == 19 given that you have checked ChunkWidth and are sure it is 20.

Put an "else" clause in there:
C#
```if(Blocks[i,j].Material != BlockMaterial.Material_Air)
else
{
}```
and put a breakpoint on the '{' - that will confirm it when the debugger hits it and allow you to check the array content.

You probably should be using Blocks.GetLength(0) and Blocks.GetLength(1) instead of ChunkWidth and ChunkHeight, or at least checking that they are within the limits of the Blocks array.

Deviant Sapphire 16-Jun-13 7:21am
Yes, true. I have just commented the if statement and it appeared to have added boxes at 608. Well, I'll look further into the if statement, thanks a lot for the help! :)
OriginalGriff 16-Jun-13 7:23am
You're welcome!
Monjurul Habib 16-Jun-13 15:57pm
5+

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Top Experts
Last 24hrsThis month
 Richard MacCutchan 68 Dave Kreskowiak 35 OriginalGriff 30 merano99 30 Graeme_Grant 10
 OriginalGriff 498 Richard MacCutchan 473 Richard Deeming 313 Dave Kreskowiak 145 CPallini 105

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900