-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbox2d-test.html
225 lines (178 loc) · 4.86 KB
/
box2d-test.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<title>Canvas game</title>
<link rel="stylesheet" href="css/reset.css" type="text/css" />
<link rel="stylesheet" href="css/base.css" type="text/css" />
<script type="text/javascript" src="lib/jquery/jquery-1.7.2.js"></script>
<script type="text/javascript" src="lib/box2d/Box2dWeb.js"></script>
<script type="text/javascript" src="lib/hotkeys/hotkeys.js"></script>
<script type="text/javascript" src="lib/jaws/jaws.js"></script>
<script type="text/javascript">
$(document).ready(function(){
init();
});
function init(){
var b2Vec2 = Box2D.Common.Math.b2Vec2;
var b2BodyDef = Box2D.Dynamics.b2BodyDef;
var b2Body = Box2D.Dynamics.b2Body;
var b2FixtureDef = Box2D.Dynamics.b2FixtureDef;
var b2Fixture = Box2D.Dynamics.b2Fixture;
var b2World = Box2D.Dynamics.b2World;
var b2MassData = Box2D.Collision.Shapes.b2MassData;
var b2PolygonShape = Box2D.Collision.Shapes.b2PolygonShape;
var b2CircleShape = Box2D.Collision.Shapes.b2CircleShape;
var b2DebugDraw = Box2D.Dynamics.b2DebugDraw;
var canvas = $("#canvas");
var _canvas = canvas.get(0);
var context = _canvas.getContext("2d");
var cHeight = canvas.height();
var cWidth = canvas.width();
var scale = "1:60"; //1m is 60px
function px2m(px){
var ratio = scale.split(":");
ratio = parseInt(ratio[0])/parseInt(ratio[1]);
return px*ratio;
}
function m2px(m){
var ratio = scale.split(":");
ratio = parseInt(ratio[1])/parseInt(ratio[0]);
return m*ratio;
}
var world = new b2World(
new b2Vec2(0,10), //gravity
true //allow sleep
);
var fixDef = new b2FixtureDef;
fixDef.density = 1.0;
fixDef.friction = 0.5;
fixDef.restitution = 0.2;
var bodyDef = new b2BodyDef;
//create boundries
bodyDef.type = b2Body.b2_staticBody;
fixDef.shape = new b2PolygonShape;
//make floor
var floor = {
x : px2m(cWidth),
y : px2m(cHeight)*2,
width : px2m(cWidth),
height : px2m(2)
}
fixDef.shape.SetAsBox(floor.width,floor.height);
bodyDef.position.Set(floor.x,floor.y);
world.CreateBody(bodyDef).CreateFixture(fixDef);
//make roof
var roof = {
x : px2m(cWidth),
y : 0,
width : px2m(cWidth),
height : px2m(2)
}
fixDef.shape.SetAsBox(roof.width,roof.height);
bodyDef.position.Set(roof.x,roof.y);
world.CreateBody(bodyDef).CreateFixture(fixDef);
//make walls
var wall = {
x : 0,
y : px2m(cHeight),
width : px2m(2),
height : px2m(cHeight)
}
fixDef.shape.SetAsBox(wall.width,wall.height);
bodyDef.position.Set(wall.x,wall.y);
world.CreateBody(bodyDef).CreateFixture(fixDef);
//make walls
var wall = {
x : px2m(cWidth)*2,
y : px2m(cHeight),
width : px2m(2),
height : px2m(cHeight)
}
fixDef.shape.SetAsBox(wall.width,wall.height);
bodyDef.position.Set(wall.x,wall.y);
world.CreateBody(bodyDef).CreateFixture(fixDef);
//create some objects
bodyDef.type = b2Body.b2_dynamicBody;
/*GUMBA ENEMY*/
//fixDef.density = 1;
//fixDef.friction = 1;
//fixDef.restitution = 1;
//make a circle
fixDef.shape = new b2CircleShape(
0.5 //radius
);
//position and draw
bodyDef.position.x = 1;
bodyDef.position.y = 1;
var enemy = world.CreateBody(bodyDef).CreateFixture(fixDef);
function drawEnemy(){
//set speed
var speed = 0.05;
//set impulse
enemy.impulse = [0,0];
//get collisions
var collisions = enemy.m_body.m_contactList;
//set jumping
enemy.jumping = true;
//get velocity
var velocity = enemy.m_body.GetLinearVelocity();
//work out direction traveling
if(velocity.x > 0){
enemy.dirX = "right";
}else if(velocity.x < 0){
enemy.dirX = "left";
}
if(velocity.y > 0){
enemy.dirY = "down";
}else if(velocity.y < 0){
enemy.dirY = "up";
}
//move player
if(collisions){
if(collisions.contact.IsTouching()){
//set x impulse
enemy.impulse[0] = speed;
if(enemy.dirX == "left"){
enemy.impulse[0] = -speed;
}
if(enemy.dirY == "down"){
enemy.jumping = false;
}
}
}
//if jumping
if(!enemy.jumping){
enemy.jumping = true;
enemy.impulse[1] = -speed*70;
}
//apply force
var force = new b2Vec2(enemy.impulse[0],enemy.impulse[1]);
enemy.m_body.ApplyImpulse(force,enemy.m_body.GetWorldCenter());
}
console.log(enemy);
/*END GUMBA ENEMY*/
var worldBody = world.GetBodyList();
//setup debug draw
var debugDraw = new b2DebugDraw();
debugDraw.SetSprite(context);
debugDraw.SetDrawScale(30.0);
debugDraw.SetFillAlpha(0.5);
debugDraw.SetLineThickness(1.0);
debugDraw.SetFlags(b2DebugDraw.e_shapeBit | b2DebugDraw.e_jointBit);
world.SetDebugDraw(debugDraw);
window.setInterval(update,1000/60);
//the animation loop
function update(){
world.Step(1/60,10,10);
world.DrawDebugData();
world.ClearForces();
drawEnemy();
}
}
</script>
</head>
<body>
<canvas id="canvas" width="400" height="200"></canvas>
</body>
</html>