Giter Site home page Giter Site logo

bhex.js's Introduction

BHex.js

JavaScript library for working with Hexagons and grids. This library does not draw grids for you, but simply sets it up for you to use your own prefered way of drawing.

BHex.Core.js

BHex.Core.js is, as the name suggests, the core library for creation and calculation of a Hexagon grid. All grids are circular, with point 0,0 being the center of the grid.

var grid = new BHex.Grid(3);

This will give you a new cricular grid with radius of 3 (excluding the center piece), giving you a total of 37 hexagons (including center piece).

New grid

Interia / Blocked hexagons

BHex.js also supports inertia (or movement speed/cost) and blocked hexagons, defined by cost on your hexagon. By default all hexagons have a cost of 1 and no hexagon should never have a cost lower than 1. Floating values are also supported, as long as they are higher than 1. Blocked hexagons are defined by the blocked variable on your hexagon.

More examples

Get hexagon

Simply returns a hexagon at a specific position.

hex = grid.getHexAt(new BHex.Axial(0, 0));

Neighbors

Get the neighboring hexagons. This does not care if a hexagon is blocked or not.

Neighbors

grid.getNeighbors(new BHex.Axial(0, 0));
grid.getNeighbors(hex);

A hexagon inherits from BHex.Axial, so whenever you should pass a BHex.Axial you can also just pass in a hexagon like above.

Range

Note: Requires BinaryHeap.js

Range

Get all the hexagons available within a specific range. The hexagon in the first argument is not included in this result.

grid.getRange(new BHex.Axial(0, 0), 2);

Path finding

Note: Requires BinaryHeap.js

Path finding

Find the shortest path from A to B using the A* algorithm.

grid.findPath(new BHex.Axial(-3, 2), new BHex.Axial(3, -2));

Distance

Get the distance between two hexagons, ignoring if a hexagon is inertia or blocked.

grid.getDistance(new BHex.Axial(-3, 2), new BHex.Axial(3, -2));

To get the distance with inertia and blocked hexagons taken into account use grid.findPath(start, end).length instead.

Line of sight

Line of sight

Gets all the hexagons between 2 points until blocked. (To get field of view you could use this by checking against all the outer hexagons on the grid.)

grid.getLine(new BHex.Axial(-3, 2), new BHex.Axial(3, -2));

BHex.Drawing.js

BHex.Drawing.js will help you prepare your grid for drawing and help you with operations regarding your grid such as finding the hexagon at a specific cursor position.

// Setup options: Size of the a single side, orientation (or PointyTop) and where the center of the grid should be.
var options = new BHex.Drawing.Options(30, BHex.Drawing.Static.Orientation.FlatTop, new BHex.Drawing.Point(400, 300));

// Create the grid as before.
var grid = new BHex.Grid(3).forDrawing(options);

Each hexagons in your grid will now have a center and an array of 6 corners (points) which you can use for drawing your hexagon grid. Note that all of these points are relative to the center of your grid (400x300 in the above example)!

More examples

Get a hexagon at a specific pixel position, such as the position of your cursor.

grid.getHexAtPoint(BHex.Drawing.Point(50, 75));

BinaryHeap.js

This helper library is an implementation by bgrins, based on an implementation by Marijn Haverbeke. It's needed for the algorithms used in grid.getRange and grid.findPath.

Credits

All the algorithms are based on Amit Patel's blogpost on Hexagonal Grids, if you're interested in the theory behind hexagons, be sure to check that out. Also check out his posts Introduction to A* and Implementation of A* to get more into detail on pathfinding (grid.getRange and grid.findPath). Both of those posts are based on rectangular grids, but the theory is the same for hexagonal grids.

bhex.js's People

Contributors

bodinaren avatar

Stargazers

Fabián avatar James B. Pollack, MFA  avatar Michal Kaliszewski avatar Maximilian Ascari avatar Aitor P. avatar Viktor Brech avatar Ben Stern avatar  avatar Aaron Murray avatar Keith Hall avatar  avatar Cristi Adam avatar  avatar Pavel Kuznetsov avatar  avatar Alexandre B A Villares avatar Paul avatar David Clearman avatar  avatar John McNeil avatar Jakub Pawlowski avatar Eric Wong avatar Sam Clements avatar Yannick Lohse avatar § avatar Tasos Protopapas avatar drewnix avatar Lucas Costa Oliveira avatar Alexander Freeman avatar Stefan Matsson avatar

Watchers

Cristi Adam avatar James Cloos avatar  avatar Paul avatar  avatar

bhex.js's Issues

Rescoring doesn't seem to work properly

I've been recently working on a game which was based on using the A* algorithm for pathfinding.
The game is online so the server also needs to calculate the position of each object in the scene, thing which will throttle my CPU up to 100%. So far I noticed that the "cost" property on each hex won't get rescored for some reason, thing which is the main cause why this is happening since I noticed that this is most likely Dijkstra's algorithm instead of A* due to the massive search in all directions.

Is there anything that i'm missing here which is responsible for making the plugin work as expected ?

Can't set up for drawing

When I attempt to use "var grid = new BHex.Grid(3).forDrawing(options);" as documented I get the error "sketch.js:10 Uncaught TypeError: (intermediate value).forDrawing is not a function" and indeed I cannot find that function in the library.

I am new to JS so I'm probably missing something, but so far i haven't been able to figure out what...
Also, I am using P5. When I use the regular grid create it all works fine and I am able to access the grid members. Code below:


var grid = [];

function setup() {
createCanvas(windowWidth, windowHeight);
// Setup options: Size of the a single side, orientation (or PointyTop) and where the center of the grid should be.
var options = new BHex.Drawing.Options(30, BHex.Drawing.Static.Orientation.FlatTop, new BHex.Drawing.Point(width / 2, height / 2));
// Create the grid as before.
grid = new BHex.Grid(3).forDrawing(options); // This fails
//grid = new BHex.Grid(3); - This works
}

function draw() {
background(100);
}


Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.