me.Body(ancestor, shapesopt, onBodyUpdateopt)

Body

new Body(ancestor, shapesopt, onBodyUpdateopt)

Parameters:
Name Type Attributes Description
ancestor me.Renderable

the parent object this body is attached to

shapes me.Rect | Array.<me.Rect> | me.Polygon | Array.<me.Polygon> | me.Line | Array.<me.Line> | me.Ellipse | Array.<me.Ellipse> | me.Bounds | Array.<me.Bounds> | Object <optional>

a initial shape, list of shapes, or JSON object defining the body

onBodyUpdate function <optional>

callback for when the body is updated (e.g. add/remove shapes)

Members

static accel :me.Vector2d

body acceleration
Not fully implemented yet. At this time accel is used to set the MaximumVelocity allowed.

Deprecated:
  • Yes
Default Value:
  • <0,0>
See:

static bounce :Number

the body bouciness level when colliding with other solid bodies : a value of 0 will not bounce, a value of 1 will fully rebound.

Default Value:
  • 0

static bounds :me.Bounds

The AABB bounds box reprensenting this body

static collisionType :Number

define the collision type of the body for collision filtering

Default Value:
  • me.collision.types.ENEMY_OBJECT
See:
Example
// set the body collision type
myEntity.body.collisionType = me.collision.types.PLAYER_OBJECT;

static, readonly falling :Boolean

falling state of the body
true if the object is falling
false if the object is standing on something

Default Value:
  • false

static force :me.Vector2d

body force or acceleration (automatically) applied to the body. when defining a force, user should also define a max velocity

Default Value:
  • <0,0>
See:
Example
// define a default maximum acceleration, initial force and friction
this.body.force.set(0, 0);
this.body.friction.set(0.4, 0);
this.body.setMaxVelocity(3, 15);

// apply a postive or negative force when pressing left of right key
update(dt) {
    if (me.input.isKeyPressed("left"))    {
         this.body.force.x = -this.body.maxVel.x;
     } else if (me.input.isKeyPressed("right")) {
        this.body.force.x = this.body.maxVel.x;
    } else {
        this.body.force.x = 0;
    }
}

static friction :me.Vector2d

body friction

Default Value:
  • <0,0>

static gravity :me.Vector2d

Default gravity value for this body. To be set to to < 0, 0 > for RPG, shooter, etc...

Deprecated:
  • since 8.0.0
Default Value:
  • <0,0.98>
See:

static gravityScale :Number

The degree to which this body is affected by the world gravity

Default Value:
  • 1.0
See:

static ignoreGravity :Boolean

If true this body won't be affected by the world gravity

Default Value:
  • false
See:

static, readonly jumping :Boolean

jumping state of the body
equal true if the body is jumping

Default Value:
  • false

static mass :Number

the body mass

Default Value:
  • 1

static maxVel :me.Vector2d

max velocity (to limit body velocity)

Default Value:
  • <490,490>

static vel :me.Vector2d

body velocity

Default Value:
  • <0,0>

ancestor :me.Renderable

a reference to the parent object that contains this body, or undefined if it has not been added to one.

Default Value:
  • undefined

Methods

static addShape(shape, batchInsert) → {Number}

add a collision shape to this body
(note: me.Rect objects will be converted to me.Polygon before being added)

Parameters:
Name Type Description
shape me.Rect | me.Polygon | me.Line | me.Ellipse | me.Bounds | Object

a shape or JSON object

batchInsert Boolean

if true the body bounds won't be updated after adding a shape

Returns:
Number -

the shape array length

Example
// add a rectangle shape
this.body.addShape(new me.Rect(0, 0, image.width, image.height));
// add a shape from a JSON object
this.body.addShape(me.loader.getJSON("shapesdef").banana);

static addShapesFromJSON(shape, batchInsert) → {Number}

add collision mesh based on a given Physics Editor JSON object

Parameters:
Name Type Description
shape me.Rect | me.Polygon | me.Line | me.Ellipse | Object

a shape or JSON object

batchInsert Boolean

if true the body bounds won't be updated after adding a shape

Returns:
Number -

the shape array length

Deprecated:
  • since 8.0.0
See:

static addVertices(vertices, indexopt)

add the given vertices to the body shape

Parameters:
Name Type Attributes Default Description
vertices Array.<me.Vector2d>

an array of me.Vector2d points defining a convex hull

index Number <optional>
0

the shape object for which to set the vertices

static contains(point) → {boolean}

Returns true if the any of the shape composing the body contains the given point.

Parameters:
Name Type Description
point me.Vector2d
Returns:
boolean -

true if contains

static contains(x, y) → {boolean}

Returns true if the any of the shape composing the body contains the given point.

Parameters:
Name Type Description
x Number

x coordinate

y Number

y coordinate

Returns:
boolean -

true if contains

static fromJSON(json, idopt) → {Number}

add collision mesh based on a JSON object (this will also apply any physic properties defined in the given JSON file)

Parameters:
Name Type Attributes Description
json Object

a JSON object as exported from a Physics Editor tool

id String <optional>

an optional shape identifier within the given the json object

Returns:
Number -

how many shapes were added to the body

