Rectangle

A Rectangle RigidBody from with and height

Events

See RigidBody

Constructor

new Rectangle(Game, width, height, position, options)

Parameters:
NameTypeDescription
GameGame

Game object the body should be simulated in; If you're creating a RigidBody from a Game object, like game.Bodies.Rectangle(...), then you must omit this parameter.

widthnumber

Width of rectangle

heightnumber

Height of rectangle

positionvec

Position of body

optionsobject

RigidBody options

Extends

Members

(readonly) nodeType

Indicates type of node. In this case, "RigidBody"

(readonly) mass :Number

Type:
  • Number

restitution :Number

Bounciness

Type:
  • Number

frictionAir :Number

How much the body is always slowed down

Type:
  • Number

frictionAngular :Number

How much body's rotation is always slowed down

Type:
  • Number

friction :Number

Type:
  • Number

(readonly) isStatic :Boolean

If the body is static (unmoving). Change through setStatic

Type:
  • Boolean

isSensor :Boolean

If the body acts like a sensor, detecting collisions while not hitting anything

Type:
  • Boolean

(readonly) hasCollisions :Boolean

If the body has any collisions. Change through setCollisions

Type:
  • Boolean

collisionFilter :Object

What bodies it can collide with. The layer is like what collision group the body belongs to and the mask is what layers the body will collide with. They are compared using their bits to indicate what layer it is in/collides with.
Another way to visualize it looking at the bits:
Layer:   0 1 0 1
Mask 1: 0 0 0 1 <-- collides
Mask 2: 0 0 1 0 <-- doesn't collide
Mask 3: 1 1 1 0 <-- collides
In other words, if layer & mask != 0 (bitwise and) for either of the bodies in the collision, the bodies can collide

Type:
  • Object
Example
// In every layer, collides with everything (the default)
body.collisionFilter = {
	layer: 0xFFFFFF,
	mask:  0xFFFFFF,
}
// In first 2 layers, collides only with 2nd layer
// So it would collide with itself and any body that has a mask in layers 1 or 2
body.collisionFilter = {
	layer: 0b0011,
	mask:  0b0010,
}

(readonly) position :vec

Position of the node

Type:
To Do
  • Implement getPosition method and make this private

(readonly) angle :number

Angle, in radians

Type:
  • number
To Do
  • Implement getAngle method and make this private

(readonly) children :Set

Children of the node To modify, use addChild or removeChild.

Type:
  • Set

Methods

add() → {RigidBody}

Adds the body to its world

Overrides
Returns:

this

Type: 
RigidBody

delete() → {RigidBody}

Removes the body from its world

Returns:

this

Type: 
RigidBody

addPolygonRender(options, containeropt) → {RigidBody}

Adds a polygon render to body

Parameters:
NameTypeAttributesDefaultDescription
optionsObject

Polygon Render options

containerPIXI.Container<optional>
this.Game.Render.app.stage

Container polygon render is added to. Defaults to the main render container of the game the body is in.

Returns:

this

Type: 
RigidBody
Example
body.addPolygonRender({
	layer: 0, // Render layer, higher means it is rendered "closer" to the camera and above other objects, like CSS z-index
	visible: true,
	alpha: 1, // Opacity, between 0-1
	
	// All colors can be a hex code, rgb, rgba, or "transparent"
	background: "#ffffff80", // fill color

	border: "#ff0000", // border color
	borderWidth: 3, // How thick border is, set to 0 to disable border
	lineCap: "butt", // How border should end. Doesn't do anything for closed bodies
	lineJoin: "miter", // How border's corners should look. Same options as ctx.lineJoin property
	
	round: 0, // How rounded the polygon should look. Only works for Rectangles
});

addSprite(options, containeropt) → {RigidBody}

Adds a sprite to body

Parameters:
NameTypeAttributesDefaultDescription
optionsObject

Sprite options

containerPIXI.Container<optional>
this.Game.Render.app.stage

Container sprite is added to. Defaults to the main render container of the game the body is in.

Returns:

this

