forked from willian12345/Box2D-for-Javascript-Games
-
Notifications
You must be signed in to change notification settings - Fork 0
/
3.html
executable file
·193 lines (176 loc) · 7.32 KB
/
3.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
<html>
<head>
<title>Box2dJS</title>
<style>
.wrap{
position: relative;
}
.info{
position: absolute;
left: 0;
top: 0;
color: #fff
}
</style>
</head>
<body onload="init();">
<div class="wrap">
<canvas id="canvas" width="640" height="480" style="background-color:#333333;" ></canvas>
<div class="info" id="textMon">
</div>
</div>
</body>
<script src="js/Box2d.min.js"></script>
<script src="js/jquery.min2.0.3.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, 5);
var sleep = true;
var world;
var velIterations = 10;// 速率约束解算器
var posIterations = 10;// 位置约束解算器
var mousePVec;
function main(){
world = new b2World(gravity, sleep);
debugDraw();
brick(275,435,30,30,"breakable");
brick(365,435,30,30,"breakable");
brick(320,405,120,30,"breakable");
brick(320,375,60,30,"unbreakable");
brick(305,345,90,30,"breakable");
brick(320,300,120,60,"unbreakable");
idol(320, 242);
floor();
setInterval(updateWorld, 1000 / 60);
$('#canvas').click(function(e){
var px = e.pageX/worldScale;
var py = e.pageY/worldScale;
mousePVec = new b2Vec2(px/worldScale, py/worldScale);
var test = world.QueryPoint(queryCallback, new b2Vec2(px, py));
console.log(test);
});
}
main();
function queryCallback(fixture){
var touchedBody = fixture.GetBody()
var userData = touchedBody.GetUserData();
if(userData == 'breakable'){
world.DestroyBody(touchedBody);
}
return false;
}
function brick(px, py, w, h, s){
var bodyDef = new b2BodyDef();
bodyDef.position.Set(px/worldScale, py/worldScale);
bodyDef.type = b2Body.b2_dynamicBody;
bodyDef.userData = s;
var polygonShape = new b2PolygonShape();
polygonShape.SetAsBox(w/2/worldScale, h/2/worldScale);
var fixtureDef = new b2FixtureDef();
fixtureDef.shape = polygonShape;
fixtureDef.density = 2;
fixtureDef.restitution = .4;
fixtureDef.friction = .5;
var theBrick = world.CreateBody(bodyDef);
theBrick.CreateFixture(fixtureDef);
}
function idol(px, py){
var bodyDef = new b2BodyDef();
bodyDef.position.Set(px/worldScale, py/worldScale);
bodyDef.type = b2Body.b2_dynamicBody;
bodyDef.userData="idol";
var polygonShape = new b2PolygonShape();
polygonShape.SetAsBox(5/worldScale, 20/worldScale);
var fixtureDef = new b2FixtureDef();
fixtureDef.shape = polygonShape;
fixtureDef.density = 1;
fixtureDef.restitution = .4;
fixtureDef.friction = .5;
var theIdol = world.CreateBody(bodyDef);
theIdol.CreateFixture(fixtureDef);
// 创建定向矩形
var bW = 5/worldScale;
var bH = 20/worldScale;
var boxPos = new b2Vec2(0, 10/worldScale);// 可以使用矩形内的相对位置
var boxAngle = -Math.PI / 4;
// 左倾矩形
polygonShape.SetAsOrientedBox(bW, bH, boxPos, boxAngle);
fixtureDef.shape = polygonShape;
theIdol.CreateFixture(fixtureDef);
// 右倾矩形
boxAngle = Math.PI / 4;
polygonShape.SetAsOrientedBox(bW, bH, boxPos, boxAngle);
fixtureDef.shape = polygonShape;
theIdol.CreateFixture(fixtureDef);
// 用多边形方式创建凸多边形
var vertices = new Box2D.NVector();
vertices.push(new b2Vec2(-15/worldScale,-25/worldScale));
vertices.push(new b2Vec2(0,-90/worldScale));
vertices.push(new b2Vec2(15/worldScale, -25/worldScale));
vertices.push(new b2Vec2(0,-10/worldScale));
polygonShape.SetAsVector(vertices, 4);
fixtureDef.shape = polygonShape;
theIdol.CreateFixture(fixtureDef);
}
function floor(){
var bodyDef = new b2BodyDef();
bodyDef.position.Set(320/worldScale, 465/worldScale);
var polygonShape = new b2PolygonShape();
polygonShape.SetAsBox(320/worldScale, 15/worldScale);
var fixtureDef = new b2FixtureDef();
fixtureDef.shape = polygonShape;
fixtureDef.restitution = .4;
fixtureDef.friction = .5;
var theFloor = world.CreateBody(bodyDef);
theFloor.CreateFixture(fixtureDef);
}
var radToDeg = 180/Math.PI;
var idolBody, $text = $('#textMon');
for (var b = world.GetBodyList();
b; b = b.GetNext()) {
console.log(b.GetUserData());
if(b.GetUserData() == 'idol'){
idolBody = b;
}
}
function updateWorld() {
world.Step(1/30, 10, 10);// 更新世界模拟
world.DrawDebugData(); // 显示刚体debug轮廓
world.ClearForces(); // 清除作用力
if(idolBody){
var position = idolBody.GetPosition();
var xPos = Math.round(position.x * worldScale);
var yPos = Math.round(position.y * worldScale);
var angle = idolBody.GetAngle()*radToDeg;
var velocity = idolBody.GetLinearVelocity();
var xVel = Math.round(velocity.x * worldScale);
var yVel = Math.round(velocity.y * worldScale);
$text.html(xPos+','+yPos+'<br/>'+angle+'<br/>'+xVel+','+yVel);
}
}
//setup debug draw
function debugDraw(){
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);
}
};
</script>
</html>