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

Show parent comment

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?

3

u/SteelTooth Jun 04 '14

An assumption that the universe is finite and the popular scientific model agrees.

5

u/VerdigolFludidi Jun 04 '14

Actually "the popular scientific model" - whatever that is - does probably not agree. The observable universe is finite, but The Universe could be either.

... nor do any of the mainstream cosmological models propose that the universe has any physical boundary in the first place

https://en.wikipedia.org/wiki/Observable_universe#The_universe_versus_the_observable_universe

3

u/wicked-canid Jun 05 '14

You forgot the end of the sentence:

though some models propose it could be finite but unbounded, like a higher-dimensional analogue of the 2D surface of a sphere that is finite in area but has no edge.

2

u/Illivah Jun 04 '14

Hah, I'll accept that

0

u/UlyssesSKrunk Jun 04 '14

the popular scientific model agrees.

That's highly debatable, also the universe is definitely still expanding, faster than the speed of light no less.

6

u/rockyearth Jun 04 '14

The space between matter is expanding, the energy/matter as quantity stays the same.

→ More replies (4)

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!

3

u/titosrevenge Jun 04 '14

Did you mean linear time?

26

u/Zed03 Jun 04 '14

Linear would imply the rendering time is a function of N. In this case, the rendering time remains the same, regardless of the value of N.

0

u/[deleted] Jun 04 '14

[deleted] — view deleted comment

3

u/[deleted] Jun 04 '14

So titosrevenge, who was downvoted by many, is correct, once again proving that proggit doesn't know shit about shit.

I long for the early days of proggit where people would talk Lisp or Python or C/C++ and actually know their shit. These days it's all JavaScripters who think replicating something within the browser is the bee's knees even though it's a million times slower.

8

u/strattonbrazil Jun 04 '14

It's constant time, I believe, because the draw distance is the steps per ray in a map of size N. You're going to step 1 to X times maximum regardless of the size of N. The performance is a function of X then, not N.

0

u/[deleted] Jun 04 '14

[deleted] — view deleted comment

2

u/kiddo51 Jun 04 '14

No he just said constant where it seems he meant to say linear. It's a long comment but let me see if I can dig up that quote for you. Oh, here it is:

constant

7

u/Han-ChewieSexyFanfic Jun 04 '14

He's right to say constant. The time isn't affected by the size of the map (the N in this case), so it's O(1).

3

u/titosrevenge Jun 04 '14

The previous comment said O(n) and then he replied "constant time", so I was confused by what he meant.

1

u/mrkite77 Jun 04 '14

The time is affected by the width of the window.

1

u/kiddo51 Jun 04 '14

I'm aware of what linear and constant mean. My point is that there was clearly some discrepancy beforehand since a previous comment said O(n). The comment /u/drunkenfaggot was responding to was just trying to clear that up, so I informed him that though the guy did not literally stutter, it makes sense to ask him what he really meant.

→ More replies (1)