forked from agentcooper/Box2d-networking
-
Notifications
You must be signed in to change notification settings - Fork 1
/
demo.html
167 lines (131 loc) · 3.86 KB
/
demo.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
<!doctype>
<html>
<head><title>Box2dWeb Demo</title></head>
<body onload="init();">
<canvas id="canvas" width="600" height="400" style="background-color:#333333;"></canvas>
</body>
<script src="box2d.js"></script>
<script src="common.js"></script>
<script>
var isMouseDown = false;
var id = null;
function setupCanvas() {
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);
}
function init() {
setupWorld();
setupCanvas();
window.setInterval(update, 1000 / 60);
// mouse
var mouseX, mouseY, mousePVec, selectedBody, mouseJoint = false;
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 = undefined;
mouseY = undefined;
}, 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;
}
var body;
//update
function update() {
if (isMouseDown) {
if (!body) {
body = getBodyAtMouse();
if (body) mouseJoint = true;
}
if (mouseJoint && (body != null)) {
var data = {"x": mouseX, "y": mouseY, "bodyId": body.GetUserData().bodyId};
data.id = id;
socket.send(JSON.stringify(data));
}
} else {
if (mouseJoint) {
var data = {"destroyId": id};
data.id = id;
socket.send(JSON.stringify(data));
}
mouseJoint = false;
body = 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>
<script src="http://127.0.0.1:2003/socket.io/socket.io.js"></script>
<script>
var socket = io.connect(xhost + ':' + xport);
socket.on('connect',function() {
console.log('Client has connected to the server!');
connected = true;
});
socket.on('message', function(data) {
data = JSON.parse(data);
if (data.hasOwnProperty("startId")) {
id = data.startId;
}
if (data.hasOwnProperty('destroyId')) {
deleteJoint(data.destroyId);
return;
}
if (data.hasOwnProperty('bodyId')) {
updateJoints(data);
return;
}
});
socket.on('disconnect',function() {
console.log('The client has disconnected!');
connected = false;
});
</script>