Body

class Body

a Generic Physic Body Object with some physic properties and behavior functionality, to add as a member of a Renderable.

See: Renderable.body

Constructor


new Body(ancestor: Renderable | Container | Entity | Sprite | NineSliceSprite, shapes: Rect | Rect<Array> | Polygon | Array<Polygon> | Line | Array<Line> | Ellipse | Array<Ellipse> | Point | Array<Point> | Bounds | Array<Bounds> | object, onBodyUpdate: Function) → {}
Parameters:
Name Type Attributes Description
ancestor Renderable | Container | Entity | Sprite | NineSliceSprite

the parent object this body is attached to

shapes Rect | Rect<Array> | Polygon | Array<Polygon> | Line | Array<Line> | Ellipse | Array<Ellipse> | Point | Array<Point> | Bounds | Array<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)

Public Properties


ancestor body.js:25
ancestor: Renderable | Container | Entity | Sprite | NineSliceSprite = undefined

Renderable | Container | Entity | Sprite | NineSliceSprite

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

bounce body.js:125
bounce: number = 0

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.

bounds body.js:35
bounds: Bounds

Bounds

The AABB bounds box reprensenting this body

collisionType body.js:62
collisionType: number = collision.types.ENEMY_OBJECT

number

define the collision type of the body for collision filtering

// set the body collision type
body.collisionType = me.collision.types.PLAYER_OBJECT;
See: collision.types
falling body.js:184
falling: boolean = false

boolean

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

force body.js:88
force: Vector2d = <0,0>

Vector2d

body force to apply to this the body in the current step. (any positive or negative force will be cancelled after every world/body update cycle)

 // define a default maximum acceleration, initial force and friction
 this.body.force.set(1, 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;
     }
 }
See: Body.setMaxVelocity
friction body.js:115
friction: Vector2d = <0,0>

Vector2d

body friction

gravityScale body.js:166
gravityScale: number = 1.0

number

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

See: World.gravity
ignoreGravity body.js:175
ignoreGravity: boolean = false

boolean

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

See: World.gravity
isStatic body.js:155
isStatic: boolean = false

boolean

Either this body is a static body or not. A static body is completely fixed and can never change position or angle.

jumping body.js:195
jumping: boolean = false

boolean

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

mass body.js:134
mass: number = 1

number

the body mass

maxVel body.js:143
maxVel: Vector2d = <490,490>

Vector2d

max velocity (to limit body velocity)

vel body.js:75
vel: Vector2d = <0,0>

Vector2d

The current velocity of the body. See to apply a force if you need to modify a body velocity

See: Body.force

Public Methods


addShape body.js:236
addShape(shape: Rect | Polygon | Line | Ellipse | Point | Array<Point> | Bounds | object) → {number}

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

// 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);
Parameters:
Name Type Description
shape Rect | Polygon | Line | Ellipse | Point | Array<Point> | Bounds | object

a shape or JSON object

Returns:
Type Description
number

the shape array length

addVertices body.js:316
addVertices(vertices: Array<Vector2d>, index: number) → {}

add the given vertices to the body shape

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

an array of me.Vector2d points defining a convex hull

index number

<optional>

0

the shape object for which to set the vertices

contains body.js:517
contains(x: number | Vector2d, y: number) → {boolean}

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

if (mySprite.body.contains(10, 10)) {
  // do something
}
// or
if (mySprite.body.contains(myVector2d)) {
  // do something
}
Parameters:
Name Type Attributes Description
x number | Vector2d

x coordinate or a vector point to check

y number

<optional>

y coordinate

Returns:
Type Description
boolean

true if contains

forEach body.js:480
forEach(callback: Function, thisArg: object) → {}

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.

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

fromJSON body.js:325
fromJSON(json: object, id: string) → {number}

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

// 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");
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:
Type Description
number

how many shapes were added to the body

See: https://www.codeandweb.com/physicseditor
getBounds body.js:375
getBounds() → {Bounds}

returns the AABB bounding box for this body

Returns:
Type Description
Bounds

bounding box Rectangle object

getShape body.js:366
getShape(index: number) → {Polygon | Line | 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:
Type Description
Polygon | Line | Ellipse

shape a shape object if defined

removeShape body.js:383
removeShape(shape: Polygon | Line | Ellipse) → {number}

remove the specified shape from the body shape list

Parameters:
Name Type Description
shape Polygon | Line | Ellipse

a shape object

Returns:
Type Description
number

the shape array length

removeShapeAt body.js:401
removeShapeAt(index: number) → {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:
Type Description
number

the shape array length

respondToCollision body.js:445
respondToCollision(response: object) → {}

the built-in function to solve the collision response

Parameters:
Name Type Description
response object

the collision response object (see ResponseObject)

rotate body.js:553
rotate(angle: number, v: Vector2d | ObservableVector2d) → {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 Vector2d | ObservableVector2d

<optional>

Body.getBounds().center

an optional point to rotate around

Returns:
Type Description
Body

Reference to this object for method chaining

setCollisionMask body.js:410
setCollisionMask(bitmask : number) → {}

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

// filter collision detection with collision shapes, enemies and collectables
body.setCollisionMask(me.collision.types.WORLD_SHAPE | me.collision.types.ENEMY_OBJECT | me.collision.types.COLLECTABLE_OBJECT);
...
// disable collision detection with all other objects
body.setCollisionMask(me.collision.types.NO_OBJECT);
Parameters:
Name Type Attributes Default Description
bitmask number

<optional>

collision.types.ALL_OBJECT

the collision mask

See: collision.types
setCollisionType body.js:427
setCollisionType(type: number) → {}

define the collision type of the body for collision filtering

// set the body collision type
body.collisionType = me.collision.types.PLAYER_OBJECT;
Parameters:
Name Type Description
type number

the collision type

See: collision.types
setFriction body.js:592
setFriction(x: number, y: number) → {}

set the body default friction

Parameters:
Name Type Default Description
x number 0

horizontal friction

y number 0

vertical friction

setMaxVelocity body.js:582
setMaxVelocity(x: number, y: number) → {}

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

setStatic body.js:227
setStatic(isStatic: boolean) → {}

set the body as a static body static body do not move automatically and do not check againt collision with others

Parameters:
Name Type Attributes Default Description
isStatic boolean

<optional>

true
setVertices body.js:293
setVertices(vertices: Array<Vector2d>, index: number, clear: boolean) → {}

set the body vertices to the given one

Parameters:
Name Type Attributes Default Description
vertices Array<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

Protected Methods


update body.js:602
protected update(dt: number) → {boolean}

Updates the parent's position as well as computes the new body's velocity based on the values of force/friction. Velocity chages are proportional to the me.timer.tick value (which can be used to scale velocities). The approach to moving the parent renderable 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)
At this time a call to Body.Update does not call the onBodyUpdate callback that is listed in the constructor arguments.

Parameters:
Name Type Description
dt number

time since the last update in milliseconds.

Returns:
Type Description
boolean

true if resulting velocity is different than 0


Powered by webdoc!