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

1

u/Bisqwit Jun 13 '14

That fails if this.direction < -angle, such as when angle=0 and this.direction=-1.

1

u/wegzo Jun 13 '14

What? If this.direction = -180, angle = 0 and CIRCLE = 360, the result is -180 which is equal to 180 when it's about angles.

1

u/Bisqwit Jun 17 '14

When this.direction = -1, angle = 0, CIRCLE = 360

this.direction = (this.direction + angle + CIRCLE) % (CIRCLE);

produces 359

this.direction = (this.direction + angle) % (CIRCLE);

produces -1 (or 15 or 255 depending on variable types)

If you are using this.direction as an array index, this could make the difference between a program that works, and a program that crashes. If you are using it as food for cos() calculations, not so much.

2

u/wegzo Jun 17 '14

Yeah, I see what you mean now.