Collision detection

collisiondetection.png

Creating a density map

Sheetengine supports simple collision detecion using a density map. The density map contains points of sheets that are sampled using a value defining the granularity of the density map. To create the density map use the following code

var densityMap = new sheetengine.DensityMap(granularity);
Parameters are:
  • granularity: integer value defining sampling granularity of sheets.
To add sheets to the density map use the following function:

densityMap.addSheets(sheets);
Parameters are:
  • sheets: array of sheets.
This will generate a density map derived from the given sheets with the given granularity.

Using the density map

The following function shows how you can use the density map in practice: for a given point it gives 0 if there is no object or gives a number >0 if there is an object there

var isThereAnything = densityMap.get({x:0,y:0,z:0});
Another handy function to use the map is:

var targetInfo = densityMap.getTargetPoint(targetp, direction, objHeight, maxClimb);
This function will return where an object will be moved given the direction of motion and target location. Parameters are:
  • targetp: point where we are trying to move an object. JSON with x,y,z keys.
  • direction: direction of object. JSON with x,y,z keys.
  • objHeight: height of the object. Numeric value.
  • maxClimb: the maximum number of pixels/units the object is allowed to climb up. Numeric value.
The getTargetPoint will return a targetInfo object of the following keys:
  • allowMove: describes if the character can move forward or not.
  • targetp: will be the resulting point corrected with the density map object detection.
  • movex, movey: will be the resulting move vector in x-y direction: they can be 0 if the character hit an object.
  • stopFall: indicates if the character hit the ground so it cannot fall further.
Example:

var targetInfo = densityMap.getTargetPoint(character.centerp, {x:dx, y:dy, z:jumpspeed}, 20, 10);
var allowMove = targetInfo.allowMove;
var targetp = targetInfo.targetp;
				
var moved = 
  targetp.x != character.centerp.x || 
  targetp.y != character.centerp.y || 
  targetp.z != character.centerp.z;
				  
if (moved && allowMove) {
  character.setPosition(targetp);
}

Dynamic changes in the density map

The above methods are adequate if you have a static environment where only your character can change its position. However, if you need to handle objects that can move around and also need to be included in the density map you will need to change the map according to the movement of such objects. You can use a combination of the removeSheet and addSheet functions.
Example:

var sheet = obj.sheets[0];

// remove sheet from the density map
densityMap.removeSheet(sheet);

// rotate sheet
obj.rotateSheet(sheet, {x:0,y:0,z:0}, {x:0,y:0,z:1}, -Math.PI/32);

// add sheet to the density map
densityMap.addSheet(sheet);

Skipping the density map

After an object has been created manually using the SheetObject constructor, calling addSheets on the DensityMap object passing sheetengine.sheets will also put the object into the density map among all other sheets:

var myobj = new sheetengine.SheetObject(centerp, rot, [sheet1,sheet2]);
densityMap.sheetengine.addSheets(sheetengine.sheets); 
// sheets of myobj is also included in the density map
This behavior can be switched off using the setCollision function on an object:

obj.setCollision(false);
You can switch it off on individual sheets, too:

sheet.skipDensityMap = true;

Last edited Dec 11, 2012 at 9:50 PM by dobsonl, version 3

Comments

damdirt May 5, 2013 at 11:01 AM 
"densityMap.sheetengine.addSheets(sheetengine.sheets);"
You wanted to say densityMap.addSheets(sheetengine.sheets); isn't it ?