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

Constructors

constructor

Properties

ancestor

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

Default

undefined

bounce

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

0

bounds

bounds: Bounds

The AABB bounds box reprensenting this body

collisionType

collisionType: number

define the collision type of the body for collision filtering

Default

collision.types.ENEMY_OBJECT

See

collision.types

Example

// set the body collision type
body.collisionType = me.collision.types.PLAYER_OBJECT;

Readonlyfalling

falling: boolean

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

Default

false

force

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

Default

<0,0>

See

Body.setMaxVelocity

Example

// 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;
} elseif (me.input.isKeyPressed("right")) {
this.body.force.x = this.body.maxVel.x;
}
}

friction

friction: Vector2d

body friction

Default

<0,0>

gravityScale

gravityScale: number

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

See

World.gravity

Default

1.0

ignoreGravity

ignoreGravity: boolean

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

See

World.gravity

Default

false

ReadonlyisStatic

isStatic: boolean

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

Default

false

Readonlyjumping

jumping: boolean

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

Default

false

mass

mass: number

the body mass

Default

1

maxVel

maxVel: Vector2d

max velocity (to limit body velocity)

Default

<490,490>

onBodyUpdate

onBodyUpdate: undefined | Function

vel

vel: Vector2d

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

See

Body.force

Default

<0,0>

Methods

addShape

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

    Parameters

    Returns number

    the shape array length

    Example

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

addVertices

  • addVertices(vertices, index?): void
  • add the given vertices to the body shape

    Parameters

    • vertices: Vector2d []

      an array of me.Vector2d points defining a convex hull

    • Optionalindex: number = 0

      the shape object for which to set the vertices

    Returns void

contains

  • contains(...args): boolean
  • Returns true if the any of the shape composing the body contains the given point.

    Parameters

    • Rest...args: any[]

    Returns boolean

    true if contains

    Example

    if (mySprite.body.contains(10, 10)) {
    // do something
    }
    // or
    if (mySprite.body.contains(myVector2d)) {
    // do something
    }

forEach

  • forEach(callback, thisArg?): void
  • 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

    • callback: Function

      fnction to execute on each element

    • OptionalthisArg: object

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

    Returns void

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

fromJSON

  • fromJSON(json, id?): number
  • add collision mesh based on a JSON object (this will also apply any physic properties defined in the given JSON file)

    Parameters

    • json: object

      a JSON object as exported from a Physics Editor tool

    • Optionalid: string

      an optional shape identifier within the given the json object

    Returns number

    how many shapes were added to the body

    See

    https://www.codeandweb.com/physicseditor

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

getBounds

getShape

removeShape

removeShapeAt

  • removeShapeAt(index): number
  • remove the shape at the given index from the body shape list

    Parameters

    • index: number

      the shape object at the specified index

    Returns number

    the shape array length

respondToCollision

rotate

  • rotate(angle, v?): Body
  • Rotate this body (counter-clockwise) by the specified angle (in radians). Unless specified the body will be rotated around its center point

    Parameters

    • angle: number

      The angle to rotate (in radians)

    • Optionalv: Vector2d = ...

      an optional point to rotate around

    Returns Body

    Reference to this object for method chaining

setCollisionMask

  • setCollisionMask(bitmask?): void
  • 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.

    Parameters

    • Optionalbitmask: number = collision.types.ALL_OBJECT

      the collision mask

    Returns void

    See

    collision.types

    Example

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

setCollisionType

  • setCollisionType(type): void
  • define the collision type of the body for collision filtering

    Parameters

    • type: number

      the collision type

    Returns void

    See

    collision.types

    Example

    // set the body collision type
    body.collisionType = me.collision.types.PLAYER_OBJECT;

setFriction

setMaxVelocity

  • setMaxVelocity(x, y): void
  • cap the body velocity (body.maxVel property) to the specified value

    Parameters

    • x: number

      max velocity on x axis

    • y: number

      max velocity on y axis

    Returns void

setStatic

  • setStatic(isStatic?): void
  • set the body as a static body static body do not move automatically and do not check againt collision with others

    Parameters

    • OptionalisStatic: boolean = true

    Returns void

setVertices

  • setVertices(vertices, index?, clear?): void
  • set the body vertices to the given one

    Parameters

    • vertices: Vector2d []

      an array of me.Vector2d points defining a convex hull

    • Optionalindex: number = 0

      the shape object for which to set the vertices

    • Optionalclear: boolean = true

      either to reset the body definition before adding the new vertices

    Returns void

Protectedupdate

  • update(): boolean
  • Protected

    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.

    Returns boolean

    true if resulting velocity is different than 0

Settings

Member Visibility

On This Page

AltStyle によって変換されたページ (->オリジナル) /