Game development with Java

Started by
24 comments, last by TheChubu 10 years, 8 months ago

So would a 2D array likely have been better, even though for say [y][x] (as well as the order being somewhat confusing) creates height+1 separate arrays (and then likely requires a temp and memcpy if it ever interacts with native code, e.g. to display it)? In 3D for a block game like minecraft that seems even worse.

If not then I guess that is actually a good reason to suggest building stuff like Minecraft in C.

regardless of language you really should use a single dimensional array and access it linearly to avoid cache misses most of the time (the cost of the extra arithmetics is far smaller than the cost of cache misses for large arrays). in C and C++ 2D array will be identical to a 1D array behind the scenes though and you only have to worry about iterating over it effectivly ([x][y] vs [y][x] depending on which way you're looping for example) making those languages a bit easier to work with for such cases.

in Java a 2D array will not be a single block of memory and thus requires 2 dereferences per access and is far more likely to cause cache misses, since you want to iterate over the datablock linearly anyway regardless of language you should use a 1D array as often as possible (As its most cache friendly) its best to figure out where to start and stop before the loop (then Java can usually do the bounds check just once for the whole loop making it a non issue).

It can be quite a bit easier to write such things well using a language with low level features though. (I wouldn't use C myself as C++ provides more or less the same low level features + a whole bunch of useful high level features anyway)

[size="1"]I don't suffer from insanity, I'm enjoying every minute of it.
The voices in my head may not be real, but they have some good ideas!
Advertisement

I meant C++ smile.png. So basically despite all these benchmarks saying how Java is as good as C/C++, as soon as you need to deal with images, maps, etc with 2D and 3D arrays your kind of sunk in Java, since many algorithms can not work with a single linear scan, because you often need data from all adjacent cells and both [y * width + x] and [y][x] have problems in Java and java an not infer things like "array length = width * height" and the "[y * width + x]" is bounded by width and height?

Wonder what a profile of Minecraft looks like. Even with optimisations like avoiding a hash map to get a chunk reference many of my algorithms (e.g. building the renderer vertex arrays, lighting calculations, things like grass spreading) are pretty much 20% reading block data...

I'm pretty sure the JIT compiler can infer those kind of things. Once I saw a guy who showed the assembly of what the JIT compiler did to an iteration over an ArrayList, it simplified it to an iteration over a primitive array. I remind you that ArrayList is operated by accessor/mutator methods only and that it uses Iterator objects for traversing the array, so it ended up throwing all that indirection away. Bytecode tells you nothing about what will eventually get executed, the only way is to look through what the JIT compiler does to it

It can do other kinds of "magic" too, like inlining virtual method calls, thus no pointer hopping for calling methods of abstract classes or interfaces.

Some VM things are pretty neat, I'd be more worried about memory usage (which is a very documented issue) rather than performance. You can lookup Oracle's articles on object construction in HotSpot VM, which will tell you the approximate costs of doing things in Java instead of native code. IIRC, i think that avg memory costs for allocating Java objects were 30% higher on a 32 bit VM, or even more when 64 bit pointers are used.

The actual allocation of objects is fast (some people even say that its faster than doing a malloc() call) but its the additional overhead of metadata about the objects that creates memory (over)usage issues. Moreover, the GC doesn't likes to work on memory constrained environments, so the memory requirements of your program end up being even bigger than the actual memory usage of the program.

Its just a big pile of tradeoffs really :D

"I AM ZE EMPRAH OPENGL 3.3 THE CORE, I DEMAND FROM THEE ZE SHADERZ AND MATRIXEZ"

My journals: dustArtemis ECS framework and Making a Terrain Generator

So, I should use Java for game development?

Well if you don't plan to exceed Minecraft's scale then I think Java is still the easier / more productive choice, although C++ these days is a lot easier to work with (than before, not Java), still has the learning curve.

On my current project going to have to say most of my problems are not really language, related, but more with designing the algorithms to achieve something,e.g. getting light calculations to work reliably with a chunked world, making use of multiple threads so I can have a larger loaded world, AI etc. So learning about those things applies to nearly any language.

There is the performance aspect, but thats the exceeding Minecraft bit, like supporting a view distance of 500m (256 in Minecraft?), and more persistent stuff like villages that actually do stuff.

So, I should use Java for game development?

Yes. Go and code something.

"I AM ZE EMPRAH OPENGL 3.3 THE CORE, I DEMAND FROM THEE ZE SHADERZ AND MATRIXEZ"

My journals: dustArtemis ECS framework and Making a Terrain Generator

This topic is closed to new replies.

Advertisement