jumpstorm/engine/utils/rotateVector.ts

16 lines
392 B
TypeScript
Raw Normal View History

2023-08-25 18:48:17 -04:00
import type { Coord2D } from '../interfaces';
2023-07-19 23:38:24 -04:00
/**
* ([[cos(θ), -sin(θ),]) ([x,)
* ([sin(θ), cos(θ)] ]) ( y])
*/
export const rotateVector = (vector: Coord2D, theta: number): Coord2D => {
const rads = (theta * Math.PI) / 180;
const [cos, sin] = [Math.cos(rads), Math.sin(rads)];
return {
x: vector.x * cos - vector.y * sin,
2023-08-25 18:48:17 -04:00
y: vector.x * sin + vector.y * cos
2023-07-19 23:38:24 -04:00
};
};