forked from Ostkaka/physicsmultiplay
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cannon_server.js
615 lines (519 loc) · 19.2 KB
/
cannon_server.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
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
/**
* Server-side script for the cannon multiplayer game
*/
"use strict";
// Process title
process.title = 'PhysicsMultiplay';
var defaultPort = 3000;
// Do some requirements that I do not understand
var express = require('express')
, routes = require('./routes')
, user = require('./routes/user')
, http = require('http')
, path = require('path')
, WebSocketServer = require('websocket').server
, Buffer = require('buffer').Buffer;
var app = express();
// Configure stuff...
app.configure(function(){
app.set('port', process.env.PORT || defaultPort);
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));
});
// I do not know what this is
app.configure('development', function(){
app.use(express.errorHandler());
});
/***********************************************************************
* Send the HTML page to render when client connects to the servers
************************************************************************/
app.get('/', function(request, response){
response.render('client_index');
});
/**************************
* Get dependencies
***************************/
var jQuery = require('./public/js/jquery-extend.js');
Object.extend = jQuery.extend; // box2d.js needs Object.extend
var CANNON = require('./public/js/cannon.js');
var util = require('./public/js/utility.js');
/**********************************************************************
* Start HTTP server and WebSockets
**********************************************************************/
var server = http.createServer(app).listen(app.get('port'), function(){
console.log("Express server listening on port " + app.get('port'));
});
/**
* Tie Web socket server in httpserver
*/
var wsServer = new WebSocketServer({
httpServer: server
});
/**********************************************************************
* Define global variables
***********************************************************************/
var DEBUG_LOG = false;
/*
* Define cannon world
*/
var cannonWorld = false;
var dt = 1/60;
var physicsMaterial = false;
var sphereShape, sphereBody;
var numBodies = 0;
/*
* Define object arrays in the cannon world
*/
// This is an associtive array for entities in the simulation
// Each cannon entity is associated with an id number
var nunmConnections = 0;
var cannonEntities = {};
var clientConnections = {};
var clientInformation = {};
var colors = generateColors();
/******************************************************************
* Define start of server
******************************************************************/
initCannon();
createScene();
/**********************************************************************
* Server request function and the connections function
***********************************************************************/
wsServer.on('request', function(request) {
// Log when someone connects to the to the websocket server
console.log((new Date()) + ' Connection from origin ' + request.origin + '.');
// Accept connection
var connection = request.accept(null, request.origin);
/************************************************************
* Show indipendent server information for each client here
*************************************************************/
//var clientIndex = clients.push(connection) - 1;
var clientId = ++nunmConnections;
clientConnections[clientId.toString()] = connection;
var clientColor = false;
var clientName = false; // Spawn some default name maybe?
// All is given a mouse joint to play with
var mouseJoint = false;
console.log((new Date()) + ' Connection accepted.' + 'Number of connections: ' + Object.size(clientConnections));
/*
* Handle infromation sent form client
*/
connection.on('message', function(message) {
if (message.type === 'utf8') {
// try to parse json (assume information from client is json)
try {
var jd = JSON.parse(message.utf8Data);
} catch (e) {
console.log('Not JSON format! : ', message.utf8Data);
return;
}
if(jd.type === "clientjoin") {
// Set user name
clientName = jd.data.name;
clientColor = colors.shift();
// Add the client information to server
addClient(clientId,clientName,clientColor);
//Set username in clientInformation
clientInformation[clientId.toString()].name = clientName;
// Send accept message
sendClientJoinAccept(clientId,clientName,clientColor);
// Send updated player info to all clients
sendUpdatedPlyersInfo();
}
else if(jd.type === "gravity") {
// Add point gravity field on all objects in the scene
addPointGravityForce(jd.data.position,jd.data.force);
} else if (jd.type === "killrequest") {
// Remove the object from the scene
if (DEBUG_LOG)
console.log(JSON.stringify(jd));
// Remove the entity
removeEntityFromScene(jd.data.entityId);
// Broadcast the kill event to all clients
broadcastJsonEvent(JSON.stringify(jd));
} else if (jd.type === "createEntity") {
if (DEBUG_LOG)
console.log(JSON.stringify(jd));
// randomize position
var pos = new CANNON.Vec3((Math.random()-0.5)*20,10 + (Math.random()-0.5)*1,(Math.random()-0.5)*20);
if (jd.data.entitytype === "box") {
var halfExtents = new CANNON.Vec3(jd.data.halfExtents[0],jd.data.halfExtents[1],jd.data.halfExtents[2]);
createBox(pos,jd.data.mass,halfExtents);
} else if (jd.data.entitytype === "sphere") {
createSphere(pos,jd.data.mass,jd.data.radius);
}
} else if (jd.type === "clearScene") {
if (DEBUG_LOG)
console.log(JSON.stringify(jd));
clearScene();
broadcastJsonEvent(JSON.stringify(jd));
createScene();
}else if (jd.type === "mousedown") {
if (DEBUG_LOG)
console.log(JSON.stringify(jd));
// Delete previous mouse joint
if(mouseJoint){
if (DEBUG_LOG)
console.log("rem old constriant")
mouseJoint.removeJointConstraint();
mouseJoint = false;
}
// Check if entity exists. Create a mousejoint
if(cannonEntities[jd.data.entityId])
mouseJoint = new MouseJoint(new CANNON.Vec3(jd.data.point[0],jd.data.point[1],jd.data.point[2]),
cannonEntities[jd.data.entityId],cannonWorld);
// Print number of constriants
if (DEBUG_LOG)
console.log("num constriants: " + cannonWorld.constraints.length);
} else if (jd.type === "mouseup") {
if (DEBUG_LOG)
console.log(JSON.stringify(jd));
// Delete previous mouse joint
if(mouseJoint){
if (DEBUG_LOG)
console.log("rem old constriant");
mouseJoint.removeJointConstraint();
mouseJoint = false;
}
} else if (jd.type === "mousemove") {
// Move the mouse joint
var pos = new CANNON.Vec3(jd.data.point[0],jd.data.point[1],jd.data.point[2]);
// Move the mouse joint
if(mouseJoint) mouseJoint.moveJointToPoint(pos);
} else if (jd.type === "message"){
// Check type of message
if (jd.data.type === "chat")
sendChatMessage(jd,clientName,clientColor);
} else {
if (DEBUG_LOG)
console.log("recieved unkown message type: " + jd.type);
}
}
});
// user disconnected
connection.on('close', function(connection) {
console.log((new Date()) + " Peer "
+ connection.remoteAddress + " disconnected.");
sendPlayerInfoEvent(clientId,clientName,clientColor,true);
// remove connection
delete clientConnections[clientId.toString()];
// remove information
delete clientInformation[clientId.toString()];
// Add color back if user got one
if (clientColor)
colors.push(clientColor);
});
});
/***********************************************************************
* CANNON utility functions
***********************************************************************/
/**
* This function clears the cannon scene
*/
function clearScene() {
// Clear from simulation
for(var key in cannonEntities) {
removeEntityFromScene(key);
}
// Clear the body array, just to be safe
cannonEntities = {};
}
/**
* This function converts a cannon world to JSON format for sending over
* a network
*/
function entities2Json(entities){
// Define contents of the json data
var jsonData = {type:"worlddata",bodies:[]}
// Get bodies
var cbodies = cannonWorld.bodies;
for (var key in entities){
var cbody = entities[key];
// Get position and rotation from the body
var pos = cbody.position;
var rot = cbody.quaternion;
// Get shape type
var ctype = cbody.shape.type;
switch(ctype) {
case CANNON.Shape.types.SPHERE:
jsonData.bodies.push({
type:ctype,
id:key,
radius:cbody.shape.radius,
position:[pos.x,pos.y,pos.z],
quaternion:[rot.x,rot.y,rot.z,rot.w]
});
break;
case CANNON.Shape.types.BOX:
var he = cbody.shape.halfExtents;
jsonData.bodies.push({
type:ctype,
id:key,
halfExtents:[he.x,he.y,he.z],
position:[pos.x,pos.y,pos.z],
quaternion:[rot.x,rot.y,rot.z,rot.w]
});
break;
case CANNON.Shape.types.PLANE:
jsonData.bodies.push({
type:ctype,
id:key,
position:[pos.x,pos.y,pos.z],
quaternion:[rot.x,rot.y,rot.z,rot.w]
});
break;
case CANNON.Shape.types.COMPOUND:
// I have no idea what to do here
console.log('WARNING: This shape is not supported by the jsonData parsing');
break;
case CANNON.Shape.types.CONVEXPOLYHEDRON:
console.log('WARNING: This shape is not supported by the jsonData parsing');
break;
default:
console.log('WARNING: This shape is not supported by the jsonData parsing');
break;
}
}
return jsonData;
}
function removeEntityFromScene(entityId) {
// Remove from the scene
if(cannonEntities[entityId] && cannonEntities[entityId].world)
cannonWorld.remove(cannonEntities[entityId]);
// Remove from the object array
delete cannonEntities[entityId];
}
function shapeTypeTostring(type) {
var retstring = false;
switch(type)
{
case CANNON.Shape.types.SPHERE:
retstring = 'CANNON.Shape.types.SPHERE';
break;
case CANNON.Shape.types.BOX:
retstring = 'CANNON.Shape.types.BOX';
break;
case CANNON.Shape.types.PLANE:
retstring = 'CANNON.Shape.types.PLANE';
break;
case CANNON.Shape.types.COMPOUND:
retstring = 'CANNON.Shape.types.COMPOUND';
break;
case CANNON.Shape.types.CONVEXPOLYHEDRON:
retstring = 'CANNON.Shape.types.CONVEXPOLYHEDRO';
break;
default:
retstring = 'WARNING: This shape is not supported';
break;
}
return retstring;
}
function printWorldInfromation(cannonWorld){
// Get bodies from world
var bodies = cannonWorld.bodies;
console.log("*************** World information ***************");
// Loop all bodies in the world and then print information
for (var i=0 ; i < cannonWorld.numObjects() ; i++){
var body = bodies[i];
var positionStr = body.position.toString();
var typeString = shapeTypeTostring(body.shape.type);
var bodystring = "type: " + typeString +
"|| position" + positionStr;
console.log(bodystring);
}
}
/**************************
* Cannon init function
***************************/
function initCannon(){
// Setup our world
cannonWorld = new CANNON.World();
cannonWorld.quatNormalizeSkip = 0;
cannonWorld.quatNormalizeFast = false;
cannonWorld.solver.setSpookParams(50000000,10);
cannonWorld.solver.iterations = 10;
cannonWorld.gravity.set(0,-20,0);
cannonWorld.broadphase = new CANNON.NaiveBroadphase();
// Create a slippery material (friction coefficient = 0.0)
physicsMaterial = new CANNON.Material("slipperyMaterial");
var physicsContactMaterial = new CANNON.ContactMaterial(physicsMaterial,
physicsMaterial,
0.2, // friction coefficient
0.3 // restitution
);
// We must add the contact materials to the world
cannonWorld.addContactMaterial(physicsContactMaterial);
}
/******************************************************************
* Create scene
******************************************************************/
function createScene() {
// Create a sphere
var mass = 5, radius = 1.3;
sphereShape = new CANNON.Sphere(radius);
sphereBody = new CANNON.RigidBody(mass,sphereShape,physicsMaterial);
sphereBody.position.set(0,10,0);
//sphereBody.linearDamping = 0.01;
cannonWorld.add(sphereBody);
cannonEntities[(numBodies++).toString()] = sphereBody;
// Create a plane
var groundShape = new CANNON.Plane();
var groundBody = new CANNON.RigidBody(0,groundShape,physicsMaterial);
groundBody.quaternion.setFromAxisAngle(new CANNON.Vec3(1,0,0),-Math.PI/2);
cannonWorld.add(groundBody);
cannonEntities[(numBodies++).toString()] = groundBody;
// Add boxes
var halfExtents = new CANNON.Vec3(1,1,1);
var boxShape = new CANNON.Box(halfExtents);
for(var i=0; i<3; i++){
var x = (Math.random()-0.5)*20;
var y = 1 + (Math.random()-0.5)*1;
var z = (Math.random()-0.5)*20;
var boxBody = new CANNON.RigidBody(5,boxShape);
cannonWorld.add(boxBody);
boxBody.position.set(x,y,z);
cannonEntities[(numBodies++).toString()] = boxBody;
}
}
/******************************************************************
* Define callbacks and diverse stuff
******************************************************************/
function addPointGravityForce(position,force) {
var pos = new CANNON.Vec3(position[0],position[1],position[2]);
// loop all boides in the scene
for (var key in cannonEntities) {
var dir = pos.vsub(cannonEntities[key].position);
// get normal of the direction
dir = dir.unit();
// Add force to cannon entity
cannonEntities[key].force = cannonEntities[key].force.vadd(dir.mult(force));
}
}
function createBox(pos,mass,he) {
var boxShape = new CANNON.Box(he);
var boxBody = new CANNON.RigidBody(mass,boxShape);
boxBody.position = pos;
cannonWorld.add(boxBody);
cannonEntities[(numBodies++).toString()] = boxBody;
}
function createSphere(pos,mass,radius) {
var sphereShape = new CANNON.Sphere(radius);
var sphereBody = new CANNON.RigidBody(mass,sphereShape);
sphereBody.position = pos;
cannonWorld.add(sphereBody);
cannonEntities[(numBodies++).toString()] = sphereBody;
}
function sendPlayerInformationToClient(clientId,players){
for (var key in players) {
var player = players[key];
var js = {
type:"playerinfo",
data:{id:player.id,
name:player.name,
disc:player.disc,
color:player.color}
};
var json = JSON.stringify(js);
clientConnections[clientId.toString()].sendUTF(json);
}
}
function sendUpdatedPlyersInfo(){
for (var key in clientInformation) {
sendPlayerInformationToClient(clientInformation[key].id,clientInformation);
}
}
function sendPlayerInfoEvent(id,name,color,disc) {
// COnstruct information
var js = {type:"playerinfo", data:{id:id,name:name,color:color,disc:disc}};
var json = JSON.stringify(js);
broadcastJsonEvent(json);
}
function sendClientJoinAccept(clientId,clientName,clientColor){
// COnstruct information
var js = {type:"clientJoinAccept", data:{id:clientId,name:clientName,color:clientColor}};
var json = JSON.stringify(js);
if (DEBUG_LOG)
console.log("sending client join accpet: " + json);
clientConnections[clientId.toString()].sendUTF(json);
}
function sendChatMessage(jd,clientName,clientColor){
// Send message to all clients
var message = { type:"chat",
data:{
time: (new Date()).getTime(),
text: htmlEntities(jd.data.string),
author: clientName,
color: clientColor
}
};
broadcastJsonEvent(JSON.stringify(message));
}
function addClient(clientIndex,clientName,clientColor){
var client = {
id : clientIndex,
name : clientName,
color : clientColor
}
clientInformation[client.id.toString()] = client;
}
function broadcastJsonEvent(jsonData) {
// Loop over all clients and send the jsonData event
for (var key in clientConnections) {
clientConnections[key].sendUTF(jsonData);
}
}
/**************************
* Simulation loop callback
***************************/
setInterval(function(){
// Step the world if there are clients connected
if(Object.size(clientConnections) > 0)
cannonWorld.step(dt);
// Broadcast world information about all boides to clients
var json = JSON.stringify(entities2Json(cannonEntities));
broadcastJsonEvent(json);
}, 1.0/60.0 * 1000);
/************************************************************
* This is a class for creating and handling mouse joint data
* A point-point constriant is created between the mouse and
* the point klicked on the rigid body
*************************************************************/
function MouseJoint(point, cannonBody, world) {
// THis is the cannon body constrained by the mouse joint
this.rigidBody = cannonBody;
// This is the first constraint point relative to the body
var v1 = point.vsub(cannonBody.position);
// Apply anti-transformation to vector to get it in the local coordinate system
var antiRot = cannonBody.quaternion.inverse();
this.rBody = antiRot.vmult(v1);
// Create a body on the mouse joint that has mass 0, that the other body can be constrianed too
this.jointBody = createJointBody();
// This is the second constraint point, which is where the mouse is. Set it to jointbod
this.jointBody.position = point;
// Create a new distance-constraint in the world
this.constraint = new CANNON.PointToPointConstraint(this.rigidBody,this.rBody,this.jointBody,new CANNON.Vec3(0,0,0));
// THe world that the mouse joint belongs too
this.world = world;
// Add constriant to world
world.addConstraint(this.constraint);
function createJointBody() {
var shape = new CANNON.Sphere(0.0001);
return new CANNON.RigidBody(0,shape,new CANNON.Material("particlematerial"));
}
}
// This functions moves the transparent joint body to a new postion in space
MouseJoint.prototype.moveJointToPoint = function(point) {
// Move the joint body to a new position
this.jointBody.position = point;
this.constraint.update();
}
MouseJoint.prototype.removeJointConstraint = function() {
// Remove constriant from world
this.world.removeConstraint(this.constraint);
}