use bounding boxes to optimize collision detection

This commit is contained in:
2025-01-19 14:24:19 +09:00
parent 0a4853aff9
commit e75c0d2944
10 changed files with 275 additions and 94 deletions
+13 -1
View File
@@ -1,10 +1,12 @@
import { Vector2 } from 'three'
import SAT from 'sat'
import SATX from './satx.js'
import Entity from './entity.js'
export default class Projectile {
id = crypto.randomUUID()
after = null // TODO: hide from reports but keep public
bbox = new Float32Array(4)
height = 50
memory = {} // TODO: hide from reports but keep public
onCollide = null // TODO: hide from reports but keep public
@@ -36,6 +38,7 @@ export default class Projectile {
checkCollisions(collider) {
(this.game?.entities ?? []).filter((e) => e.id != this.id).forEach((e) => {
if (this.game == null) { return }
if (e.id == this.owner?.id) { return }
if (SATX.collideObject(collider, e.collider())) {
@@ -54,10 +57,18 @@ export default class Projectile {
update() {
this.#move()
this.#calculateBbox()
if (this.onCollide != null) { this.checkCollisions(this.collider()) }
this.#checkIfArrived()
}
#calculateBbox() {
this.bbox[0] = this.position.y + this.radius
this.bbox[1] = this.position.x + this.radius
this.bbox[2] = this.position.y - this.radius
this.bbox[3] = this.position.x - this.radius
}
#checkIfArrived() {
if (this.destination == null) { return }
if (!this.position.equals(this.destination)) { return }
@@ -82,7 +93,8 @@ export default class Projectile {
this.position.add(step)
}
const tunnel = SATX.entityTunnel(prevPos.x, prevPos.y, this.position.x, this.position.y, this.radius)
// TODO: decouple from entity's tunnel collider
const tunnel = Entity.tunnelCollider(prevPos.x, prevPos.y, this.position.x, this.position.y, this.radius)
if (this.onCollide != null) { this.checkCollisions(tunnel) }
}
}