Skip to content
obiot edited this page Nov 24, 2014 · 9 revisions

since version 1.x, melonJS introduced the concept of Collision shape, that replaced the old collisionBox implementation by a more generic and flexible API. Entity class hierarchy was changed significantly; as all "physics" properties now belong to the entity's "Body", including the collision shape.

A collision shape is either a me.Rect, me.Ellipse, me.Line or me.Polygon object attached to the object entity body, and (when using Tiled), a collision Shape is automatically added to your entity based on the object size and shape defined in Tiled.

Collision shape can also be manually managed through the following functions :

  • addShape() to add a new shape to your objects
  • removeShape()/removeShapeAt() to remove the specified shape from the body shape list
  • getShape() to get the current collision shape at the given index

Note that when adding a me.Rect object using addShape to an entity body, it will be automatically converted to a corresponding me.Polygon object.

Shape object are also relative to their parent object (here an entity), which means that by default their position vector is (0, 0)

Additionally Shape objects also define a getBounds function that is used to get the corresponding bounding box (rectangle) of that Shape, that is useful most of all when managing non rectangular shape (melonJS uses this internally to define to calculate the collision using AABB and generate the quadtree, or identify which objects has been clicked).

Usage example

Let's take the example where for any reason we need to know the size of the object collision shape :

var size = entity.body.getBounds().width;

This gives you the width of the body's bounding box (the smallest rectangular set which contains all of its shapes).

At the end yes it's a bit more complex than before, but we do however have now a very flexible API that can transparently manage any kind of shape, and the advantages can only fully be realized since v2.0.x, where each Body may contain multiple shapes, and all collisions are performed by the same code globally; Shapes colliding with other shapes.