Java Raycaster - need help with performance.

Recently I’ve been working on a Java Raycaster (an applet), and have come across the obvious fact that Java2D is generally too slow for this kind of task (at least when at 640x480).
I was firstly using stretched images for each ‘wall slice’, but then converted to a memoryimagesource as i planned to implement floors and ceilings.
Of course, this all came to a grinding halt when i was greeted with 2fps when close to a wall on an Athlon XP 2000 :(.

Here are some screenshots of what I managed:


I’ve been doing much searching in regards to better 2D performance (in the area of DirectX, OpenGL etc, but generally tutorials for these in 2D in Java are scarce), but to no avail.

I’m hoping that someone can lead me in the direction of the best way to create a high performance 2D game in Java, and where I can find tutorials for whatever methods they suggest.

I hope to not inconvenience anyone with my ‘amateur’ question, but I’m really in need of the help :(.

Thanks for your time in advance,
§oulßlighter

MemoryImageSource isn’t the fastest way of doing per-pixel rendering.

in 1.1 the fastest way is with the ImageProducer interface.

in 1.2+ its with BufferedImage and getDataBuffer

Thanks for that - one prob though.

I’m looking for something that would speed the rendering up a LOT (ie. directX or openGL).

I’ve been taking a look into the open GL APIs (LWJGL, JOGL, etc), and there’s one thing that stops me dead - I can’t find any good beginner tutorials on them! (well i did find one, but it extends a BaseWindow class that is ‘supposedly’ under java.awt, but actually isn’t as far as I can find, so it won’t compile…)
They’re all code samples, and I find it’s generally not so easy to learn by looking at code when you’ve never coded it before :(.

So I’m hoping you could direct me to some good tutorials on these, especially LWJGL.

Also, if these are not the best approach for 2D, please suggest better options.

Switching to something GL based won’t help you much, you’ll still have to do all the same raycasting in software and actually displaying the image won’t magically speed up. To get proper advantage of GL you’ll want to be throwing your textured polys at it direct, but they you won’t be raycasting anymore :o

Theres plenty of old skool raycasting articles around, so maybe see if theres any algorithm improvements you can make.

If you want to move to GL proper, then LWJGL and Jogl both have example apps in their distributions, and NeHe is usually a good place to start for the basics.

You don’t need ‘2D performance’ here. Using OpenGL or D3D will certainly accelerate 2D games that rely on sprites and blitting, but will not aid you in plotting pixels to the frame buffer (which is what raycasting is all about). The problem, I think, lies in the technique you are using.

There’s a decent (albeit old) tutorial on Java raycasting here: http://www.permadi.com/tutorial/raycast/.

There’s a couple of old books out there devoted to raycasting which you might be able to find in a bargain bin or perhaps Ebay (maybe even Amazon):

Gardens of Imagination by Cristopher Lampton (ISBN:1-878739-59-X)
Amazing 3-D Games: Adventure Set by Larry L. Myers (ISBN: 1-883577-15-2)

This one has a chapter regarding raycasting:

Tricks of the Game Programming Gurus by Andre LaMothe, John Ratcliff, Mark Seminatore, and Denise Tyler (ISBN: ISBN 0-672-30507-0)

If you are dead set on making a rasycaster, you should probably be putting the pixels on the screen yourself, rather than going through Java2D. As mentioned above, ImageProducer or BufferedImage are the way to go for that. Java Game Programmin for Dummies, by Wayne Holder and Doug Bell has an example of using Image Producer (as might the Permadi tutorial above).

At any rate, no hardware-centric API can assist you in true raycasting. There are some techniques to do something similar using 3D APIs such as OpenGL, but that’s not real raycasting, and pointless nonetheless. If you are going to go that route might as well take the plunge to full 3D.

[quote] Tricks of the Game Programming Gurus by Andre LaMothe, John Ratcliff, Mark Seminatore, and Denise Tyler (ISBN: ISBN 0-672-30507-0)
[/quote]
If you don’t have this book on your shelf, get it! This book is the best source of gaming information in existance (IMHO).

Are you sure Java2D is the main bottleneck? 2fps seems way too slow even for java2d.
Implementing your own ImageProducer instead of using MemoryImageSource will, if you’re lucky, give you 3fps (if not even less) instead of 2fps.
Did you profile your code?