views:

280

answers:

2

It would be easy enough to make my own system for this, but I'm figuring that the .NET library, being as massive as it is, probably already has a class made for this very purpose.

For hit detection, my VB.NET game uses two 2D arrays that store the state of each pixel in the level. Each element in these arrays is a Byte (or, more accurately, a value of a flag enumeration type based on Byte).

Public TiHD(,) As HitDT ''//State of each pixel in the level based on
                        ''// its underlying tile.
Public SpHD(,) As HitDT ''//State of each pixel in the level based on
                        ''// sprite occupation.

<Flags> _
Public Enum HitDT As Byte
    Solid = 1
    Danger = 2
    Water = 4
    Healing = 8
    Restore = 16
    AreaExit = 32
End Enum

I would enjoy being able to just give each sprite & tile its own 2D array of hit detection data (defining which pixels of the object are solid, dangerous, etc) that can be directly written to TiHD or SpHD at a specified offset.

I'm willing to bet that .NET has a class that can do this for me.
Presumably I would just have to pass TiHD or SpHD to the constructor, and then I would be able to use the object to indirectly modify the array with extended functionality (similar to using a Graphics object to modify a Bitmap). Is there such a class?

+1  A: 

Have you tried using Buffer.BlockCopy?

Jon Skeet
Buffer.BlockCopy could simplify the task of creating my own system for writing one 2D array onto another, but there's two problems: I'd still have to iterate through each row, and even then I don't think it could be used on my 2D array. I would have to use a jagged array so that I can input a row as an array.
Exp HP
Just an FYI, if you ever find that lookup speed is causing an issue in your 2D arrays, jagged (or 'ragged') array lookup speed is almost twice as fast as 2D array lookup involves a method call.
Ed Swangren
Thanks for pointing that out, Ed. Upped you.
Exp HP
A: 

Array.Copy works (sorry this is C#):

int[,] i1 = new int[10, 10];
int[,] i2 = new int[10, 10];
Array.Copy(i1, i2, i1.GetLength(0) * i1.GetLength(1));

Update: never mind, this doesn't work like I thought. Array.Copy apparently treats a 2D array as one big (dimension1 X dimension2) array, so using Array.Copy with an offset doesn't work like you need. You're right, you'd need to use this or BlockCopy on a row by row basis.

Update 2: this is a total hack suggestion (and probably more of a pain than just writing your own row-by-row Array.Copy or BlockCopy code), but instead of using 2D byte[,] arrays to store this info, you could just use actual Bitmap objects for this, where you interpret the 32-bit values of each pixel as your enum instead of using them as actual Colors. Then you could just use Graphics.DrawImage to copy the smaller arrays into the bigger one.

You could even make these Bitmaps use Format4bppIndexed, on the theory (I'm not standing by this one) that it would use only 2 bytes per pixel instead of 4. I have no idea if that's true or not.

MusiGenesis
Sorry, but it doesn't quite work for this purpose because where I'm using it the destination array is larger than the source array. Since rows in the destination array are much wider than in the source array, it will copy basically the entire source array element by element into a single contiguous line in the destination array rather than in a neat rectangle.
Exp HP
Oops, you already made a note of that. I really should refresh the page before I post a comment.
Exp HP
@Exp HP: you're totally right, I just updated my answer after testing that scenario. Kind of annoying that Array.Copy handles 2D arrays like that.
MusiGenesis