44 lines
721 B
TypeScript
44 lines
721 B
TypeScript
import { copyVector, type Vector } from "#/engine/Vector.ts";
|
|
|
|
export type AABB = {
|
|
position: Vector;
|
|
width: number;
|
|
height: number;
|
|
};
|
|
|
|
export function createAABB(
|
|
position: Vector,
|
|
width: number,
|
|
height: number,
|
|
): AABB {
|
|
return {
|
|
position,
|
|
width,
|
|
height,
|
|
};
|
|
}
|
|
|
|
export function fromMinMax(min: Vector, max: Vector): AABB {
|
|
return {
|
|
position: copyVector(min),
|
|
width: max.x - min.x,
|
|
height: max.y - min.y,
|
|
};
|
|
}
|
|
|
|
export function drawAABB(
|
|
ctx: CanvasRenderingContext2D,
|
|
aabb: AABB,
|
|
color: string = "#fff",
|
|
): void {
|
|
ctx.save();
|
|
ctx.strokeStyle = color;
|
|
ctx.strokeRect(
|
|
Math.round(aabb.position.x) - 0.5,
|
|
Math.round(aabb.position.y) - 0.5,
|
|
aabb.width,
|
|
aabb.height,
|
|
);
|
|
ctx.restore();
|
|
}
|