Class: Body

me. Body

new Body(ancestor, shapes, onBodyUpdate)

a Generic Body Object
Parameters:
Name Type Argument 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)
Source:
body.js, line 20

Extends

Classes

Error

Members

<static> accel :me.Vector2d

body acceleration
Type:
Deprecated:
  • Yes
Default Value:
  • <0,0>
Source:
body.js, line 86
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.
Type:
  • Number
Default Value:
  • 0
Source:
body.js, line 148

<static> collisionType :Number

define the collision type of the body for collision filtering
Type:
  • Number
Default Value:
  • me.collision.types.ENEMY_OBJECT
Source:
body.js, line 59
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
Type:
  • Boolean
Default Value:
  • false
Source:
body.js, line 202

<static> force :me.Vector2d

body force or acceleration (automatically) applied to the body. when defining a force, user should also define a max velocity
Type:
Default Value:
  • <0,0>
Source:
body.js, line 101
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
Type:
Default Value:
  • <0,0>
Source:
body.js, line 135

<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
Type:
Default Value:
  • <0,0.98>
Source:
body.js, line 183
See:

<static, readonly> jumping :Boolean

jumping state of the body
equal true if the body is jumping
Type:
  • Boolean
Default Value:
  • false
Source:
body.js, line 215

<static> mass :Number

the body mass
Type:
  • Number
Default Value:
  • 1
Source:
body.js, line 159

<static> maxVel :me.Vector2d

max velocity (to limit body velocity)
Type:
Default Value:
  • <490,490>
Source:
body.js, line 169

<static> vel :me.Vector2d

body velocity
Type:
Default Value:
  • <0,0>
Source:
body.js, line 73

ancestor :me.Renderable

a reference to the parent object that contains this bodt, or undefined if it has not been added to one.
Type:
Default Value:
  • undefined
Source:
body.js, line 26

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
Source:
body.js, line 251
Returns:
the shape array length
Type
Number

<static> addShapesFromJSON(json, id, scale) → {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 Argument 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)
Source:
body.js, line 279
See:
Returns:
the shape array length
Type
Number
Example
this.body.addShapesFromJSON(me.loader.getJSON("shapesdef1"), settings.banana);

<static> getShape(index) → {me.Polygon|me.Line|me.Ellipse}

return the collision shape at the given index
Parameters:
Name Type Argument Default Description
index Number <optional>
0 the shape object at the specified index
Source:
body.js, line 367
Returns:
shape a shape object if defined
Type
me.Polygon | me.Line | me.Ellipse

<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
Source:
body.js, line 380
Returns:
the shape array length
Type
Number

<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
Source:
body.js, line 399
Returns:
the shape array length
Type
Number

<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
Source:
body.js, line 433

<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
Source:
body.js, line 412
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
Source:
body.js, line 533

<protected, static> setMaxVelocity(x, y)

cap the body velocity to the specified value
Parameters:
Name Type Description
x Number max velocity on x axis
y Number max velocity on y axis
Source:
body.js, line 519

<protected, static> setVelocity(x, y)

set the body default velocity
note : velocity is by default limited to the same value, see setMaxVelocity if needed
Parameters:
Name Type Description
x Number velocity on x axis
y Number velocity on y axis
Deprecated:
  • Yes
Source:
body.js, line 498
See:

<static> update() → {boolean}

update the body position
Source:
body.js, line 609
Returns:
true if resulting velocity is different than 0
Type
boolean