Hacker News new | ask | show | jobs
by tgandrews 4944 days ago
Can anyone explain how this works? I never have got to grips with 3D.
3 comments

It looks like he's using a matrix to create cartesian coordinates in a model-view space and then performing some basic linear algebra on the points to animate it. For a primer on 3D worlds check out http://learningwebgl.com/blog/?page_id=1217 where they go over it using WebGL.
Looks like he's ray casting it, no 3D code implemented, just the illusion itself.
Well... actually the code contains a minimal 3D engine, it is not an illusion, you have the observer variable and could use it to navigate inside the map created in the initialization function. What is more 3D than that? :-)
There is no explicit geometry, polygons, or GPU acceleration, but raycasting in 3D is certainly 3D.
Update: Thanks for the repliers. You are absolutely right: this is a 3D rendering.

What I meant to say is that it is using Canvas 2D, instead of Canvas 3D, also known as WebGl. Antirez gave a wonderful explanation above[1]:

And indeed it is pretty impressive how fast the rendering happens given that the code operates at such lower level... Even selecting the color of every pixel requires non trivial work in the inner loop

[1] http://news.ycombinator.com/item?id=4862910

I appreciate it is written onto a 2d plain. But it does look like 3D. This appearance of looking like 3D when a screen is 2D is complicated and very mathsy (not a word - I know). I've tried reading about it before but it is not straight forward.
It is relatively straightforward (at least conceptually) - you're just projecting 3D vectors down onto a 2D plane (your screen)[1]. Conceptually, this is just the bread and butter of vector algebra (first year physics undergrad).

[1] Of course, it gets more complicated if you want to be more realistic, like including a field of view (cone) rather than a screen-sized chunk (prism) of 3D objects, and so on, but these are just details.

In that sense, all games are rendering in 2D, because screens are 2D. They just do it in a much more complicated way.

What I'm seeing in this demo is 3D in every sense, the code is basically a very simple 3D engine, and it requires knowledge of computer graphics to understand it, so the top-level commenter here asked a valid question.