From 26aa471b14ecd34c1b6651abacda863564ed1a50 Mon Sep 17 00:00:00 2001 From: logsol Date: Sun, 17 Jun 2012 00:42:28 +0200 Subject: [PATCH] some more hardcore network testing --- networking/app.js~ | 15 +++++--- networking/client.js | 29 +++++++++++++--- networking/client.js~ | 31 ++++++++++++++--- networking/common.js | 10 +++--- networking/common.js~ | 10 +++--- networking/index.html | 2 +- networking/{app.js => server.js~} | 57 ++++++++++++++++++++++++++----- 7 files changed, 123 insertions(+), 31 deletions(-) rename networking/{app.js => server.js~} (64%) diff --git a/networking/app.js~ b/networking/app.js~ index 0bc5121..1e036a5 100644 --- a/networking/app.js~ +++ b/networking/app.js~ @@ -18,20 +18,26 @@ setInterval(updateClients, 500); function updateClients() { var body = world.GetBodyList(); var update = {}; + var isUpdateNeeded = false; do { var userData = body.GetUserData(); - - if(userData && userData.bodyId){ + + if(userData && userData.bodyId && body.IsAwake()){ + console.log(body.IsAwake()); update[userData.bodyId] = { p: body.GetPosition(), a: body.GetAngle(), v: body.GetLinearVelocity(), }; + isUpdateNeeded = true; } } while (body = body.GetNext()); - sendToClients('world-update', update); + + if(isUpdateNeeded) { + sendToClients('world-update', update); + } } function sendToClients(message, data) { @@ -41,7 +47,7 @@ function sendToClients(message, data) { } //console.log(JSON.stringify(packet)); for (var i = 0; i < clients.length; i++) { - clients[i].send(JSON.stringify(packet)); + clients[i].send( JSON.stringify(packet)); } } @@ -86,6 +92,7 @@ socket.on('connection', function(client) { break; } } + updateClients(); }); client.on('disconnect', function(){ diff --git a/networking/client.js b/networking/client.js index 61c5997..d5e6f87 100644 --- a/networking/client.js +++ b/networking/client.js @@ -1,4 +1,14 @@ -var id = null; +var id = null; + +//window.setInterval(ping, 1000); + +function ping(){ + var packet = { + m: 'ping', + d: new Date().getTime() + } + socket.send(JSON.stringify(packet)); +} function setupCanvas() { var debugDraw = new b2DebugDraw(); @@ -13,12 +23,13 @@ function setupCanvas() { } function _jump() { + console.log('---jump---'); jump(); - var data = { + var packet = { m: 'jump' }; - socket.send(JSON.stringify(data)); + socket.send(JSON.stringify(packet)); } function init() { @@ -63,11 +74,18 @@ function updateWorld(data) { var body = world.GetBodyList(); do { var userData = body.GetUserData(); - if(userData && userData.bodyId){ + if(userData && userData.bodyId && data[userData.bodyId]){ var update = data[userData.bodyId]; + + console.log('position difference:', (body.GetPosition().y - update.p.y) * 30, body.GetLinearVelocity().y); + + /*body.SetAwake(true); body.SetPosition(update.p); body.SetAngle(update.a); body.SetLinearVelocity(update.v); + */ + + } } while (body = body.GetNext()); } @@ -91,6 +109,9 @@ socket.on('message', function(packet) { case 'world-update': updateWorld(packet.d); break; + case 'pong': + console.log('pong', new Date().getTime() - packet.d); + break; default: break; } diff --git a/networking/client.js~ b/networking/client.js~ index efbf1f1..c273cfb 100644 --- a/networking/client.js~ +++ b/networking/client.js~ @@ -1,4 +1,14 @@ -var id = null; +var id = null; + +//window.setInterval(ping, 1000); + +function ping(){ + var packet = { + m: 'ping', + d: new Date().getTime() + } + socket.send(JSON.stringify(packet)); +} function setupCanvas() { var debugDraw = new b2DebugDraw(); @@ -13,12 +23,13 @@ function setupCanvas() { } function _jump() { + console.log('---jump---'); + jump(); - var data = { + var packet = { m: 'jump' }; - - socket.send(JSON.stringify(data)); + socket.send(JSON.stringify(packet)); } function init() { @@ -63,11 +74,18 @@ function updateWorld(data) { var body = world.GetBodyList(); do { var userData = body.GetUserData(); - if(userData && userData.bodyId){ + if(userData && userData.bodyId && data[userData.bodyId]){ var update = data[userData.bodyId]; + + console.log('position difference:', (body.GetPosition().y - update.p.y) * 30, body.GetLinearVelocity().y); + + body.SetAwake(true); body.SetPosition(update.p); body.SetAngle(update.a); body.SetLinearVelocity(update.v); + + + } } while (body = body.GetNext()); } @@ -91,6 +109,9 @@ socket.on('message', function(packet) { case 'world-update': updateWorld(packet.d); break; + case 'pong': + console.log('pong', new Date().getTime() - packet.d); + break; default: break; } diff --git a/networking/common.js b/networking/common.js index 1f6c28c..8f73a25 100644 --- a/networking/common.js +++ b/networking/common.js @@ -2,7 +2,7 @@ var bodiesNum = 3; var world; var xport = 8003; -var xhost = '127.0.0.1'; +var xhost = 'fuuuuu.de'; var b2Vec2 = Box2D.Common.Math.b2Vec2, b2AABB = Box2D.Collision.b2AABB, @@ -22,8 +22,8 @@ function setupWorld() { var fixDef = new b2FixtureDef; fixDef.density = 1.0; - fixDef.friction = 0.5; - fixDef.restitution = 0.2; + fixDef.friction = 0.99; + fixDef.restitution = 0.01; var bodyDef = new b2BodyDef; @@ -56,7 +56,9 @@ function setupWorld() { bodyDef.position.x = (i + 1) * 4; bodyDef.position.y = 3; - bodyDef.userData = {"bodyId" : parseInt(i) }; + bodyDef.userData = { + 'bodyId': i + '' + }; world.CreateBody(bodyDef).CreateFixture(fixDef); } diff --git a/networking/common.js~ b/networking/common.js~ index 9793e9c..7a28b49 100644 --- a/networking/common.js~ +++ b/networking/common.js~ @@ -2,7 +2,7 @@ var bodiesNum = 3; var world; var xport = 8003; -var xhost = '127.0.0.1'; +var xhost = 'fuuuuu.de'; var b2Vec2 = Box2D.Common.Math.b2Vec2, b2AABB = Box2D.Collision.b2AABB, @@ -23,7 +23,7 @@ function setupWorld() { var fixDef = new b2FixtureDef; fixDef.density = 1.0; fixDef.friction = 0.5; - fixDef.restitution = 0.2; + fixDef.restitution = 0.01; var bodyDef = new b2BodyDef; @@ -56,7 +56,9 @@ function setupWorld() { bodyDef.position.x = (i + 1) * 4; bodyDef.position.y = 3; - bodyDef.userData = {"bodyId" : parseInt(i) }; + bodyDef.userData = { + 'bodyId': i + '' + }; world.CreateBody(bodyDef).CreateFixture(fixDef); } @@ -65,7 +67,7 @@ function setupWorld() { function jump() { var body = findBody(1); body.SetAwake(true); - body.ApplyImpulse(new b2Vec2(0, -5), body.GetPosition()); + body.ApplyImpulse(new b2Vec2(0, -9), body.GetPosition()); } function findBody(index) { diff --git a/networking/index.html b/networking/index.html index 635dffd..670bec7 100644 --- a/networking/index.html +++ b/networking/index.html @@ -2,7 +2,7 @@ Box2dWeb Demo - + diff --git a/networking/app.js b/networking/server.js~ similarity index 64% rename from networking/app.js rename to networking/server.js~ index 0bc5121..273f2dc 100644 --- a/networking/app.js +++ b/networking/server.js~ @@ -13,25 +13,30 @@ function update() { } setInterval(update, 1000 / 60); -setInterval(updateClients, 500); +setInterval(updateClients, 100); function updateClients() { var body = world.GetBodyList(); var update = {}; + var isUpdateNeeded = false; do { var userData = body.GetUserData(); - - if(userData && userData.bodyId){ + + if(userData && userData.bodyId && body.IsAwake()){ update[userData.bodyId] = { p: body.GetPosition(), a: body.GetAngle(), v: body.GetLinearVelocity(), }; + isUpdateNeeded = true; } } while (body = body.GetNext()); - sendToClients('world-update', update); + + if(isUpdateNeeded) { + sendToClients('world-update', update); + } } function sendToClients(message, data) { @@ -45,7 +50,13 @@ function sendToClients(message, data) { } } - +function pong(client, data) { + var packet = { + m: 'pong', + d: data + } + client.send(JSON.stringify(packet)); +} setupWorld(); //update(); @@ -74,21 +85,49 @@ socket.on('connection', function(client) { client.send(JSON.stringify({"startId" : clients.length})); - client.on('message', function(data){ - data = JSON.parse(data); + client.on('message', function(packet){ + packet = JSON.parse(packet); - if(data && data.m){ - switch(data.m){ + if(packet && packet.m){ + switch(packet.m){ case 'jump': jump(); break; + case 'ping': + pong(client, packet.d); + break; default: break; } } + updateClients(); }); client.on('disconnect', function(){ console.log("disconnect"); }); }); + +/******* collision ********/ + +function createCollisionDetector() { + var listener = new b2ContactListener(); + + listener.BeginContact = function(contact){ + + } + listener.PostSolve = function(contact, impulse){ + + } + listener.EndContact = function(contact){ + + } + + return listener; +} + + + + + +