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

patroljs

v0.1.0

Published

Navigation mesh toolkit for ThreeJS

Downloads

21

Readme

PatrolJS

Navigation mesh toolkit for ThreeJS

PatrolJS helps your AI agents navigate around your world. It uses the A* and Funnel algorithms to calculate a path on a navigation mesh.

Introduction

Traditionally games and 3D apps used waypoints to help their AI agents navigate. This is bad and has a lot of problems, but is generally easier to implement than navigation meshes. Navmeshes are far more accurate, faster and take into account the size of the AI agent (e.g. tanks require move space to maneuver than soldiers).

For a thorough introduction to Navigation mesh pathfinding, see AI Blog's article Fixing Pathfinding Once and For All

Demo

Demo alt text

Usage inside Ironbane MMO

Installation

PatrolJS can be used on both the client and the server.

You need ThreeJS and Underscore in order to use this library. On the client, PatrolJS will assume these are availabe on the window object. On the server, just install this library using npm install patroljs. Afterwards just include it using var patrol = require('patroljs').

Usage

Before we get started, you need to understand a few stages of PatrolJS's navigation mesh building and planning.

PatrolJS currently does not build navigation meshes for you. I've tried but failed horribly. If you have knowledge of the matter and would like to help, please send a PR!

Fortunately there is a great tool to assist us with navigation meshes, namely Blender. See this video (6:08) on how to generate your own navmesh. After saving it as OBJ, you have to convert the file to ThreeJS's JSON format.

PatrolJS only accepts raw JSON model data. You must take care of loading the JSON file yourself.

Loading a navmesh

jsonLoader.load( 'meshes/level.nav.js', function( geometry, materials ) {
	var zoneNodes = patrol.buildNodes(geometry);

	patrol.setZoneData('level', zoneNodes);

	// Set the player's navigation mesh group
	playerNavMeshGroup = patrol.getGroup('level', player.position);
}, null);

First, you need to build nodes from the navigation mesh using buildNodes(geometry). These nodes are then saved in memory by passing them to PatrolJS using setZoneData(levelName, nodes).

Every navigation mesh consists out of many groups of polygons. This is because, you can't always calculate a path from everywhere to everywhere. A path can only be constructed between points that lie on one group of polygons.

A level may only contain one group of polygons, but in practice these are usually more. The demo scene has around 50 groups for example. This is because Blender tries to bake the top surfaces of obstacles like crates and other inaccessible areas as well. You may remove these areas yourself for an optimization, if you wish, before converting the file to JSON for use with PatrolJS.

When calculating a path, PatrolJS needs to know what group of polygons you are searching in. In your game or app, you should precalculate this position using getGroup(levelName, position) and store it for when you want to calculate a path later.

// Calculate a path to the target
calculatedPath = patrol.findPath(player.position, target.position, 'level', playerNavMeshGroup);

Finally you can calculate a path using findPath(beginPosition, targetPosition, levelName, group). This returns an array of THREE.Vector3. Simply go to these positions in order in order to complete your path. See the demo code for an example.

Thanks to

License

MIT