HTML5 game development-box2dweb application (I)-create various Rigid Bodies

Source: Internet
Author: User

At the beginning of this article, we will introduce the new functions of lufylegend-1.4.0, how to combine box2dweb to create a physical world and various rigid bodies in this physical world

Preparations

First you need to download HTML5 open source library lufylegend-1.4.0

Http://blog.csdn.net/lufy_legend/article/details/7644932

Box2dweb can be downloaded here

Http://code.google.com/p/box2dweb/downloads/list

Prepare three images,

Create a circular, rectangular, and triangular rigid body respectively.

Prepare now.
The following uses the lloadmanage class in lufylegend. js to read images:

init(10,"mylegend",800,400,main);var backLayer,cLayer,wallLayer,bitmap,loadingLayer;var imglist = {};var imgData = new Array({name:"bird1",path:"./images/bird1.png"},{name:"bird2",path:"./images/bird2.png"},{name:"stage01",path:"./images/stage01.png"});function main(){LGlobal.setDebug(true);backLayer = new LSprite();addChild(backLayer);loadingLayer = new LoadingSample3();backLayer.addChild(loadingLayer);LLoadManage.load(imgData,function(progress){loadingLayer.setProgress(progress);},function(result){imglist = result;backLayer.removeChild(loadingLayer);loadingLayer = null;gameInit();});}

Lglobal. setdebug (true); debug mode is enabled. When creating a rigid body, the debug rigid body created by box2dweb is displayed together. When publishing a finished product, Disable debug mode.

The lloadmanage class has three parameters: $ list, $ onupdate, $ oncomplete,

$ List is the Image array to be read, $ onupdate is the function to be called during reading, and $ oncomplete is the function called when reading is complete.

The loadingsample3 class is the progress bar display function in the lufylegend. js library. In addition to the loadingsample3 class, there are also loadingsample1 class and loadingsample2 class.

Before creating a rigid body, create up to four walls, left to right, and right to control the moving range of the created rigid body.

wallLayer = new LSprite();wallLayer.x = 400;wallLayer.y = 0;backLayer.addChild(wallLayer);wallLayer.addBodyPolygon(800,10,0);wallLayer = new LSprite();wallLayer.x = 0;wallLayer.y = 200;backLayer.addChild(wallLayer);wallLayer.addBodyPolygon(10,400,0);wallLayer = new LSprite();wallLayer.x = 400;wallLayer.y = 400;backLayer.addChild(wallLayer);wallLayer.addBodyPolygon(800,10,0);wallLayer = new LSprite();wallLayer.x = 800;wallLayer.y = 200;backLayer.addChild(wallLayer);wallLayer.addBodyPolygon(10,400,0);

If you do not know how to use lsprite, please read my previous articles.

Addbodypolygon (W, H, type, density, friction, restitution) adds a rectangular body to lsprite. The parameters are: (width, height, static or dynamic, density, friction, elasticity)

If the body is set to static in the 3rd parameters, the following parameters can be omitted. Here, the four walls are set to static.

Then, add a mouse click event to the backlayer to add a rigid body when you click the mouse.

backLayer.addEventListener(LMouseEvent.MOUSE_UP,add);

Add a rigid body to the add method. First, add a circular rigid body. The code for adding a circular rigid body is as follows:

cLayer = new LSprite();cLayer.x = 50 + Math.random()*700;cLayer.y = 50;backLayer.addChild(cLayer);bitmap = new LBitmap(new LBitmapData(imglist["bird1"]));cLayer.addChild(bitmap);cLayer.addBodyCircle(bitmap.getWidth()*0.5,bitmap.getHeight()*0.5,bitmap.getWidth()*0.5,1,.5,.4,.5);

Addbodycircle (radius, CX, Cy, type, density, friction, restitution) is to add a circular body to lsprite. The parameters are (radius, Center Coordinate x, y, static or dynamic, density, friction, elasticity)

Okay. After running the code, click the mouse to get the following result.

Test URL

Http://lufy.netne.net/lufylegend-js/lufylegend-1.4/box2d/sample01/index.html

Next, modify the add method. In addition to adding a circular rigid body, you are likely to add a Rectangular Rigid Body.

var rand = Math.random();if(rand < 0.5){cLayer = new LSprite();cLayer.x = 50 + Math.random()*700;cLayer.y = 50;backLayer.addChild(cLayer);bitmap = new LBitmap(new LBitmapData(imglist["bird1"]));cLayer.addChild(bitmap);cLayer.addBodyCircle(bitmap.getWidth()*0.5,bitmap.getHeight()*0.5,bitmap.getWidth()*0.5,1,.5,.4,.5);}else{cLayer = new LSprite();cLayer.x = 50 + Math.random()*700;cLayer.y = 50;backLayer.addChild(cLayer);bitmap = new LBitmap(new LBitmapData(imglist["stage01"]));cLayer.addChild(bitmap);cLayer.addBodyPolygon(bitmap.getWidth(),bitmap.getHeight(),1,5,.4,.2);}

The addbodypolygon method has been described above. It is used to add a Rectangular Rigid Body to lsprite.

After running the code, click the mouse to get the following result.

Test URL

Http://lufy.netne.net/lufylegend-js/lufylegend-1.4/box2d/sample01/index2.html

Finally, it is to add a triangle rigid body, and the starting point can be any polygon rigid body. It is used by the addbodyvertices method of lsprite,

Addbodyvertices (vertices, CX, Cy, type, density, friction, restitution) is based on the vertex array to add any polygon rigid bodies, parameters are (vertex array, central coordinate, static or dynamic, density, friction, elasticity)

It should be mentioned that this method has a small bug, that is, to change the coordinate of lsprite to (), so we need to use the setposition method to set the coordinates for lsprite again. This bug is found in lufylegend. the next JS update will solve the problem. The code for adding a triangle rigid body is as follows:

cLayer = new LSprite();backLayer.addChild(cLayer);bitmap = new LBitmap(new LBitmapData(imglist["bird2"]));cLayer.addChild(bitmap);var shapeArray = [[[0,54],[27,0],[54,54]]];cLayer.addBodyVertices(shapeArray,27,27,1,.5,.4,.5);cLayer.box2dBody.SetPosition(new LGlobal.box2d.b2Vec2((50 + Math.random()*700)/LGlobal.box2d.drawScale,50/LGlobal.box2d.drawScale));

After running the code, click the mouse to get the following result.

Test URL

Http://lufy.netne.net/lufylegend-js/lufylegend-1.4/box2d/sample01/index3.html

This article ends with the final code. Please look forward to the next tutorial.

<!DOCTYPE html>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.