NaiveBroadphase2D

View source

Provides a simple in-memory 2D broadphase for a small number of bodies.

The implementation keeps each EntityHandle beside its PhysicsHitbox and scans every stored entry for each query. It is intended for small collections, such as a scene with fewer than about 100 bodies, where a linear scan is sufficient.

Box and circle queries test the stored bounds against their respective regions. Rebuild the index by calling NaiveBroadphase2D#clear and then adding the current entity bounds with NaiveBroadphase2D#push. The point-query method currently does not perform a point test and returns its target array unchanged.

Examples

import { BoundingBox2D } from '@wimaengine/geometry'
import { World } from '@wimaengine/ecs'
import { NaiveBroadphase2D, PhysicsHitbox } from '@wimaengine/broadphase'

const world = new World()
const entity = world.spawn([])
const broadphase = new NaiveBroadphase2D()

broadphase.push(entity, new PhysicsHitbox(-1, -1, 1, 1))
const candidates = broadphase.queryBox(new BoundingBox2D(-2, -2, 2, 2))

Constructor


constructor()

Methods


queryBox(bound: BoundingBox2D, target?: EntityHandle[]): EntityHandle[]

View source

Finds stored entities whose bounds overlap an axis-aligned box.

Matches are appended to target, so an existing result array can be reused across queries.

Parameters

NameDescriptionDefault
bound

BoundingBox2D region to check.

—
target?

Array to append matching entities to.

[]

Returns

The supplied or newly created result array.

queryCircle(bound: BoundingCircle, target?: EntityHandle[]): EntityHandle[]

View source

Finds stored entities whose bounds overlap a circle.

Matches are appended to target, so an existing result array can be reused across queries.

Parameters

NameDescriptionDefault
bound

BoundingCircle region to check.

—
target?

Array to append matching entities to.

[]

Returns

The supplied or newly created result array.

queryPoint(_point: Vector2, target?: EntityHandle[]): EntityHandle[]

View source

Returns the point-query target without testing stored bounds.

Point matching is not implemented yet. The supplied point is ignored and no entities are appended.

Parameters

NameDescriptionDefault
_point

Point to check; currently unused.

—
target?

Array returned without modification.

[]

Returns

The supplied or newly created target array.

clear(): void

View source

Removes all entities and bounds from the in-memory index.

Call this before pushing the current broadphase entries when rebuilding the index for a new world state.

push(entity: EntityHandle, bound: PhysicsHitbox): void

View source

Adds an entity and its current PhysicsHitbox to the index.

The bound is retained by reference until the index is cleared or the entry is replaced by a new rebuild.

Parameters

NameDescriptionDefault
entity

Entity represented by the bound.

—
bound

Bound stored for the entity.

—