HTML5 creates a game based on simulated physical effects-

Source: Internet
Author: User
HTML5 technology provides a very broad space for today's Web applications to play in the browser. Its powerful features make it no longer difficult for us to develop and play games on the browser. With Canvas and powerful JavaScript engine, we can easily develop casual games. Players just need to open their browsers and enjoy the game without installing plug-ins. Introduction

HTML5 technology provides a very broad space for today's Web applications to play in the browser. Its powerful features make it no longer difficult for us to develop and play games on the browser. With Canvas and powerful JavaScript engine, we can easily develop casual games. Players just need to open their browsers and enjoy the game without installing plug-ins.

This article was made by the NTFusion team, combined with the "save PAPA" released on Google Chrome Web Store, to share with you the experience of using HTML5 to develop physical games.

Build a physical world

All objects in a physical game operate in a set physical world. To create a physical world, we now use the JavaScript version of The Box2D physical engine. When building a physical world, we first need to define the relevant physical objects, and then set the relevant physical parameters according to the characteristics of the game.

// Create a physical world instance var world = new b2World (new b2Vec2 (0, 9.8), true); var scale = 1/30; /* length unit conversion between pixels and Box2D * // create the ground and physical settings var bodyDef = new b2BodyDef (); var body = world. createBody (bodyDef); var fixtureDef = new b2FixtureDef (); fixtureDef. density = 10; fixtureDef. friction = 0.3; fixtureDef. restitution = 0.1; var polygonShape = new b2PolygonShape (); polygonShape. setAsBox (800 * scale, 20 * scale); fixtureDef. shape = polygonShape; body. setType (b2Body. b2_staticBody); body. createFixture (fixtureDef); body. setPosition (new b2Vec2 (400 * scale, 490 * scale); // create the PAPA block and its physical settings var bodyDef = new b2BodyDef (); var body = world. createBody (bodyDef); var fixtureDef = new b2FixtureDef (); fixtureDef. density = 10;/* density is the density */fixtureDef. friction = 0.3;/* friction is the friction coefficient */fixtureDef. restitution = 0.8;/* restitution is the elastic coefficient */var polygonShape = new b2PolygonShape (); polygonShape. setAsBox (30 * scale, 30 * scale); fixtureDef. shape = polygonShape; body. setType (b2Body. b2_dynamicBody); body. createFixture (fixtureDef); body. setPosition (new b2Vec2 (400 * scale, 100 * scale ));


The Box2D engine has a good function, DebugDraw, which can simulate our preset physical world and show it to us. It is very suitable for rapid game prototype development. After the physical world is built, we can see the prototype of the game through the DebugDraw function, so that we can adjust the object parameters and create checkpoints on this basis.

// DebugDraw requires a canvas instance. Therefore, create a b2DebugDraw instance and set the relevant parameter var debugDraw = new b2DebugDraw (); debugDraw. setSprite (document. getElementById ("canvas "). getContext ("2d"); debugDraw. setDrawScale (30.0); debugDraw. setFillAlpha (0.5); debugDraw. setlinehickness (1.0); debugDraw. setFlags (b2DebugDraw. e_shapeBit | b2DebugDraw. e_jointBit); // bound to the physical world instance world. setDebugDraw (debugDraw );



It is the physical world instance in saving PAPA drawn by DebugDraw:

var position =body.GetPosition();var angle =body.GetAngle();displayObject.x = position.x / scale;displayObject.y = position.y / scale;displayObject.rotation = angle * 180 / Math.PI; 



Based on the physical world in the image, we can see the following images:

Var canvas = document. createElement ("canvas"); canvas. width = width;/* width indicates the width of the image */canvas. height = height;/* height indicates the height of the image */canvas. getContext ('2d '). drawImage (sheet, x, y, width, height, 0, 0, width, height );


Summary


HTML5 technology is gradually being improved, and our programmers are constantly working hard to create more good works. The construction of the physical world, display objects, and animation playing technologies discussed in this article are very useful for HTML5 games. We are looking forward to seeing the continuous development of HTML5 technology and more innovative HTML5 web games.

The above is the content for creating a game based on simulated physical effects in HTML5. For more information, see PHP Chinese Network (www.php1.cn )!

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.