Introduction
The latest, most basic version had some deficiencies. One of the largest deficiencies was that the car’s physical body didn’t have the same shape as the bitmap drawn onto it.
The car’s chassis was created as a rectangle body, but the chassis is not a rectangle in real. This article is going to show how to adapt the car’s shape onto its real shape.
Image: Car’s physical shape vs. car bitmap.
Polygon body
Except a circle and rectangle body, box2d offers also polygon body, which allows creating all shapes of bodies. In native box2d, polygon shapes are created by some complicated
structures, but Moscrif simplifies this procedure. In Moscrif, a polygon body is created by
the addPolygonBody
method. The addPolygonBody
method is used to create
a polygon body as well as a rectangle body. The difference is in the last two parameters. If the last two (sixth and seventh) parameters are integers, box2d
creates a rectangle body (width is sixth parameter, height is seventh). Otherwise, if seventh parameter is not specified the method creates
a polygon body.
All vertex is set in an array by its x and y position. The position is calculated from the centre of the shape in pixels. Vertex must be ordered counter clock wise.
Example: Create a triangle
var triangle = [
{x : 0, y : 50},
{x : -50, y : -50},
{x : 50, y : -50},
]
triangle = scene.addPolygonBody(this._images.wheel, #dynamic, 1.2, 1.0, 0.0, triangle);
Adding a polygon body allows creating convex polygons. A convex polygon is every polygon whose all internal angles are less than or equal to 180 degrees and every line
segment between two vertices remains inside or on the boundary of the polygon. Of course, there is also a simple way to create non-convex shapes. The simplest convex
shape is a triangle. The triangles are used to construct concave shapes. Constructing concave (non convex) shapes from triangles is often used also in other frameworks
or SDKs including Direct3d. All concave bodies can be created by two or more triangles.
Image: A concave pentagon can be created by two convex triangles as it in the next picture.
The following code shows how to make it in Moscrif. The pentagon consists of two triangles which are connected together by
a weld joint.
Example: create pentagon body
var partA = [
{x : -50, y : 50},
{x : -50, y : -50},
{x : 50, y : -50},
]
var bodyA = scene.addPolygonBody(null, #dynamic, 1.2, 1.0, 0.0, partA);
bodyA.setPosition(System.width / 2, System.height / 2);
var partB = [
{x : 50, y : 50},
{x : 0, y : 0},
{x : 50, y : -50},
]
var bodyB = scene.addPolygonBody(null, #dynamic, 1.2, 1.0, 0.0, partB);
bodyB.setPosition(System.width / 2, System.height / 2);
this._weldJoint = scene.createWeldJoint(bodyA, bodyB, System.width / 2, System.height / 2, false);
Now it is clear how to create a polygon body. So let’s change the car’s shape onto its real shape. To simplify this sample the chassis is created from
a convex body:
Image: Car shape
The car’s chassis shape is defined in an array with eight vertex.
var shape = [
{x:122,y:7},
{x:11,y:51},
{x:-45,y:50},
{x:-137,y:-4},
{x:-150,y:-40},
{x:-69,y:-54},
{x:140,y:-50},
{x :150,y:-21}
];
this._body = scene.addPolygonBody(this._images.body, #dynamic, 0.1, 0.0,
0.0, shape);
this._body.z = 2;
this._body.scale = this._scale;
this._body.setPosition(x, y);
The minimum number of vertices in the body is three and the maximum is the value of
the b2_maxPolygonVertices
constant. The value of b2_maxPolygonVertices
is usually 8 (its limitation in box2d).
Summary
This article showed how to create a polygon body in Moscrif, but also offered many useful information for all box2d developers.