Class: Collider

Collider


new Collider()

Collider is the core element of physics module.

Source:
Examples

Create a collider

const Collider = require('engine/physics/Collider');

// Create collider instance
let collider = Collider({
  shape: 'Box',
  width: 20, height: 20,
  dumping: 0.6,
});

Define collision groups

const { getGroupMask } = require('engine/physics');

// It is recommend to define once in a module, and import it to use.
const GROUPS = {
  SOLID:   getGroupMask(0),
  PLAYER:  getGroupMask(1),
  TRIGGER: getGroupMask(2),
};

Setup collision

let bodyA = Collider({
  // A is a SOLID collider
  collisionGroup: GROUPS.SOLID,
});

let bodyB = Collider({
  // B is a player collider
  collisionGroup: GROUPS.PLAYER,
  // This collider will collide with SOLID bodies
  collideAgainst: GROUPS.SOLID,
  // Collision response handler
  collide: function(other) {
    // Response to the collision when collide with something SOLID,
    // which means this will be moved back and won't get through
    // the SOLID collider.
    if (other.collisionGroup & GROUPS.SOLID) {
      // When return false here, this collider will keep as is.
      // In this case, player will get through the SOLID collider.
      return true;
    }
  },
});

For more complex samples, take a look at the [physics sample code](https://github.com/pixelpicosean/lesser-panda-samples/blob/master/src/game/samples/physics.js).

Members


collideAgainst :array|number

Collision groups that this collider collides against.
Note: this will be a Number when broadPhase is "SpatialHash",
but will be an Array while using "Simple".

Type:
  • array | number
Source:

collisionGroup :number

Collider's collision group.

Type:
  • number
Default Value:
  • null
Source:

damping :number

Collider's damping. Should be number between 0 and 1.

Type:
  • number
Default Value:
  • 0
Source:

force :Vector

Collider's force.

Type:
Default Value:
  • 0,0
Source:

<readonly> height :number

Height of this collider(of its shape or 0).

Type:
  • number
Source:

id :number

ID of this collider.

Type:
  • number
Source:

isStatic :Boolean

Static collider will never update or response to collisions.

Type:
  • Boolean
Default Value:
  • false
Source:

last :Vector

Last position of collider.

Type:
Source:

mass :number

Collider's mass.

Type:
  • number
Default Value:
  • 0
Source:

position :Vector

Position of collider.

Type:
Source:

shape :Box|Circle

Collider's shape.

Type:
Source:

velocity :Vector

Collider's velocity.

Type:
Source:

velocityLimit :Vector

Collider's maximum velocity.

Type:
Default Value:
  • 400, 400
Source:

<readonly> width :number

Width of this collider(of its shape or 0).

Type:
  • number
Source:

world :SystemPhysics

Collider's parent world.

Type:
Source:

Methods


addTo(world)

Add this collider to the world.

Parameters:
Name Type Description
world SystemPhysics

Physics system instance to add to

Source:
Returns:

Self for chaining

Type
Collider

afterCollide()

This is called after hit response.

Source:

beforeCollide()

This will be called before collision checking.
You can clean up collision related flags here.

Source:

collide(other, response)

This is called while overlapping another collider.

Parameters:
Name Type Description
other Collider

Collider that is currently overlapping.

response *

Response infomration(direction for box, and angle for circle).

Source:
Returns:

Return true to apply hit response.

Type
boolean

handleMovementTrace(res)

Handle collision map tracing result.

Parameters:
Name Type Description
res Object

Tracing result to handle.

Source:

remove()

Remove collider from it's world.

Source:

setup(settings)

Setup this collider with settings.

Parameters:
Name Type Description
settings Object

Setting object.

Source:
Returns:

Self for chaining

Type
Collider

Class: Collider

Collider


new Collider( [properties])

Parameters:
Name Type Argument Description
properties object <optional>

Settings to merge.

Source:

Members


collideAgainst :array|number

Collision groups that this collider collides against.
Note: this will be a Number when broadPhase is "SpatialHash",
but will be an Array while using "Simple".

Type:
  • array | number
Source:

collisionGroup :number

Collider's collision group.

Type:
  • number
Default Value:
  • null
Source:

damping :number

Collider's damping. Should be number between 0 and 1.

Type:
  • number
Default Value:
  • 0
Source:

force :Vector

Collider's force.

Type:
Default Value:
  • 0,0
Source:

<readonly> height :number

Height of this collider(of its shape or 0).

Type:
  • number
Source:

id :number

ID of this collider.

Type:
  • number
Source:

isStatic :Boolean

Static collider will never update or response to collisions.

Type:
  • Boolean
Default Value:
  • false
Source:

last :Vector

Last position of collider.

Type:
Source:

mass :number

Collider's mass.

Type:
  • number
Default Value:
  • 0
Source:

position :Vector

Position of collider.

Type:
Source:

shape :Box|Circle

Collider's shape.

Type:
Source:

velocity :Vector

Collider's velocity.

Type:
Source:

velocityLimit :Vector

Collider's maximum velocity.

Type:
Default Value:
  • 400, 400
Source:

<readonly> width :number

Width of this collider(of its shape or 0).

Type:
  • number
Source:

world :SystemPhysics

Collider's parent world.

Type:
Source:

Methods


addTo(world)

Add this collider to the world.

Parameters:
Name Type Description
world SystemPhysics

Physics system instance to add to

Source:
Returns:

Self for chaining

Type
Collider

afterCollide()

This is called after hit response.

Source:

beforeCollide()

This will be called before collision checking.
You can clean up collision related flags here.

Source:

collide(other, response)

This is called while overlapping another collider.

Parameters:
Name Type Description
other Collider

Collider that is currently overlapping.

response *

Response infomration(direction for box, and angle for circle).

Source:
Returns:

Return true to apply hit response.

Type
boolean

handleMovementTrace(res)

Handle collision map tracing result.

Parameters:
Name Type Description
res Object

Tracing result to handle.

Source:

remove()

Remove collider from it's world.

Source:

setup(settings)

Setup this collider with settings.

Parameters:
Name Type Description
settings Object

Setting object.

Source:
Returns:

Self for chaining

Type
Collider