JavaScript-mechanics-box2D

Материал из Department of Theoretical and Applied Mechanics
Перейти к: навигация, поиск
Кафедра ТМ > Программирование > Интернет > JavaScript > Механика Box2D

Текст программы на языке JavaScript, использованы библиотека Box2DJS, а также исходный код с сайта библиотеки. Для простоты js-скрипт интегрирован прямо в html файл:

Файл "box2D_example.html"

 1 <html>
 2    <head>
 3       <title>Box2dWeb example</title>
 4    </head>
 5    <body onload="init();">
 6       <canvas id="canvas" width="600" height="400"></canvas>
 7    </body>
 8    <script type="text/javascript" src="Box2dWeb-2.1.a.3.min.js"></script>
 9    <script type="text/javascript">
10       var world;
11 
12       function init() {
13          var   b2Vec2 = Box2D.Common.Math.b2Vec2
14          	,	b2BodyDef = Box2D.Dynamics.b2BodyDef
15          	,	b2Body = Box2D.Dynamics.b2Body
16          	,	b2FixtureDef = Box2D.Dynamics.b2FixtureDef
17          	,	b2Fixture = Box2D.Dynamics.b2Fixture
18          	,	b2World = Box2D.Dynamics.b2World
19          	,	b2MassData = Box2D.Collision.Shapes.b2MassData
20          	,	b2PolygonShape = Box2D.Collision.Shapes.b2PolygonShape
21          	,	b2CircleShape = Box2D.Collision.Shapes.b2CircleShape
22          	,	b2DebugDraw = Box2D.Dynamics.b2DebugDraw
23             ;
24 
25          world = new b2World(
26                new b2Vec2(0, 10)    //gravity
27             ,  true                 //allow sleep
28          );
29 
30          var fixDef = new b2FixtureDef;
31          fixDef.density = 1.0;
32          fixDef.friction = 0.5;
33          fixDef.restitution = 0.2;
34 
35          var bodyDef = new b2BodyDef;
36 
37          //create ground
38          bodyDef.type = b2Body.b2_staticBody;
39          bodyDef.position.x = 10;
40          bodyDef.position.y = 13;
41          fixDef.shape = new b2PolygonShape;
42          fixDef.shape.SetAsBox(10, 0.01);
43          world.CreateBody(bodyDef).CreateFixture(fixDef);
44 
45 
46          //create some objects
47           bodyDef.type = b2Body.b2_dynamicBody;
48 
49           fixDef.shape = new b2PolygonShape;    // добавим прямоугольник
50           fixDef.shape.SetAsBox(0.5, 2);        // размеры прямоугольника
51           bodyDef.position.x = 10;
52           bodyDef.position.y = 10;
53           world.CreateBody(bodyDef).CreateFixture(fixDef);
54 
55           fixDef.shape = new b2PolygonShape;
56           fixDef.shape.SetAsBox(2, 0.5);
57           bodyDef.position.x = 10;
58           bodyDef.position.y = 8;
59           world.CreateBody(bodyDef).CreateFixture(fixDef);
60 
61           fixDef.shape = new b2CircleShape(2);  // добавим круг
62           bodyDef.position.x = 13;
63           bodyDef.position.y = 3;
64           world.CreateBody(bodyDef).CreateFixture(fixDef);
65 
66 
67          //setup debug draw
68          var debugDraw = new b2DebugDraw();
69 			debugDraw.SetSprite(document.getElementById("canvas").getContext("2d"));
70 			debugDraw.SetDrawScale(30.0);
71 			debugDraw.SetFillAlpha(0.3);
72 			debugDraw.SetLineThickness(1.0);
73 			debugDraw.SetFlags(b2DebugDraw.e_shapeBit | b2DebugDraw.e_jointBit);
74 			world.SetDebugDraw(debugDraw);
75 
76          window.setInterval(update, 1000 / 60);
77       };
78 
79       function update() {
80          world.Step(
81                1 / 60   //frame-rate
82             ,  10       //velocity iterations
83             ,  10       //position iterations
84          );
85          world.DrawDebugData();
86          world.ClearForces();
87       };
88 
89    </script>
90 </html>

См. также[править]

Примеры