-
Notifications
You must be signed in to change notification settings - Fork 0
/
Demo Juryrig.html
executable file
·262 lines (224 loc) · 9.5 KB
/
Demo Juryrig.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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
<html>
<head>
<title>Box2dWeb Demo</title>
</head>
<body onload="init();">
<canvas id="canvas" width="600" height="400" style="background-color:#333333;" ></canvas>
</body>
<script type="text/javascript" src="Box2dWeb-2.1.a.3.min.js"></script>
<script type="text/javascript">
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
, b2WeldJointDef = Box2D.Dynamics.Joints.b2WeldJointDef
;
var world = new b2World(
new b2Vec2(0, 0) //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 ground
bodyDef.type = b2Body.b2_staticBody;
fixDef.shape = new b2PolygonShape;
fixDef.shape.SetAsBox(20, 2);
bodyDef.position.Set(10, 400 / 30 + 1.8);
world.CreateBody(bodyDef).CreateFixture(fixDef);
bodyDef.position.Set(10, -1.8);
world.CreateBody(bodyDef).CreateFixture(fixDef);
fixDef.shape.SetAsBox(2, 14);
bodyDef.position.Set(-1.8, 13);
world.CreateBody(bodyDef).CreateFixture(fixDef);
bodyDef.position.Set(21.8, 13);
world.CreateBody(bodyDef).CreateFixture(fixDef);
//create some objects
bodyDef.type = b2Body.b2_dynamicBody;
for(var i = 0; i < 50; ++i) {
if(Math.random() > 0.5) {
fixDef.shape = new b2PolygonShape;
fixDef.shape.SetAsBox(
Math.random() + 0.1 //half width
, Math.random() + 0.1 //half height
);
//TODO JVEDIT
} else if(Math.random() > 0.9) {
px = Math.random() * 9;
py = Math.random() * 9;
fixDef.shape = new b2PolygonShape;
fixDef.shape.SetAsBox(0.5,0.5);
bodyDef.position.Set(px+2,py+2);
bodyDef.position.x = px + 0.5;
bodyDef.position.y = py + 0.5;
b1 = world.CreateBody(bodyDef);
b1.CreateFixture(fixDef);
bodyDef.position.x = px - 0.5;
bodyDef.position.y = py + 0.5;
b2 = world.CreateBody(bodyDef);
b2.CreateFixture(fixDef);
bodyDef.position.x = px + 0.5;
bodyDef.position.y = py - 0.5;
b3 = world.CreateBody(bodyDef);
b3.CreateFixture(fixDef);
bodyDef.position.x = px - 0.5;
bodyDef.position.y = py - 0.5;
b4 = world.CreateBody(bodyDef);
b4.CreateFixture(fixDef);
jdef = new b2WeldJointDef();
jdef.Initialize(b1,b2,b1.GetWorldCenter());
world.CreateJoint(jdef);
jdef.Initialize(b1,b3,b1.GetWorldCenter());
world.CreateJoint(jdef);
jdef.Initialize(b2,b4,b2.GetWorldCenter());
world.CreateJoint(jdef);
jdef.Initialize(b3,b4,b3.GetWorldCenter());
world.CreateJoint(jdef);
//TODO /JVEDIT*/
} else {
fixDef.shape = new b2CircleShape(
Math.random() + 0.1 //radius
);
}
bodyDef.position.x = Math.random() * 10;
bodyDef.position.y = Math.random() * 10;
world.CreateBody(bodyDef).CreateFixture(fixDef);
}
//setup debug draw
var debugDraw = new b2DebugDraw();
debugDraw.SetSprite(document.getElementById("canvas").getContext("2d"));
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);
//mouse
var mouseX, mouseY, mousePVec, isMouseDown, selectedBody, mouseJoint;
var canvasPosition = getElementPosition(document.getElementById("canvas"));
document.addEventListener("mousedown", function(e) {
isMouseDown = true;
handleMouseMove(e);
document.addEventListener("mousemove", handleMouseMove, true);
}, true);
document.addEventListener("mouseup", function() {
document.removeEventListener("mousemove", handleMouseMove, true);
isMouseDown = false;
mouseX = (e.clientX - canvasPosition.x) / 30;
mouseY = (e.clientY - canvasPosition.y) / 30;
}, true);
function handleMouseMove(e) {
mouseX = (e.clientX - canvasPosition.x) / 30;
mouseY = (e.clientY - canvasPosition.y) / 30;
};
function getBodyAtMouse() {
mousePVec = new b2Vec2(mouseX, mouseY);
var aabb = new b2AABB();
aabb.lowerBound.Set(mouseX - 0.001, mouseY - 0.001);
aabb.upperBound.Set(mouseX + 0.001, mouseY + 0.001);
// Query the world for overlapping shapes.
selectedBody = null;
world.QueryAABB(getBodyCB, aabb);
return selectedBody;
}
function getBodyCB(fixture) {
if(fixture.GetBody().GetType() != b2Body.b2_staticBody) {
if(fixture.GetShape().TestPoint(fixture.GetBody().GetTransform(), mousePVec)) {
selectedBody = fixture.GetBody();
return false;
}
}
return true;
}
//update
//TODO JVEDIT
var forcexo, forceyo, forcext, forceyt, tagbody, orig, pos, diff;
//TODO /JVEDIT
function update() {
if(isMouseDown && (!/*mouseJoint*/tagbody)) {
var body = getBodyAtMouse();
if(body) {
/*
var md = new b2MouseJointDef();
md.bodyA = world.GetGroundBody();
md.bodyB = body;
md.target.Set(mouseX, mouseY);
md.collideConnected = true;
md.maxForce = 300.0 * body.GetMass();
mouseJoint = world.CreateJoint(md);
body.SetAwake(true);
*/
//TODO JVEDIT
tagbody = body;
forcexo = mouseX;
forceyo = mouseY;
//mouseJoint = 1;
//tagbody.ApplyForce(new b2Vec2(0,-500),new b2Vec2(mouseX, mouseY));
//tagbody.SetAwake(true);
//TODO /JVEDIT
}
}
if(/*mouseJoint*/tagbody) {
if(isMouseDown) {
//tagbody.ApplyForce(new b2Vec2(0,-500),new b2Vec2(mouseX, mouseY));
//tagbody.SetAwake(true);
//TODO JVEDIT DEL mouseJoint.SetTarget(new b2Vec2(mouseX, mouseY));
} else {
//world.DestroyJoint(mouseJoint);
//TODO JVEDIT
orig=new b2Vec2(forcexo, forceyo);
pos=new b2Vec2(mouseX, mouseY);
mag = Math.sqrt((mouseX-forcexo)*(mouseX-forcexo)+(mouseY-forceyo)*(mouseY-forceyo));
xm=(mouseX-forcexo)*10/mag;
ym=(mouseY-forceyo)*10/mag;
diff=new b2Vec2(xm, ym);
if(mag > 5) {mag = 5;}
diff.Multiply(mag);
//Force method
//tagbody.ApplyForce(diff, orig);
//Velocity method (me likey more)
initl=tagbody.GetLinearVelocity();
initl.Add(diff);
tagbody.SetLinearVelocity(initl);
tagbody.SetAwake(true);
//*/
tagbody = null;
//TODO /JVEDIT
//mouseJoint = null;
}
}
world.Step(1 / 60, 10, 10);
world.DrawDebugData();
world.ClearForces();
};
//helpers
//http://js-tut.aardon.de/js-tut/tutorial/position.html
function getElementPosition(element) {
var elem=element, tagname="", x=0, y=0;
while((typeof(elem) == "object") && (typeof(elem.tagName) != "undefined")) {
y += elem.offsetTop;
x += elem.offsetLeft;
tagname = elem.tagName.toUpperCase();
if(tagname == "BODY")
elem=0;
if(typeof(elem) == "object") {
if(typeof(elem.offsetParent) == "object")
elem = elem.offsetParent;
}
}
return {x: x, y: y};
}
};
</script>
</html>