disable collision to fix pathfinding phasing through walls

This commit is contained in:
2024-12-25 03:55:32 +09:00
parent 3bb34ed012
commit f48a6bf9aa
4 changed files with 71 additions and 28 deletions
+18 -9
View File
@@ -2,6 +2,7 @@ import { Vector2 } from 'three'
import SAT from 'sat'
import SATX from './satx.js'
import Pathfind from './pathfind.js'
import Terrain from './terrain.js'
export default class Entity {
id = crypto.randomUUID()
@@ -94,13 +95,18 @@ export default class Entity {
if (this.#path.length < 1 || !this.#path.at(-1).equals(this.#dest)) {
console.time('pathfinding')
console.time('waypoints')
const waypoints = (this.game?.unadjustedWaypoints.map(([unadjusted, direction]) => unadjusted.clone().add(direction.clone().multiplyScalar(this.radius))) ?? []).concat([this.position, fixedDest])
const extraWaypoints = [[this.position, new Vector2()], [fixedDest, new Vector2()]]
const waypoints = (this.game?.unadjustedWaypoints ?? []).concat(extraWaypoints)
console.timeEnd('waypoints')
console.time('graph')
const graph = Pathfind.buildGraph(waypoints, this.collidables(), this.radius)
console.timeEnd('graph')
console.time('extraEntries')
const extraGraphEntries = Pathfind.buildGraph(waypoints, this.collidables(), extraWaypoints)
console.timeEnd('extraEntries')
console.time('unadjustedGraph')
const unadjustedGraph = (this.game?.waypointGraph ?? []).concat(extraGraphEntries)
console.timeEnd('unadjustedGraph')
console.time('path')
this.#path = Pathfind.shortestPath(graph, this.position, fixedDest)
this.#path = Pathfind.shortestPath(unadjustedGraph, this.position, fixedDest).map(([unadjusted, direction]) => unadjusted.clone().add(direction.clone().multiplyScalar(this.radius)))
console.log(this.#path)
console.timeEnd('path')
console.timeEnd('pathfinding')
}
@@ -113,12 +119,15 @@ export default class Entity {
const position = distance <= speed ? destination : stepTaken
const collider = Entity.collider(position.x, position.y, this.radius)
const isColliding = SATX.collideObjects(collider, this.collidables())
// TODO: fix collisions while pathfinding
// const isColliding = SATX.collideObjects(collider, this.collidables())
if (!isColliding) {
this.position.copy(position)
}
// if (!isColliding) {
// this.position.copy(position)
// }
this.position.copy(position)
if (this.position.equals(destination)) {
this.#path = this.#path.slice(1)
if (this.#path.length > 0) {