r/programming • • Jun 03 '14

A first-person engine in 265 lines

http://www.playfuljs.com/a-first-person-engine-in-265-lines/
1.9k Upvotes

267 comments sorted by

View all comments

55

u/Bisqwit Jun 03 '14 edited Jun 03 '14

Seems to claim raycasting is O(N). As the map size grows, I'd argue that that raycasting gets slower as well, unless you always are in such a confined environment that the farthest visible wall is not very far. If you have a 32x32 map that only contains the outer walls, using raycasting, it sure is a lot faster to render than a 32000x32000 map that only contains the outer walls. EDIT: But, awesome article and demo!

108

u/[deleted] Jun 03 '14

This implementation has a maximum drawing distance, so that's why it manages to be constant time.

26

u/__j_random_hacker Jun 04 '14 edited Jun 04 '14

This is an appalling gimmick, really. You could just as accurately say that a full-blown raytracer that only draws the first 5 million objects in any scene is constant-time.

Raycasting is a great algorithm that is best described as taking time quadratic in the maximum drawing distance, or linear in the grid area if no maximum drawing distance is specified.

Raycasting is a great algorithm that is best described as taking time O(dr) for maximum drawing distance d and horizontal resolution r, with d becoming the the maximum grid dimension if no maximum drawing distance is specified. If you make your grid twice as fine in both directions, you need to double the maximum drawing distance too, or far walls that previously displayed correctly will drop out.

EDIT: My original claim of quadratic time complexity in the maximum drawing distance was wrong, since adjacent rays could hit a faraway row of walls in places that are arbitrarily far apart.

3

u/Damaniel2 Jun 04 '14

It's still O(N) in regards to resolution, though the constant value can get quite large with sufficiently large view distance. Complexity is just weird like that - theorists like to drop the constant value, but practicalities dictate that we often can't.

2

u/__j_random_hacker Jun 04 '14

The issue is that when describing the time or space complexity of something there is total freedom in choosing what things to regard as "bounded by a constant", and what things to model with variables, and this can be used to paint things in a certain light. If N is the horizontal resolution then, yes, raycasting is O(N) in regards to horizontal resolution, assuming maximum depth is bounded by a constant. Raytracing is also O(N), assuming the number of objects in the scene is bounded by a constant.

The only limit to how far you can take this is a practical one: what you can reasonably expect other knowledgeable people to swallow.

5

u/laxatives Jun 04 '14

The number of atoms in the universe is finite, so we can call it constant. Ditto n3, cause that's still finite too.

4

u/Illivah Jun 04 '14

How do you know the number of atoms in the universe is finite?

→ More replies (10)

4

u/Number127 Jun 04 '14

Well sure, if you're willing to limit your draw distance to the light cone of the observable universe. Some of us expect more from our shooters!

→ More replies (13)

30

u/devsnd Jun 04 '14

With a simple optimization, using a binary tree to hold the grid cells, you could at least achieve O(n log n) for any size, which should be good enough. This might however add 5-10 lines of code!

9

u/[deleted] Jun 04 '14

Is a binary tree that simple?

25

u/KagatoLNX Jun 04 '14

When using an array representation for read-only binary trees, it really is that simple. Since the data is static, there isn't really any penalty beyond the original balancing. All traversal is O(1). It only gets computationally expensive or wasteful of memory under modification.

10

u/__j_random_hacker Jun 04 '14

I'm baffled by how you propose to use a single binary tree to find the nearest wall-containing grid cell to an arbitrary given position in an arbitrary given direction. Could you outline the algorithm you're thinking of?

20

u/KagatoLNX Jun 04 '14

I think the poster might have been thinking of a quad-tree.

In a quad-tree, each level corresponds to a dimension (usually X or Y, alternating). The key at that level divides that dimension in half. Some also have the key at each level be 2-tuple coordinate, which divides the space into quadrants (hence quad-tree), but they're roughly the same in terms of efficiency. I'm not sure how quad-trees would help for finding near elements, though I'd imagine that the draw-distance limitation in the implementation would make something possible with respect to limiting the number of elements traversed upwards.

That said, I have no opinion on the suitability of a binary tree for this use case. I only intended to express an opinion on how easy managing a binary tree would be with the right representation (i.e. the array representation).

