-
Notifications
You must be signed in to change notification settings - Fork 4
/
demo.js
138 lines (125 loc) · 3.58 KB
/
demo.js
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
if (process.getuid() === 0)
require('fs').stat(__filename, function(err, stats) {
if (err) return console.log(err)
process.setuid(stats.uid);
});
var Box2D = require('./public/javascripts/Box2D.min.js').Box2D;
var io = require('socket.io').listen(3001);
io.set('log level', 1);
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,
b2Shape = Box2D.Collision.Shapes.b2Shape;
b2PolygonShape = Box2D.Collision.Shapes.b2PolygonShape,
b2CircleShape = Box2D.Collision.Shapes.b2CircleShape;
function buildWorld() {
var world = new b2World(new b2Vec2(0, 10), true);
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 < 6; ++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
);
} else {
fixDef.shape = new b2CircleShape(
Math.random() + 0.1 //radius
);
}
bodyDef.position.x = Math.random() * 10;
bodyDef.position.y = Math.random() * 10;
bodyDef.userData = i.toString();
world.CreateBody(bodyDef).CreateFixture(fixDef);
}
return world;
}
function serializeWorld(world) {
var data, body, shape;
data = {
bodies: []
};
for (var b = world.m_bodyList; b; b = b.m_next) {
body = {
id: b.m_userData,
transform: b.m_xf,
shapes: []
}
for (f = b.GetFixtureList(); f; f = f.m_next) {
shape = f.GetShape();
switch (shape.m_type) {
case b2Shape.e_circleShape:
{
body.shapes.push({
type: 'c',
p: shape.m_p,
radius: shape.m_radius
});
}
break;
case b2Shape.e_polygonShape:
{
body.shapes.push({
type: 'p',
vertices: shape.m_vertices
});
}
break;
case b2Shape.e_edgeShape:
{
body.shapes.push({
type: 'e',
v1: shape.m_v1,
v2: shape.m_v2
});
}
break;
}
}
data.bodies.push(body);
}
return data;
}
io.sockets.on('connection', function (socket) {
var world = buildWorld();
socket.emit('world', serializeWorld(world));
var intid = setInterval(update, 1000 / 24);
function update() {
var data = {};
var isDone = true;
world.Step(1 / 24, 6, 2);
for (var b = world.m_bodyList; b; b = b.m_next) {
if (b.m_userData && b.IsAwake() && b.IsActive()) {
data[b.m_userData] = b.m_xf;
isDone = false;
}
}
socket.volatile.emit('update', data);
if (isDone) clearInterval(intid);
world.ClearForces();
}
});