add unoptimized pathfinding

This commit is contained in:
2024-12-25 00:32:33 +09:00
parent 47aade7b3f
commit 05360208b0
8 changed files with 353 additions and 37 deletions
+34 -2
View File
@@ -9,30 +9,58 @@ export default class Terrain {
#colliders = []
#vertices = []
#unadjustedWaypoints = []
constructor(vertices) {
this.#vertices = vertices.map((v) => new Vector2(v.x, v.y))
if (ShapeUtils.isClockWise(this.#vertices)) {
this.#vertices.reverse()
}
this.#calculateColliders()
this.#calculatePosition()
this.#calculateRelativeVertices()
this.#calculateUnadjustedWaypoints()
}
get colliders() { return this.#colliders }
get unadjustedWaypoints() { return this.#unadjustedWaypoints }
get vertices() { return this.#vertices }
static waypointsForSide(fromVertex, toVertex, isClockwise = false) {
const from = isClockwise ? toVertex : fromVertex
const to = isClockwise ? fromVertex : toVertex
const origin = new Vector2()
const sideNormal = to.clone().sub(from).clone().normalize()
const margin = sideNormal.clone().rotateAround(origin, -3 * Math.PI / 4)
const offset = margin.clone().multiplyScalar(Math.SQRT2)
const inverseMargin = sideNormal.clone().negate().rotateAround(origin, 3 * Math.PI / 4)
const inverseOffset = inverseMargin.clone().multiplyScalar(Math.SQRT2)
return [
[margin.clone().add(from), offset],
[inverseMargin.clone().add(to), inverseOffset],
]
}
state() {
return {
...this,
}
}
#calculateColliders() {
#shape() {
const complexShape = new Shape()
complexShape.moveTo(this.#vertices.at(0).x, this.#vertices.at(0).y)
this.#vertices.slice(1).forEach((v) => complexShape.lineTo(v.x, v.y))
const points = complexShape.extractPoints(16)
return complexShape
}
#calculateColliders() {
const points = this.#shape().extractPoints(16)
const indicesToPolygon = (indices) => {
const satPoints = [
@@ -54,4 +82,8 @@ export default class Terrain {
#calculateRelativeVertices() {
this.relativeVertices = this.#vertices.map((v) => v.clone().sub(this.position))
}
#calculateUnadjustedWaypoints() {
this.#unadjustedWaypoints = this.#vertices.map((v, i, arr) => Terrain.waypointsForSide(v, i + 1 < arr.length ? arr[i + 1] : arr[0])).flat()
}
}