This commit is contained in:
cha0s 2021-03-20 02:24:35 -05:00
parent d3a0c2a21d
commit c9962637ed
4 changed files with 19 additions and 20 deletions

View File

@ -1,14 +1,14 @@
import {TWO_PI} from '@avocado/math';
import {TAU} from '@avocado/math';
import {Packet} from '@latus/socket';
export default () => class TraitUpdateVisiblePacket extends Packet {
static pack(data) {
const rotation = ((data.rotation % TWO_PI) + TWO_PI) % TWO_PI;
const rotation = ((data.rotation % TAU) + TAU) % TAU;
return {
isVisible: data.isVisible,
opacity: Math.floor(data.opacity * 255),
rotation: Math.floor((rotation / TWO_PI) * 65536),
rotation: Math.floor((rotation / TAU) * 65536),
};
}
@ -24,7 +24,7 @@ export default () => class TraitUpdateVisiblePacket extends Packet {
return {
isVisible: data.isVisible,
opacity: data.opacity / 255,
rotation: (data.rotation / 65536) * TWO_PI,
rotation: (data.rotation / 65536) * TAU,
};
}

View File

@ -46,7 +46,7 @@ export const SQRT_2_2 = Math.sqrt(2) / 2;
export const EIGHTH_PI = Math.PI / 8;
export const QUARTER_PI = Math.PI / 4;
export const HALF_PI = Math.PI / 2;
export const TWO_PI = Math.PI * 2;
export const TAU = Math.PI * 2;
export const PI_180 = Math.PI / 180;
export const add = (l, r) => l + r;
export const div = (l, r) => l / r;
@ -142,7 +142,7 @@ export function children() {
],
},
sub: binaryOp('subtract'),
TWO_PI: {
TAU: {
type: 'number',
},
};

View File

@ -3,7 +3,7 @@ import {
SQRT_2_2,
EIGHTH_PI,
QUARTER_PI,
TWO_PI,
TAU,
} from '../math';
// export function {VectorMixin as Mixin} from './mixin'
@ -301,15 +301,14 @@ export function toDirection8(v) {
if (0 === rad) {
return 1;
}
rad = (TWO_PI + rad) % TWO_PI;
rad = (TAU + rad) % TAU;
// Truncate.
rad = Math.floor(rad * 100000) / 100000;
let stepStart = TWO_PI - EIGHTH_PI;
// Clockwise direction map.
const directions = [1, 5, 2, 6, 3, 7, 0, 4];
let stepStart = TAU - EIGHTH_PI;
const directions = [1, 4, 0, 7, 3, 6, 2, 5];
for (let i = 0; i < directions.length; i++) {
const direction = directions[i];
let stepEnd = (stepStart + QUARTER_PI) % TWO_PI;
let stepEnd = (stepStart + QUARTER_PI) % TAU;
stepEnd = Math.floor(stepEnd * 100000) / 100000;
if (rad >= stepStart && rad < stepEnd) {
return direction;

View File

@ -1,6 +1,6 @@
import {expect} from 'chai';
import {QUARTER_PI} from '../src/math';
import {QUARTER_PI, TAU} from '../src/math';
import * as Vector from '../src/vector';
describe('Vector', () => {
@ -72,13 +72,13 @@ describe('Vector', () => {
});
it('can calculate angle', () => {
expect(Vector.toRadians([1, 0])).to.be.closeTo(0, 0.0001);
expect(Vector.toRadians([1, 1])).to.be.closeTo(1 * QUARTER_PI, 0.0001);
expect(Vector.toRadians([0, 1])).to.be.closeTo(2 * QUARTER_PI, 0.0001);
expect(Vector.toRadians([-1, 1])).to.be.closeTo(3 * QUARTER_PI, 0.0001);
expect(Vector.toRadians([-1, 0])).to.be.closeTo(4 * QUARTER_PI, 0.0001);
expect(Vector.toRadians([-1, -1])).to.be.closeTo(5 * QUARTER_PI, 0.0001);
expect(Vector.toRadians([0, -1])).to.be.closeTo(6 * QUARTER_PI, 0.0001);
expect(Vector.toRadians([1, -1])).to.be.closeTo(7 * QUARTER_PI, 0.0001);
expect(Vector.toRadians([1, 1])).to.be.closeTo(TAU - 1 * QUARTER_PI, 0.0001);
expect(Vector.toRadians([0, 1])).to.be.closeTo(TAU - 2 * QUARTER_PI, 0.0001);
expect(Vector.toRadians([-1, 1])).to.be.closeTo(TAU - 3 * QUARTER_PI, 0.0001);
expect(Vector.toRadians([-1, 0])).to.be.closeTo(TAU - 4 * QUARTER_PI, 0.0001);
expect(Vector.toRadians([-1, -1])).to.be.closeTo(TAU - 5 * QUARTER_PI, 0.0001);
expect(Vector.toRadians([0, -1])).to.be.closeTo(TAU - 6 * QUARTER_PI, 0.0001);
expect(Vector.toRadians([1, -1])).to.be.closeTo(TAU - 7 * QUARTER_PI, 0.0001);
});
it('can convert to/from directions', () => {
expect(Vector.toDirection4([0, 1])).to.equal(2);