This site requires JavaScript, please enable it in your browser!
Greenfoot back

Report as inappropriate.

RcCookie
RcCookie presents ...

2021/10/28

2D Raytracing

This is a demo of a sort of "2D Raytracing" engine. The world you see is NOT ACTUALLY 3D but a 2d world where the height of a wall is determined by how far you are away from it. Unlike traditional rendering engines ("rasterizers") who use triangles to represent objects and basically just draw these trianges back to front onto an image, my engine uses a technique called "Raytracing".

Raytracing works by calculating the path of a light ray for each pixel of the screen. You check what object the ray collides with and the color of that object determines the color of that pixel. The advantage of raytracing over rasterizing is that reflections can be calculated very easily by just calculating another ray from the point where the first ray hit the object, in the direction it would reflect. This is also present in my implementation, BUT ONLY OFFLINE, because online you cannot draw with transparency (for whatever reason).

My implementation works a bit different to normal raytracing implementations; instead of calculating a ray for each pixel I only calculate a ray for each column of pixels. The further away the hit point of the ray is, the smaller is the wall in that column.

Of course, this means that you cannot move vertically or "look above" a wall, but many very old games actually had this restriction as well (for different reasons). On the other hand, raytracing is, compared to rasterizing, very performance heavy, so it's probably a good idea to reduce the number of rays per second from 600p x 400p x 60fps = 14400000 to 600p x 60fps = 36000. After all, raycasts are normally calculated on the GPU which is very good at doing many simple things at once, rather than the CPU, which can only do very few things concurrently. Since Java runs on the CPU and you won't change that so easily that kills performance for these kind of things. My engine makes use of all of your CPU cores (off you are in the Greenfoot application, online that stuff is not possible) but even powerful CPUs like mine can only do 12-16 things at once whereas even low-end GPUs can do more than a 1000 things at once.

Unfortunately, the JavaScript conversion for the browser and the browser itself take a ton of performance away, so that I had to reduce resulution and render scale a lot to get any kind of framerate. HOWEVER, in the GREENFOOT APPLICATION this can run about 50 times faster (seriously!) with your normal 600x400 resolutions and reflections. So, please try downloading it and running it on native Java! Also, thanks for reading all the way until down here, so that the 20 minutes of typing weren't completly for nothing! Have fun!

FOR THE TIME BEING YOU WILL HAVE TO DOWNLOAD THE SCENARIO ANYWAYS AS THE JSVASCRIPT CONVERTER I COMPLAINING AGAIN. At some point I may fix this but not now:)

757 views / 550 in the last 7 days

1 vote | 1 in the last 7 days

Tags: mouse game demo with-source graphics ray rendering raytracing render reflection

open in greenfoot

There is no HTML 5 translation of this scenario available.
View legacy version (requires Java plugin)

This scenarios uses Java features that are not available for use after conversion to HTML 5. Please try the legacy version, which requires the Java plugin to be installed.

A new version of this scenario was uploaded on 2021-10-28 16:29:30 UTC
A new version of this scenario was uploaded on 2021-10-28 16:31:15 UTC
A new version of this scenario was uploaded on 2021-10-28 16:36:13 UTC

Want to leave a comment? You must first log in.

Who likes this?

Greenfoot_Admin