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

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)