See:
Example
// define the body based on the banana shape
this.body.fromJSON(me.loader.getJSON("shapesdef").banana);
// or ...
this.body.fromJSON(me.loader.getJSON("shapesdef"), "banana");

static getBounds() → {me.Bounds}

returns the AABB bounding box for this body

Returns:
me.Bounds -

bounding box Rectangle object

static getShape(indexopt) → {me.Polygon|me.Line|me.Ellipse}

return the collision shape at the given index

Parameters:
Name Type Attributes Default Description
index Number <optional>
0

the shape object at the specified index

Returns:
me.Polygon | me.Line | me.Ellipse -

shape a shape object if defined

static removeShape(shape) → {Number}

remove the specified shape from the body shape list

Parameters:
Name Type Description
shape me.Polygon | me.Line | me.Ellipse

a shape object

Returns:
Number -

the shape array length

static removeShapeAt(index) → {Number}

remove the shape at the given index from the body shape list

Parameters:
Name Type Description
index Number

the shape object at the specified index

Returns:
Number -

the shape array length

protected, static respondToCollision(response)

the built-in function to solve the collision response

Parameters:
Name Type Description
response me.collision.ResponseObject

the collision response object

static rotate(angle, vopt) → {me.Body}

Rotate this body (counter-clockwise) by the specified angle (in radians). Unless specified the body will be rotated around its center point

Parameters:
Name Type Attributes Default Description
angle Number

The angle to rotate (in radians)

v me.Vector2d | me.ObservableVector2d <optional>
me.Body.getBounds().center

an optional point to rotate around

Returns:
me.Body -

Reference to this object for method chaining

static setCollisionMask(bitmaskopt)

By default all entities are able to collide with all other entities,
but it's also possible to specify 'collision filters' to provide a finer
control over which entities can collide with each other.

Parameters:
Name Type Attributes Default Description
bitmask Number <optional>
me.collision.types.ALL_OBJECT

the collision mask

See:
Example
// filter collision detection with collision shapes, enemies and collectables
myEntity.body.setCollisionMask(me.collision.types.WORLD_SHAPE | me.collision.types.ENEMY_OBJECT | me.collision.types.COLLECTABLE_OBJECT);
...
// disable collision detection with all other objects
myEntity.body.setCollisionMask(me.collision.types.NO_OBJECT);

static setCollisionType(type)

define the collision type of the body for collision filtering

Parameters:
Name Type Description
type Number

the collision type

See:
Example
// set the body collision type
myEntity.body.collisionType = me.collision.types.PLAYER_OBJECT;

protected, static setFriction(x, y)

set the body default friction

Parameters:
Name Type Description
x Number

horizontal friction

y Number

vertical friction

protected, static setMaxVelocity(x, y)

cap the body velocity (body.maxVel property) to the specified value

Parameters:
Name Type Description
x Number

max velocity on x axis

y Number

max velocity on y axis

protected, static setVelocity(x, y)

Sets accel to Velocity if x or y is not 0. Net effect is to set the maxVel.x/y to the passed values for x/y
note: This does not set the vel member of the body object. This is identical to the setMaxVelocity call except that the accel property is updated to match the passed x and y. setMaxVelocity if needed

Parameters:
Name Type Description
x Number

velocity on x axis

y Number

velocity on y axis

Deprecated:
  • Yes
See:

static setVertices(vertices, indexopt, clearopt)

set the body vertices to the given one

Parameters:
Name Type Attributes Default Description
vertices Array.<me.Vector2d>

an array of me.Vector2d points defining a convex hull

index Number <optional>
0

the shape object for which to set the vertices

clear boolean <optional>
true

either to reset the body definition before adding the new vertices

static update() → {boolean}

Updates the parent's position as well as computes the new body's velocity based on the values of force/friction/gravity. Velocity chages are proportional to the me.timer.tick value (which can be used to scale velocities). The approach to moving the parent Entity is to compute new values of the Body.vel property then add them to the parent.pos value thus changing the postion the amount of Body.vel each time the update call is made.
Updates to Body.vel are bounded by maxVel (which defaults to viewport size if not set)

In addition, when the gravity calcuation is made, if the Body.vel.y > 0 then the Body.falling property is set to true and Body.jumping is set to !Body.falling.

At this time a call to Body.Update does not call the onBodyUpdate callback that is listed in the init: function.

Returns:
boolean -

true if resulting velocity is different than 0

See:
  • source code for me.Body.computeVelocity (private member)

forEach(callback, thisArgopt)

The forEach() method executes a provided function once per body shape element.
the callback function is invoked with three arguments:

  • The current element being processed in the array
  • The index of element in the array.
  • The array forEach() was called upon.
Parameters:
Name Type Attributes Description
callback function

fnction to execute on each element

thisArg Object <optional>

value to use as this(i.e reference Object) when executing callback.

Example
// iterate through all shapes of the physic body
mySprite.body.forEach((shape) => {
   shape.doSomething();
});
mySprite.body.forEach((shape, index) => { ... });
mySprite.body.forEach((shape, index, array) => { ... });
mySprite.body.forEach((shape, index, array) => { ... }, thisArg);