-
Notifications
You must be signed in to change notification settings - Fork 12
/
demo2-2.html
executable file
·71 lines (64 loc) · 2.82 KB
/
demo2-2.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
<html>
<head>
<title>Demo1-1 Box2dJS</title>
</head>
<body>
<canvas id="canvas" width="640" height="480" style="background-color:#333333;" ></canvas>
<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 world;
var worldScale = 30;
function main(){
world = new b2World(new b2Vec2(0, 9.81), true);
var bodyDef = new b2BodyDef();
bodyDef.position.Set(320/worldScale,30/worldScale);
var circleShape = new b2CircleShape(25/worldScale);
var fixtureDef = new b2FixtureDef();
fixtureDef.shape = circleShape;
fixtureDef.density = 1;
fixtureDef.restitution = .6;
fixtureDef.friction = .1;
var theBall = world.CreateBody(bodyDef);
theBall.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);
//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);
setInterval(updateWorld, 1000 / 60);
}
function updateWorld() {
world.Step(1/30,10,10);
world.ClearForces(); // 清除作用力
world.DrawDebugData();
}
main();
}
init();
</script>
</body>
</html>