Type: 
RigidBody
Example
body.addSprite(Render.app.stage, {
	layer: 0, // number

	visible: true,
	alpha: 1, // number between [0, 1]
	src: "path/to/sprite.png",
	
	scale: new vec(1, 1),
	width:  undefined, // number, defaults to fit body if a Rectangle or Circle, or width of image if not
	height: undefined, // number, defaults to fit body if a Rectangle or Circle, or height of image if not
});

addSpritesheet(container, options) → {RigidBody}

Adds a new Spritesheet to body

Parameters:
NameTypeDescription
containerPIXI.Container

Container the Spritesheet is added to

optionsObject

Spritesheet options

Returns:

this

Type: 
RigidBody

setStatic(isStatic)

Changes if the body is static

Parameters:
NameTypeDescription
isStaticboolean

If the body should be static

setMass(mass)

Changes the body's mass to a new value

Parameters:
NameTypeDescription
massnumber

setCollisions(hasCollisions)

Changes if the body can collide with other bodies

Parameters:
NameTypeDescription
hasCollisionsboolean

Whether the body can collide with other bodies

containsPoint(point) → {boolean}

Finds if a point is inside the body's collision shapes

Parameters:
NameTypeDescription
pointvec

Point to query

Returns:

If the point is inside the body's vertices

Type: 
boolean

setPosition(position)

Instantly sets body's position to position

Parameters:
NameTypeDescription
positionvec

Position the body should be

Example
body.setPosition(new vec(100, 100)); // Sets body's position to (100, 100) 

setVelocity(velocity)

Instantly changes the body's velocity to a specific value

Parameters:
NameTypeDescription
velocityvec

Velocity the body should have

setAngularVelocity(velocity)

Instantly changes the body's angular velocity to a specific value

Parameters:
NameTypeDescription
velocitynumber

Angular velocity the body should have

applyForce(force, deltaopt)

Applies a force to the body, ignoring mass. The body's velocity changes by force * delta

Parameters:
NameTypeAttributesDefaultDescription
forcevec

Amount of force to be applied, in px / sec^2

deltanumber<optional>
Engine.delta

Amount of time that the force is applied, in seconds. Set to 1 if applying instantaneous force

applyTorque(force, deltaopt)

Applies a rotational force (torque) to the body, ignoring mass. The body's angular velocity changes by force * delta

Parameters:
NameTypeAttributesDefaultDescription
forcenumber

Amount of torque to be applied, in radians / sec^2

deltanumber<optional>
Engine.delta

Amount of time that the force is applied, in seconds. Set to 1 if applying instantaneous force

on(event, callback)

Bind a callback to an event

Parameters:
NameTypeDescription
eventstring

Name of the event

callbackfunction

Callback run when event is fired

Overrides

off(event, callback)

Unbinds a callback from an event

Parameters:
NameTypeDescription
eventstring

Name of the event

callbackfunction

Function to unbind

Overrides

trigger(event, …args)

Triggers an event, firing all bound callbacks

Parameters:
NameTypeAttributesDescription
eventstring

Name of the event

args*<repeatable>

Arguments passed to callbacks

isAdded() → {Boolean}

Gets if the node is added

Returns:

if the node is added

Type: 
Boolean

addChild(…children)

Adds all children to this node's children

Parameters:
NameTypeAttributesDescription
childrenNode<repeatable>

Children added

Example
let parentNode = new Node();
let childNode = new Node();
node.addChild(childNode);

removeChild(…children)

Removes all children from this node's children

Parameters:
NameTypeAttributesDescription
childrenNode<repeatable>

Children removed

Example
let parentNode = new Node();
let childNode = new Node();
node.addChild(childNode); // node.children: Set {childNode}
node.removeChild(childNode); // node.children: Set {}

translate(positionDelta)

Shifts this node's position by positionDelta

Parameters:
NameTypeDescription
positionDeltavec

Amount to shift the position

setAngle(angle)

Sets the node's angle to angle

Parameters:
NameTypeDescription
anglenumber

Angle body should be in radians

Example
node.setAngle(Math.PI); // Sets node's angle to Pi radians, or 180 degrees

translateAngle(angle)

Rotates the body by angle- Relative

Parameters:
NameTypeDescription
anglenumber

Amount the body should be rotated, in radians