Moscrif API Docs for: 2012q3
Show:

b2PolygonShape Class

Library: box2d

A convex polygon. It is assumed that the interior of the polygon is to the left of each edge.

Item Index

Methods

fromEdge

(
  • v1x
  • v1y
  • v2x
  • v2y
)
B2PolygonShape

Create edge shape. Edge shapes are line segments. These are provided to assist in making a free-form static environment. Edge shapes can collide with circles and polygons bodies but not with themselves. Edge bodies have no volume.

Parameters:

  • v1x Float

    X coordinate of the first point of the body from the centre of the shape. The positive values are in right and top directions.

  • v1y Float

    Y coordinate of the first point of the body from the centre of the shape. The positive values are in right and top directions.

  • v2x Float

    X coordinate of the second point of the body from the centre of the shape. The positive values are in right and top directions.

  • v2y Float

    Y coordinate of the second point of the body from the centre of the shape. The positive values are in right and top directions.

Returns:

B2PolygonShape: Created edge shape.

Example:

var shape = b2PolygonShape.fromEdge(v1x, v1y, v2x, v2y);
var body = ...world.createBody(shape, #dynamic, 1 , 0.3, 0.03);

fromPoly

(
  • array
)
B2PolygonShape

Create new polygon shape acording to the parameters.

Parameters:

  • array Array

    Array of verticles. The array should contains objects with two values: x and y -> distance from center in pixels (positive values are located up and right). Points in array should by in CCW order. Minimum number of vertex are 3 maximum (ussally) 8.

Returns:

B2PolygonShape: Created polygon shape.

Example:

// This defines a triangle in CCW order.
var verticles = {                                                                                                                                                                                                                                    var shape = new Array(
    // top - left
    {x : 0.0 y : 0.0},
    // bottom - left
    {x : 1.0, y : 0.0},
    // bottom - right
    {x : 0.0, y : 1.0}
)
// create a triangle body
var shape = b2PolygonShape.fromPoly(verticles);
var body = ...world.createBody(shape, #dynamic, 1 , 0.3, 0.03);

fromRect

(
  • width
  • height
  • [center]
  • [angle]
)
B2PolygonShape

Create new rectangle shape acording to the parameters.

Parameters:

  • width Float

    The half-width.

  • height Float

    The half-height.

  • [center] Object optional

    X andthe y coordinates of the center of the box in local coordinates.

  • [angle] Float optional

    Rotation angle.

Returns:

B2PolygonShape: Created rectangle shape.

Example:

// load image
var img = Bitmap.fromFile("app://img.png");
// create shape
var shape = b2PolygonShape.fromRect(img.width, img.height);
// create body
var body = this._world.createBody(shape, #dynamic, 1 , 0.3, 0.03);