- Home /
How could I store negative coordinates with negative indexes with C#?
Hey!
I'm looking for somekind of 2 dimensional "table" solution (with C#) which's size could start from negative indexes. For example From -3,-3 to 3,3. And this table should be able to store one value, most likely a string and I'd like to easily access offset values with iterators. Dynamic size would be a plus but not must.
I have this randomly generated world consisting of blocks, similar to Minecraft/Cubeworld/Trove etc, and I'd like to save my chunks with zero point of 0,0.
Currently my game stores the chunks in a normal 2 dimensional array, for example size of 100, so the zero point of the game would be 49,49 but this causes a bit too much hassle with all the other code.
So are there any kind of storing solutions supporting negative indexes?
Answer by Agemennon · Apr 24, 2014 at 10:04 PM
If you use a data structure like a Dictionary (in System.Collections.Generic), you can index chunks by their two dimensional coordinates (or rather, the hash of that value).
To my knowledge, this is the solution that Minecraft uses for indexing their chunks.
That sounds promising but would you $$anonymous$$d clarifying what do you mean by hashing the coordinates? A quick look at dictionaries told that it's using keys for stored values and in my case those keys should be the coordinates. So could I for example have a table as they key? Like map[2][2] as a key which stores value X?
A Dictionary organizes items based on the hash of the key.
Usage would look something like:
chunkDict[new Vector2(indexX, indexY)].Foo();
You may want to look at writing your own Vector2 class that uses integer values in order to avoid floating point issues, but otherwise that should work.
Your answer
Follow this Question
Related Questions
Javascript array: Negative Indexes? 2 Answers
Indexing lists based on positions 2 Answers
Multi-Arrays help 0 Answers
Transferring variables between JavaScript and C# 0 Answers
NullReference 1 Answer