Fork me on GitHub

Glazz


A JavaScript library that models vision in a 2D world.

A sonar rotates, shooting out a single beam. If the beam hits a circle, the circle is momentarily marked.

Learn more about lookTo().
A floodlight rotates. If any circles are caught in its span, they are lit up.

Learn more about lookAcross().
Intelligence feeds coordinates of new circle sightings to a spotter. The spotter sights the coordinates. If they see a circle, they momentarily mark it.

Learn more about lookAt().

Can I see an example?

I require SolidityMap. I instantiate it, passing in the width and height of your world. This initialises every cell to 0, meaning empty.

  var SolidityMap = require('src/solidityMap').SolidityMap;
  var sm = new SolidityMap(10, 10);

I define an Obj constructor that includes getPosition(), getSize() and getMaterial() accessors. I use it to instantiate the actor, which is the object that will have eyes.

  var Obj = function(posX, posY, sizeX, sizeY) {
    this.pos = { x:posX, y:posY };
    this.size = { x:sizeX, y:sizeY };

    this.getPosition = function() {
      return this.pos;
    };

    this.getSize = function() {
      return this.size;
    };

    this.getMaterial = function() {
      return 1;
    };
  }

  var actor = new Obj(1, 1, 1, 1);

I give the actor eyes.

  var eyes = new Eyes(actor, sm);

I instantiate an object for the actor to look at. I put it in the solidity map.

  var obj = new Obj(3, 3, 1, 1);
  sm.updateObj(obj);

I make the actor look at the object. I see that he sees.

  var lookEndpoint = eyes.lookAt(obj);
  console.log(lookEndpoint.obj === obj); // => true

The object moves. I update the solidity map with its new position.

  obj.pos = { x:4, y:4 };
  sm.clearMaterial(obj.getMaterial());
  sm.updateObj(obj);

Get the code

npm

  $ npm install glazz

git

  $ git clone git://github.com/maryrosecook/glazz.git

Reference

Solidity Map

A solidity map holds a two-dimensional array that represents the world. It has one cell for each point that can be occupied. Each cell contains a number that represents the substance occupying that point.

new SolidityMap(width, height)

width: the width of the solidity map.
height: the height of the solidity map.

clearMaterial(material)

material: integer representing material to clear from sm.

Sets to 0 all solidity map cells containing this material. This is often invoked before calling updateObj() to update the solidity map with the new positions of objects composed of the passed material.

updateObj(obj)

obj: the object for which to update solidity map

Sets the cells covered by this object to the material that represents the object. Object must implement getPosition(), getSize() and getMaterial().

update(coordinates, dimensions, material)

coordinates: object with x and y attributes representing top-left of area to update.
dimensions: object with x and y attributes representing dimensions of area to update.
material: integer representing the material covering the cells.

Sets the cells covered to the material passed.

get(coordinates)

coordinates: object with x and y attributes representing coordinates of cell to get.
returns: material of cell at coordinate.

getArea(coordinates, dimensions)

coordinates: object with x and y attributes representing top-left of area to get.
dimensions: object with x and y attributes representing dimensions of area to get.
returns: two-dimensional array of cells covered by area.

print([tilesize])

tilesize (optional): integer size of tiles in the world that the solidity map represents.
returns: array of cells in solidity map.

Prints out and returns a grid representing the solidity map. If supplied, tilesize indicates that cells in blocks of tilesize are homogoneous and may be consolidated into a single array entry when printed.

Eyes

new Eyes(actor, sm, [overlook])

actor: object to give eyes.
sm: solidity map representing the world.
overlook (optional): array of materials that should be overlooked by default when raycasting.

Constructor takes an actor to see for and a solidity map to look across. overlook can be specified as a default set of materials to overlook when raycasting.

  var eyes = new Eyes(obj, sm, [1, 7, 9]);

lookAt(obj, [overlook])

obj: object to look at.
overlook (optional): array of materials that should be overlooked when raycasting. Overrides default overlook.
returns: Endpoint object.

Shoots a ray at the passed object. Returns an Endpoint object that represents the point at which the ray encountered an impassable cell. Returns undefined if the ray hits nothing. This might happen if the object is somehow outside the solidity map.

lookTo(vector, [overlook])

vector: direction to look in.
overlook (optional): array of materials that should be overlooked when raycasting. Overrides default overlook.
returns: Endpoint object.

Shoots a ray in the passed direction. Returns an Endpoint object that represents the point at which the ray encountered an impassable cell. Returns undefined if the ray hits nothing before going outside the solidity map.

lookAcross(objs, facing, span, [overlook])

objs: objects to survey.
facing: angle in degrees of centre of gaze.
span: angle in degrees of left to right span of gaze.
returns: Endpoint object.

Filters out all the passed objects that do not fall in the passed span. Calls lookAt for each remaining object and returns corresponding Endpoint objects.

Endpoint

Represents the place a cast ray hit.

point: coordinates at which the ray stopped.
lineOfSight: array of points along the cast ray.
material: material at the point at which the ray stopped.

Licence

The code is open source, under the MIT licence.