-
-
Notifications
You must be signed in to change notification settings - Fork 653
Shapes
since version 1.0.0, melonJS introduced the concept of Collision shape, that replaced the old collisionBox implementation by a more generic and flexible API. In 1.1.0, Entity class hierarchy was changed significantly; all "physics" properties now belong to the entity's "Body", including the collision shape.
A collision shape is either a me.Rect, me.Ellipse or me.PolyShape object attached to the object entity, 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 -
setShape()to set the default shape to be used for collision -
getShape()to get the current collision shape (also used internally by the engine)
Shape object are now 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 can be 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 for now, generate the quadtree in the ticket-103 branch, or identify which objects has been clicked).
Let's take the example where for any reason we need to know the size of the object collision shape :
- Previous Implementation (melonJS 0.9.x) :
var size = entity.collisionBox.width;- Shape Implementation (melonJS 1.0.x)
var size = entity.getShape().width;- Shape Implementation (melonJS 1.1.x +)
var size = entity.body.getShape().width;If you currently only use rectangular shapes, the safest upgrade path is to use the getBounds() function:
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, but we do however have now a very flexible API that can transparently manage any kind of shape!
The advantages can only fully be realized starting with v1.2.0, where each Body may contain multiple shapes, and all collisions are performed by the same code globally; Shapes colliding with other shapes.