refactor: aabbFromPoints

This commit is contained in:
cha0s 2024-11-16 06:32:21 -06:00
parent bfe9e427b9
commit 5aa17441a1
2 changed files with 21 additions and 13 deletions

View File

@ -53,6 +53,23 @@ export const HALF_PI = Math.PI / 2;
export const TAU = Math.PI * 2;
export const PI_180 = Math.PI / 180;
export function aabbFromPoints(points) {
let x0 = Infinity, x1 = -Infinity, y0 = Infinity, y1 = -Infinity;
for (const point of points) {
const {x, y} = point;
if (x < x0) x0 = x;
if (x > x1) x1 = x;
if (y < y0) y0 = y;
if (y > y1) y1 = y;
}
return {
x0: x0 > x1 ? x1 : x0,
x1: x0 > x1 ? x0 : x1,
y0: y0 > y1 ? y1 : y0,
y1: y0 > y1 ? y0 : y1,
};
}
export function bresenham({x: x1, y: y1}, {x: x2, y: y2}) {
const points = [];
let x; let y; let px; let py; let xe; let ye;

View File

@ -1,5 +1,5 @@
import {aabbFromPoints, distance, intersects, transform} from '@/lib/math.js';
import Component from '@/silphius/ecs/component.js';
import {distance, intersects, transform} from '@/lib/math.js';
import vector2d from './helpers/vector-2d';
@ -237,24 +237,15 @@ export default class Collider extends Component {
direction = 0;
}
for (const body of bodies) {
let x0 = Infinity, x1 = -Infinity, y0 = Infinity, y1 = -Infinity;
for (const point of transform(body.points, {rotation: direction})) {
const points = transform(body.points, {rotation: direction});
this.$$aabbs.push(aabbFromPoints(points));
for (const point of points) {
const {x, y} = point;
if (x < x0) x0 = x;
if (x < this.$$aabb.x0) this.$$aabb.x0 = x;
if (x > x1) x1 = x;
if (x > this.$$aabb.x1) this.$$aabb.x1 = x;
if (y < y0) y0 = y;
if (y < this.$$aabb.y0) this.$$aabb.y0 = y;
if (y > y1) y1 = y;
if (y > this.$$aabb.y1) this.$$aabb.y1 = y;
}
this.$$aabbs.push({
x0: x0 > x1 ? x1 : x0,
x1: x0 > x1 ? x0 : x1,
y0: y0 > y1 ? y1 : y0,
y1: y0 > y1 ? y0 : y1,
});
}
}
}