PuppyKevin
PuppyKevin

Reputation: 3057

Storing a three-dimensional object in a one-dimensional array

Let's say I have a cube, and the cube's dimensions are 16 * 16 * 16, where each dimension starts at index 0, and ends at index 15. Every unit of that cube has to be stored separately; no two units can collide. If I'm trying to store the data in an array such as:

Unit[] units = new Unit[4096];

How would I go about correctly indexing objects? I can't seem to come up with a formula to do it correctly.

Upvotes: 1

Views: 137

Answers (1)

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285405

Just use modular math and think base 16

16 * 16 * z + 16 * y + x.

Upvotes: 4

Related Questions