me.Body(ancestor, shapesopt, onBodyUpdateopt)

new Body(ancestor, shapesopt, onBodyUpdateopt)

a Generic Body Object with some physic properties and behavior functionality
The body object is offten attached as a member of an Entity. The Body object can handle movements of the parent with the body.update call. It important to know that when body.update is called there are several things that happen related to the movement and positioning of the parent entity (assuming its an Entity). 1) The force/gravity/friction parameters are used to calcuate a new velocity and 2) the parent position is updated by adding this to the parent.pos (position me.Vector2d) value. Thus Affecting the movement of the parent. Look at the source code for /src/physics/body.js:update (me.Body.update) for a better understanding.

Parameters:
Name Type Attributes Description
ancestor me.Renderable

the parent object this body is attached to

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

the initial list of shapes

onBodyUpdate function <optional>

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

Extends

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 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
init: function () {
   // 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 : function (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...
Note: y axis gravity can also globally be defined through me.sys.gravity

Default Value:
  • <0,0.98>
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

points :Array.<me.Vector2d>

Array of points defining the Polygon
Note: If you manually change points, you must call recalcafterwards so that the changes get applied correctly.

Inherited From:

pos :me.Vector2d

origin point of the Polygon

Inherited From:

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

a shape object

batchInsert Boolean

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

Returns:
Number -

the shape array length

static addShapesFromJSON(json, id, scaleopt) → {Number}

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

Parameters:
Name Type Attributes Default Description
json Object

a JSON object as exported from the a Physics Editor tool

id String

the shape identifier within the given the json object

scale String <optional>
1

the desired scale of the body (physic-body-editor only)

Returns:
Number -

the shape array length

See:
Example
this.body.addShapesFromJSON(me.loader.getJSON("shapesdef1"), settings.banana);

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 setCollisionMask(bitmask)

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

Parameters:
Name Type Description
bitmask Number

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);

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 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)

clone() → {me.Rect}

clone this rectangle

Returns:
me.Rect -

new rectangle

Inherited From:

contains(rect) → {boolean}

check if this rectangle contains the specified one

Parameters:
Name Type Description
rect me.Rect
Returns:
boolean -

true if contains

Inherited From:

containsPoint(x, y) → {boolean}

check if this rectangle contains the specified point

Parameters:
Name Type Description
x Number

x coordinate

y Number

y coordinate

Returns:
boolean -

true if contains

Inherited From:

containsPointV(point) → {boolean}

check if this Polygon contains the specified point

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

true if contains

Inherited From:

copy(rect) → {me.Rect}

copy the position and size of the given rectangle into this one

Parameters:
Name Type Description
rect me.Rect

Source rectangle

Returns:
me.Rect -

new rectangle

Inherited From:

equals(rect) → {boolean}

check if this rectangle is identical to the specified one

Parameters:
Name Type Description
rect me.Rect
Returns:
boolean -

true if equals

Inherited From:

getBounds() → {me.Rect}

returns the bounding box for this shape, the smallest rectangle object completely containing this shape.

Returns:
me.Rect -

this shape bounding box Rectangle object

Inherited From:

getIndices(a) → {me.Polygon}

returns a list of indices for all triangles defined in this polygon

Parameters:
Name Type Description
a Array.<Vector2d>

list of vector

Returns:
me.Polygon -

this Polygon

Inherited From:

isFinite() → {boolean}

determines whether all coordinates of this rectangle are finite numbers.

Returns:
boolean -

false if all coordinates are positive or negative Infinity or NaN; otherwise, true.

Inherited From:

overlaps(rect) → {boolean}

check if this rectangle is intersecting with the specified one

Parameters:
Name Type Description
rect me.Rect
Returns:
boolean -

true if overlaps

Inherited From:

resize(w, h) → {me.Rect}

resize the rectangle

Parameters:
Name Type Description
w Number

new width of the rectangle

h Number

new height of the rectangle

Returns:
me.Rect -

this rectangle

Inherited From:

rotate(angle) → {me.Polygon}

Rotate this Polygon (counter-clockwise) by the specified angle (in radians).

Parameters:
Name Type Description
angle Number

The angle to rotate (in radians)

Returns:
me.Polygon -

Reference to this object for method chaining

Inherited From:

scale(x, yopt) → {me.Polygon}

Scale this Polygon by the given scalar.

Parameters:
Name Type Attributes Default Description
x Number
y Number <optional>
x
Returns:
me.Polygon -

Reference to this object for method chaining

Inherited From:

scaleV(v) → {me.Polygon}

Scale this Polygon by the given vector

Parameters:
Name Type Description
v me.Vector2d
Returns:
me.Polygon -

Reference to this object for method chaining

Inherited From:

setPoints(points) → {me.Rect}

resize the rectangle to contain all the given points coordinates.

Parameters:
Name Type Description
points Array.<me.Vector2d>

array of vector defining a shape

Returns:
me.Rect -

this shape bounding box Rectangle object

Inherited From:

setShape(x, y, w|points, hopt) → {me.Rect}

set new value to the rectangle shape

Parameters:
Name Type Attributes Description
x Number

position of the Rectangle

y Number

position of the Rectangle

w|points Number | Array

width of the rectangle, or an array of vector defining the rectangle

h Number <optional>

height of the rectangle, if a numeral width parameter is specified

Returns:
me.Rect -

this rectangle

Inherited From:

to2d() → {me.Polygon}

apply a 2d projection to this shape

Returns:
me.Polygon -

Reference to this object for method chaining

Inherited From:

toIso() → {me.Polygon}

apply an isometric projection to this shape

Returns:
me.Polygon -

Reference to this object for method chaining

Inherited From:

toPolygon() → {me.Polygon}

Returns a polygon whose edges are the same as this box.

Returns:
me.Polygon -

a new Polygon that represents this rectangle.

Inherited From:

transform(matrix) → {me.Polygon}

apply the given transformation matrix to this Polygon

Parameters:
Name Type Description
matrix me.Matrix2d

the transformation matrix

Returns:
me.Polygon -

Reference to this object for method chaining

Inherited From:

translate(x, y) → {me.Rect}

translate the rect by the specified offset

Parameters:
Name Type Description
x Number

x offset

y Number

y offset

Returns:
me.Rect -

this rectangle

Inherited From:

translateV(v) → {me.Rect}

translate the rect by the specified vector

Parameters:
Name Type Description
v me.Vector2d

vector offset

Returns:
me.Rect -

this rectangle

Inherited From:

union(rect) → {me.Rect}

merge this rectangle with another one

Parameters:
Name Type Description
rect me.Rect

other rectangle to union with

Returns:
me.Rect -

the union(ed) rectangle

Inherited From:

updateBounds() → {me.Rect}

update the bounding box for this shape.

Returns:
me.Rect -

this shape bounding box Rectangle object

Inherited From: