Java: What is a good data structure for storing a coordinate map for an infinite game world?

前端 未结 11 1318
心在旅途
心在旅途 2020-12-23 11:35

I am used to coding in PHP but I am not really proficient with Java and this has been a problem for some time now. I expect it to be a fairly easy solution, however I cannot

11条回答
  •  感动是毒
    2020-12-23 11:55

    I'm not an expert in game programming, but if arrays are OK, you could simply translate your coordinates from (-x, +x) to (0, 2x) (idem for the y axis).

    Or if you're used to associative arrays like PHP has, the use the corresponding structure in Java, which is a Map (HashMap would be OK) : define a Coordinate class with appropriate equals and hashCode methods, and use a HashMap. Making Coordinate immutable makes the code more robust, and allows caching the hashCode.

提交回复
热议问题