Constructor
new RigidBody(Game, vertices, position, options)
Creates a new RigidBody
Name | Type | Description |
---|---|---|
Game | Game | Game object the body should be simulated in; If you're creating a RigidBody from a game object, like |
vertices | Array | Array of |
position | vec | Position of the body |
options | Object | RigidBody options |
- Source
// Includes all RigidBody options
new RigidBody(game, [new vec(0, 0), new vec(10, 0), new vec(10, 10), new vec(0, 10)], new vec(0, 0), {
mass: 1,
restitution: 0.1,
frictionAir: 0.05,
frictionAngular: 0.01,
friction: 0.1,
round: 0,
roundQuality: 20,
isStatic: false,
isSensor: false,
hasCollisions: true,
collisionFilter: {
layer: 0xFFFFFF,
mask: 0xFFFFFF,
},
});
Extends
Members
(readonly) nodeType
Indicates type of node. In this case, "RigidBody"
- Overrides
- Source
(readonly) mass :Number
- Number
- Source
restitution :Number
Bounciness
- Number
- Source
frictionAir :Number
How much the body is always slowed down
- Number
- Source
frictionAngular :Number
How much body's rotation is always slowed down
- Number
- Source
friction :Number
- Number
- Source
(readonly) isStatic :Boolean
If the body is static (unmoving). Change through setStatic
- Boolean
- Source
isSensor :Boolean
If the body acts like a sensor, detecting collisions while not hitting anything
- Boolean
- Source
(readonly) hasCollisions :Boolean
If the body has any collisions. Change through setCollisions
- Boolean
- Source
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
- Object
- Source
// 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
- Overrides
- Source
- To Do
- Implement getPosition method and make this private
(readonly) angle :number
Angle, in radians
- number
- Overrides
- Source
- To Do
- Implement getAngle method and make this private
(readonly) children :Set
Children of the node To modify, use addChild
or removeChild
.
- Set
- Overrides
- Source
Methods
add() → {RigidBody}
Adds the body to its world
- Overrides
- Source
this
- Type:
- RigidBody
delete() → {RigidBody}
Removes the body from its world
- Overrides
- Source
this
- Type:
- RigidBody
addPolygonRender(options, containeropt) → {RigidBody}
Adds a polygon render to body
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
options | Object | Polygon Render options | ||
container | PIXI. | <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. |
- Source
this
- Type:
- RigidBody
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
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
options | Object | Sprite options | ||
container | PIXI. | <optional> | this.Game.Render.app.stage | Container sprite is added to. Defaults to the main render container of the game the body is in. |
- Source
this
- Type:
- RigidBody
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
Name | Type | Description |
---|---|---|
container | PIXI. | Container the Spritesheet is added to |
options | Object | Spritesheet options |
- Source
this
- Type:
- RigidBody
setStatic(isStatic)
Changes if the body is static
Name | Type | Description |
---|---|---|
isStatic | boolean | If the body should be static |
- Source
setMass(mass)
Changes the body's mass to a new value
Name | Type | Description |
---|---|---|
mass | number |
- Source
setCollisions(hasCollisions)
Changes if the body can collide with other bodies
Name | Type | Description |
---|---|---|
hasCollisions | boolean | Whether the body can collide with other bodies |
- Source
containsPoint(point) → {boolean}
Finds if a point is inside the body's collision shapes
Name | Type | Description |
---|---|---|
point | vec | Point to query |
- Source
If the point is inside the body's vertices
- Type:
- boolean
setPosition(position)
Instantly sets body's position to position
Name | Type | Description |
---|---|---|
position | vec | Position the body should be |
- Overrides
- Source
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
Name | Type | Description |
---|---|---|
velocity | vec | Velocity the body should have |
- Source
setAngularVelocity(velocity)
Instantly changes the body's angular velocity to a specific value
Name | Type | Description |
---|---|---|
velocity | number | Angular velocity the body should have |
- Source
applyForce(force, deltaopt)
Applies a force to the body, ignoring mass. The body's velocity changes by force * delta
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
force | vec | Amount of force to be applied, in px / sec^2 | ||
delta | number | <optional> | Engine.delta | Amount of time that the force is applied, in seconds. Set to 1 if applying instantaneous force |
- Source
applyTorque(force, deltaopt)
Applies a rotational force (torque) to the body, ignoring mass. The body's angular velocity changes by force * delta
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
force | number | Amount of torque to be applied, in radians / sec^2 | ||
delta | number | <optional> | Engine.delta | Amount of time that the force is applied, in seconds. Set to 1 if applying instantaneous force |
- Source
on(event, callback)
Bind a callback to an event
Name | Type | Description |
---|---|---|
event | string | Name of the event |
callback | function | Callback run when event is fired |
- Overrides
- Source
off(event, callback)
Unbinds a callback from an event
Name | Type | Description |
---|---|---|
event | string | Name of the event |
callback | function | Function to unbind |
- Overrides
- Source
trigger(event, …args)
Triggers an event, firing all bound callbacks
Name | Type | Attributes | Description |
---|---|---|---|
event | string | Name of the event | |
args | * | <repeatable> | Arguments passed to callbacks |
- Overrides
- Source
isAdded() → {Boolean}
Gets if the node is added
- Overrides
- Source
if the node is added
- Type:
- Boolean
addChild(…children)
Adds all children
to this node's children
Name | Type | Attributes | Description |
---|---|---|---|
children | Node | <repeatable> | Children added |
- Overrides
- Source
let parentNode = new Node();
let childNode = new Node();
node.addChild(childNode);
removeChild(…children)
Removes all children
from this node's children
Name | Type | Attributes | Description |
---|---|---|---|
children | Node | <repeatable> | Children removed |
- Overrides
- Source
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
Name | Type | Description |
---|---|---|
positionDelta | vec | Amount to shift the position |
- Overrides
- Source
setAngle(angle)
Sets the node's angle to angle
Name | Type | Description |
---|---|---|
angle | number | Angle body should be in radians |
- Overrides
- Source
node.setAngle(Math.PI); // Sets node's angle to Pi radians, or 180 degrees
translateAngle(angle)
Rotates the body by angle
- Relative
Name | Type | Description |
---|---|---|
angle | number | Amount the body should be rotated, in radians |
- Overrides
- Source