Sorry for the not too informative title, I couldn't think of anything better. When I say efficient I mean code that isn't cpu intensive.
Imagine a 3D spaceship made out of blocks, like one build in minecraft. I want to store the location of every single block in this shapeship in a self-made Ship class.
For this I was using
private List<ShipBlock> shipBlocks;
Vec3(int x, int y, int z)
Map<Vec3, ShipBlock> shipBlocksMap
public class ShipBlock
public Vec3 position;
public String shipBlockType;
public ShipBlock(Vec3 position, String shipBlockType)
this.shipBlockType = shipBlockType;
public class Ship
private Map<Vec3, ShipBlock> shipBlocksMap;
public Ship(Map<Vec3, ShipBlock> shipBlocksMap)
this.shipBlocksMap = shipBlocksMap;
public ShipBlock containsShipBlock(Vec3 position)
I think the most important design change is to save the blocks' positions relative to the ship's position.
This way, you do not need to change the keys of the blocks when the ship moves, and can easily use your
Map<Vec3, ShipBlock> shipBlocksMap for a constant-time test, where the keys are then the relative positions.
You can get the relative position by computing (conceptually)
relative_block_pos = absolute_block_pos - ship_pos.
That means you need to store the position of the ship.
As one option, you may to simply choose the first block of a ship as the ship's position, thus the first block always has coordinates
(0, 0, 0).
This way, changing the coordinates of all blocks is reduced to a minimum (but should be not required in most cases).