5 ms·
My favourite way of handling angles was always with either unsigned char or 16bit unsigned int that was treated as 1/nth of turn. Usually in these cases cos/sin
by flowerbreeze 4y ago
My favourite way of handling angles was always with either unsigned char or 16bit unsigned int that was treated as 1/nth of turn. Usually in these cases cos/sin tables were pre-calculated for speed, although that need went away to an extent. As long as as the calculations wrap around on the underlying system, it makes angles much easier to manage, because angle1 + angle2 = angle3 is always within 0 to 255 or 0 to 65535. Unfortunately I mostly work with higher level languages now that have mostly dropped integer types.
If anybody knows how similar calculations can be easily achieved in JS for example, I'd love to hear about it. I'm sure there must be a better way than boundary checks and manual wrap-around.
- Klaster_1 4y agoFor overflowing integers in JS, you can use single-value long typed arrays. I just checked with an Uint16Array and it wraps back to 0 after 65536.
- flowerbreeze 4y agoThank you! This works great as well for my purposes: > a = new Uint16Array(1) Uint16Array(1) [ 0 ] > a[0] 0 > a[0] = a[0] + 65537 65537 > a[0] 1
- vardump 4y ago> If anybody knows how similar calculations can be easily achieved in JS for example Simply "a = (a + 0x1234) & 0xffffffff;". Or whatever width you require, 0xff or 0xffff. JIT is going to optimize that and-operation away (at least for 32-bit mask 0xffffffff) and keep the integer value internally. You can also "cast" a var to int by "ORring" 0 with it, like "a |= 0;"
- flowerbreeze 4y agoThank you! This does exactly what I meant. I think this is the best solution for my use-cases. It even handles floating point operations to a correctly, something I didn't expect.
- codeflo 4y agoIn JavaScript, I’d stay with floating-point (don’t fight the language if you don’t have to) and use something like x => x - Math.floor(x) to normalize.
- deleted 4y ago[deleted]
- xeonmc 4y agoGod please no, Unreal Engine 3 forced that approach which is why games derived from that engine all had wonky mouse inputs
- rjmunro 4y agoWonky mouse inputs? Why? For the same number of bits, an integer representation of angle is always going to be more precise than a floating point one for angles away from 0. It's also going to be equally precise for the whole circle.
- deleted 4y ago[deleted]
- gernb 4y agoindex = angle1 + angle2 | 0 Will keep things an integer. The | requires casting to int, the 0 makes it a no-op after the cast. This is from asm.js which had to emulate integers so they looked through what it would take http://asmjs.org/spec/latest/ http://asmjs.org/spec/latest/