Shaku

Shaku JS

Back To Table of Content

Collision World

CollisionWorld

A collision world is a set of collision shapes that interact with each other. You can use different collision worlds to represent different levels or different parts of your game world.

Kind: global class

new CollisionWorld(resolver, gridCellSize)

Create the collision world.

Param Type Description
resolver CollisionResolver Collision resolver to use for this world.
gridCellSize Number | Vector2 For optimize collision testing, the collision world is divided into a collision grid. This param determine the grid cell size.

collisionWorld.resolver

Collision resolver used in this collision world. By default, will inherit the collision manager default resolver.

Kind: instance property of CollisionWorld

collisionWorld.stats ⇒ \*

Get current stats.

Kind: instance property of CollisionWorld
Returns: \* - Dictionary with the following stats: updatedShapes: number of times we updated or added new shapes. addedShapes: number of new shapes added. deletedGridCells: grid cells that got deleted after they were empty. createdGridCell: new grid cells created. broadPhaseShapesChecksPrePredicate: how many shapes were tested in a broadphase check, before the predicate method was called. broadPhaseShapesChecksPostPredicate: how many shapes were tested in a broadphase check, after the predicate method was called. broadPhaseCalls: how many broadphase calls were made collisionChecks: how many shape-vs-shape collision checks were actually made. collisionMatches: how many collision checks were positive.

collisionWorld.resetStats()

Reset stats.

Kind: instance method of CollisionWorld

collisionWorld.iterateShapes(callback)

Iterate all shapes in world.

Kind: instance method of CollisionWorld

Param Type Description
callback function Callback to invoke on all shapes. Return false to break iteration.

collisionWorld.addShape(shape)

Add a collision shape to this world.

Kind: instance method of CollisionWorld

Param Type Description
shape CollisionShape Shape to add.

collisionWorld.removeShape(shape)

Remove a collision shape from this world.

Kind: instance method of CollisionWorld

Param Type Description
shape CollisionShape Shape to remove.

collisionWorld.testCollision(sourceShape, sortByDistance, mask, predicate) ⇒ CollisionTestResult

Test collision with shapes in world, and return just the first result found.

Kind: instance method of CollisionWorld
Returns: CollisionTestResult - A collision test result, or null if not found.

Param Type Description
sourceShape CollisionShape Source shape to check collision for. If shape is in world, it will not collide with itself.
sortByDistance Boolean If true will return the nearest collision found (based on center of shapes).
mask Number Optional mask of bits to match against shapes collisionFlags. Will only return shapes that have at least one common bit.
predicate function Optional filter to run on any shape we’re about to test collision with. If the predicate returns false, we will skip this shape.

collisionWorld.testCollisionMany(sourceShape, sortByDistance, mask, predicate, intermediateProcessor) ⇒ Array.<CollisionTestResult>

Test collision with shapes in world, and return all results found.

Kind: instance method of CollisionWorld
Returns: Array.<CollisionTestResult> - An array of collision test results, or empty array if none found.

Param Type Description
sourceShape CollisionShape Source shape to check collision for. If shape is in world, it will not collide with itself.
sortByDistance Boolean If true will sort results by distance.
mask Number Optional mask of bits to match against shapes collisionFlags. Will only return shapes that have at least one common bit.
predicate function Optional filter to run on any shape we’re about to test collision with. If the predicate returns false, we will skip this shape.
intermediateProcessor function Optional method to run after each positive result with the collision result as param. Return false to stop and return results.

collisionWorld.pick(position, radius, sortByDistance, mask, predicate) ⇒ Array.<CollisionShape>

Return array of shapes that touch a given position, with optional radius.

Kind: instance method of CollisionWorld
Returns: Array.<CollisionShape> - Array with collision shapes we picked.

Param Type Description
position \* Position to pick.
radius \* Optional picking radius to use a circle instead of a point.
sortByDistance \* If true, will sort results by distance from point.
mask \* Collision mask to filter by.
predicate \* Optional predicate method to filter by.

Example

let shapes = world.pick(Shaku.input.mousePosition);

collisionWorld.setDebugDrawBatch(batch)

Set the shapes batch to use for debug-drawing this collision world.

Kind: instance method of CollisionWorld

Param Type Description
batch ShapesBatch Batch to use for debug draw.

collisionWorld.getOrCreateDebugDrawBatch() ⇒ ShapesBatch

Return the currently set debug draw batch, or create a new one if needed.

Kind: instance method of CollisionWorld
Returns: ShapesBatch - Shapes batch instance used to debug-draw collision world.

collisionWorld.debugDraw(gridColor, gridHighlitColor, opacity, camera)

Debug-draw the current collision world.

Kind: instance method of CollisionWorld

Param Type Description
gridColor Color Optional grid color (default to black).
gridHighlitColor Color Optional grid color for cells with shapes in them (default to red).
opacity Number Optional opacity factor (default to 0.5).
camera Camera Optional camera for offset and viewport.