Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Mobile / iOS

Box 2D vehicles - Part 2 (box2d polygon shape)

4.71/5 (7 votes)
30 Oct 2012CPOL2 min read 36.3K   1  
In the previous part (Box 2d vehicles - part 1), I showed you how to create a basic box2d car using Moscrif SDK. The following article is going to show some advanced techniques and upgrades the game.

Image 1

Image 2

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.

Image 3

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

Java
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.

Image 4

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

Java
// create first triangle
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);
 
// create second triangle
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);
 
// connect triangles together
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

Image 5

The car’s chassis shape is defined in an array with eight vertex.

Java
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._images.body.width, this._images.body.height);
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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)