3 ms·
Back in the early 80's a common thing to do in games on 8 bit computers was to implement sin and cos as lookup tables with the angles being 0-255 or 0-128 or so
by smcameron 4y ago
Back in the early 80's a common thing to do in games on 8 bit computers was to implement sin and cos as lookup tables with the angles being 0-255 or 0-128 or something like that and the result also an integer that was some fixed point representation, so you'd do something like:
vx = (cos[angle] * speed) >> 8;
vy = (sin[angle] * speed) >> 8;
- kennywinker 4y agoOuch. Less than 1 degree of accuracy? Well, I guess if it’s good enough for Asteroid, it’s good enough for me
- londons_explore 4y agoYou can always do one cycle of newton-raphson to get nearly double the accuracy with only one extra lookup-multiply-add.
- kennywinker 4y agoGreat, now I'm banging my brain against newton-raphson trying to understand it
- LukeShu 4y agoThe MOS 6502 didn't have a multiply.
- newobj 4y agoPico8 is 128x18 res, so I think the maximum number of visibly unique lines you could draw from one point is 512. You can't even express sub-one-degree (sub-1.422222 really) rotations on Pico8 visually without subsampling etc.
- kennywinker 4y agoInteresting. I couldn't imagine asteroids without at least 1 degree accuracy feeling right, but I just fired up a pico8 emulation of asteroids and it had what seemed like about 24 steps of rotation, and played beautifully. Funny how my memory gave it way higher resolution. https://sazazel.itch.io/pico-8-asteroids https://sazazel.itch.io/pico-8-asteroids
- lupire 4y agoThe objects you are shooting at have large angular width.
- aaaaaaaaaaab 4y agoThe sin/cos lookup tables would only contain an octant of the circle and use various symmetries to map other values to that range. Thus the resolution is ~0.176 degrees for a 256-entry table. sin(x) = -sin(-x) = sin(pi - x) = cos(pi/2 - x) cos(x) = cos(-x) = -cos(pi - x) = sin(pi/2 - x)
- LukeShu 4y agoThe limit on precision wasn't the size of the lookup table, it was the size of the integers. The CPU's integers were only 8-bits. So if you're using native integers (i.e.: [0,256) == [0°,360°) ), you can't get angles more precise than 1.4°.
- LukeShu 4y agoThe original Asteroids source code has been released, we can look at what it did! (Also, even before that the EPROM images had been disassembled, but looking at the original sources is more fun.) It indeed used (-128, 128) for (-180°,180°) For instance, here it is using 64 (written as "40", because hexadecimal) for "PI/2": COS: CLC ;COS(A)=SIN(A+PI/2) ADC I,40 ; JMP SIN It implements COS in terms of SIN, and SIN wraps everything to be in the domain [0,64] (that is [0°,90°] ) then retrieves that from the lookup table. code: https://github.com/historicalsource/asteroids/blob/main/A35131.1A#L2411-L2438 https://github.com/historicalsource/asteroids/blob/main/A351... table: https://github.com/historicalsource/asteroids/blob/main/A35127.XX#L567-L575 https://github.com/historicalsource/asteroids/blob/main/A351...