npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

hitbox

v3.0.0

Published

tiny helper functions for handling axis-aligned bounding boxes

Downloads

10

Readme

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 world.blocks) {
  if (intersects2D(actor.hitbox, block.hitbox)) {
    if (dx < 0) {
      left(actor.hitbox, right(block.hitbox))
      actor.velocity[0] = 0
    } else if (dx > 0) {
      right(actor.hitbox, left(block.hitbox))
      actor.velocity[0] = 0
    }
    if (dy < 0) {
      top(actor.hitbox, bottom(block.hitbox))
      actor.velocity[1] = 0
    } else if (dy > 0) {
      bottom(actor.hitbox, top(block.hitbox))
      actor.velocity[1] = 0
    }
  }
}

usage

npm badge

left(hitbox[, x])

Finds the x-coordinate of the left edge of the hitbox, or sets it if x is provided.

left(hitbox) === hitbox.position[0] - hitbox.halfsize[0]

right(hitbox[, x])

Finds the x-coordinate of the right edge of the hitbox, or sets it if x is provided.

right(hitbox) === hitbox.position[0] + hitbox.halfsize[0]

top(hitbox[, y])

Finds the y-coordinate of the top of the hitbox, or sets it if y is provided.

top(hitbox) === hitbox.position[1] - hitbox.halfsize[1]

bottom(hitbox[, y])

Finds the y-coordinate of the bottom of the hitbox, or sets it if y is provided.

bottom(hitbox) === hitbox.position[1] + hitbox.halfsize[1]

back(hitbox[, z])

Finds the z-coordinate of the back of the hitbox, or sets it if z is provided.

back(hitbox) === hitbox.position[2] - hitbox.halfsize[2]

front(hitbox[, z])

Finds the z-coordinate of the front of the hitbox, or sets it if z is provided.

front(hitbox) === hitbox.position[2] + hitbox.halfsize[2]

intersects2D(a, b)

Determines if hitbox a and hitbox b are intersecting in two dimensions.

if (intersects2D(actor.hitbox, enemy.hitbox)) {
  actor.health-- // ouch!
}

contains2D(a, b)

Determines if hitbox a contains hitbox b in two dimensions.

for (let actor of world.actors) {
  if (contains2D(viewport, actor.hitbox)) {
    draw(actor)
  }
}

intersects3D(a, b)

Determines if hitbox a and hitbox b are intersecting in three dimensions.

if (intersects3D(actor.hitbox, enemy.hitbox)) {
  actor.health-- // ouch but in 3D!
}

contains3D(a, b)

Determines if hitbox a contains hitbox b in three dimensions.

if (!contains3D(package, product) && !intersects3D(package, product)) {
  // oh no
}