hitbox
tiny helper functions for handling axis-aligned bounding boxes
If you remember that one article on collision detection written by the good folks over at Metanet Software, you may recall that we can model an axis-aligned bounding box by its central position and its halfsize vectors. If we were to translate this definition over to JavaScript, it might look something like this:
halfsize: 8 12 position: 128 128
Notice the use of array vectors, which enables us to further extend this definition into higher dimensions.
halfsize: 10 20 30 position: 100 200 150
hitbox
provides flexible helper functions to enable easier reading and manipulation of these kinds of data structures in two and three dimensions. For example, the code snippet below employs generous use of these functions in order to simplify collision detection.
for let block of worldblocks if if dx < 0 actorvelocity0 = 0 else if dx > 0 actorvelocity0 = 0 if dy < 0 actorvelocity1 = 0 else if dy > 0 actorvelocity1 = 0
usage
left(hitbox[, x])
Finds the x-coordinate of the left edge of the hitbox, or sets it if x
is provided.
=== hitboxposition0 - hitboxhalfsize0
right(hitbox[, x])
Finds the x-coordinate of the right edge of the hitbox, or sets it if x
is provided.
=== hitboxposition0 + hitboxhalfsize0
top(hitbox[, y])
Finds the y-coordinate of the top of the hitbox, or sets it if y
is provided.
=== hitboxposition1 - hitboxhalfsize1
bottom(hitbox[, y])
Finds the y-coordinate of the bottom of the hitbox, or sets it if y
is provided.
=== hitboxposition1 + hitboxhalfsize1
back(hitbox[, z])
Finds the z-coordinate of the back of the hitbox, or sets it if z
is provided.
=== hitboxposition2 - hitboxhalfsize2
front(hitbox[, z])
Finds the z-coordinate of the front of the hitbox, or sets it if z
is provided.
=== hitboxposition2 + hitboxhalfsize2
intersects2D(a, b)
Determines if hitbox a
and hitbox b
are intersecting in two dimensions.
if actorhealth-- // ouch!
contains2D(a, b)
Determines if hitbox a
contains hitbox b
in two dimensions.
for let actor of worldactors if
intersects3D(a, b)
Determines if hitbox a
and hitbox b
are intersecting in three dimensions.
if actorhealth-- // ouch but in 3D!
contains3D(a, b)
Determines if hitbox a
contains hitbox b
in three dimensions.
if ! && ! // oh no