# How to store negative coordinates?

Hey monkeys, me again
I have a galaxy. This galaxy is broken up into sectors. I want to keep some kind of array so I can get the sector at a certain position relative on the sector’s own grid. I could use Arrays and store the coordinates for the sectors like sector = Sectors[12][29][24];, but that only works with positive numbers. How could I store negatives, like [-32][-12][14]? I have thought about using an offset value I can just change as the galaxy expands, then just offset the number I request to get the positive form of it, but I was curious if there was a better way to do this. It seems like something very common, I’ve seen a lot of games which use negative coordinates (Minecraft, Shores of Hazeron, even Mythruna!) Any ideas?
Sorry if this sounds weird. I should be asleep but I’m nutty.

Thanks!

You probably should not use arrays for holding this kind of data. Just slap it in the Map<SectorCoordinate,Sector> and access as you need. Don’t worry about performance at this point - different is going to be minimal and if it proves to be a bottleneck in future, you can always implement your own Matrix<Integer,Integer,Integer->Sector> class in future.

3 Likes

Simple solution one: offset your array.
i.e. 256 items in array. Use index+128 and put 0 in the middle, -128 at 0.

Better solution: use a sparse array.

Simplest implementation is a mapping. Create your own key class (or just use a vector3f) and then use a HashMap to map from key->contents.

If the result if null there is nothing there, if not then there is whatever the map contains there.

Thanks all I’ll try out a Map and see how it goes.

Yeah, don’t use arrays, they are too rigid. Think more like myBlackBox.getDataForCoordinate(x,y,z), this way you can also replace the underlying data model with whatever you like plus you don’t waste memory for locations where there is no data (e.g. space simulations etc. where 99% of the space is empty).

Thanks all, it works perfectly For future readers: Don’t forget equals() and hashCode() if you make your own Vector3Int etc, that caught me offguard.

Actually, at the level of detail of “galaxy sector”, you may want data like star density per subsector, so the “mostly empty” argument doesn’t always hold.

An array might be the endpoint of tuning the implementation. However, what’s far more important is controlling level of detail; no optimization in the world is going to help you if your data model requires petabytes of storage just because you’re generating details in areas that won’t be needed.
At best, an optimized implementation will allow you to store and process minimally more detail. The additional detail may be exactly what you need, or it may be entirely irrelevant in the end; just wait and see whether you really need it, and choose a simple Map-based implementation for now.

Personally, I’d prefer a sorted map over a hashmap because I find it easier to read data dumps when all subelements are sorted, but that’s only relevant if you inspect such dumps.

<cite>@toolforger said:</cite> Actually, at the level of detail of "galaxy sector", you may want data like star density per subsector, so the "mostly empty" argument doesn't always hold.

An array might be the endpoint of tuning the implementation. However, what’s far more important is controlling level of detail; no optimization in the world is going to help you if your data model requires petabytes of storage just because you’re generating details in areas that won’t be needed.
At best, an optimized implementation will allow you to store and process minimally more detail. The additional detail may be exactly what you need, or it may be entirely irrelevant in the end; just wait and see whether you really need it, and choose a simple Map-based implementation for now.

Personally, I’d prefer a sorted map over a hashmap because I find it easier to read data dumps when all subelements are sorted, but that’s only relevant if you inspect such dumps.

Thanks for the info. I’m not planning on any special details in my game, it’s just a little space game I’m experimenting with where players can fly from system to system, start empires, fight, build, etc. Everything is made of voxels. Like you said though I’ll wait and see, and stick with Maps for now. Thanks!

Sorry for not being too clear.
What I meant is that if you’re doing voxel stuff and your largest voxels cover entire sectors, then you need to be careful about how small the subvoxels can get. You’ll want to stop subdividing (adding more detail) before you drown in petabytes of voxels.

<cite>@toolforger said:</cite> Sorry for not being too clear. What I meant is that if you're doing voxel stuff and your largest voxels cover entire sectors, then you need to be careful about how small the subvoxels can get. You'll want to stop subdividing (adding more detail) before you drown in petabytes of voxels.
I understand now. You probably explained it well in your first post, I pulled an all-nighter and might have read it wrong. I don't think my system will consume a lot of data, though I haven't attempted saving yet. In fact I don't even know how to save. That's around the middle of my (mental) priority list at the moment though. Thanks