forked from willian12345/Box2D-for-Javascript-Games
-
Notifications
You must be signed in to change notification settings - Fork 0
/
1.html
executable file
·97 lines (79 loc) · 3.47 KB
/
1.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
<html>
<head>
<title>Box2dJS box2dweb</title>
</head>
<body onload="init();">
<canvas id="canvas" width="640" height="480" style="background-color:#333333;" ></canvas>
</body>
<script src="js/Box2d.min.js"></script>
<script>
function init() {
var b2Vec2 = Box2D.Common.Math.b2Vec2
,b2AABB = Box2D.Collision.b2AABB
,b2BodyDef = Box2D.Dynamics.b2BodyDef
,b2Body = Box2D.Dynamics.b2Body
,b2FixtureDef = Box2D.Dynamics.b2FixtureDef
,b2Fixture = Box2D.Dynamics.b2Fixture
,b2World = Box2D.Dynamics.b2World
,b2MassData = Box2D.Collision.Shapes.b2MassData
,b2PolygonShape = Box2D.Collision.Shapes.b2PolygonShape
,b2CircleShape = Box2D.Collision.Shapes.b2CircleShape
,b2DebugDraw = Box2D.Dynamics.b2DebugDraw
,b2MouseJointDef = Box2D.Dynamics.Joints.b2MouseJointDef
;
var worldScale = 30; // box2d中以米为单位,1米=30像素
var gravity = new b2Vec2(0, 9.81);
var sleep = true;
var world = new b2World(gravity, sleep);
var velIterations = 10;// 速率约束解算器
var posIterations = 10;// 位置约束解算器
var bodyDef = new b2BodyDef();
var fixtureDef = new b2FixtureDef();
bodyDef.position.Set(320/worldScale, 30/ worldScale);
bodyDef.type = b2Body.b2_dynamicBody;
var circleShape = new b2CircleShape(25/worldScale);
fixtureDef.shape = circleShape;
fixtureDef.density = 1;
fixtureDef.restitution = .6;
fixtureDef.friction = .1;
var theBall = world.CreateBody(bodyDef);
theBall.CreateFixture(fixtureDef);
bodyDef.position.Set(308/worldScale, 60/ worldScale);
bodyDef.type = b2Body.b2_dynamicBody;
var circleShape1 = new b2CircleShape(25/worldScale);
fixtureDef.shape = circleShape1;
fixtureDef.density = 1;
fixtureDef.restitution = .6;
fixtureDef.friction = .1;
var theBall1 = world.CreateBody(bodyDef);
theBall1.CreateFixture(fixtureDef);
// 定义矩形地面
bodyDef.position.Set(320/worldScale, 470/worldScale); // 复用定义刚体
bodyDef.type = b2Body.b2_staticBody;
var polygonShape = new b2PolygonShape();
polygonShape.SetAsBox(320/worldScale, 10/worldScale);
fixtureDef.shape = polygonShape; // 复用夹具
var theFloor = world.CreateBody(bodyDef);
theFloor.CreateFixture(fixtureDef);
function updateWorld() {
world.Step(1/30, 10, 10);// 更新世界模拟
world.DrawDebugData(); // 显示刚体debug轮廓
world.ClearForces(); // 清除作用力
}
setInterval(updateWorld, 1000 / 60);
//setup debug draw
var debugDraw = new b2DebugDraw();
debugDraw.SetSprite(document.getElementById("canvas").getContext("2d"));
debugDraw.SetDrawScale(worldScale);
debugDraw.SetFillAlpha(0.5);
debugDraw.SetFlags(b2DebugDraw.e_shapeBit | b2DebugDraw.e_jointBit);
world.SetDebugDraw(debugDraw);
//update
function update() {
world.Step(1 / 60, 10, 10);
world.DrawDebugData();
world.ClearForces();
};
};
</script>
</html>