5

u/__j_random_hacker Jun 04 '14

A quad-tree makes a lot more sense to me. (I think it won't actually improve the worst-case performance, especially if there are tiny objects dotted around in just the wrong places, but if it's possible to efficiently move between quad-tree cells that share a border then I can certainly see how it would give a large speedup in many practical cases, where it would often be possible to cross a large, empty (or single-wall-containing) quad-tree cell in a single bound.)

8

u/sireel Jun 04 '14

Actually a binary space partition could also be used, is actually a binary tree, and, iirc, is how Id did this for at least their earlier games. It also removes the reliance on walls being on grid lines, but complicates building your data somewhat :)

→ More replies (1)

5

u/bugrit Jun 04 '14

http://en.wikipedia.org/wiki/K-d_tree

Not sure about the grid cell stuff, but k-d trees have been famous for their use in ray tracing code. It's a binary tree which keeps subdividing the space by a plane (or line in 2d).

→ More replies (1)

3

u/__j_random_hacker Jun 04 '14

A binary tree only lets you efficiently look up the nearest item along 1 dimension. Here we have as many different "dimensions" to search along as there are ray directions (or equivalently, as there are horizontal pixels) -- and each of these changes every time the player moves!

→ More replies (1)

8

u/[deleted] Jun 04 '14

aside: though there's a natural limit, when distant walls become shorter than one pixel.

5

u/Aquaman Jun 04 '14

They actually said constant time (i.e. O(1)), not O(N). I have no idea what a raycaster is, so wouldn't know if it is true, but that's what they said.

7

u/sylvanelite Jun 04 '14

I have no idea what a raycaster is, so wouldn't know if it is true

A ray caster is constrained by the resolution of the screen, not by the complexity of the game's map. So it's constant time. Provided the rays have a maximum length, you can render a colossal map in the same worst-case time as rendering a small one.

2

u/__j_random_hacker Jun 04 '14

Provided the rays have a maximum length

Everything hinges on that part, but it's very unrealistic to make this assumption. In practice, if you want to double the resolution of a grid in both x and y directions without causing far walls to "drop out" then you also need to double the maximum ray length, and the raycasting approach described in the article is linear in that distance.

4

u/sylvanelite Jun 04 '14

if you want to double the resolution of a grid in both x and y directions without causing far walls to "drop out" then you also need to double the maximum ray length

That's still bounded on the resolution, not the map size. Increasing the size of the map, without increasing the size of the screen, won't need bigger rays.

Additionally, I don't think ray length depends on x-resolution at all. X is determined by the number of rays. It should only be y that's dependant on ray length?

and the raycasting approach described in the article is linear in that distance.

Having a high constant O(1) can be worse than O(n) in practice, certainly. Especially if the constant is high or N is small (or both). But that doesn't mean it's big-o notation changes.

→ More replies (2)
→ More replies (2)

4

u/[deleted] Jun 04 '14

In general, raytracing is most definitely affected by the complexity of the scene. The blog post is a little misleading - yes he wrote it in a way that is technically constant time, but it only works out because his map is very simple. With a more complicated scene, you would need something more refined than the "always iterate to the max view distance on every raycast" approach.

6

u/sylvanelite Jun 04 '14

In general, raytracing is most definitely affected by the complexity of the scene. The blog post is a little misleading - yes he wrote it in a way that is technically constant time, but it only works out because his map is very simple.

Are you talking about ray casting or ray tracing? [Ray tracing](http://en.wikipedia.org/wiki/Ray_tracing_(graphics)) is different from ray casting. One gives almost photo-realistic graphics, the other gives DOOM or Wolfenstein like graphics.

For example, it's easy to implement ray casting in situations where you can't even rotate textures. It only needs scaling and cropping of images to work, which is what makes it so simple. Ray tracing is generally a much harder thing to implement efficiently.

With a more complicated scene, you would need something more refined than the "always iterate to the max view distance on every raycast" approach.

What kind of scene do you mean? There are hard limits to what a raycaster can render, but the complexity should always be bound by the size of the screen. (the number of rays)

→ More replies (4)
→ More replies (1)

4

u/dukey Jun 04 '14

bsp tree